Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
12 Jun 22 - 14 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by theswedishswede · Sep 26, 2018 at 08:10 PM · meshverticesgenerationtriangles

Connecting flatshaded vertices

Using Sebastian Lague's tutorial on procedural landmass generation I've been able to create a working mesh. However i would like to add faces between the overlapping vertices to connect them.

alt text

(The green rectangles is where i want the faces).

How can this be done?

 using System.Collections;
 using UnityEngine;
  
 public class MeshGenerator : MonoBehaviour {
  
     public static MeshData GenerateTerrainMesh(float[,] heightMap,float heightMultiplier,AnimationCurve _heightCurve,int levelOfDetail, bool useFlatShading)
     {
         AnimationCurve heightCurve = new AnimationCurve(_heightCurve.keys);
  
         int meshSimplificationIncrement = (levelOfDetail == 0) ? 1 : levelOfDetail * 2;
  
         int borderedSize = heightMap.GetLength(0);
         int meshSize = borderedSize - 2 * meshSimplificationIncrement;
         int meshSizeUnsimplified = borderedSize - 2;
  
         float topLeftX = (meshSizeUnsimplified - 1) / -2f;
         float topLeftZ = (meshSizeUnsimplified - 1) / 2f;
  
        
         int verticesPerLine = (meshSize - 1) / meshSimplificationIncrement + 1;
  
         MeshData meshData = new MeshData(verticesPerLine, useFlatShading);
         int[,] vertexIndicesMap = new int[borderedSize, borderedSize];
         int meshVertexIndex = 0;
         int borderVertexIndex = -1;
  
         for (int y = 0; y < borderedSize; y += meshSimplificationIncrement)
         {
             for (int x = 0; x < borderedSize; x += meshSimplificationIncrement)
             {
                 bool isBorderVertex = y == 0 || y == borderedSize - 1 || x == 0 || x == borderedSize - 1;
  
                 if(isBorderVertex)
                 {
                     vertexIndicesMap[x, y] = borderVertexIndex;
                     borderVertexIndex--;
                 }
                 else
                 {
                     vertexIndicesMap[x, y] = meshVertexIndex;
                     meshVertexIndex++;
                 }
             }
         }
  
  
         for (int y = 0; y < borderedSize; y += meshSimplificationIncrement)
         {
             for (int x = 0; x < borderedSize; x += meshSimplificationIncrement)
             {
                 int vertexIndex = vertexIndicesMap[x, y];
                 Vector2 percent = new Vector2((x - meshSimplificationIncrement) / (float)meshSize, (y - meshSimplificationIncrement) / (float)meshSize);
                 float height =  heightCurve.Evaluate(heightMap[x, y])*heightMultiplier;
                 Vector3 vertexPosition = new Vector3(topLeftX + percent.x * meshSizeUnsimplified, height, topLeftZ - percent.y * meshSizeUnsimplified);
  
                 meshData.AddVertex(vertexPosition, percent, vertexIndex);
  
                 if (x < borderedSize - 1 && y < borderedSize - 1)
                 {
                     int a = vertexIndicesMap[x, y];
                     int b = vertexIndicesMap[x + meshSimplificationIncrement, y];
                     int c = vertexIndicesMap[x, y + meshSimplificationIncrement];
                     int d = vertexIndicesMap[x + meshSimplificationIncrement, y + meshSimplificationIncrement];
                     meshData.AddTriangle(a,d,c);
                     meshData.AddTriangle(d,a,b);
                 }
                 vertexIndex++;
  
             }
         }
         meshData.Finalize();
         return meshData;
     }
  
 }
  
 public class MeshData
 {
     Vector3[] vertices;
     int[] triangles;
     Vector2[] uvs;
  
     Vector3[] borderVertices;
     int[] borderTriangles;
  
     int triangleIndex;
     int borderTriangleIndex;
  
     bool useFlatShading;
  
     public MeshData(int verticesPerLine,bool useFlatShading)
     {
         this.useFlatShading = useFlatShading;
         vertices = new Vector3[verticesPerLine * verticesPerLine];
         uvs = new Vector2[verticesPerLine * verticesPerLine];
         triangles = new int[(verticesPerLine - 1) * (verticesPerLine - 1) * 6];
  
         borderVertices = new Vector3[verticesPerLine * 4 + 4];
         borderTriangles = new int[24 * verticesPerLine];
     }
  
     public void AddVertex(Vector3 vertexPosition,Vector2 uv, int vertexIndex)
     {
         if (vertexIndex < 0)
         {
             borderVertices[-vertexIndex - 1] = vertexPosition;
         }
         else
         {
             vertices[vertexIndex] = vertexPosition;
             uvs[vertexIndex] = uv;
         }
     }
  
     public void AddTriangle(int a, int b , int c)
     {
         if (a < 0 || b < 0 || c < 0)
         {
             borderTriangles[borderTriangleIndex] = a;
             borderTriangles[borderTriangleIndex + 1] = b;
             borderTriangles[borderTriangleIndex + 2] = c;
             borderTriangleIndex += 3;
         }
         else
         {
             triangles[triangleIndex] = a;
             triangles[triangleIndex + 1] = b;
             triangles[triangleIndex + 2] = c;
             triangleIndex += 3;
         }
      
     }
     public void Finalize()
     {
         if (useFlatShading)
         {
             FlatShading();
         }
         else
         {
            
         }
     }
  
     void FlatShading()
     {
         Vector3[] flatShadedVertices = new Vector3[triangles.Length];
         Vector2[] flatShadedUvs = new Vector2[triangles.Length];
  
         for(int i = 0; i < triangles.Length; i++)
         {
             flatShadedVertices[i] = vertices[triangles[i]];
             flatShadedUvs[i] = uvs[triangles[i]];
             triangles[i] = i;
         }
  
         vertices = flatShadedVertices;
         uvs = flatShadedUvs;
     }
  
     Vector3[] CalculateNormals()
     {
         Vector3[] vertexNormals = new Vector3[vertices.Length];
         int triangleCount = triangles.Length / 3;
         for(int i = 0; i < triangleCount; i++)
         {
             int normalTriangleIndex = i * 3;
             int vertexIndexA = triangles[normalTriangleIndex];
             int vertexIndexB = triangles[normalTriangleIndex + 1];
             int vertexIndexC = triangles[normalTriangleIndex + 2];
  
             Vector3 triangleNormal = SurfaceNormalFromIndices(vertexIndexA, vertexIndexB, vertexIndexC);
             vertexNormals[vertexIndexA] += triangleNormal;
             vertexNormals[vertexIndexB] += triangleNormal;
             vertexNormals[vertexIndexC] += triangleNormal;
         }
         int borderTriangleCount = borderTriangles.Length / 3;
         for (int i = 0; i < borderTriangleCount; i++)
         {
             int normalTriangleIndex = i * 3;
             int vertexIndexA = borderTriangles[normalTriangleIndex];
             int vertexIndexB = borderTriangles[normalTriangleIndex + 1];
             int vertexIndexC = borderTriangles[normalTriangleIndex + 2];
  
             Vector3 triangleNormal = SurfaceNormalFromIndices(vertexIndexA, vertexIndexB, vertexIndexC);
             if(vertexIndexA >= 0)
             {
                 vertexNormals[vertexIndexA] += triangleNormal;
             }
             if(vertexIndexB >= 0)
             {
                 vertexNormals[vertexIndexB] += triangleNormal;
             }
             if(vertexIndexC >= 0)
             {
                 vertexNormals[vertexIndexC] += triangleNormal;
             }
         }
         for(int i = 0; i < vertexNormals.Length; i++)
         {
             vertexNormals[i].Normalize();
         }
         return vertexNormals;
     }
  
     Vector3 SurfaceNormalFromIndices(int indexA,int indexB, int indexC)
     {
         Vector3 pointA = (indexA < 0) ? borderVertices[-indexA - 1] : vertices[indexA];
         Vector3 pointB = (indexB < 0) ? borderVertices[-indexB - 1] : vertices[indexB];
         Vector3 pointC = (indexC < 0) ? borderVertices[-indexC - 1] : vertices[indexC];
  
         Vector3 sideAB = pointB - pointA;
         Vector3 sideAC = pointC - pointA;
         return Vector3.Cross(sideAB, sideAC).normalized;
     }
  
     public Mesh CreateMesh()
     {
         Mesh mesh = new Mesh();
         mesh.vertices = vertices;
         mesh.triangles = triangles;
         mesh.uv = uvs;
         mesh.normals = CalculateNormals();
         if(useFlatShading)
         {
             mesh.RecalculateNormals();
         }
         else
         {
  
         }
  
         return mesh;
     }
 }

mesh.png (27.6 kB)
Comment
Add comment
10 |3000 characters needed characters left characters exceeded
â–¼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

0 Replies

· Add your reply
  • Sort: 

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

100 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Voxel mesh generation not working. 1 Answer

Setting triangles failing 0 Answers

Null Reference Exception Assigning Vertices to Mesh 0 Answers

How to return from shader the indexes of triangles/vertices that was actually rendered? 1 Answer

Lighting up all triangles between two points in a mesh? 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges