public void ConstructMesh()
    {
        //array of vertices = r^2
        vertices = new Vector3[resolution * resolution];

        //total number of meshes (3 connected vertecies) = (r-1)^2 * 2
        //total number of meshes * (vertecies each mesh uses) = ((r-1)^2 * 2) * 3
        int[] triangles = new int[(resolution - 1) * (resolution - 1) * 2 * 3];
        int   triIndex  = 0;

        Vector2[] uv = (mesh.uv.Length == vertices.Length) ? mesh.uv : new Vector2[vertices.Length];

        for (int y = 0; y < resolution; y++)
        {
            for (int x = 0; x < resolution; x++)
            {
                //index counter
                int i = x + y * resolution;

                //it varies between 0-1
                //starts with 0, each time added by a fraction
                Vector2 percent = new Vector2(x, y) / (resolution - 1);

                //the percent usage is very tricky and nice
                Vector3 pointOnUnitCube =
                    localUp                           //point position on the facing axis
                    + (percent.x - 0.5f) * 2 * axisA  //point position on the "relative" +x
                    + (percent.y - 0.5f) * 2 * axisB; //point position on the "relative" -y

                Vector3 pointOnUnitSphere = pointOnUnitCube.normalized;
                //vertices[i] = pointOnUnitCube;
                float unscaledElevation = shapeGenerator.CalculateUnscaledElevation(pointOnUnitSphere);
                vertices[i] = pointOnUnitSphere * shapeGenerator.GetScaledElevation(unscaledElevation);
                uv[i].y     = unscaledElevation;

                //I can not create any mesh on the edge
                if (x != resolution - 1 && y != resolution - 1)
                {
                    //first triangle
                    triangles[triIndex + 0] = i;
                    triangles[triIndex + 1] = i + resolution + 1;
                    triangles[triIndex + 2] = i + resolution;

                    //second triangle
                    triangles[triIndex + 3] = i;
                    triangles[triIndex + 4] = i + 1;
                    triangles[triIndex + 5] = i + resolution + 1;

                    triIndex += 6;
                }
            }
        }

        mesh.Clear(); //clear previous data
        mesh.vertices  = vertices;
        mesh.triangles = triangles;
        mesh.RecalculateNormals();
        mesh.uv = uv;
    }
