Esempio n. 1
0
            public static SDFGrid FromTexture(Texture texture)
            {
                var       grid  = new SDFGrid(texture.Width, texture.Height);
                const int EMPTY = 9999;

                if (texture.Format.SizeInBytes() != 4)
                {
                    Debug.Log("Don't compress the textures!!");
                }

                byte[] data = texture.GetMipData(0, out int rowPitch, out int slicePitch);
                unsafe
                {
                    fixed(byte *dataPtr = data)
                    {
                        var colorsPtr = (Color32 *)dataPtr;

                        for (int y = 0; y < texture.Height; y++)
                        {
                            for (int x = 0; x < texture.Width; x++)
                            {
                                var color = colorsPtr[y * texture.Width + x];
                                int dist  = (int)(color.R > 128 + 50 ? EMPTY : 0);
                                grid._gridPos[x + 1, y + 1] = new Int2(dist);
                                grid._gridNeg[x + 1, y + 1] = new Int2(EMPTY - dist);
                            }
                        }
                    }
                }

                int gridWidth  = grid._gridPos.GetLength(0);
                int gridHeight = grid._gridPos.GetLength(1);

                for (int i = 0; i < gridWidth; i++)
                {
                    grid._gridPos[i, 0] = new Int2(0);
                    grid._gridNeg[i, 0] = new Int2(EMPTY);

                    grid._gridPos[i, gridHeight - 1] = new Int2(0);
                    grid._gridNeg[i, gridHeight - 1] = new Int2(EMPTY);
                }

                for (int i = 0; i < gridHeight; i++)
                {
                    grid._gridPos[0, i] = new Int2(0);
                    grid._gridNeg[0, i] = new Int2(EMPTY);

                    grid._gridPos[gridWidth - 1, i] = new Int2(0);
                    grid._gridNeg[gridWidth - 1, i] = new Int2(EMPTY);
                }

                var positiveOffsets = new[]
                {
                    new Int2(-1, 0),
                    new Int2(0, -1),
                    new Int2(-1, -1),
                    new Int2(1, -1)
                };

                grid.RunPass(grid._gridPos, positiveOffsets, new Int2(1, 0), false);
                grid.RunPass(grid._gridNeg, positiveOffsets, new Int2(1, 0), false);

                var negativeOffsets = new[]
                {
                    new Int2(1, 0),
                    new Int2(0, 1),
                    new Int2(-1, 1),
                    new Int2(1, 1)
                };

                grid.RunPass(grid._gridPos, negativeOffsets, new Int2(-1, 0), true);
                grid.RunPass(grid._gridNeg, negativeOffsets, new Int2(-1, 0), true);
                return(grid);
            }
Esempio n. 2
0
        /// <summary>
        /// Converts a black and white image to a SDF
        /// </summary>
        public static Texture BlackAndWhiteToSDF(Texture texture, float spreadRadius)
        {
            SDFGrid grid = SDFGrid.FromTexture(texture);

            return(grid.ToTexture(spreadRadius));
        }