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

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

            GameObject obj = CogBlockVolume.CreateGameObject(data);

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

            int            floorThickness = 8;
            QuantizedColor floorColor     = CubSub.HexColor.QuantHex("FF5555FF");

            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);
                    }
                }
            }
        }
Example #2
0
    // Use this for initialization
    void Start()
    {
        int width  = 100;
        int height = 10;
        int depth  = 100;

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

        QuantizedColor green = new QuantizedColor(0, 255, 0, 255);

        for (int z = 0; z < depth; z++)
        {
            for (int y = 0; y < height; y++)
            {
                for (int x = 0; x < width; x++)
                {
                    data.SetVoxel(x, y, z, green);
                }
            }
        }
        ColoredCubesVolume coloredCubesVolume = gameObject.AddComponent <ColoredCubesVolume>();

        coloredCubesVolume.data = data;

        gameObject.AddComponent <ColoredCubesVolumeRenderer>();
    }
Example #3
0
        public async Task <Windows.UI.Color> Base64ToDominantColorAsync(string base64)
        {
            Arguments.NotNullOrWhiteSpace(base64, nameof(base64));

            try
            {
                var bytes = Convert.FromBase64String(base64);

                using (InMemoryRandomAccessStream memoryStream = new InMemoryRandomAccessStream())
                {
                    using (DataWriter writer = new DataWriter(memoryStream.GetOutputStreamAt(0)))
                    {
                        writer.WriteBytes(bytes);
                        await writer.StoreAsync();
                    }

                    BitmapDecoder decoder = await BitmapDecoder.CreateAsync(memoryStream);

                    var            colorThief     = new ColorThief();
                    QuantizedColor quantizedColor = await colorThief.GetColor(decoder).ConfigureAwait(false);

                    return(Windows.UI.Color.FromArgb(255, quantizedColor.Color.R, quantizedColor.Color.G, quantizedColor.Color.B));
                }
            }
            catch
            {
                return(Windows.UI.Color.FromArgb(255, 255, 255, 255));
            }
        }
 public void drawVoxel(int x, int y, int z, QuantizedColor color, bool explode)
 {
     if (explode)
     {
         ExplodeVoxel(x, y, z);
     }
     data.SetVoxel(x, y, z, color);
 }
    //from Cubiquity class' ClickToDestroy. generate a cube to go flying:
    void ExplodeVoxel(int xPos, int yPos, int zPos)
    {
        // Set up a material which we will apply to the cubes which we spawn to replace destroyed voxels.

        if (diffuseMap != null)
        {
            List <string> keywords = new List <string> {
                "DIFFUSE_TEXTURE_ON"
            };
            fakeVoxelMaterial.shaderKeywords = keywords.ToArray();
            fakeVoxelMaterial.SetTexture("_DiffuseMap", diffuseMap);
        }
        fakeVoxelMaterial.SetTexture("_NormalMap", volume.GetComponent <ColoredCubesVolumeRenderer>().material.GetTexture("_NormalMap"));
        fakeVoxelMaterial.SetFloat("_NoiseStrength", volume.GetComponent <ColoredCubesVolumeRenderer>().material.GetFloat("_NoiseStrength"));


        // Get the current color of the voxel
        QuantizedColor color = volume.data.GetVoxel(xPos, yPos, zPos);

        // Check the alpha to determine whether the voxel is visible.
        if (color.alpha > 127)
        {
            Vector3i voxel = new Vector3i(xPos, yPos, zPos);

            if (IsSurfaceVoxel(xPos, yPos, zPos))
            {
                GameObject cube = GameObject.CreatePrimitive(PrimitiveType.Cube);
                cube.AddComponent <Rigidbody>();
                cube.transform.parent                   = volume.transform;
                cube.transform.localPosition            = new Vector3(xPos, yPos, zPos);
                cube.transform.localRotation            = Quaternion.identity;
                cube.transform.localScale               = new Vector3(0.9f, 0.9f, 0.9f);
                cube.GetComponent <Renderer>().material = fakeVoxelMaterial;
                cube.GetComponent <Renderer>().material.SetColor("_CubeColor", (Color32)color);
                cube.GetComponent <Renderer>().material.SetVector("_CubePosition", new Vector4(xPos, yPos, zPos, 0.0f));

                Vector3 explosionForce = cube.transform.position - new Vector3(xPos, yPos, zPos);

                // These are basically random values found through experimentation.
                // They just add a bit of twist as the cubes explode which looks nice
                float xTorque = (xPos * 1436523.4f) % 56.0f;
                float yTorque = (yPos * 56143.4f) % 43.0f;
                float zTorque = (zPos * 22873.4f) % 38.0f;

                Vector3 up = new Vector3(0.0f, 2.0f, 0.0f);

                cube.GetComponent <Rigidbody>().AddTorque(xTorque, yTorque, zTorque);
                cube.GetComponent <Rigidbody>().AddForce((explosionForce.normalized + up) * 100.0f);

                // Cubes are just a temporary visual effect, and we delete them after a few seconds.
                float lifeTime = Random.Range(8.0f, 12.0f);
                Destroy(cube, lifeTime);
            }
        }
    }
    /// 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)
    {
        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);
            }
        }
    }
 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);
         }
     }
 }