Beispiel #2
0
    // ********************************************************************** //

    public void ConstructMesh()
    {
        // Make a mesh out of lots of small triangles defined over a grid of resolution size 'resolution'

        Vector3[] vertices  = new Vector3[resolution * resolution];
        int[]     triangles = new int[(resolution - 1) * (resolution - 1) * 6]; // this many triangles as a function of number of vertices on each dimension (i.e. resolution).
        int       triIndex  = 0;

        Vector2[] uv = (mesh.uv.Length == vertices.Length) ? mesh.uv : new Vector2[vertices.Length];  // if uv is correct length then all good, otherwise make a new one

        for (int y = 0; y < resolution; y++)
        {
            for (int x = 0; x < resolution; x++)
            {
                int     i               = x + y * resolution;                                                        // this is a counter, same as setting int i=0 outside y loop and incrementing on inner loop
                Vector2 percent         = new Vector2(x, y) / (resolution - 1);
                Vector3 pointOnUnitCube = localUp + (percent.x - 0.5f) * 2 * axisA + (percent.y - 0.5f) * 2 * axisB; // geometry magic from youtube tutorial
                //vertices[i] = pointOnUnitCube; // if you want a cube use this

                // Turn the cube into a sphere
                Vector3 pointOnUnitSphere = pointOnUnitCube.normalized;
                float   unscaledElevation = shapeGenerator.CalculateUnscaledElevation(pointOnUnitSphere);
                vertices[i] = pointOnUnitSphere * shapeGenerator.GetScaledElevation(unscaledElevation);
                uv[i].y     = unscaledElevation;

                // Note that vertices are labelled as: e.g. 0  1  2
                //                                          3  4  5
                //                                          6  7  8
                // and so triangles making up each square in this grid are indexed as e.g. 0-4-3; 0-1-4; 1-5-4; etc

                if ((x != resolution - 1) && (y != resolution - 1))
                {
                    // first triangle
                    triangles[triIndex]     = i;
                    triangles[triIndex + 1] = i + resolution + 1;
                    triangles[triIndex + 2] = i + resolution;

                    triangles[triIndex + 3] = i;
                    triangles[triIndex + 4] = i + 1;
                    triangles[triIndex + 5] = i + resolution + 1;
                    triIndex += 6;
                }
            }
        }
        mesh.Clear();
        mesh.vertices  = vertices;
        mesh.triangles = triangles;
        mesh.RecalculateNormals();
        mesh.uv = uv;
    }
    public void ConstructMesh()
    {
        Vector3[] vertices = new Vector3[resolution * resolution];
        //-> 6 = number of triangles to make a square * number of vertices to make a triangle (2*3)
        int[] triangles = new int[(resolution - 1) * (resolution - 1) * 6];
        int   i         = 0;
        int   triIndex  = 0;

        Vector2[] UV = (mesh.uv.Length == vertices.Length) ? mesh.uv : new Vector2[vertices.Length];

        for (int y = 0; y < resolution; y++)
        {
            for (int x = 0; x < resolution; x++)
            {
                // how close to complete each of these loops is
                Vector2 percent         = new Vector2(x, y) / (resolution - 1);
                Vector3 pointOnUnitCube = localUp + (percent.x - .5f) * 2 * axisA + (percent.y - .5f) * 2 * axisB;
                // transform the current planar cube as a spherical/round plane
                Vector3 pointOnUnitSphere = pointOnUnitCube.normalized;
                float   unscaledElevation = shapeGenerator.CalculateUnscaledElevation(pointOnUnitSphere);
                vertices[i] = pointOnUnitSphere * shapeGenerator.GetScaledElevation(unscaledElevation);
                // using the y axis for the biomes
                UV[i].y = unscaledElevation;

                // make a square for this mesh
                if (x != resolution - 1 && y != resolution - 1)
                {
                    // first triangle
                    triangles[triIndex]     = i;
                    triangles[triIndex + 1] = i + resolution + 1;
                    triangles[triIndex + 2] = i + resolution;
                    // second triangle
                    triangles[triIndex + 3] = i;
                    triangles[triIndex + 4] = i + 1;
                    triangles[triIndex + 5] = i + resolution + 1;
                    // we added 6 vertices, update the index count
                    triIndex += 6;
                }
                ++i;
            }
        }
        // clear all the data from the mesh to prevent
        // adding to the previous lower resolution mesh
        mesh.Clear();

        mesh.vertices  = vertices;
        mesh.triangles = triangles;
        mesh.RecalculateNormals();
        mesh.uv = UV;
    }
Beispiel #4
0
    public void ConstructMesh()
    {
        Vector3[] vertices  = new Vector3[resolution * resolution];
        int[]     triangles = new int[(resolution - 1) * (resolution - 1) * 6]; // number of triangles for a square mesh with resolution vertices per side, times 3 for the number of vertex triplets
        int       triIndex  = 0;

        Vector2[] uv = (mesh.uv.Length == vertices.Length) ? mesh.uv : new Vector2[vertices.Length]; // save uv before resetting mesh, to avoid recalculating

        for (int y = 0; y < resolution; y++)
        {
            for (int x = 0; x < resolution; x++)
            {
                int i = x + y * resolution;                                                                          // number of total iterations

                Vector2 percent           = new Vector2(x, y) / (resolution - 1);                                    // percent on the x-axis and y-axis, between 0 and 1
                Vector3 pointOnUnitCube   = localUp + (percent.x - .5f) * 2 * axisA + (percent.y - .5f) * 2 * axisB; // sum of base vectors
                Vector3 pointOnUnitSphere = pointOnUnitCube.normalized;

                float unscaledElevation = shapeGenerator.CalculateUnscaledElevation(pointOnUnitSphere);
                vertices[i] = pointOnUnitSphere * shapeGenerator.GetScaledElevation(unscaledElevation);
                uv[i].y     = unscaledElevation;

                // Create the triangles, if not on right or bottom edges
                if (x != resolution - 1 && y != resolution - 1)
                {
                    // Create triangle South-East
                    triangles[triIndex]     = i;
                    triangles[triIndex + 1] = i + resolution + 1;
                    triangles[triIndex + 2] = i + resolution;

                    // Create triangle East-South
                    triangles[triIndex + 3] = i;
                    triangles[triIndex + 4] = i + 1;
                    triangles[triIndex + 5] = i + resolution + 1;

                    // Increment triIndex
                    triIndex += 6;
                }
            }
        }

        // Refresh mesh
        mesh.Clear(); // clear all data to avoid errors when downgrading resolution
        mesh.vertices  = vertices;
        mesh.triangles = triangles;
        mesh.RecalculateNormals();

        mesh.uv = uv; // reassigned saved uv
    }
    public void ConstructMesh()
    {
        Vector3[] vertices  = new Vector3[resolution * resolution];
        int[]     triangles = new int[(resolution - 1) * (resolution - 1) * 6];
        int       triIndex  = 0;

        Vector2[] uv;
        if (mesh.uv.Length == vertices.Length)
        {
            uv = mesh.uv;
        }
        else
        {
            uv = new Vector2[vertices.Length];
        }

        for (int y = 0; y < resolution; y++)
        {
            for (int x = 0; x < resolution; x++)
            {
                int     i                 = x + y * resolution;
                Vector2 percent           = new Vector2(x, y) / (resolution - 1);
                Vector3 pointOnUnitCube   = localUp + (percent.x - .5f) * 2 * axisA + (percent.y - .5f) * 2 * axisB;
                Vector3 pointOnUnitSphere = pointOnUnitCube.normalized;
                float   unscaledElevation = shapeGenerator.CalculateUnscaledElevation(pointOnUnitSphere);
                vertices[i] = pointOnUnitSphere * shapeGenerator.GetScaledElevation(unscaledElevation);
                uv[i].y     = unscaledElevation;

                if (x != resolution - 1 && y != resolution - 1)
                {
                    triangles[triIndex]     = i;
                    triangles[triIndex + 1] = i + resolution + 1;
                    triangles[triIndex + 2] = i + resolution;

                    triangles[triIndex + 3] = i;
                    triangles[triIndex + 4] = i + 1;
                    triangles[triIndex + 5] = i + resolution + 1;
                    triIndex += 6;
                }
            }
        }
        mesh.Clear();
        mesh.vertices  = vertices;
        mesh.triangles = triangles;
        mesh.RecalculateNormals();
        mesh.uv = uv;
        meshCollider.sharedMesh = mesh;
    }
    public void ConstructMesh()
    {
        Vector3[] _vertices      = new Vector3[resolution * resolution];
        int[]     _triangles     = new int[(resolution - 1) * (resolution - 1) * 6];
        int       _triangleIndex = 0;

        Vector2[] _uvData = (mesh.uv.Length == _vertices.Length) ? mesh.uv : new Vector2[_vertices.Length];

        for (int y = 0; y < resolution; y++)
        {
            for (int x = 0; x < resolution; x++)
            {
                int _index = y * resolution + x;

                Vector2 _percent           = new Vector2(x, y) / (resolution - 1);
                Vector3 _pointOnUnitCube   = localUp + (_percent.x - 0.5f) * 2 * axisA + (_percent.y - 0.5f) * 2 * axisB;
                Vector3 _pointOnUnitSphere = _pointOnUnitCube.normalized;

                float _unscaledElevation = shapeGenerator.CalculateUnscaledElevation(_pointOnUnitSphere);
                _vertices[_index] = _pointOnUnitSphere * shapeGenerator.GetScaledElevation(_unscaledElevation);
                _uvData[_index].y = _unscaledElevation;

                if (x != resolution - 1 && y != resolution - 1)
                {
                    // first triangle in square
                    _triangles[_triangleIndex]     = _index;
                    _triangles[_triangleIndex + 1] = _index + resolution + 1;
                    _triangles[_triangleIndex + 2] = _index + resolution;

                    // second triangle in square
                    _triangles[_triangleIndex + 3] = _index;
                    _triangles[_triangleIndex + 4] = _index + 1;
                    _triangles[_triangleIndex + 5] = _index + resolution + 1;

                    _triangleIndex += 6;
                }
            }
        }

        mesh.Clear();
        mesh.vertices  = _vertices;
        mesh.triangles = _triangles;
        mesh.RecalculateNormals();
        mesh.uv = _uvData;
    }
