Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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
1
Question by lancer · Aug 24, 2015 at 09:43 PM · c#meshheightmapruntime-generation

Remove vertices and triangles that are at a cirtain y value

Currently I'm generating a mesh at runtime based off of a heightmap. I have the UV unwraping and heightmap generation all working, but now I have an issue where I want there to be no mesh where there is black on the heightmap.

It's working OK so far, but there are some spots where the triangles come down to the 0/no data area, and I'm not sure how to fix it.

Here is my heightmap: alt text (There are some "no data" values on the heightmap, it's square but the no data values show as transparent on here. In Unity they are converted to black)

When I generate my mesh, it comes out looking like this: alt text

I don't want anything coming down to the "no data" area, but you can see there is stuff coming down. I believe this has to do with me generating all the vertices, and then if the area has data, adding the triangles, but I'm not sure how to fix it.

Here is my relevant function:

 void CreateMesh (Texture2D texture, int Startx, int Starty, int Width, int Height)
     {
         if (Width > 250 && Height > 250)
             return;
         List<Vector3> verts = new List<Vector3> ();
         List<int> tris = new List<int> ();
         List<Vector2> uvs = new List<Vector2> ();
         if (Startx + Width > texture.width)
             Width = texture.width - Startx;
         if (Starty + Height > texture.height)
             Height = texture.height - Starty;
         //uses 0x0 as the zero "no data" value
         float zero = texture.GetPixel (0, 0).grayscale * 100;
         //Bottom left section of the map, other sections are similar
         for (int i = 0; i < Width; i++) {
             for (int j = 0; j < Height; j++) {
                 float y = texture.GetPixel (Startx + i, Starty + j).grayscale * 100;
                 //Add each new vertex in the plane
                 verts.Add (new Vector3 (i, y, j));
                 uvs.Add (new Vector2 ((Startx + i) / ((float)texture.width), (Starty + j) / ((float)texture.height)));
 
                 //Skip if a new square on the plane hasn't been formed
                 if (i == 0 || j == 0)
                     continue;
                 if (texture.GetPixel(Startx+i,Starty+j) == Color.black || y < zero + ErrorMargin) continue;
 
                 //Adds the index of the three vertices in order to make up each of the two tris
                 tris.Add (Width * i + j); //Top right
                 tris.Add (Width * i + j - 1); //Bottom right
                 tris.Add (Width * (i - 1) + j - 1); //Bottom left - First triangle
                 tris.Add (Width * (i - 1) + j - 1); //Bottom left 
                 tris.Add (Width * (i - 1) + j); //Top left
                 tris.Add (Width * i + j); //Top right - Second triangle
             }
         }
         GameObject plane = new GameObject ("ProcPlane"); //Create GO and add necessary components
         plane.AddComponent<MeshFilter> ();
         MeshRenderer renderer = plane.AddComponent<MeshRenderer> () as MeshRenderer;
         renderer.material = mat;
         Mesh procMesh = new Mesh ();
         procMesh.vertices = verts.ToArray (); //Assign verts, uvs, and tris to the mesh
         procMesh.uv = uvs.ToArray ();
         procMesh.triangles = tris.ToArray ();
         procMesh.RecalculateNormals (); //Determines which way the triangles are facing
         plane.GetComponent<MeshFilter> ().mesh = procMesh; //Assign Mesh object to MeshFilter
         plane.transform.position = new Vector3 (Startx, 0, Starty);
         MeshCollider meshC = plane.AddComponent<MeshCollider> () as MeshCollider;
         meshC.sharedMesh = procMesh;
 
     }

ca-sub1.png (27.5 kB)
mesh-gen.png (87.5 kB)
Comment
Add comment · Show 1
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
avatar image cjdev · Aug 25, 2015 at 02:34 AM 0
Share

When you add triangles you're going back and connecting vertices from the current vertex to those on the bottom, bottom left, and left of the vertex. You're getting those artifacts because the current vertex has a valid y-value but the vertices below it do not. You'll have to put in a check to see if those are also valid in order to get rid of the artifacting.

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

26 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

Related Questions

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

RunTime hole in mesh not working 0 Answers

Spawned Object Gradient Mesh Colors? 2 Answers

Algorithm for automatic mesh creation 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