Example #8
0
        public Material Translate(QuantizedColor color)
        {
            int decColor = CubSub.HexColor.DecQuant(color);

            if (translator.ContainsKey(decColor))
            {
                return(translator[decColor]);
            }
            else
            {
                return(translator[0]);
            }
        }
Example #9
0
        /// <summary>
        ///     Use the median cut algorithm to cluster similar colors and return the base color from the largest cluster.
        /// </summary>
        /// <param name="sourceImage">The source image.</param>
        /// <param name="quality">
        ///     1 is the highest quality settings. 10 is the default. There is
        ///     a trade-off between quality and speed. The bigger the number,
        ///     the faster a color will be returned but the greater the
        ///     likelihood that it will not be the visually most dominant color.
        /// </param>
        /// <param name="ignoreWhite">if set to <c>true</c> [ignore white].</param>
        /// <returns></returns>
        public async Task <QuantizedColor> GetColor(BitmapDecoder sourceImage, int quality = DefaultQuality, bool ignoreWhite = DefaultIgnoreWhite)
        {
            var palette = await GetPalette(sourceImage, 3, quality, ignoreWhite);

            var dominantColor = new QuantizedColor(new Color
            {
                A = Convert.ToByte(palette.Average(a => a.Color.A)),
                R = Convert.ToByte(palette.Average(a => a.Color.R)),
                G = Convert.ToByte(palette.Average(a => a.Color.G)),
                B = Convert.ToByte(palette.Average(a => a.Color.B))
            }, Convert.ToInt32(palette.Average(a => a.Population)));

            return(dominantColor);
        }
        /// <summary>
        /// Builds the Mesh represented by the Node. Uses the Node's nodeHandle (inherited from OctreeNodeAlt)
        /// </summary>
        /// <returns>The mesh.</returns>
        public override Mesh BuildMesh()
        {
            // At some point I should read this: http://forum.unity3d.com/threads/5687-C-plugin-pass-arrays-from-C

            Vector3 offset = new Vector3(0.5f, 0.5f, 0.5f);             // Required for the CubicVertex decoding process.

            // Meshes for rendering and collition.
            Mesh mesh = new Mesh();

            //prevents memory leaks >:)
            mesh.hideFlags = HideFlags.DontSave;

            // Get the data from where it is stored in Cubiquity.
            int[] indices = CubiquityDLL.GetIndices(nodeHandle);
            ColoredCubesVertex[] cubiquityVertices = CubiquityDLL.GetVertices(nodeHandle);

            // Create the arrays which we'll copy the data to.
            Vector3[] rendererVertices = new Vector3[cubiquityVertices.Length];
            Color32[] rendererColors   = new Color32[cubiquityVertices.Length];

            //translate the data from Cubiquity's forms to Unity's
            for (int ct = 0; ct < cubiquityVertices.Length; ct++)
            {
                // Get the vertex data from Cubiquity.
                Vector3 position = new Vector3(cubiquityVertices[ct].x, cubiquityVertices[ct].y, cubiquityVertices[ct].z);

                // Part of the CubicVertex decoding process.
                position -= offset;

                //nab the color
                QuantizedColor color = cubiquityVertices[ct].color;

                // Copy it to the arrays.
                rendererVertices[ct] = position;
                rendererColors[ct]   = (Color32)color;
            }

            // Assign vertex data to the meshes.
            mesh.vertices  = rendererVertices;
            mesh.colors32  = rendererColors;
            mesh.triangles = indices;

            // FIXME - Get proper bounds
            mesh.bounds.SetMinMax(new Vector3(0.0f, 0.0f, 0.0f), new Vector3(32.0f, 32.0f, 32.0f));
            mesh.name = "nodeHandle: " + nodeHandle.ToString();

            return(mesh);
            //return null;
        }
        /// 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);
        }
    /// 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)
    {
        QuantizedColor result;

        if (volumeHandle.HasValue)
        {
            CubiquityDLL.GetVoxel(volumeHandle.Value, x, y, z, out result);
        }
        else
        {
            //Should maybe throw instead.
            result = new QuantizedColor();
        }
        return(result);
    }
        /// 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 String printData()
        {
            String         toReturn = "";
            QuantizedColor result   = new QuantizedColor();

            int lx = enclosingRegion.lowerCorner.x;
            int ux = enclosingRegion.upperCorner.x;
            int ly = enclosingRegion.lowerCorner.y;
            int uy = enclosingRegion.upperCorner.y;
            int lz = enclosingRegion.lowerCorner.z;
            int uz = enclosingRegion.upperCorner.z;

            Debug.Log("Dimensions on x: " + lx.ToString() + " to " + ux.ToString());
            Debug.Log("Dimensions on z: " + lz.ToString() + " to " + uz.ToString());
            Debug.Log("Dimensions on y: " + ly.ToString() + " to " + uy.ToString());
            Debug.Log("volume Handle: " + volumeHandle.Value.ToString());


            if (volumeHandle == null)
            {
                Debug.Log("null volume handle");
                return(toReturn);
            }

            int i = 0;
            //for(int i = lx; i < ux; i++)
            //{

            int j = 0;

            //for(int j = lz; j < uz; j++)
            //{
            for (int k = ly; k < uy; k++)
            {
                CubiquityDLL.GetVoxel(volumeHandle.Value, i, k, j, out result);

                Debug.Log(" " + i + "," + j + "," + k + ": " + result.red + "|" + result.green + "|" + result.blue + "|" + result.alpha);
            }
            //toReturn = toReturn + "|";
            //}
            //toReturn = toReturn + ";";
            //}

            return(toReturn);
        }
    public void clear()
    {
        int            width  = (data.enclosingRegion.upperCorner.x - data.enclosingRegion.lowerCorner.x) + 1;
        int            height = (data.enclosingRegion.upperCorner.y - data.enclosingRegion.lowerCorner.y) + 1;
        int            depth  = (data.enclosingRegion.upperCorner.z - data.enclosingRegion.lowerCorner.z) + 1;
        QuantizedColor c      = TransparentColor;

        for (int x = 0; x < width; x++)
        {
            for (int y = 0; y < height; y++)
            {
                for (int z = 0; z < depth; z++)
                {
                    drawVoxel(x, y, z, c, false);
                }
            }
        }
    }