Beispiel #7
0
    public void ConstructMesh()
    {
        var vertices  = new Vector3[resolution * resolution];
        var triangles = new int[(resolution - 1) * (resolution - 1) * 6];
        var triIndex  = 0;
        var uv        = mesh.uv.Length == vertices.Length ? mesh.uv : new Vector2[vertices.Length];

        for (var y = 0; y < resolution; y++)
        {
            for (var x = 0; x < resolution; x++)
            {
                var i                 = x + y * resolution;
                var percent           = new Vector2(x, y) / (resolution - 1);
                var pointOnCube       = localUp + (percent.x - .5f) * 2 * axisA + (percent.y - .5f) * 2 * axisB;
                var pointOnSphere     = pointOnCube.normalized;
                var unscaledElevation = shapeGenerator.CalculateUnscaledElevation(pointOnSphere);
                vertices[i] = pointOnSphere * shapeGenerator.GetScaledElevation(unscaledElevation);
                uv[i].y     = unscaledElevation;

                if (x == resolution - 1 || y == resolution - 1)
                {
                    continue;
                }

                triangles[triIndex]     = i;
                triangles[triIndex + 1] = i + resolution + 1;
                triangles[triIndex + 2] = i + resolution;

                triangles[triIndex + 3] = i;
                triangles[triIndex + 4] = i + 1;
                triangles[triIndex + 5] = i + resolution + 1;

                triIndex += 6;
            }
        }

        mesh.Clear();
        mesh.vertices  = vertices;
        mesh.triangles = triangles;
        mesh.RecalculateNormals();
        mesh.uv = uv;
    }
    public void ConstructMesh()
    {
        Vector3[] vertices  = new Vector3[resolution * resolution];
        int[]     triangles = new int[(resolution - 1) * (resolution - 1) * 6]; // https://prnt.sc/r6if91
        int       triIndex  = 0;

        Vector2[] uv = (mesh.uv.Length == vertices.Length) ? mesh.uv : new Vector2[vertices.Length];

        for (int y = 0; y < resolution; y++)
        {
            for (int x = 0; x < resolution; x++)
            {
                int     i                 = x + y * resolution;
                Vector2 percent           = new Vector2(x, y) / (resolution - 1);
                Vector3 pointOnUnitCube   = axisY + (percent.x - 0.5f) * 2 * axisX + (percent.y - 0.5f) * 2 * axisZ;
                Vector3 pointOnUnitSphere = pointOnUnitCube.normalized;
                float   unscaledElevation = shapeGenerator.CalculateUnscaledElevation(pointOnUnitSphere);
                vertices[i] = pointOnUnitSphere * shapeGenerator.GetScaledElevation(unscaledElevation);
                uv[i].y     = unscaledElevation;

                // do not construct for the border of the mesh
                if (x != resolution - 1 && y != resolution - 1)
                {
                    triangles[triIndex]     = i;
                    triangles[triIndex + 1] = i + resolution + 1;
                    triangles[triIndex + 2] = i + resolution;

                    triangles[triIndex + 3] = i;
                    triangles[triIndex + 4] = i + 1;
                    triangles[triIndex + 5] = i + resolution + 1;

                    triIndex += 6;
                }
            }
        }

        mesh.Clear();
        mesh.vertices  = vertices;
        mesh.triangles = triangles;
        mesh.RecalculateNormals();
        mesh.uv = uv;
    }
Beispiel #9
0
    public void ConstructMesh()
    {
        Vector3[] vertices  = new Vector3[resolution * resolution];             //Store all vertices
        int[]     triangles = new int[(resolution - 1) * (resolution - 1) * 6]; //Int array to indeces of all triangles. size = resolution-1 squared(number of squares) * 2 (number of triangles per square) * 3 (number of vertices per triangle)
        int       triIndex  = 0;

        //Debug.Log(mesh); //mesh is setto null for some reason...
        Vector2[] uv = (mesh.uv.Length == vertices.Length)?mesh.uv:new Vector2[vertices.Length];

        for (int y = 0; y < resolution; y++)
        {
            for (int x = 0; x < resolution; x++) //Iterate over entire face
            {
                int     i                 = x + y * resolution;
                Vector2 percent           = new Vector2(x, y) / (resolution - 1); //Tells us how far through the loop we are
                Vector3 pointOnUnitCube   = localUp + (percent.x - .5f) * 2 * axisA + (percent.y - .5f) * 2 * axisB;
                Vector3 pointOnUnitSphere = pointOnUnitCube.normalized;           //used to go from a cube to a sphere
                float   unscaledElevation = shapeGenerator.CalculateUnscaledElevation(pointOnUnitSphere);
                vertices[i] = pointOnUnitSphere * shapeGenerator.GetScaledElevation(unscaledElevation);
                uv[i].y     = unscaledElevation;

                if (x != resolution - 1 && y != resolution - 1) //vertex is not along the right or bottom edges
                {
                    triangles[triIndex]     = i;                //Build the two triangles that make up a square by defining their 6 points (3 per triangle)
                    triangles[triIndex + 1] = i + resolution + 1;
                    triangles[triIndex + 2] = i + resolution;

                    triangles[triIndex + 3] = i;
                    triangles[triIndex + 4] = i + 1;
                    triangles[triIndex + 5] = i + resolution + 1;

                    triIndex += 6;
                }
            }
        }
        mesh.Clear(); //If we update with a lower resolution, then when we set mesh.vertices, mesh.triangles will reference vertices that no longer exist and will throw an error. Therefore we need to clear the mesh before setting the new vertices
        mesh.vertices  = vertices;
        mesh.triangles = triangles;
        mesh.RecalculateNormals();
        mesh.uv = uv;
    }
