public void ConstructMesh()
    {
        var vertices      = new Vector3[resolution * resolution];
        var triangles     = new int[(resolution - 1) * (resolution - 1) * 2 * 3];
        var triangleIndex = 0;

        for (int y = 0; y < resolution; ++y)
        {
            for (int x = 0; x < resolution; ++x)
            {
                var 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 pointOnUnitSpere = pointOnUnitCube.normalized;

                vertices[i] = shapeGenerator.CalculatePointOnPlanet(pointOnUnitSpere);

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

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

        mesh.Clear();
        mesh.vertices  = vertices;
        mesh.triangles = triangles;
        mesh.RecalculateNormals();
    }
Exemple #2
0
    public void ConstructMesh()
    {
        vertices = new Vector3[resolution * resolution];

        for (int y = 0; y < resolution; y++)
        {
            for (int x = 0; x < 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;

                int i = x + y * resolution;
                vertices [i] = shapeGenerator.CalculatePointOnPlanet(pointOnUnitSphere);
            }
        }

        int mod = -1;

        for (int i = 0; i < tileCount; i++)
        {
            Vector3[] tileVertices = new Vector3[4];

            mod += (i % (resolution - 1) == 0) ? 1 : 0;

            tileVertices [0] = vertices [i + mod];
            tileVertices [1] = vertices [i + 1 + mod];
            tileVertices [2] = vertices [i + resolution + mod];
            tileVertices [3] = vertices [i + resolution + 1 + mod];


            tiles [i].UpdateMesh(tileVertices);
        }
    }
Exemple #3
0
    public void ConstructMesh()
    {
        Vector3[] vertices  = new Vector3[resolution * resolution];
        int[]     triangles = new int[(resolution - 1) * (resolution - 1) * 6];
        int       triIndex  = 0;

        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 - 0.5f) * 2 * axisA + (percent.y - 0.5f) * 2 * axisB;
                Vector3 pointOnUnitSphere = pointOnUnitCube.normalized;
                vertices[i] = shapeGenerator.CalculatePointOnPlanet(pointOnUnitSphere);

                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();
    }
    public void ConstructMesh()
    {
        Vector3[] vertices = new Vector3[resolution * resolution];
        // (resolution -1) * (resolution -1) counts quads, times 6 to count all four cube sides
        int[] triangles = new int[(resolution - 1) * (resolution - 1) * 6];
        int   triIndex  = 0;

        for (int y = 0; y < resolution; y++)
        {
            for (int x = 0; x < resolution; x++)
            {
                // index math below is same as having i = 0 above for loops and iterating at end of each loop
                int     i                 = x + y * resolution;
                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;
                vertices[i] = shapeGenerator.CalculatePointOnPlanet(pointOnUnitSphere);

                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();
    }
Exemple #5
0
    public void ConstructMesh()
    {
        Vector3[] vertices  = new Vector3[resolution * resolution];
        int[]     triangles = new int[(resolution - 1) * (resolution - 1) * 6];
        int       triIndex  = 0;

        for (int y = 0; y < resolution; ++y)
        {
            for (int x = 0; x < resolution; ++x)
            {
                int     index             = x + y * resolution;
                Vector2 percent           = new Vector2(x, y) / (resolution - 1);
                Vector3 pointOnUnitCube   = normal + (percent.x - .5f) * 2 * tangent + (percent.y - .5f) * 2 * biTangent;
                Vector3 pointOnUnitSphere = Vector3.Normalize(pointOnUnitCube);
                vertices[index] = shapeGenerator.CalculatePointOnPlanet(pointOnUnitSphere);

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

                    triangles[triIndex + 3] = index;
                    triangles[triIndex + 4] = index + 1;
                    triangles[triIndex + 5] = index + resolution + 1;
                    triIndex += 6;
                }
            }
        }

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

        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 pointOnUnitCube   = localUp + ((percent.x - .5f) * 2 * axisA) + ((percent.y - .5f) * 2 * axisB);
                var pointOnUnitSphere = pointOnUnitCube.normalized;
                vertices[i] = shapeGenerator.CalculatePointOnPlanet(pointOnUnitSphere);

                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();
    }
    public void ConstructMesh()
    {
        Vector3[] vertices  = new Vector3[resolution * resolution];
        int[]     triangles = new int[(resolution - 1) * (resolution - 1) * 6];
        int       triIndex  = 0;

        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;
                vertices[i]          = shapeGenerator.CalculatePointOnPlanet(pointOnUnitSphere);
                Sommets[i]           = meshObj.AddComponent <Sommet>();
                Sommets[i].hideFlags = HideFlags.HideInInspector;
                Sommets[i].Initialize(planet, faceID, vertices[i], x, y);

                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();

        MeshCollider meshCollider = meshObj.AddComponent <MeshCollider>();

        meshCollider.sharedMesh = mesh;
    }
Exemple #8
0
    public void constructMesh()
    {
        // hold vertices
        Vector3[] vertices = new Vector3[resolution * resolution];

        int amountOfVerticesPerSide = resolution - 1;
        int trianglesInASquare      = 2;
        int verticesInATriangle     = 3;

        int[] triangles = new int[
            amountOfVerticesPerSide * amountOfVerticesPerSide
            * trianglesInASquare * verticesInATriangle
                          ];

        int triangleIndex = 0;

        for (int y = 0; y < resolution; y++)
        {
            for (int x = 0; x < resolution; x++)
            {
                int     i = x + y * resolution;
                Vector2 meshCompletion = new Vector2(x, y) / (resolution - 1);

                // Where the vertex is along the face
                Vector3 pointOnUnitCube = localUp + (meshCompletion.x - 0.5f) * 2 * axisA + (meshCompletion.y - 0.5f) * 2 * axisB;

                // Normalize the unit cube
                Vector3 pointOnUnitSphere = pointOnUnitCube.normalized;

                // Calculate index to add to vertices array
                vertices[i] = shapeGenerator.CalculatePointOnPlanet(pointOnUnitSphere);

                if (x != resolution - 1 && y != resolution - 1)
                {
                    // Lower Left Hand Triangle of a quad
                    triangles[triangleIndex]     = i;
                    triangles[triangleIndex + 1] = i + resolution + 1;
                    triangles[triangleIndex + 2] = i + resolution;

                    // Upper Right hand Triangle of a quad
                    triangles[triangleIndex + 3] = i;
                    triangles[triangleIndex + 4] = i + 1;
                    triangles[triangleIndex + 5] = i + resolution + 1;

                    // We moved along by 6 entries in triangles so increment by 6
                    triangleIndex += 6;
                }
            }
        }
        // Assign vertices and triangles to mesh
        mesh.Clear();  // in case we lower resolution to avoid indexOutOfBounds error
        mesh.vertices  = vertices;
        mesh.triangles = triangles;
        mesh.RecalculateNormals();
    }
Exemple #9
0
    public void ConstructMesh()
    {
        Vector3[] vertices = new Vector3[resolution * resolution];

        int[] triangles = new int[(resolution - 1) * (resolution - 1) * 6];
        int[] uvs       = new int[(resolution - 1) * (resolution - 1) * 4];
        int   triIndex  = 0;
        int   uvIndex   = 0;

        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   noise             = shapeGenerator.getNoise(pointOnUnitSphere);
                vertices[i] = shapeGenerator.CalculatePointOnPlanet(noise, pointOnUnitSphere);

                if (!isTutorialWorld)
                {
                    if (rg.ShouldPlaceTree(vertices[i]) && noise > 0.001f && x % 3 == 0 && y % 3 == 0 && this.context.PType().planetName == PlanetName.Normal)
                    {
                        rg.PlaceTree(vertices[i] - min);
                    }
                    else if (rg.ShouldPlaceMine(vertices[i]) && noise > 0.001f && x % 7 == 0 && y % 7 == 0)
                    {
                        rg.PlaceMine(vertices[i]);
                    }
                }

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

                    //uvs[uvIndex]

                    triangles[triIndex + 3] = i;
                    triangles[triIndex + 4] = i + 1;
                    triangles[triIndex + 5] = i + resolution + 1;
                    triIndex += 6;
                    uvIndex  += 4;
                }
            }
        }
        mesh.Clear();
        mesh.vertices  = vertices;
        mesh.triangles = triangles;
        mesh.RecalculateNormals();
        collider.sharedMesh = mesh;
        collider.convex     = true;
    }
 public void UpdateMesh()
 {
     Vector3[] updatedVertices = new Vector3[resolution * resolution];
     int[]     tempTriangles   = mesh.triangles;
     for (int i = 0; i < resolution * resolution; i++)
     {
         updatedVertices[i] = shapeGenerator.CalculatePointOnPlanet(this.vertices[i]);
     }
     mesh.Clear();
     mesh.vertices  = updatedVertices;
     mesh.triangles = tempTriangles;
     mesh.RecalculateNormals();
 }