Example #15
0
            public Mesh BuildMeshFromNodeHandleForColoredCubesVolume(uint nodeHandle)
            {
                // At some point I should read this: http://forum.unity3d.com/threads/5687-C-plugin-pass-arrays-from-C

                Vector3 offset = new Vector3(0.5f, 0.5f, 0.5f);                 // Required for the CubicVertex decoding process.

                // Create rendering and possible collision meshes.
                Mesh renderingMesh = new Mesh();

                renderingMesh.hideFlags = HideFlags.DontSave;

                // Get the data from Cubiquity.
                int[] indices = CubiquityDLL.GetIndices(nodeHandle);
                ColoredCubesVertex[] cubiquityVertices = CubiquityDLL.GetVertices(nodeHandle);

                // Create the arrays which we'll copy the data to.
                Vector3[] renderingVertices = new Vector3[cubiquityVertices.Length];
                Color32[] renderingColors   = new Color32[cubiquityVertices.Length];

                for (int ct = 0; ct < cubiquityVertices.Length; ct++)
                {
                    // Get the vertex data from Cubiquity.
                    Vector3 position = new Vector3(cubiquityVertices[ct].x, cubiquityVertices[ct].y, cubiquityVertices[ct].z);
                    position -= offset;                     // Part of the CubicVertex decoding process.
                    QuantizedColor color = cubiquityVertices[ct].color;

                    // Copy it to the arrays.
                    renderingVertices[ct] = position;
                    renderingColors[ct]   = (Color32)color;
                }

                // Assign vertex data to the meshes.
                renderingMesh.vertices  = renderingVertices;
                renderingMesh.colors32  = renderingColors;
                renderingMesh.triangles = indices;

                // FIXME - Get proper bounds
                renderingMesh.bounds.SetMinMax(new Vector3(0.0f, 0.0f, 0.0f), new Vector3(32.0f, 32.0f, 32.0f));

                return(renderingMesh);
            }
    public void generateSphere(Vector3 position, float radius, QuantizedColor color, bool explode)
    {
        int r = Mathf.FloorToInt(radius);
        int x = (int)position.x;
        int y = (int)position.y;
        int z = (int)position.z;

        for (int tx = -r; tx < r + 1; tx++)
        {
            for (int ty = -r; ty < r + 1; ty++)
            {
                for (int tz = -r; tz < r + 1; tz++)
                {
                    if (Mathf.Sqrt(Mathf.Pow(tx, 2) + Mathf.Pow(ty, 2) + Mathf.Pow(tz, 2)) <= r - 2)
                    {
                        drawVoxel(tx + x, ty + y, tz + z, color, explode);
                    }
                }
            }
        }
    }
