Beispiel #1
0
        static void CreateColoredCubesVolume()
        {
            int width = 256;
            int height = 32;
            int depth = 256;

            ColoredCubesVolumeData data = VolumeDataAsset.CreateEmptyVolumeData<ColoredCubesVolumeData>(new Region(0, 0, 0, width-1, height-1, depth-1));

            GameObject coloredCubesGameObject = ColoredCubesVolume.CreateGameObject(data, true, false);

            // And select it, so the user can get straight on with editing.
            Selection.activeGameObject = coloredCubesGameObject;

            int floorThickness = 8;
            QuantizedColor floorColor = new QuantizedColor(192, 192, 192, 255);

            for(int z = 0; z <= depth-1; z++)
            {
                for(int y = 0; y < floorThickness; y++)
                {
                    for(int x = 0; x <= width-1; x++)
                    {
                        data.SetVoxel(x, y, z, floorColor);
                    }
                }
            }
        }
        static void CreateColoredCubesVolume()
        {
            int width  = 256;
            int height = 32;
            int depth  = 256;

            ColoredCubesVolumeData data = VolumeDataAsset.CreateEmptyVolumeData <ColoredCubesVolumeData>(new Region(0, 0, 0, width - 1, height - 1, depth - 1));

            GameObject coloredCubesGameObject = ColoredCubesVolume.CreateGameObject(data, true, true);

            // And select it, so the user can get straight on with editing.
            Selection.activeGameObject = coloredCubesGameObject;

            int            floorThickness = 8;
            QuantizedColor floorColor     = new QuantizedColor(192, 192, 192, 255);

            for (int z = 0; z <= depth - 1; z++)
            {
                for (int y = 0; y < floorThickness; y++)
                {
                    for (int x = 0; x <= width - 1; x++)
                    {
                        data.SetVoxel(x, y, z, floorColor);
                    }
                }
            }
        }
        private void CreateAVolumeDB()
        {
            System.Random randomIntGenerator = new System.Random();
            int           randomInt          = randomIntGenerator.Next();
            string        saveLocation       = Paths.voxelDatabases + "/Matt-test" + randomInt + ".vdb";
            var           volumeBounds       = new Region(Vector3i.zero, size);

            ColoredCubesVolumeData data = VolumeData.CreateEmptyVolumeData <ColoredCubesVolumeData>(volumeBounds, null); // saveLocation);

            var coloredCubeVolume = GetComponent <ColoredCubesVolume>();

            coloredCubeVolume.data = data;

            float invRockScale = 1f / noiseScale;

            MaterialSet materialSet = new MaterialSet();

            // It's best to create these outside of the loop.
            QuantizedColor red   = new QuantizedColor(255, 0, 0, 255);
            QuantizedColor blue  = new QuantizedColor(122, 122, 255, 255);
            QuantizedColor gray  = new QuantizedColor(127, 127, 127, 255);
            QuantizedColor white = new QuantizedColor(255, 255, 255, 255);

            // Iterate over every voxel of our volume
            for (int z = 0; z < size.x; z++)
            {
                for (int y = 0; y < size.y; y++)
                {
                    for (int x = 0; x < size.z; x++)
                    {
                        // Simplex noise is quite high frequency. We scale the sample position to reduce this.
                        float sampleX = (float)x * invRockScale;
                        float sampleY = (float)y * invRockScale;
                        float sampleZ = (float)z * invRockScale;

                        // range -1 to +1
                        float simplexNoiseValue = SimplexNoise.Noise.Generate(sampleX, sampleY, sampleZ);

                        simplexNoiseValue -= y / size.y * .75f;
                        // mul by 5 and clamp?

                        //simplexNoiseValue *= 5f;
                        //simplexNoiseValue = Mathf.Clamp(simplexNoiseValue, -.5f, .5f);
                        //simplexNoiseValue += .5f;
                        //simplexNoiseValue *= 255;

                        if (simplexNoiseValue > 0f)
                        {
                            data.SetVoxel(x, y, z, blue);
                        }
                    }
                }
            }
            data.CommitChanges();

            Debug.Log("Voxel db saved to: " + saveLocation);
        }
        /// Sets the color of the specified position.

        /**
         * \param x The 'x' position of the voxel to set.
         * \param y The 'y' position of the voxel to set.
         * \param z The 'z' position of the voxel to set.
         * \param quantizedColor The color the voxel should be set to.
         */
        public void SetVoxel(int x, int y, int z, QuantizedColor quantizedColor)
        {
            // The initialization can fail (bad filename, database locked, etc), so the volume handle could still be null.
            if (volumeHandle.HasValue)
            {
                if (x >= enclosingRegion.lowerCorner.x && y >= enclosingRegion.lowerCorner.y && z >= enclosingRegion.lowerCorner.z &&
                    x <= enclosingRegion.upperCorner.x && y <= enclosingRegion.upperCorner.y && z <= enclosingRegion.upperCorner.z)
                {
                    CubiquityDLL.SetVoxel(volumeHandle.Value, x, y, z, quantizedColor);
                }
            }
        }
		/// Sets the color of the specified position.
		/**
		 * \param x The 'x' position of the voxel to set.
		 * \param y The 'y' position of the voxel to set.
		 * \param z The 'z' position of the voxel to set.
		 * \param quantizedColor The color the voxel should be set to.
		 */
		public void SetVoxel(int x, int y, int z, QuantizedColor quantizedColor)
		{
			// The initialization can fail (bad filename, database locked, etc), so the volume handle could still be null.
			if(volumeHandle.HasValue)
			{
				if(x >= enclosingRegion.lowerCorner.x && y >= enclosingRegion.lowerCorner.y && z >= enclosingRegion.lowerCorner.z
					&& x <= enclosingRegion.upperCorner.x && y <= enclosingRegion.upperCorner.y && z <= enclosingRegion.upperCorner.z)
				{						
					CubiquityDLL.SetVoxel(volumeHandle.Value, x, y, z, quantizedColor);
				}
			}
		}
        /// Gets the color of the specified position.

        /**
         * \param x The 'x' position of the voxel to get.
         * \param y The 'y' position of the voxel to get.
         * \param z The 'z' position of the voxel to get.
         * \return The color of the voxel.
         */
        public QuantizedColor GetVoxel(int x, int y, int z)
        {
            // The initialization can fail (bad filename, database locked, etc), so the volume handle could still be null.
            QuantizedColor result;

            if (volumeHandle.HasValue)
            {
                CubiquityDLL.GetVoxel(volumeHandle.Value, x, y, z, out result);
            }
            else
            {
                result = new QuantizedColor();
            }
            return(result);
        }
Beispiel #7
0
        void OnWizardCreate()
        {
            ColoredCubesVolumeData data = VolumeDataAsset.CreateEmptyVolumeData <ColoredCubesVolumeData>(new Region(0, 0, 0, width - 1, height - 1, depth - 1));

            if (generateFloor)
            {
                // Create a floor so the volume data is actually visible in the editor.
                int            floorThickness = 8;
                QuantizedColor floorColor     = new QuantizedColor(192, 192, 192, 255);

                for (int z = 0; z <= depth - 1; z++)
                {
                    for (int y = 0; y < floorThickness; y++)
                    {
                        for (int x = 0; x <= width - 1; x++)
                        {
                            data.SetVoxel(x, y, z, floorColor);
                        }
                    }
                }
            }
        }
        void OnWizardCreate()
        {
            ColoredCubesVolumeData data = VolumeDataAsset.CreateEmptyVolumeData<ColoredCubesVolumeData>(new Region(0, 0, 0, width-1, height-1, depth-1));

            if(generateFloor)
            {
                // Create a floor so the volume data is actually visible in the editor.
                int floorThickness = 8;
                QuantizedColor floorColor = new QuantizedColor(192, 192, 192, 255);

                for(int z = 0; z <= depth-1; z++)
                {
                    for(int y = 0; y < floorThickness; y++)
                    {
                        for(int x = 0; x <= width-1; x++)
                        {
                            data.SetVoxel(x, y, z, floorColor);
                        }
                    }
                }
            }
        }
    // Use this for initialization
    void Start()
    {
        // We will load our texture from the supplied maze image. If you wish to supply your own image then please
        // note that in Unity 4 you have to set the 'Read/Write Enabled flag' in the texture import properties.
        // To do this, find the texture in your project view, go to the import settings in the inspector, change
        // the 'Texture Type' to 'Advanced' and then check the 'Read/Write Enabled' checkbox.
        Texture2D mazeImage = Resources.Load("Images/Maze") as Texture2D;

        // The size of the volume we will generate. Note that our source image cn be considered
        // to have x and y axes,  but we map these to x and z because in Unity3D the y axis is up.
        int width = mazeImage.width;
        int height = 32;
        int depth = mazeImage.height;

        // Start with some empty volume data and we'll write our maze into this.
        /// [DoxygenSnippet-CreateEmptyColoredCubesVolumeData]
        // Create an empty ColoredCubesVolumeData with dimensions width * height * depth
        ColoredCubesVolumeData data = VolumeData.CreateEmptyVolumeData<ColoredCubesVolumeData>(new Region(0, 0, 0, width-1, height-1, depth-1));
        /// [DoxygenSnippet-CreateEmptyColoredCubesVolumeData]

        //Get the main volume component
        ColoredCubesVolume coloredCubesVolume = gameObject.GetComponent<ColoredCubesVolume>();

        // Attactch the empty data we created previously
        coloredCubesVolume.data = data;

        // At this point we have a volume created and can now start writting our maze data into it.

        // It's best to create these outside of the loop.
        QuantizedColor red = new QuantizedColor(255, 0, 0, 255);
        QuantizedColor blue = new QuantizedColor(0, 0, 255, 255);
        QuantizedColor gray = new QuantizedColor(127, 127, 127, 255);
        QuantizedColor white = new QuantizedColor(255, 255, 255, 255);

        // Iterate over every pixel of our maze image.
        for(int z = 0; z < depth; z++)
        {
            for(int x = 0; x < width; x++)
            {
                // The exact logic here isn't important for the purpose of the example, but basically we decide which
                // tile a voxel is part of based on it's position. You can tweak the values to get an dea of what they do.
                QuantizedColor tileColor;
                int tileSize = 4;
                int tileXOffset = 2;
                int tileZOffset = 2;
                int tileXPos = (x + tileXOffset) / tileSize;
                int tileZPos = (z + tileZOffset) / tileSize;
                if((tileXPos + tileZPos) % 2 == 1)
                {
                    tileColor = blue;
                }
                else
                {
                    tileColor = white;
                }

                // For each pixel of the maze image determine whether it is a wall or empty space.
                bool isWall = mazeImage.GetPixel(x, z).r < 0.5; // A black pixel represents a wall

                // Height of the wall and floor in our maze.
                int floorHeight = 5;
                int wallHeight = 20;

                // Iterate over every voxel in the current column.
                for(int y = height-1; y > 0; y--)
                {
                    // If the current column is a wall then we set every voxel
                    // to gray except for the top one (which we set to red).
                    if(isWall)
                    {
                        if(y < wallHeight)
                        {
                            data.SetVoxel(x, y, z, gray);
                        }
                        else if(y == wallHeight)
                        {
                            data.SetVoxel(x, y, z, red);
                        }
                    }
                    else // Floor is also gray underneath but the top voxel is set to the tile color.
                    {
                        if(y < floorHeight)
                        {
                            data.SetVoxel(x, y, z, gray);
                        }
                        else if(y == floorHeight)
                        {
                            data.SetVoxel(x, y, z, tileColor);
                        }
                    }
                }
            }
        }
    }