Esempio n. 1
0
        public MovingGlowCells GenerateBaseCells(float radius, int mapXSize, int baseIndex)
        {
            var result   = new MovingGlowCells(radius, mapXSize);
            int intRad   = Mathf.RoundToInt(radius * 100);
            int maxCount = intRad / 100;

            for (int x = 0; x <= maxCount; x++)
            {
                int distAlongDiag = baseDist + (x * diagDist);
                for (int z = x; z <= maxCount; z++)
                {
                    int sumDist = distAlongDiag + (strDist * (z - x));

                    if (sumDist <= intRad)
                    {
                        if (x == 0 && z == 0)
                        {
                            result.AddNew(baseIndex, sumDist);
                        }
                        else if (x == 0)
                        {
                            result.AddNew((z * mapXSize) + baseIndex, sumDist);
                            result.AddNew(z + baseIndex, sumDist);
                            result.AddNew((-z * mapXSize) + baseIndex, sumDist);
                            result.AddNew(-z + baseIndex, sumDist);
                        }
                        else if (x == z)
                        {
                            for (int i = 0; i < 4; i++)
                            {
                                result.AddNew((rotT[i, 0] * x) + (rotT[i, 1] * z * mapXSize) + baseIndex, sumDist);
                            }
                        }
                        else
                        {
                            for (int i = 0; i < 4; i++)
                            {
                                result.AddNew((rotT[i, 0] * x) + (rotT[i, 1] * z * mapXSize) + baseIndex, sumDist);
                                result.AddNew((rotT[i, 0] * z) + (rotT[i, 1] * x * mapXSize) + baseIndex, sumDist);
                            }
                        }
                    }
                }
            }

            result.Sort();

            return(result);
        }
Esempio n. 2
0
        public void InitialiseGlow()
        {
            edifices = map.edificeGrid.InnerArray;

            int intRad = Mathf.RoundToInt(glowRadius);

            CellIndices = map.cellIndices;

            int locIndex = CellIndices.CellToIndex(position);

            movingCells = GenerateBaseCells(glowRadius, mapSizeX, locIndex);

            movingCells.Sort();
            SetUpGlowCells();
            Log.Message($"movingCells = {movingCells.ToStringColour()}");
            //map.mapDrawer.MapMeshDirty(position, MapMeshFlag.GroundGlow);
            mesh = DynamicLightingOverlay.MakeNewMesh(movingCells, locIndex, intRad, glowColor, offset: out offset);

            //mesh = DynamicLightingOverlay.MakeTestCircle(out offset, mat);
        }
Esempio n. 3
0
        public static Mesh MakeNewMesh(MovingGlowCells glowCells, int rootCellIndex, int glowRadius, ColorInt glowColor, out Vector3 offset)
        {
            var   mesh = new Mesh();
            float y    = 0;

            List <Vector3> verts   = new List <Vector3>();
            List <Color32> colours = new List <Color32>();

            int[,] rowIndices = new int[glowCells.NumRows + 1, 2];
            int currentIndex = 0;

            offset = Vector3.zero;

            float AttenLinearSlope = -1f / glowRadius;

            foreach (GlowCell glowCell in glowCells)
            {
                int dist = glowCell.dist;

                float rad = (float)dist / 100f;
                //ColorInt addedColour = baseColour;

                //float b = 1f / (rad * rad);
                //float a = 1f + AttenLinearSlope * rad;

                //addedColour += (glowColor - baseColour) * Math.Min(1 ,(0.8f * a) +( 0.2f * b));
                Color32 addedColour = Color32.Lerp(glowColor.ToColor32, clear, rad / glowRadius);
                glowCell.displayColour = addedColour;
            }


            for (int z = 0; z <= glowCells.NumRows; z++)
            {
                int  rowLength      = glowCells[z]?.Count ?? glowCells[z - 1].Count;
                bool foundFirstCell = false;
                rowIndices[z, 0] = currentIndex;
                for (int x = 0; x <= rowLength; x++)
                {
                    if (glowCells[z, x] != null || glowCells[z - 1, x] != null || glowCells[z, x - 1] != null || glowCells[z - 1, x - 1] != null)
                    {
                        ColorInt averageColour = default(ColorInt);

                        for (int nCell = 0; nCell < 4; nCell++)
                        {
                            averageColour += glowCells[z + cellOffsets[nCell, 0], x + cellOffsets[nCell, 1]]?.displayColour ?? clear;
                        }

                        averageColour /= 4;
                        averageColour.ClampToNonNegative();
                        Log.Message($"averageColour = {averageColour.ToColor32}");

                        colours.Add(averageColour.ToColor32);
                        verts.Add(new Vector3(x, y, z));
                        currentIndex++;

                        if (glowCells[z, x] != null)
                        {
                            if (glowCells[z, x].index == rootCellIndex)
                            {
                                offset = new Vector3(-x - 0.5f, y, -z - 0.5f);
                            }

                            if (!foundFirstCell)
                            {
                                foundFirstCell   = true;
                                rowIndices[z, 1] = x;
                            }
                        }
                    }
                }
            }

            rowIndices[glowCells.NumRows, 1] = rowIndices[glowCells.NumRows - 1, 1];

            int firstCentreIndex = verts.Count;

            for (int z = 0; z < glowCells.NumRows; z++)
            {
                var row = glowCells[z];
                for (int x = 0; x < row.Count; x++)
                {
                    if (row[x] != null)
                    {
                        verts.Add(new Vector3(x, y, z));
                        colours.Add(glowCells[z, x].displayColour);
                    }
                }
            }

            List <int> tris = new List <int>();

            int count = 0;

            for (int z = 0; z < glowCells.matrix.Count; z++)
            {
                var row       = glowCells[z];
                int botOffset = z == 0 ? 0 : Math.Max(0, rowIndices[z, 1] - rowIndices[z - 1, 1]);
                int topOffset = Math.Max(rowIndices[z, 1] - rowIndices[z + 1, 1], 0);
                int botIndex  = rowIndices[z, 0] + botOffset;
                int topIndex  = rowIndices[z + 1, 0] + topOffset;
                for (int x = 0; x < row.Count; x++)
                {
                    if (row[x] != null)
                    {
                        tris.Add(botIndex);
                        tris.Add(firstCentreIndex + count);
                        tris.Add(botIndex + 1);

                        tris.Add(botIndex);
                        tris.Add(topIndex);
                        tris.Add(firstCentreIndex + count);

                        tris.Add(topIndex);
                        tris.Add(topIndex + 1);
                        tris.Add(firstCentreIndex + count);

                        tris.Add(topIndex + 1);
                        tris.Add(botIndex + 1);
                        tris.Add(firstCentreIndex + count);

                        count++;
                        botIndex++;
                        topIndex++;
                    }
                }
            }


            mesh.SetVertices(verts);
            mesh.colors32 = colours.ToArray();
            mesh.SetTriangles(tris, 0);
            //mesh.SetColors(colours);

            return(mesh);
        }
 public GlowCellsSafeEnumerator(MovingGlowCells parent)
 {
     glowCells = parent;
 }