Example #17
0
        void OnWizardCreate()
        {
            CogBlockVolumeData data = Cubiquity.VolumeDataAsset.CreateEmptyVolumeData <CogBlockVolumeData>(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(255, 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);
                        }
                    }
                }
            }
        }
Example #18
0
    IEnumerator CreateMap()
    {
        int[,] map = LoadImage(textureLocation);
        ColoredCubesVolumeData cubesData = VolumeData.CreateEmptyVolumeData <ColoredCubesVolumeData>(new Region(0, 0, 0, width, maxHeight, depth));

        color = new QuantizedColor(255, 255, 255, 255);

//		coloredCubesVolume = cubesHolder.AddComponent<ColoredCubesVolume>();
//		coloredCubesVolume.data = cubesData;
//		cubesHolder.AddComponent<ColoredCubesVolumeRenderer>();
//		cubesHolder.AddComponent <ColoredCubesVolumeCollider>();

        for (int z = 0; z < depth; z++)
        {
            for (int x = 0; x < width; x++)
            {
                for (int y = 0; y < (int)(((float)map[x, z] / 255f) * maxHeight); y++)
                {
                    //if(map[x,z] > VoxelThresHold){
                    cubesData.SetVoxel(x, y, z, color);
                    //cubesData.SetVoxel(x, y, z, color);
                    //}
                }
            }
            loadPercentage = (float)z / (float)depth;
            yield return(null);
        }
        Debug.Log("Finish");
        cubesData.CommitChanges();
        coloredCubesVolume      = cubesHolder.AddComponent <ColoredCubesVolume>();
        coloredCubesVolume.data = cubesData;
        cubesHolder.AddComponent <ColoredCubesVolumeRenderer>();
        cubesHolder.AddComponent <ColoredCubesVolumeCollider>();
        CreateTerrain();

        cubesHolder.transform.localScale = new Vector3(3f, 3f, 3f);
        transform.localScale            *= 1f / 3f;
    }
Example #19
0
    void Start()
    {
        Texture2D image = Resources.Load("Images/OW_TileMap_ORIG") as Texture2D;

        int width  = image.width;
        int height = 16;
        int depth  = image.height;

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

        ColoredCubesVolume coloredCubesVolume = gameObject.GetComponent <ColoredCubesVolume>();

        coloredCubesVolume.data = data;

        for (int z = 0; z < depth; z++)
        {
            for (int x = 0; x < width; x++)
            {
                Color          color  = image.GetPixel(x, z);
                QuantizedColor qColor = new QuantizedColor(
                    (byte)(color.r * 255),
                    (byte)(color.g * 255),
                    (byte)(color.b * 255),
                    (byte)(color.a * 255));

                /*int tileSize = 1;
                 *              int tileXOffset = 0;
                 *              int tileZOffset = 0;
                 *              int tileXPos = (x + tileXOffset) / tileSize;
                 *              int tileZPos = (z + tileZOffset) / tileSize;*/

                data.SetVoxel(x, 0, z, qColor);

                //for(int y = height-1; y > 0; y--) { }
            }
        }
    }