Beispiel #10
0
    public void GenerateMesh()
    {
        var vertices  = new Vector3[resolution * resolution];
        var triangles = new int[(resolution - 1) * (resolution - 1) * 6];

        Vector2[] uv = (mesh.uv.Length == vertices.Length) ? mesh.uv : new Vector2[vertices.Length];

        int triIndex = 0;

        for (int y = 0; y < resolution; y++)
        {
            for (int x = 0; x < resolution; x++)
            {
                int   i                 = x + y * resolution;
                var   percent           = new Vector2(x, y) / (resolution - 1);
                var   pointOnUnitCube   = localUp + (percent.x - 0.5f) * 2 * axisA + (percent.y - 0.5f) * 2 * axisB;
                var   pointOnUnitSphere = pointOnUnitCube.normalized;
                float unscaledElevation = shapeGenerator.CalculateUnscaledElevation(pointOnUnitSphere);
                vertices[i] = pointOnUnitSphere * shapeGenerator.GetScaledElevation(unscaledElevation);
                uv[i].y     = unscaledElevation;

                if (x != resolution - 1 && y != resolution - 1)
                {
                    triangles[triIndex]     = i;
                    triangles[triIndex + 1] = i + resolution + 1;
                    triangles[triIndex + 2] = i + resolution;

                    triangles[triIndex + 3] = i;
                    triangles[triIndex + 4] = i + 1;
                    triangles[triIndex + 5] = i + resolution + 1;

                    triIndex += 6;
                }
            }
        }
        mesh.Clear();
        mesh.vertices  = vertices;
        mesh.triangles = triangles;
        mesh.RecalculateNormals();
        mesh.uv = uv;
    }
Beispiel #11
0
    public void ConstructMesh()
    {
        Vector3[] vertices  = new Vector3[resolution * resolution];
        int[]     triangles = new int[(resolution - 1) * (resolution - 1) * 6];

        int triIndex = 0;

        Vector2[] uv = (mesh.uv.Length == vertices.Length) ? mesh.uv : new Vector2[vertices.Length];

        for (int y = 0, i = 0; y < resolution; ++y)
        {
            for (int x = 0; x < resolution; ++x, ++i)
            {
                Vector2 percent           = new Vector2(x, y) / (resolution - 1);
                Vector3 pointOnUnitCube   = localUp + (percent.x - 0.5f) * 2 * axisA + (percent.y - 0.5f) * 2 * axisB;
                Vector3 pointOnUnitSphere = pointOnUnitCube.normalized;

                float unscaledElevation = shapeGenerator.CalculateUnscaledElevation(pointOnUnitSphere);
                vertices[i] = pointOnUnitSphere * shapeGenerator.GetScaledElevation(unscaledElevation);
                uv[i].y     = unscaledElevation;

                if (x != resolution - 1 && y != resolution - 1)
                {
                    triangles[triIndex++] = i;
                    triangles[triIndex++] = i + resolution + 1;
                    triangles[triIndex++] = i + resolution;

                    triangles[triIndex++] = i;
                    triangles[triIndex++] = i + 1;
                    triangles[triIndex++] = i + resolution + 1;
                }
            }
        }

        mesh.Clear();
        mesh.vertices  = vertices;
        mesh.triangles = triangles;
        mesh.RecalculateNormals();
        mesh.uv = uv;
    }
