Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 11 Next capture
2021 2022 2023
1 capture
11 Jun 22 - 11 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 Jer_J · Jul 23, 2015 at 12:23 AM · meshverticesmeshrenderertrianglesmeshfilter

Generated Mesh Triangles not Being Made/Visible?

I wrote code for generating a mesh at specific coordinates as such:

 FixedUpdate(){
 
 //Lots of code that tracks mouse position 
 
 if (Input.GetMouseButtonDown (0)) {
             createTileMesh (listIndex); 
         }
 
 
 }
 
 void createTileMesh(int tileIndex){
         Mesh tileMesh = new Mesh ();
         MeshFilter tileFilter; 
         MeshRenderer tileRender;
 
         Vector3 debugTileCentre; 
         Vector3[] vertices = new Vector3[4]; 
         int[] triangles = new int[6];
         Vector3[] normals = new Vector3[4];  
 
         vertices = tileList [listIndex].VERTICES; 
         debugTileCentre = tileList [listIndex].TILECENTRE; 
         Debug.Log (tileIndex); 
 
         Debug.Log (debugTileCentre); 
 
         vertices [0].y = vertices [0].y + 1f;
         vertices [1].y = vertices [1].y + 1f;
         vertices [2].y = vertices [2].y + 1f;
         vertices [3].y = vertices [3].y + 1f;
 
         Debug.Log (vertices [0]); 
         Debug.Log (vertices [1]);
         Debug.Log (vertices [2]);
         Debug.Log (vertices [3]);
 
         triangles[0] = 0; 
         triangles[1] = 2;
         triangles[2] = 1;
 
         triangles[3] = 1;
         triangles[4] = 3;
         triangles[5] = 2;
 
         normals [0] = Vector3.up; 
         normals [1] = Vector3.up; 
         normals [2] = Vector3.up; 
         normals [3] = Vector3.up; 
 
 
 
 
         tileMesh.vertices = vertices; 
         tileMesh.normals = normals; 
         tileMesh.triangles = triangles; 
          
 
         tileMesh = GetComponent<MeshFilter> ().mesh;  
         tileRender = GetComponent<MeshRenderer> (); 
         MeshCollider mesh_collider = GetComponent<MeshCollider>();
          
         
         //tileFilter = GetComponent<MeshFilter>().mesh; 
 
          
         Debug.Log ("Done Mesh"); 
 
 
 
 
     }


For those of you wondering about the vertices[] array a sample output (I've tested with the debug log thing) would be something like:

 //Sample values 
 vertices[0] = (1.8, 1, -22.3) //Top left corner
 vertices[1] = (1.8, 1, -23.8) //Top right corner
 vertices[2] = (0.3, 1, -22.3) //Bottom left corner
 vertices[3] = (0.3, 1, -23.8) //Bottom right corner 
 
 
 Try as I might, no triangles in game view, or scene view from the bottom or top. 
 
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

1 Reply

· Add your reply
  • Sort: 
avatar image
3
Best Answer

Answer by Cherno · Jul 23, 2015 at 06:43 AM

 tileMesh = GetComponent<MeshFilter> ().mesh;  

means that the tileMesh mesh, which you declared at the start at your method and to which you have assigned the created vertices, triangles and normals, gets overriden with the MeshFilter's own mesh... So the method's work is basically wasted ;) It has to be the other way around:

 GetComponent<MeshFilter> ().mesh = tileMesh;  
Comment
Add comment · Show 2 · Share
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 Bunny83 · Jul 23, 2015 at 09:16 AM 0
Share

Right, there are other things that get created and are then replaced by something else, like the Vector3 array.

Also:

  • Using "Get$$anonymous$$ouseButtonDown" in FixedUpdate is a very bad idea. "XXXDown" events shouldn't be checked in FixedUpdate since FixedUpdate might not run every frame and it could simply miss the keypress. Use Update for such events.

  • If you fixed that mesh replacement (like Cherno said) you will notice that one of your triangle is facing up and the other is facing down. The first triangle is counter-clockwise and will face down, the other one is clockwise and will face up.

  • Your labelling "top left / right" seems to be wrong in relation to the actual axes. keep in $$anonymous$$d that -23.8 is smaller than -22.3. It looks like you use the x-axis as top-down axis and the z-axis as left-right axis. When viewed from the top (use use Vector3.up as normal so "+y" would be the front side) you labelled the more negative z position as "right" but it's actually left. You should draw your points on a piece of paper and work out the required indices from there. You need clockwise winding in Unity.

Also keep in $$anonymous$$d that the vertex positions are in local space. So they are offset from the $$anonymous$$eshFilters origin.

avatar image Jer_J · Jul 23, 2015 at 01:31 PM 0
Share

Thanks! This worked to fix my issue, I'm not sure why I thought it was a good idea to create a mesh then overwrite it after being made haha...

@Bunny8, thanks for the suggestions! $$anonymous$$y map extends from 0,0 to +x,-z so left is 0,0 and right is 0,-z; but nonetheless you were right about the winding order and triangle facing the wrong direction.

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

3 People are following this question.

avatar image avatar image avatar image

Related Questions

Save only visible part of mesh 1 Answer

Extruding faces of a mold. Need to properly assign vertices to triangles 0 Answers

Null Reference Exception Assigning Vertices to Mesh 0 Answers

Why vertex positions appear (0.0, 0.0, 0.0) ? 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