Пример #1
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);
        }