Esempio n. 1
0
                private void GenerateGrid(Range left, Range bottom, int vertexIndex, byte connectedDirection)
                {
                    int c0, c1, c2, c3;
                    int width     = cellResolution + 1;
                    int rowOffset = left.IsValid ? width - 1 : width;
                    int colOffset = left.IsValid ? -1 : 0;

                    for (int y = 0; y < cellResolution; ++y)
                    {
                        for (int x = 0; x < cellResolution; ++x)
                        {
                            // corner
                            c0 = vertexIndex;
                            // above
                            c1 = c0 + rowOffset;
                            // right
                            c2 = c0 + 1;
                            // diagonal corner
                            c3 = c0 + rowOffset + 1;

                            if (x == 0 && y == 0 && bottom.IsValid && left.IsValid)
                            {
                                c0 = bottom.At(0, borderIndices);
                                c1 = left.At(1, borderIndices);
                                c2 = bottom.At(1, borderIndices);
                                c3 = vertexIndex;
                            }
                            else if (y == 0 && bottom.IsValid)
                            {
                                c0 = bottom.At(x, borderIndices);
                                c1 = vertexIndex + x + colOffset;
                                c2 = bottom.At(x + 1, borderIndices);
                                c3 = vertexIndex + x + 1 + colOffset;
                            }
                            else if (x == 0 && left.IsValid)
                            {
                                c0 = left.At(y, borderIndices);
                                c1 = left.At(y + 1, borderIndices);
                                c2 = vertexIndex;
                                c3 = vertexIndex + cellResolution;
                            }
                            else
                            {
                                ++vertexIndex;
                            }

                            SetHeightLerpValues(c0, c1, c2, c3, connectedDirection, x, y, cellResolution);
                        }

                        // right side vertex
                        if (y > 0 || !bottom.IsValid)
                        {
                            ++vertexIndex;
                        }
                    }
                }
Esempio n. 2
0
            private void GenerateGrid(int3 coord, Range left, Range bottom, Range right, Range top, int vertexIndex, int triangleIndex)
            {
                float startX = coord.x * cellSize.x;
                float startY = (coord.y + 1f) * cellSize.y;
                float startZ = coord.z * cellSize.z;

                float startU = coord.x;
                float startV = coord.z;

                float3 v = new float3 {
                    x = startX, y = startY, z = startZ
                };
                float2 uv = new float2 {
                    x = startU, y = startV
                };
                int c0, c1, c2, c3;
                int width     = cellResolution + 1;
                int rowOffset = left.IsValid ? width - 1 : width;
                int colOffset = left.IsValid ? -1 : 0;

                for (int y = 0; y < cellResolution; ++y)
                {
                    v.x  = startX;
                    uv.x = startU;

                    for (int x = 0; x < cellResolution; ++x)
                    {
                        // corner
                        c0 = vertexIndex;
                        // above
                        c1 = c0 + rowOffset;
                        // right
                        c2 = c0 + 1;
                        // diagonal corner
                        c3 = c0 + rowOffset + 1;

                        if (x == 0 && y == 0 && bottom.IsValid && left.IsValid)
                        {
                            c0 = bottom.At(0, borderIndices);
                            c1 = left.At(1, borderIndices);
                            c2 = bottom.At(1, borderIndices);
                            c3 = vertexIndex;
                        }
                        else if (y == 0 && bottom.IsValid)
                        {
                            c0 = bottom.At(x, borderIndices);
                            c1 = vertexIndex + x + colOffset;
                            c2 = bottom.At(x + 1, borderIndices);
                            c3 = vertexIndex + x + 1 + colOffset;
                        }
                        else if (x == 0 && left.IsValid)
                        {
                            c0 = left.At(y, borderIndices);
                            c1 = left.At(y + 1, borderIndices);
                            c2 = vertexIndex;
                            c3 = vertexIndex + cellResolution;
                        }
                        else
                        {
                            Add(v, uv, vertexIndex);
                            ++vertexIndex;
                        }

                        AddTris(c0, c1, c2, c3, triangleIndex);
                        triangleIndex += 6;

                        v.x  += stepX;
                        uv.x += stepU;
                    }

                    // right side vertex
                    if (y > 0 || !bottom.IsValid)
                    {
                        Add(v, uv, vertexIndex);
                        ++vertexIndex;
                    }

                    v.z  += stepZ;
                    uv.y += stepV;
                }

                // top row vertices
                v.x  = startX;
                uv.x = startU;

                for (int i = 0; i < width; ++i)
                {
                    if (i > 0 || !left.IsValid)
                    {
                        Add(v, uv, vertexIndex);
                        ++vertexIndex;
                    }
                    v.x  += stepX;
                    uv.x += stepU;
                }
            }