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 allan36j · Jul 27, 2016 at 01:58 PM · shadermeshrendererdepth-bufferztest

Mesh rendered wierdley without depth buffer

I'm trying to achieve an optical illusion.
In order to do so I cut my mesh into two pieces. One of them should be rendered in front of the other even though it's behind it. This is my setup: setup that's how it looks like in the end: result

Now my question is: How to remove the wrongly rendered part at the left edge?
Btw: It happens not only there, but on the other site too.
Thank you.

optical-illusion-working-shaded.png (18.5 kB)
optical-illusion-perspective.png (17.4 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

Answer by Bunny83 · Jul 27, 2016 at 02:36 PM

Well, since you disabled the depth test, the triangles are rendered in the order they are specified in the Mesh. You problem is that the lower left part of the mesh is most likely more at the beginning of the mesh triangle list while that part that "looks through" is more to the end of your mesh triangle list.

If you disable the depthbuffer you actually could have used a single mesh. You just have to make sure you ordered the triangles the way you need them. If that extra part would be added to the first mesh at the end of the triangle list it would appear above the other.

So your solutions are:

  • either use the depth buffer, render the first object, clear the depth buffer and then render the second mesh.

  • If you don't use the depth buffer you should reorder your triangles of your mesh so they are drawn in the right order.

edit
Here's a script that you should simply add to your mesh and it should reorder the triangles from right to left.

 // ReorderMeshTriangles.cs
 using UnityEngine;
 using System.Collections.Generic;
 
 public class ReorderMeshTriangles : MonoBehaviour
 {
     private struct Triangle
     {
         public int I1;
         public int I2;
         public int I3;
         public Vector3 center;
         public Triangle(int aI1, int aI2, int aI3, Vector3[] verts)
         {
             I1 = aI1;
             I2 = aI2;
             I3 = aI3;
             center = (verts[I1] + verts[I2] + verts[I3]) / 3;
         }
     }
 
     void Start()
     {
         var mf = GetComponent<MeshFilter>();
         var m = mf.mesh;
         var verts = m.vertices;
         var indices = m.triangles;
         var tris = new List<Triangle>(indices.Length / 3);
         for(int i = 0; i < indices.Length; i+=3)
         {
             tris.Add(new Triangle(indices[i], indices[i + 1], indices[i + 2], verts));
         }
 
         tris.Sort((a,b)=>a.center.x.CompareTo(b.center.x));
 
         for (int i = 0; i < tris.Count; i++)
         {
             indices[i * 3    ] = tris[i].I1;
             indices[i * 3 + 1] = tris[i].I2;
             indices[i * 3 + 2] = tris[i].I3;
         }
         m.triangles = indices;
     }
 }

When you run your game it should look right.

Note: I haven't tested it so i'm not sure if the sorting direction is correct. If it's not you just want to swap "a" and "b" inside the sorting function:

 tris.Sort((a,b)=>b.center.x.CompareTo(a.center.x));
                 /|\                  /|\
                  |                    |
     //         here               and here


Of course if "right" isn't the x axis you have to change the "x" as well to whatever you need.

Comment
Add comment · Show 6 · 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 allan36j · Jul 27, 2016 at 04:34 PM 0
Share

@Bunny83 Thanks for your answer. But how can I reorder the triangles in the right order? - I could't find anything here.
And how could I clear the depth buffer after one mesh has already been rendered?OnPostRender?
Also what would the depth be now? Still 1.0?

Sorry the many questions.

avatar image Bunny83 allan36j · Jul 27, 2016 at 11:14 PM 0
Share

Well, reordering the triangles can be tricky and it depends on where does this mesh come from? If it's created in a modelling application you could search for such a feature there. However a lot exporter don't give a damn about the order ^^.

An imported mesh can't be edited in place as the imported mesh could reimported and any changes would be lost. With an editor script you could save a modifed mesh as seperate asset.

Apart from creating a new asset you can also edit the mesh during runtime. In your case an automatic sorting in some direction would probably be enough. So sorting them from right to left should fix the problem. Calculating the center of each triangle and sorting them based on the x component should do the trick.

ps: I've edited my answer and added a script that will reorder the triangles based on their x position.

avatar image allan36j · Jul 28, 2016 at 04:44 PM 0
Share

Thanks for the script. When I run it the $$anonymous$$esh gets both times replaced by the first mesh and both are scaled up and rotated somewhere in the space. Also it shows two different errors:

  • Not allowed to access vertices on mesh 'Combined $$anonymous$$esh (root: scene) Instance' UnityEngine.$$anonymous$$esh:get_vertices() Reorder$$anonymous$$eshTriangles:Start() (at Assets/Scripts/Reorder$$anonymous$$eshTriangles.cs:25)
    (also at line 26)

  • Not allowed to access triangles on mesh 'Combined $$anonymous$$esh (root: scene) Instance' UnityEngine.$$anonymous$$esh:set_triangles(Int32[]) Reorder$$anonymous$$eshTriangles:Start() (at Assets/Scripts/Reorder$$anonymous$$eshTriangles.cs:41)

I think I'll find another way doing this Thanks anyway.

avatar image Bunny83 allan36j · Jul 28, 2016 at 10:57 PM 0
Share

Since it says "Combined $$anonymous$$esh" seems that you either have combined the meshes yourself or they are statically batched. Have you marked them as static? Try to unceck the static checkbox if it is checked.

Where does the mesh actually come from? Is it an imported FBX file? It's clear that the meshes got combined into one.

I don't have your mesh to test it. If i attach the script to a default sphere gameobject i don't get any errors.

avatar image allan36j Bunny83 · Jul 29, 2016 at 09:17 PM 0
Share

Yes I could get rid of the error by unchecking the static box.
I created the model in blender an exported it as a FBX file.

But somehow my problem won't go away. picture

optical-illusion.png (19.7 kB)
Show more comments

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

70 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

Related Questions

Shaders: How to ztest against specific layers? 1 Answer

Depth Rendering Issue 0 Answers

Clear depth buffer after each mesh 0 Answers

Rendering hard edges via vertex shader 2 Answers

Why is only the last Material in the Materials array on a Mesh Renderer used? 2 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