Example #20
0
        protected override void MakeChunk(ColoredCubesVolumeData data)
        {
            float invRockScale = 1f / noiseScale;

            QuantizedColor grass = voxelTypeToColor.getQuantizedColor(VoxelType.Grass);
            QuantizedColor empty = new QuantizedColor(0, 0, 0, 0);

            int debugCount = 0;

            //
            // Iterate over every voxel in the 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 = x * invRockScale;
                        float sampleY = y * invRockScale;
                        float sampleZ = z * invRockScale;

                        float heightOfWorld = size.y;

                        //
                        // if y = 0, testDepth = heightOfWorld
                        // if y = heightOfWorld - 1, testDepth = close to zero, small number
                        //
                        float testDepth = heightOfWorld - y;

                        // ranges from -1 to +1
                        float noise2DValue = SimplexNoise.Noise.Generate(sampleX, sampleZ);

                        // adjust the range to -.5 to +.5
                        noise2DValue = noise2DValue * .5f;

                        // adjust the range by 'snoise2DScale'
                        //noiseValue = noiseValue * snoise2DScale;

                        //
                        // 'Perturb' the testDepth using the noiseValue.
                        // In other words, push down testDepth if noiseValue
                        // is negative.
                        // This gives higher voxels a chance to be solid (creates hills).
                        // Push up testDepth if noiseValue is positive.
                        // These values now have a greater chance of being air (creates valleys).
                        //
                        testDepth = testDepth + (heightOfWorld * noise2DValue);

                        float testIsAVoxel = Gradient(testDepth, heightOfWorld); // testDepth, heightOfWorld);

                        /*
                         *
                         * float altitude = y / size.y;
                         * altitude = altitude * 4;
                         *
                         * noiseValue -= altitude;
                         */

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

                        if (testIsAVoxel > isSolidThreshhold)
                        {
                            data.SetVoxel(x, y, z, grass);
                            debugCount++;
                        }
                        else
                        {
                            data.SetVoxel(x, y, z, empty);
                        }
                    }
                }
            }

            Debug.Log("generated " + debugCount + " solid voxels out of area: " + (size.x * size.y * size.z) +
                      ". solid / area ratio: " + (debugCount / (float)(size.x * size.y * size.z)));
        }