Exemple #11
0
 public void UpdateMesh()
 {
     Vector3[] updatedVertices = new Vector3[this.vertices.Count];
     int[]     tempTriangles   = mesh.triangles;
     Vector2[] uvs             = mesh.uv;
     for (int i = 0; i < this.vertices.Count; i++)
     {
         updatedVertices[i] = shapeGenerator.CalculatePointOnPlanet(this.vertices[i]);
     }
     mesh.Clear();
     mesh.vertices  = updatedVertices;
     mesh.triangles = tempTriangles;
     mesh.uv        = uvs;
     mesh.RecalculateNormals();
 }
    public void ConstructMesh()
    {
        Vector3[] vertices  = new Vector3[_resolution * _resolution];
        int[]     triangles = new int[(_resolution - 1) * (_resolution - 1) * 6];
        int       triIndex  = 0;

        for (int y = 0; y < _resolution; y++)
        {
            for (int x = 0; x < _resolution; x++)
            {
                int     iterations      = x + y * _resolution;
                Vector2 percent         = new Vector2(x, y) / (_resolution - 1);
                Vector3 pointOnUnitCube = _localUp + (percent.x - 0.5f) * 2 * _axisA
                                          + (percent.y - 0.5f) * 2 * _axisB;
                Vector3 pointsOnUnitSphere = pointOnUnitCube.normalized;
                vertices[iterations] = _shapeGenerator.CalculatePointOnPlanet(pointsOnUnitSphere);

                // Another approach from catlikecoding  - https://catlikecoding.com/unity/tutorials/cube-sphere/
//                float x2 = pointOnUnitCube.x * pointOnUnitCube.x;
//                float y2 = pointOnUnitCube.y * pointOnUnitCube.y;
//                float z2 = pointOnUnitCube.z * pointOnUnitCube.z;
//                Vector3 pointOnUnitSphere = new Vector3(pointOnUnitCube.x * Mathf.Sqrt(1f - y2 / 2f - z2 / 2f + y2 * z2 / 3f), pointOnUnitCube.y * Mathf.Sqrt(1f - x2 / 2f - z2 / 2f + x2 * z2 / 3f), pointOnUnitCube.z * Mathf.Sqrt(1f - x2 / 2f - y2 / 2f + x2 * y2 / 3f));
//                vertices[iterations] = pointsOnUnitSphere;

                // avoiding the edges
                if (x == _resolution - 1 || y == _resolution - 1)
                {
                    continue;
                }

                // two triangles, clockwise order
                triangles[triIndex]     = iterations;
                triangles[triIndex + 1] = iterations + _resolution + 1;
                triangles[triIndex + 2] = iterations + 1;

                triangles[triIndex + 3] = iterations;
                triangles[triIndex + 4] = iterations + _resolution;
                triangles[triIndex + 5] = iterations + _resolution + 1;
                triIndex += 6;
            }
        }

        _mesh.Clear();
        _mesh.vertices  = vertices;
        _mesh.triangles = triangles;
        _mesh.RecalculateNormals();
    }
