Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 robinking · Feb 07, 2011 at 09:37 PM · meshproceduraltriangle

Weird triangle indexing problem

I have a GameObject called "Ground", which has an empty Mesh Filter, Mesh Renderer and Mesh Collider attached, plus the following script. It creates a new terrain-like mesh - instead of quads, it is arranged in a hex-like formation. Then it allows you to click on the mesh with a mouse, and it prints the triangle index to the console. All fine - except that the triangle index reported seems to be wrong. It should run from 0 at the lower left, up to 19 at the lower right, then start the next row at 20 and so on. But instead, it's reported as 0 in the lower right, 19 in the lower left, then skip 20-39 and the next row begins with 40.

The really odd thing is, if I create one less row or column of triangles than I should, the triangle index reported is correct (see the note attached to the line below). I can't work out how this is happening?

EDIT: I just tried printing both the contents of tri[] and ground.triangles[] in order, just after the line "ground.triangles = tri"... and they were in a completely different order to each other! How on earth can that happen?

var ground : Mesh; var groundCollider : MeshCollider; var cam : Camera; var width : int = 10; var depth : int = 10; var verts : int; var tris : int; //-------------------------------------------------------------------------------------------------------- function Awake() { ground = GetComponent(MeshFilter).mesh; groundCollider = GetComponent(MeshCollider); cam = GameObject.Find("/Main Camera").GetComponent(Camera); verts = (width+1)*(depth+1); tris = width*depth*2; } //-------------------------------------------------------------------------------------------------------- function Start() { Build(); } //-------------------------------------------------------------------------------------------------------- function Build() {

 var vert : Vector3[] = new Vector3[verts];
 var uvA : Vector2[] = new Vector2[verts];
 var uvB : Vector2[] = new Vector2[verts];
 var norm : Vector3[] = new Vector3[verts];
 var col : Color[] = new Color[verts];
 var tri : int[] = new int[tris*3];
 //----------------------------------------------------------------
 var n : int;
 for (var z : int=0;z<=depth;z++) {
     for (var x : int=0;x<=width;x++) {
         n = GridToVert(x,z);
         if (z%2 == 0) { // z is even
             vert[n] = Vector3( x, 0, z );
             uvA[n] = Vector2( x, z );
         } else {
             vert[n] = Vector3( x+0.5, 0, z );
             uvA[n] = Vector2( x+0.5, z );
         }
     }
 }
 //----------------------------------------------------------------
 n=0;
 for (z=0;z<depth;z++) { // change to "depth-1" and everything works right
     for (x=0;x<width;x++) { // OR change to "width-1" and everything works right
         if (z%2 == 0) { // z is even
             tri[n+0] = GridToVert(x,z);
             tri[n+1] = GridToVert(x,z+1);
             tri[n+2] = GridToVert(x+1,z);
             tri[n+3] = GridToVert(x+1,z);
             tri[n+4] = GridToVert(x,z+1);
             tri[n+5] = GridToVert(x+1,z+1);
         } else {
             tri[n+0] = GridToVert(x,z);
             tri[n+1] = GridToVert(x,z+1);
             tri[n+2] = GridToVert(x+1,z+1);
             tri[n+3] = GridToVert(x,z);
             tri[n+4] = GridToVert(x+1,z+1);
             tri[n+5] = GridToVert(x+1,z);
         }
         n=n+6;
     }
 }
 print(n);
 //----------------------------------------------------------------
 ground.Clear();
 ground.vertices = vert;
 ground.uv = uvA;
 ground.triangles = tri;
 ground.RecalculateNormals();
 ground.Optimize();
 groundCollider.sharedMesh = ground;

} //-------------------------------------------------------------------------------------------------------- function GridToVert( x : int, z : int ) { if (x<0 || x>width+1 || z<0 || z>depth+1) { return float.NaN; } else return (z*(width+1)) + x; } //-------------------------------------------------------------------------------------------------------- function Update() {

 if (Input.GetMouseButtonDown(0)) {

     // Get ground position of mouse
     var ray : Ray = cam.ScreenPointToRay( Input.mousePosition );
     var hit : RaycastHit;
     if( Physics.Raycast(ray, hit) ) {

         var t = hit.triangleIndex;
         print(t);

     }

 }

}

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
1

Answer by robinking · Feb 07, 2011 at 10:00 PM

I've discovered the culprit, it's:

ground.Optimize();

Is it a good idea for me to keep this line in, or can I do without it? (iPhone)

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 Eric5h5 · Feb 08, 2011 at 01:41 AM 0
Share

I don't use it myself; it never seems to make any difference with the generated meshes I've done.

avatar image robinking · Feb 08, 2011 at 11:30 AM 0
Share

Cheers, good to know.

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

No one has followed this question yet.

Related Questions

Trouble recalculating 3D mesh's triangles after deleting verts 2 Answers

How to colorize a face of a Mesh 0 Answers

Duplicate and separate triangles from a mesh 1 Answer

Procedural Cylinder Generation 2 Answers

Create the visual spring in Unity? 3 Answers


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