Example #21
0
    void DestroyVoxels(int xPos, int yPos, int zPos, int range)
    {
        // Set up a material which we will apply to the cubes which we spawn to replace destroyed voxels.
        Material fakeVoxelMaterial = Resources.Load("Materials/FakeColoredCubes", typeof(Material)) as Material;
        Texture  diffuseMap        = coloredCubesVolume.GetComponent <ColoredCubesVolumeRenderer>().material.GetTexture("_DiffuseMap");

        if (diffuseMap != null)
        {
            List <string> keywords = new List <string> {
                "DIFFUSE_TEXTURE_ON"
            };
            fakeVoxelMaterial.shaderKeywords = keywords.ToArray();
            fakeVoxelMaterial.SetTexture("_DiffuseMap", diffuseMap);
        }
        fakeVoxelMaterial.SetTexture("_NormalMap", coloredCubesVolume.GetComponent <ColoredCubesVolumeRenderer>().material.GetTexture("_NormalMap"));
        fakeVoxelMaterial.SetFloat("_NoiseStrength", coloredCubesVolume.GetComponent <ColoredCubesVolumeRenderer>().material.GetFloat("_NoiseStrength"));

        // Initialise outside the loop, but we'll use it later.
        Vector3 pos          = new Vector3(xPos, yPos, zPos);
        int     rangeSquared = range * range;

        // Later on we will be deleting some voxels, but we'll also be looking at the neighbours of a voxel.
        // This interaction can have some unexpected results, so it is best to first make a list of voxels we
        // want to delete and then delete them later in a separate pass.
        List <Vector3i> voxelsToDelete = new List <Vector3i>();

        // Iterage over every voxel in a cubic region defined by the received position (the center) and
        // the range. It is quite possible that this will be hundreds or even thousands of voxels.
        for (int z = zPos - range; z < zPos + range; z++)
        {
            for (int y = yPos - range; y < yPos + range; y++)
            {
                for (int x = xPos - range; x < xPos + range; x++)
                {
                    // Compute the distance from the current voxel to the center of our explosion.
                    int xDistance = x - xPos;
                    int yDistance = y - yPos;
                    int zDistance = z - zPos;

                    // Working with squared distances avoids costly square root operations.
                    int distSquared = xDistance * xDistance + yDistance * yDistance + zDistance * zDistance;

                    // We're iterating over a cubic region, but we want our explosion to be spherical. Therefore
                    // we only further consider voxels which are within the required range of our explosion center.
                    // The corners of the cubic region we are iterating over will fail the following test.
                    if (distSquared < rangeSquared)
                    {
                        // Get the current color of the voxel
                        QuantizedColor color = coloredCubesVolume.data.GetVoxel(x, y, z);

                        // Check the alpha to determine whether the voxel is visible.
                        if (color.alpha > 127)
                        {
                            Vector3i voxel = new Vector3i(x, y, z);
                            voxelsToDelete.Add(voxel);

                            if (IsSurfaceVoxel(x, y, z))
                            {
                                GameObject cube = GameObject.CreatePrimitive(PrimitiveType.Cube);
                                cube.AddComponent <Rigidbody>();
                                cube.transform.parent                   = coloredCubesVolume.transform;
                                cube.transform.localPosition            = new Vector3(x, y, z);
                                cube.transform.localRotation            = Quaternion.identity;
                                cube.transform.localScale               = new Vector3(0.9f, 0.9f, 0.9f);
                                cube.GetComponent <Renderer>().material = fakeVoxelMaterial;
                                cube.GetComponent <Renderer>().material.SetColor("_CubeColor", (Color32)color);
                                cube.GetComponent <Renderer>().material.SetVector("_CubePosition", new Vector4(x, y, z, 0.0f));

                                Vector3 explosionForce = cube.transform.position - pos;

                                // These are basically random values found through experimentation.
                                // They just add a bit of twist as the cubes explode which looks nice
                                float xTorque = (x * 1436523.4f) % 56.0f;
                                float yTorque = (y * 56143.4f) % 43.0f;
                                float zTorque = (z * 22873.4f) % 38.0f;

                                Vector3 up = new Vector3(0.0f, 2.0f, 0.0f);

                                cube.GetComponent <Rigidbody>().AddTorque(xTorque, yTorque, zTorque);
                                cube.GetComponent <Rigidbody>().AddForce((explosionForce.normalized + up) * 100.0f);

                                // Cubes are just a temporary visual effect, and we delete them after a few seconds.
                                float lifeTime = Random.Range(8.0f, 12.0f);
                                Destroy(cube, lifeTime);
                            }
                        }
                    }
                }
            }
        }

        foreach (Vector3i voxel in voxelsToDelete)         // Loop through List with foreach
        {
            coloredCubesVolume.data.SetVoxel(voxel.x, voxel.y, voxel.z, new QuantizedColor(0, 0, 0, 0));
        }
    }
Example #22
0
        public static Color ToColor(this QuantizedColor color)
        {
            var c = color.Color;

            return(Color.FromArgb(c.A, c.R, c.G, c.B));
        }
Example #23
0
        public static DColor ToDiscordColor(this QuantizedColor color)
        {
            var c = color.Color;

            return((DColor)SColor.FromArgb(c.A, c.R, c.G, c.B));
        }
Example #24
0
 public static int DecQuant(QuantizedColor color)
 {
     return((int)(color.alpha + color.blue << 2 + color.green << 4 + color.red << 6));
 }
Example #25
0
 public static void GetVoxel(uint volumeHandle, int x, int y, int z, out QuantizedColor color)
 {
     Validate(cuGetVoxel(volumeHandle, x, y, z, out color));
 }