Exemple #13
0
    public void ConstructMesh()
    {
        Vector3[] vertices      = new Vector3[m_res * m_res];
        int[]     triangles     = new int[(m_res - 1) * (m_res - 1) * 2 * 3];
        int       triangleIndex = 0;

        Vector2[] uv = m_mesh.uv;

        CellHeighMap = new float[m_res, m_res];

        for (int y = 0; y < m_res; y++)
        {
            for (int x = 0; x < m_res; x++)
            {
                int     i                 = x + y * m_res;
                Vector2 percent           = new Vector2(x, y) / (m_res - 1);
                Vector3 pointOnUnitCube   = m_localUp + (percent.x - 0.5f) * 2.0f * m_axisA + (percent.y - 0.5f) * 2.0f * m_axisB;
                Vector3 pointOnUnitSphere = pointOnUnitCube.normalized;
                vertices[i] = m_shapeGenerator.CalculatePointOnPlanet(pointOnUnitSphere);

                float elevation = vertices[i].x / pointOnUnitSphere.x;
                CellHeighMap[x, y] = elevation;

                if (y != m_res - 1 && x != m_res - 1)
                {
                    triangles[triangleIndex + 0] = i;
                    triangles[triangleIndex + 1] = i + m_res + 1;
                    triangles[triangleIndex + 2] = i + m_res;

                    triangles[triangleIndex + 3] = i;
                    triangles[triangleIndex + 4] = i + 1;
                    triangles[triangleIndex + 5] = i + m_res + 1;

                    triangleIndex += 6;
                }
            }
        }

        m_mesh.Clear();
        m_mesh.vertices  = vertices;
        m_mesh.triangles = triangles;
        m_mesh.RecalculateNormals();
        m_mesh.uv = uv;
    }
        public void ConstructMesh()
        {
            _vertices  = new Vector3[_resolution * _resolution];
            _triangles = new int[(_resolution - 1) * (_resolution - 1) * 6];
            int triIndex = 0;

            Vector2[] uv = _mesh.uv;

            for (int i = 0; i < _resolution; i++)
            {
                for (int j = 0; j < _resolution; j++)
                {
                    int     loop            = i + j * _resolution;
                    Vector2 percent         = new Vector2(i, j) / (_resolution - 1);
                    Vector3 pointOnUnitCube =
                        _localUp + (percent.x - 0.5f) * 2 * _axisA + (percent.y - 0.5f) * 2 * _axisB;
                    Vector3 pointOnUnitSphere = pointOnUnitCube.normalized;
                    _vertices[loop] = _shapeGenerator.CalculatePointOnPlanet(pointOnUnitSphere);


                    if (i != _resolution - 1 && j != _resolution - 1)
                    {
                        _triangles[triIndex]     = loop;
                        _triangles[triIndex + 1] = loop + _resolution + 1;
                        _triangles[triIndex + 2] = loop + _resolution;

                        _triangles[triIndex + 3] = loop;
                        _triangles[triIndex + 4] = loop + 1;
                        _triangles[triIndex + 5] = loop + _resolution + 1;
                        triIndex += 6;
                    }
                }
            }

            _mesh.Clear();
            _mesh.vertices  = _vertices;
            _mesh.triangles = _triangles;
            _mesh.RecalculateNormals();
            _mesh.uv = uv;
            InvertNormals();
        }