Beispiel #12
0
    public void ConstructMesh(int levelOfDetail)
    {
        int simplificationIncriment = levelOfDetail;        //levelOfDetail==0?1:levelOfDetail*2;
        int verticesPerLine         = (resolution - 1) / simplificationIncriment + 1;
        //LOD
        MeshData meshData    = new MeshData(verticesPerLine, verticesPerLine);
        int      vertexIndex = 0;

        Vector3[] vertices  = new Vector3[resolution * resolution];
        int[]     triangles = new int[(resolution - 1) * (resolution - 1) * 6];
        Vector2[] uv        = (mesh.uv.Length == vertices.Length)?mesh.uv:new Vector2[vertices.Length];

        for (int y = 0; y < resolution; y += simplificationIncriment)
        {
            for (int x = 0; x < resolution; x += simplificationIncriment)
            {
                //LOD
                //vertexIndex=x + y * resolution;

                Vector2 percent           = new Vector2(x, y) / (resolution - 1);
                Vector3 pointOnUnitCube   = localUp + (percent.x - .5f * subfaceNumber + (subfaceIndex % subfaceNumber)) * (2f / subfaceNumber) * axisA + (percent.y - .5f * subfaceNumber + (subfaceIndex / subfaceNumber)) * (2f / subfaceNumber) * axisB;
                Vector3 pointOnUnitSphere = pointOnUnitCube.normalized;
                float   unscaledElevation = shapeGenerator.CalculateUnscaledElevation(pointOnUnitSphere);

                //LOD
                meshData.vertices[vertexIndex] = pointOnUnitSphere * shapeGenerator.GetScaledElevation(unscaledElevation);
                meshData.uvs[vertexIndex].y    = unscaledElevation;
                if (x < resolution - 1 && y < resolution - 1)
                {
                    meshData.AddTriangle(vertexIndex, vertexIndex + verticesPerLine + 1, vertexIndex + verticesPerLine);
                    meshData.AddTriangle(vertexIndex, vertexIndex + 1, vertexIndex + verticesPerLine + 1);
                }
                vertexIndex++;
            }
        }
        mesh.Clear();
        mesh = meshData.CreateMesh(mesh);
    }
Beispiel #13
0
    public void ConstructMesh(int xMesh, int yMesh, int divisions, Vector3 center)
    {
        Vector3[] vertices      = new Vector3[resolution * resolution];
        int[]     triangles     = new int[(resolution - 1) * (resolution - 1) * 6];
        int[]     realTriangles = new int[(resolution - 1) * (resolution - 1) * 6];
        int       triIndex      = 0;

        Vector2[] uv = (mesh.uv.Length == vertices.Length)?mesh.uv:new Vector2[vertices.Length];

        Vector3[] norms = new Vector3[resolution * resolution];
        for (int y = 0; y < resolution; y++)
        {
            for (int x = 0; x < resolution; x++)
            {
                int     i                 = x + y * resolution;
                Vector2 percent           = new Vector2(x - 0, y - 0) / (resolution - 1 - 0);
                Vector3 pointOnUnitCube   = localUp + ((float)xMesh / divisions + percent.x / divisions - .5f) * 2 * axisA + ((float)yMesh / divisions + percent.y / divisions - .5f) * 2 * axisB;
                Vector3 pointOnUnitSphere = pointOnUnitCube.normalized;
                float   unscaledElevation = shapeGenerator.CalculateUnscaledElevation(pointOnUnitSphere);
                vertices[i] = pointOnUnitSphere * shapeGenerator.GetScaledElevation(unscaledElevation);
                uv[i].x     = (unscaledElevation > 0) ? unscaledElevation : unscaledElevation * 1;
                if (unscaledElevation > 0.2f)
                {
                    //Debug.Log(unscaledElevation);
                }

                if (x < resolution - 1 && y < resolution - 1)
                {
                    triangles[triIndex]     = i;
                    triangles[triIndex + 1] = i + resolution + 1;
                    triangles[triIndex + 2] = i + resolution;

                    triangles[triIndex + 3] = i;
                    triangles[triIndex + 4] = i + 1;
                    triangles[triIndex + 5] = i + resolution + 1;
                    if (x < hiddenResolution - 1 && y < hiddenResolution - 1)
                    {
                        realTriangles[triIndex]     = i;
                        realTriangles[triIndex + 1] = i + resolution + 1;
                        realTriangles[triIndex + 2] = i + resolution;

                        realTriangles[triIndex + 3] = i;
                        realTriangles[triIndex + 4] = i + 1;
                        realTriangles[triIndex + 5] = i + resolution + 1;
                    }
                    triIndex += 6;
                }
            }
        }
        mesh.Clear();
        mesh.vertices  = vertices;
        mesh.triangles = triangles;
        mesh.RecalculateNormals();
        //mesh.triangles = realTriangles;
        mesh.uv = uv;

        norms = mesh.normals;
        for (int y = 0; y < resolution; y++)
        {
            for (int x = 0; x < resolution; x++)
            {
                if (x == 0 || x == (resolution - 1) || y == 0 || y == (resolution - 1))
                {
                    int     i                 = x + y * resolution;
                    Vector2 percent           = new Vector2(x - 0, y - 0) / (resolution - 1 - 0);
                    Vector3 pointOnUnitCube   = localUp + ((float)xMesh / divisions + percent.x / divisions - .5f) * 2 * axisA + ((float)yMesh / divisions + percent.y / divisions - .5f) * 2 * axisB;
                    Vector3 pointOnUnitSphere = pointOnUnitCube.normalized;
                    norms[i] = pointOnUnitSphere;
                }
            }
        }
        mesh.normals = norms;
    }