Example #26
0
 private static extern int cuGetVoxel(uint volumeHandle, int x, int y, int z, out QuantizedColor color);
    public void generateCircle(Vector3 position, float radius, bool filledIn, ShapeDirection dir, QuantizedColor color, bool explode)
    {
        int r  = (int)radius;
        int x0 = (int)position.x;
        int y0 = (int)position.y;
        int z0 = (int)position.z;


        int x   = r;
        int y   = 0;
        int err = 0;

        //TODO: handle switches for ShapeDirection (ugh)
        while (x >= y)
        {
            drawVoxel(x0 + x, y0 + y, z0, color, explode);
            drawVoxel(x0 + y, y0 + x, z0, color, explode);
            drawVoxel(x0 - y, y0 + x, z0, color, explode);
            drawVoxel(x0 - x, y0 + y, z0, color, explode);
            drawVoxel(x0 - x, y0 - y, z0, color, explode);
            drawVoxel(x0 - y, y0 - x, z0, color, explode);
            drawVoxel(x0 + y, y0 - x, z0, color, explode);
            drawVoxel(x0 + x, y0 - y, z0, color, explode);

            if (err <= 0)
            {
                y   += 1;
                err += 2 * y + 1;
            }
            if (err > 0)
            {
                x   -= 1;
                err -= 2 * x + 1;
            }
        }

        if (filledIn)
        {
            r--;
            while (r > 0)
            {
                x   = r;
                y   = 0;
                err = 0;
                while (x >= y)
                {
                    drawVoxel(x0 + x, y0 + y, z0, color, explode);
                    drawVoxel(x0 + y, y0 + x, z0, color, explode);
                    drawVoxel(x0 - y, y0 + x, z0, color, explode);
                    drawVoxel(x0 - x, y0 + y, z0, color, explode);
                    drawVoxel(x0 - x, y0 - y, z0, color, explode);
                    drawVoxel(x0 - y, y0 - x, z0, color, explode);
                    drawVoxel(x0 + y, y0 - x, z0, color, explode);
                    drawVoxel(x0 + x, y0 - y, z0, color, explode);

                    if (err <= 0)
                    {
                        y   += 1;
                        err += 2 * y + 1;
                    }
                    if (err > 0)
                    {
                        x   -= 1;
                        err -= 2 * x + 1;
                    }
                }
                r--;
            }
        }
    }
    public void generateRect2(Vector3 position, Vector2 size, ShapeDirection dir, QuantizedColor color, bool explode)
    {
        int rx = Mathf.FloorToInt(size.x) / 2;
        int ry = Mathf.FloorToInt(size.y) / 2;

        //static offset on whatever plane we're drawing on:
        int pos = 0;

        int xMin = 0;
        int yMin = 0;

        switch (dir)
        {
        case ShapeDirection.UP:
            xMin = Mathf.FloorToInt(position.x) - rx;
            yMin = Mathf.FloorToInt(position.z) - ry;
            pos  = Mathf.FloorToInt(position.y);
            break;

        case ShapeDirection.FRONT:
            xMin = Mathf.FloorToInt(position.x) - rx;
            yMin = Mathf.FloorToInt(position.y) - ry;
            pos  = Mathf.FloorToInt(position.z);
            break;

        case ShapeDirection.SIDE:
            xMin = Mathf.FloorToInt(position.z) - rx;
            yMin = Mathf.FloorToInt(position.y) - ry;
            pos  = Mathf.FloorToInt(position.x);
            break;

        default:
            break;
        }

        int xMax = xMin + rx * 2;
        int yMax = yMin + ry * 2;

        int x = xMin;
        int y = yMin;

        Debug.Log("rectangle x bounds: " + xMin + " to " + xMax + " y bounds: " + yMin + " to " + yMax);

        while (x <= xMax)
        {
            while (y <= yMax)
            {
                switch (dir)
                {
                case ShapeDirection.UP:
                    drawVoxel(x, pos, y, color, explode);
                    break;

                case ShapeDirection.FRONT:
                    drawVoxel(x, y, pos, color, explode);
                    break;

                case ShapeDirection.SIDE:
                    drawVoxel(pos, y, x, color, explode);
                    break;

                default:
                    break;
                }
                y++;
            }
            y = yMin;
            x++;
        }
    }
    public void generateRect3(Vector3 position, Vector3 size, QuantizedColor color, bool explode)
    {
        //start at bottom left front corner:
        int xMin = Mathf.CeilToInt(position.x - size.x / 2);
        int yMin = Mathf.CeilToInt(position.y - size.y / 2);
        int zMin = Mathf.CeilToInt(position.z - size.z / 2);

        int xMax = Mathf.FloorToInt(position.x + size.x / 2);
        int yMax = Mathf.FloorToInt(position.y + size.y / 2);
        int zMax = Mathf.FloorToInt(position.z + size.z / 2);

        int x = xMin;
        int y = yMin;
        int z = zMin;

        //draw bottom face:
        while (x <= xMax)
        {
            while (z <= zMax)
            {
                drawVoxel(x, y, z, color, explode);
                z++;
            }
            z = zMin;
            x++;
        }

        //draw side faces by turtling our way around a square on each level of y:
        y++;
        z = zMax;
        while (y < yMax)
        {
            while (x > xMin)
            {
                drawVoxel(x, y, z, color, explode);
                x--;
            }

            while (z > zMin)
            {
                drawVoxel(x, y, z, color, explode);
                z--;
            }

            while (x < xMax)
            {
                drawVoxel(x, y, z, color, explode);
                x++;
            }

            while (z < zMax)
            {
                drawVoxel(x, y, z, color, explode);
                z++;
            }
            y++;
        }

        //finally, draw the top face.
        x = xMin;
        z = zMin;
        while (x <= xMax)
        {
            while (z <= zMax)
            {
                drawVoxel(x, y, z, color, explode);
                z++;
            }
            z = zMin;
            x++;
        }
    }
    public void generateArrayIn2d(float[] array, uint arrayLength, Vector3 position, Vector2 size, ShapeDirection dir, ShapeFront front, ShapeOrientation orientation, QuantizedColor color, bool explode)
    {
        int y0   = 0;
        int xMin = 0;

        int y   = 0;
        int x   = 0;
        int idx = 0;

        //plane we're drawing on:
        int pos = 0;

        //how many voxels we need for each value in the array:
        int voxelsPerValue = Mathf.FloorToInt(size.x / arrayLength);

        //if we end up getting 0, then just round up to 1.
        if (voxelsPerValue <= 0)
        {
            voxelsPerValue++;
        }

        float val = 0.0f;

        switch (dir)
        {
        case ShapeDirection.UP:
            y0   = Mathf.FloorToInt(position.x);
            xMin = Mathf.FloorToInt(position.z - size.x / 2);
            pos  = Mathf.FloorToInt(position.y);
            break;

        case ShapeDirection.FRONT:
            y0   = Mathf.FloorToInt(position.y);
            xMin = Mathf.FloorToInt(position.x - size.x / 2);
            pos  = Mathf.FloorToInt(position.z);
            break;

        case ShapeDirection.SIDE:
            y0   = Mathf.FloorToInt(position.y);
            xMin = Mathf.FloorToInt(position.z - size.x / 2);
            pos  = Mathf.FloorToInt(position.x);
            break;

        default:
            break;
        }
        y = y0;
        x = xMin;

        //draw array:
        for (idx = 0; idx < arrayLength; idx++)
        {
            //if we are rendering facing the back or left, we reverse the draw.
            if (front == ShapeFront.DOWN_BACK_OR_LEFT && (dir == ShapeDirection.FRONT || dir == ShapeDirection.SIDE))
            {
                val = array[arrayLength - idx];
            }
            else
            {
                val = array[idx];
            }

            //if we are rendering facing downwards, we just negate the array:
            if (front == ShapeFront.DOWN_BACK_OR_LEFT && dir == ShapeDirection.FRONT)
            {
                y = y0 - Mathf.FloorToInt(val * size.y);
            }
            else
            {
                y = y0 + Mathf.FloorToInt(val * size.y);
            }
            //to do: if voxels per value is more than 1, draw squares rather than single voxels
            //Debug.Log("Drawing value " + val + " at x: " + x + " y: " + y);
            if (orientation == ShapeOrientation.HORIZONTAL)
            {
                switch (dir)
                {
                case ShapeDirection.UP:
                    drawVoxel(y, pos, x, color, explode);
                    break;

                case ShapeDirection.FRONT:
                    drawVoxel(x, y, pos, color, explode);
                    break;

                case ShapeDirection.SIDE:
                    drawVoxel(pos, y, x, color, explode);
                    break;

                default:
                    break;
                }
            }
            else
            {
                switch (dir)
                {
                case ShapeDirection.UP:
                    drawVoxel(x, pos, y, color, explode);
                    break;

                case ShapeDirection.FRONT:
                    drawVoxel(y, x, pos, color, explode);
                    break;

                case ShapeDirection.SIDE:
                    drawVoxel(pos, x, y, color, explode);
                    break;

                default:
                    break;
                }
            }
            x += voxelsPerValue;
        }
    }