Exemple #15
0
    public void ConstructMesh()
    {
        int faceResolution = resolution - 1;

        Vector3[] vertices  = new Vector3[resolution * resolution];
        int[]     triangles = new int[faceResolution * faceResolution * 6];
        int       triIndex  = 0;

        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) / faceResolution;
                Vector3 pointOnUnitCube = localUp +
                                          (percent.x - .5f) * 2 * tangent +
                                          (percent.y - .5f) * 2 * biTangent;
                vertices[i] = shapeGenerator.CalculatePointOnPlanet(pointOnUnitCube.normalized);

                if (x < faceResolution && y < faceResolution)
                {
                    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.RecalculateBounds();
            mesh.RecalculateNormals();
            mesh.RecalculateTangents();
        }
    }
Exemple #16
0
    public void ConstructMesh()
    {
        Vector3[] vertices  = new Vector3[resolution * resolution];             //Mesh will have resolution^2 number of vertices
        int[]     triangles = new int[(resolution - 1) * (resolution - 1) * 6]; //Mesh will have (resoltuion - 1)^2 *6 triangles
        int       triIndex  = 0;


        for (int y = 0; y < resolution; y++)
        {
            for (int x = 0; x < resolution; x++)
            {
                //Creates vertices according to resolution size
                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;
                vertices[i] = shapeGenerator.CalculatePointOnPlanet(pointOnUnitSphere);

                //Prevents triangles from being created outside of mesh
                if (x != (resolution - 1) && y != (resolution - 1))
                {
                    //Creates first triangle for face
                    triangles[triIndex]     = i;
                    triangles[triIndex + 1] = i + resolution + 1;
                    triangles[triIndex + 2] = i + resolution;
                    //Creates second triangle for face
                    triangles[triIndex + 3] = i;
                    triangles[triIndex + 4] = i + 1;
                    triangles[triIndex + 5] = i + resolution + 1;

                    triIndex += 6; //Increments index counter by 6 because 6 vertices were already used
                }
            }
        }
        mesh.Clear(); //Clear mesh data because updating with lower resolution may cause reference errors
        //Assign vertices and triangles to mesh, and recalculates the normals
        mesh.vertices  = vertices;
        mesh.triangles = triangles;
        mesh.RecalculateNormals();
    }
