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
0
Question by Joshdbb · Nov 27, 2014 at 01:25 AM · 2dmeshdepthorderz

How do I render triangles in a mesh in depth order?

I'm using Unity to create a 2D game and I'm having a problem with meshes. It seems that triangles in the same mesh are drawn in creation order instead of depth order.

I created a simple example:

 public class MeshTest : MonoBehaviour
 {
     Mesh mesh;
 
     void Start ()
     {
         mesh = GetComponent<MeshFilter> ().mesh;
         BuildMesh ();
     }
     
     List<Vector3> newVertices = new List<Vector3>();
     List<int> newTriangles = new List<int>();
     List<Color> newColor = new List<Color>();
 
     void BuildMesh()
     {
         Vector3 pos = Vector3.zero;

         // Create three quads in the mesh
         GenerateQuad (Vector3.zero, Color.red);
         GenerateQuad (new Vector3(1f, 1f, 1f), Color.yellow);

         GenerateQuad (new Vector3(0.5f, 0.5f, 0.5f), Color.green);
         // The green square has a z coordinate in-between the other two squares

 
         mesh.Clear ();
         mesh.vertices = newVertices.ToArray();
         mesh.triangles = newTriangles.ToArray();
         mesh.colors = newColor.ToArray ();
         mesh.Optimize ();
         mesh.RecalculateNormals ();
         
         newVertices.Clear ();
         newTriangles.Clear ();
         newColor.Clear ();
     }
 
 
     void GenerateQuad(Vector3 topLeft, Color color)
     {
         int triangleIndex = newVertices.Count;
         float x = topLeft.x;
         float y = topLeft.y;
         float z = topLeft.z;
         
         newVertices.Add( new Vector3 (x, y, z) );
         newVertices.Add( new Vector3 (x + 1f, y, z));
         newVertices.Add( new Vector3 (x, y - 1f, z));
         newVertices.Add( new Vector3 (x + 1f, y - 1f, z));
         
         newTriangles.Add(triangleIndex+0);
         newTriangles.Add(triangleIndex+1);
         newTriangles.Add(triangleIndex+2);
         newTriangles.Add(triangleIndex+1);
         newTriangles.Add(triangleIndex+2);
         newTriangles.Add(triangleIndex+3);
 
         newColor.Add (color);
         newColor.Add (color);
         newColor.Add (color);
         newColor.Add (color);        
     }
 }


When I run a scene with just this game object in I see this:

alt text

The green square is on top of the other two when it should be in between them.

Any ideas what I'm doing wrong? Is this the normal behaviour?

Thanks

screen shot 2014-11-27 at 01.17.35.png (9.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

1 Reply

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

Answer by Eric5h5 · Nov 27, 2014 at 02:59 AM

You need to use a shader that writes to the depth buffer. (Also, unrelated, your bottom-right triangles in each quad have the winding order reversed.)

Comment
Add comment · Show 3 · 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 Joshdbb · Nov 27, 2014 at 03:33 AM 0
Share

$$anonymous$$any thanks. I know very little about shaders. I solved the problem by adding this: ZWrite On to my shader.

Thanks for pointing out the triangles, should it be:

newTriangles.Add(triangleIndex+0); newTriangles.Add(triangleIndex+1); newTriangles.Add(triangleIndex+2); newTriangles.Add(triangleIndex+2); newTriangles.Add(triangleIndex+1); newTriangles.Add(triangleIndex+3);? (also what could go wrong by having it the wrong way around?)

avatar image Eric5h5 · Nov 27, 2014 at 03:54 AM 0
Share

Yes, 2, 1, 3 would work; also 1, 3, 2. The surface normal was pointing the opposite way otherwise, which can be seen with a shader that culls backfaces. If you never used the mesh for collisions and never culled backfaces, it wouldn't really matter, but I think it's best to be consistent just in case.

avatar image Joshdbb · Nov 27, 2014 at 03:59 AM 0
Share

Thanks again.

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

27 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

Related Questions

3d collider mesh to 2d collider mesh 0 Answers

Any way to access SpriteRenderer mesh UV2? 0 Answers

I want do my own mesh "quad", but not square, I want a 2D polygon to apply in a parallax scroll 1 Answer

Having trouble implementing edge collapse operation with half-edge data structure 0 Answers

UI Button order 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