Beispiel #14
0
    public void ConstructMesh()
    {
        Vector3[] vertices  = new Vector3[resolution * resolution];
        int[]     triangles = new int[(resolution - 1) * (resolution - 1) * 6];

        Vector3[] borderVertices  = new Vector3[resolution * 4 + 4];
        int[]     borderTriangles = new int[24 * resolution];

        int triIndex       = 0;
        int borderTriIndex = 0;

        Vector2[] uv = (mesh.uv.Length == vertices.Length) ? mesh.uv : new Vector2[vertices.Length];

        int[,] vertexIndicesMap = new int[borderedResolution, borderedResolution];
        int meshVertexIndex   = 0;
        int borderVertexIndex = -1;

        for (int y = 0; y < borderedResolution; y++)
        {
            for (int x = 0; x < borderedResolution; x++)
            {
                bool isBorderVertex = y == 0 || y == borderedResolution - 1 || x == 0 || x == borderedResolution - 1;

                if (isBorderVertex)
                {
                    vertexIndicesMap[x, y] = borderVertexIndex;
                    borderVertexIndex--;
                }
                else
                {
                    vertexIndicesMap[x, y] = meshVertexIndex;
                    meshVertexIndex++;
                }
            }
        }

        for (int y = 0; y < borderedResolution; y++)
        {
            for (int x = 0; x < borderedResolution; x++)
            {
                int     i                 = vertexIndicesMap[x, y];
                Vector2 percent           = new Vector2(x - 1, y - 1) / (resolution - 1);
                Vector3 pointOnUnitCube   = localUp + (percent.x - 0.5f) * 2 * axisA + (percent.y - 0.5f) * 2 * axisB;
                Vector3 pointOnUnitSphere = pointOnUnitCube.normalized;
                float   unscaledElevation = shapeGenerator.CalculateUnscaledElevation(pointOnUnitSphere);
                float   scaledElevation   = shapeGenerator.GetScaledElevation(unscaledElevation);
                // shapeGenerator.elevationMinMax.AddValue(scaledElevation);
                Vector3 vertexPosition = pointOnUnitSphere * scaledElevation;
                float   vertexUV       = unscaledElevation;
                AddVertex(vertexPosition, vertexUV, i, ref vertices, ref uv, ref borderVertices);

                if (x != borderedResolution - 1 && y != borderedResolution - 1)
                {
                    int a = vertexIndicesMap[x, y];
                    int b = vertexIndicesMap[x + 1, y];
                    int c = vertexIndicesMap[x, y + 1];
                    int d = vertexIndicesMap[x + 1, y + 1];

                    AddTriangle(a, d, c, ref triIndex, ref borderTriIndex, ref triangles, ref borderTriangles);

                    AddTriangle(d, a, b, ref triIndex, ref borderTriIndex, ref triangles, ref borderTriangles);
                }
            }
        }

        mesh.Clear();
        mesh.vertices  = vertices;
        mesh.triangles = triangles;
        mesh.normals   = CalculateNormals(ref triangles, ref borderTriangles, ref vertices, ref borderVertices);

        mesh.uv = uv;
    }