Exemple #17
0
    public void ConstructMesh(Transform transform)
    {
        Vector3[] vertices  = new Vector3[_resolution * _resolution];
        int[]     triangles = new int[(_resolution - 1) * (_resolution - 1) * 6];
        int       triIndex  = 0;

        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 - 0.5f - _offset.x) * _faceSize * _axisA + (percent.y - 0.5f - _offset.y) * _faceSize * _axisB;
                Vector3 pointOnUnitSphere = pointOnUnitCube.normalized;

                vertices[i] = _shapeGenerator.CalculatePointOnPlanet(pointOnUnitSphere, transform.position);

                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.name      = "Chunk";
        _mesh.vertices  = vertices;
        _mesh.triangles = triangles;
        _mesh.normals   = vertices;
    }
Exemple #18
0
    public void ConstructMesh()
    {
        Vector3[] vertices  = new Vector3[resolution * resolution];
        int[]     triangles = new int[(resolution - 1) * (resolution - 1) * 2 * 3]; // 2=triangles in square, 3 = verts in triangle
        int       triIndex  = 0;

        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 - 0.5f) * 2 * axisA + (percent.y - 0.5f) * 2 * axisB;
                Vector3 pointOnUnitSphere = pointOnUnitCube.normalized; // convert cube to sphere
                vertices[i] = shapeGenerator.CalculatePointOnPlanet(pointOnUnitSphere);

                // Create triangles
                bool notOnEdge = (x != resolution - 1 && y != resolution - 1);
                if (notOnEdge)
                {
                    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();
    }
    public void ConstructMesh()
    {
        Vector3[] vertices  = new Vector3[resolution * resolution];
        int[]     triangles = new int[(resolution - 1) * (resolution - 1) * 6];
        int       triIndex  = 0;

        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 - 0.5f) * 2 * axisA + (percent.y - 0.5f) * 2 * axisB;
                Vector3 pointOnUnitSphere = pointOnUnitCube.normalized;//there's a better way to do this with more even points
                vertices[i] = shapeGenerator.CalculatePointOnPlanet(pointOnUnitSphere);

                if (x != resolution - 1 && y != resolution - 1)//this gets the three triangle points for each of the two triangles per small square on the mesh
                {
                    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();
    }
    public void ConstructMesh()
    {
        Vector3[] vertices  = new Vector3[resolution * resolution];
        int[]     triangles = new int[(resolution - 1) * (resolution - 1) * 6]; //imagine a grid of vertices (square) which have two triangles with three vertices (six)

        int triIndex = 0;

        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);                                     //when x = 0, the percent is 0; when x is max then percent is 1 on x axis
                Vector3 pointOnUnitCube   = localUp + (percent.x - .5f) * 2 * axisA + (percent.y - 0.5f) * 2 * axisB; //starting at 0 find our pos on the cube btw -1, 1
                Vector3 pointOnUnitSphere = pointOnUnitCube.normalized;
                vertices[i] = shapeGenerator.CalculatePointOnPlanet(pointOnUnitSphere);


                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();
    }
Exemple #21
0
    public void ConstructMesh()
    {
        // Resolution is on one side of the mesh.
        Vector3[] meshVertices = new Vector3[resolution * resolution];

        // Number of squares in mesh = (resolution - 1) * (resolution - 1)
        // Number of triangles per square = 2
        // Number of indices per triangle = 3
        int[] triangleIndices = new int[(resolution - 1) * (resolution - 1) * 6];

        int triangleIndex = 0;

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

                // Gives the percent position of the vertex along the edge.
                Vector2 percent = new Vector2(x, y) / (resolution - 1);

                Vector3 pointOnUnitMesh   = localUp + (percent.x - 0.5f) * 2 * xAxis + (percent.y - 0.5f) * 2 * yAxis;
                Vector3 pointOnUnitSphere = pointOnUnitMesh.normalized;
                meshVertices[vertexIndex] = shapeGenerator.CalculatePointOnPlanet(pointOnUnitSphere);

                // 0        1
                // ----------
                // |\       |
                // | \      |
                // |  \     |
                // |   \    |
                // |    \   |
                // |     \  |
                // |      \ |
                // |       \|
                // ----------
                // 2        3

                if (x != resolution - 1 && y != resolution - 1)
                {
                    // First triangle. (0, 3, 2)
                    triangleIndices[triangleIndex]     = vertexIndex;
                    triangleIndices[triangleIndex + 1] = vertexIndex + resolution + 1;
                    triangleIndices[triangleIndex + 2] = vertexIndex + resolution;

                    // Second triangle. (0, 1, 3)
                    triangleIndices[triangleIndex + 3] = vertexIndex;
                    triangleIndices[triangleIndex + 4] = vertexIndex + 1;
                    triangleIndices[triangleIndex + 5] = vertexIndex + resolution + 1;

                    triangleIndex += 6;
                }
            }
        }


        // Assign mesh.
        mesh.Clear();
        mesh.vertices  = meshVertices;
        mesh.triangles = triangleIndices;
        mesh.RecalculateNormals();
    }
    void GenerateMesh()
    {
        //Debug.Log("Called Generate mesh, 88");

        if (mesh == null)
        {
            mesh = new Mesh();
        }

        List <Vector3>         vertList = new List <Vector3>();
        Dictionary <long, int> middlePointIndexCache = new Dictionary <long, int>();

        // create 12 vertices of a icosahedron
        float t = (1f + Mathf.Sqrt(5f)) / 2f;

        vertList.Add(new Vector3(-1f, t, 0f).normalized);
        vertList.Add(new Vector3(1f, t, 0f).normalized);
        vertList.Add(new Vector3(-1f, -t, 0f).normalized);
        vertList.Add(new Vector3(1f, -t, 0f).normalized);

        vertList.Add(new Vector3(0f, -1f, t).normalized);
        vertList.Add(new Vector3(0f, 1f, t).normalized);
        vertList.Add(new Vector3(0f, -1f, -t).normalized);
        vertList.Add(new Vector3(0f, 1f, -t).normalized);

        vertList.Add(new Vector3(t, 0f, -1f).normalized);
        vertList.Add(new Vector3(t, 0f, 1f).normalized);
        vertList.Add(new Vector3(-t, 0f, -1f).normalized);
        vertList.Add(new Vector3(-t, 0f, 1f).normalized);


        // create 20 triangles of the icosahedron
        List <TriangleIndices> faces = new List <TriangleIndices>();

        // 5 faces around point 0
        faces.Add(new TriangleIndices(0, 11, 5));
        faces.Add(new TriangleIndices(0, 5, 1));
        faces.Add(new TriangleIndices(0, 1, 7));
        faces.Add(new TriangleIndices(0, 7, 10));
        faces.Add(new TriangleIndices(0, 10, 11));

        // 5 adjacent faces
        faces.Add(new TriangleIndices(1, 5, 9));
        faces.Add(new TriangleIndices(5, 11, 4));
        faces.Add(new TriangleIndices(11, 10, 2));
        faces.Add(new TriangleIndices(10, 7, 6));
        faces.Add(new TriangleIndices(7, 1, 8));

        // 5 faces around point 3
        faces.Add(new TriangleIndices(3, 9, 4));
        faces.Add(new TriangleIndices(3, 4, 2));
        faces.Add(new TriangleIndices(3, 2, 6));
        faces.Add(new TriangleIndices(3, 6, 8));
        faces.Add(new TriangleIndices(3, 8, 9));

        // 5 adjacent faces
        faces.Add(new TriangleIndices(4, 9, 5));
        faces.Add(new TriangleIndices(2, 4, 11));
        faces.Add(new TriangleIndices(6, 2, 10));
        faces.Add(new TriangleIndices(8, 6, 7));
        faces.Add(new TriangleIndices(9, 8, 1));


        // refine triangles
        for (int i = 0; i < resolution; i++)
        {
            List <TriangleIndices> faces2 = new List <TriangleIndices>();
            foreach (var tri in faces)
            {
                // replace triangle by 4 triangles
                int a = GetMiddlePoint(tri.v1, tri.v2, ref vertList, ref middlePointIndexCache, 1f);
                int b = GetMiddlePoint(tri.v2, tri.v3, ref vertList, ref middlePointIndexCache, 1f);
                int c = GetMiddlePoint(tri.v3, tri.v1, ref vertList, ref middlePointIndexCache, 1f);

                faces2.Add(new TriangleIndices(tri.v1, a, c));
                faces2.Add(new TriangleIndices(tri.v2, b, a));
                faces2.Add(new TriangleIndices(tri.v3, c, b));
                faces2.Add(new TriangleIndices(a, b, c));
            }
            faces = faces2;
        }

        // add noise to surface
        for (int i = 0; i < vertList.Count; i++)
        {
            vertList[i] = shapeGenerator.CalculatePointOnPlanet(vertList[i]);
        }


        //Debug.Log("Defined vertices and triangles, 160");

        // update mesh
        mesh.Clear();
        mesh.vertices = vertList.ToArray();

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

        for (int i = 0; i < faces.Count; i++)
        {
            triList.Add(faces[i].v1);
            triList.Add(faces[i].v2);
            triList.Add(faces[i].v3);
        }
        mesh.triangles = triList.ToArray();

        //Debug.Log("Updated Mesh, 175");

        /*
         * var nVertices = mesh.vertices;
         * Vector2[] UVs = new Vector2[nVertices.Length];
         *
         * for (var i = 0; i < nVertices.Length; i++)
         * {
         *  var unitVector = nVertices[i].normalized;
         *  Vector2 ICOuv = new Vector2(0, 0);
         *  ICOuv.x = (Mathf.Atan2(unitVector.x, unitVector.z) + Mathf.PI) / Mathf.PI / 2;
         *  ICOuv.y = (Mathf.Acos(unitVector.y) + Mathf.PI) / Mathf.PI - 1;
         *  UVs[i] = new Vector2(ICOuv.x, ICOuv.y);
         * }
         *
         * mesh.uv = UVs;
         *
         *
         * Vector3[] normales = new Vector3[vertList.Count];
         * for (int i = 0; i < normales.Length; i++)
         *  normales[i] = vertList[i].normalized;
         *
         * mesh.normals = normales;
         *
         */

        // extra code added
        mesh.RecalculateNormals();

        mesh.RecalculateBounds();
        mesh.Optimize();

        //Debug.Log("Optimesed Mesh, 207");


        if (meshFilter == null)
        {
            meshFilter = GetComponent <MeshFilter>();
        }

        meshFilter.sharedMesh = mesh;

        //Debug.Log("Apply to filter, 207");
        //mesh.RecalculateBounds();

        colourGenerator.UpdateElevation(shapeGenerator.elevationMinMax);
    }