Search Unity

Question Instanced mesh drawn twice when viewed far away

Discussion in 'General Graphics' started by luyu2020, Dec 9, 2021.

  1. luyu2020

    luyu2020

    Joined:
    Sep 1, 2021
    Posts:
    5
    I placed 3 cubes. Two of them shared the gray material (Let's call them "A1", " A2") and one with red (Named "B").
    When A1 and A2 placed near each other, it seems unity decide to draw with an odd order:
    InstanceBroken1.png.jpg
    And if I rotate A2 a little bit (rotation.x from 2 to 30), it worked again.
    InstanceFixed1.png.jpg
    Or if I just place camera closer, it works.
    InstanceFixed2.png.jpg
    I first assume it is a depth precision issue (Because opaque mesh are drawn from near to far, so I thought if two mesh is so close that unity can't decide which is closer it stops instancing). But when I tried to push the near clip plane further, the instance still does not work properly.
    I tried this scene in Unity 2019.4.29f1 and Unity 2020.3.19f1, both with built-in pipeline and corresponding newest urp. They behaved exactly same.
    The package for test is appended below.
     

    Attached Files:

  2. cosmochristo

    cosmochristo

    Joined:
    Sep 24, 2018
    Posts:
    250
    This is just a guess based on what you are showing. The two objects are close, so rotation (or changes in resolution due to distance) can change what parts of the objects penetrate. When one changes from not penetrating to not penetrating, then parts of one object will have to be sorted into a different depth order. In this case it appears one is being broken into two parts, parts in front, parts behind the other object.
    If I am right the the drawing system is doing what it is supposed to do and your idea about precision was on the right track.
     
  3. luyu2020

    luyu2020

    Joined:
    Sep 1, 2021
    Posts:
    5
    Thanks for your reply.
    As mentioned, I did an experiment by changing near clip plane, and you remind me that in CPU side we actually don't care about near plane. When sorting we are just calculating the distance from camera itself(rather than near plan) to the object pivot(or maybe bounds?).
    Anyway, I'll try figuring out how unity sort objects, and hopefully solve the problem.
     
  4. luyu2020

    luyu2020

    Joined:
    Sep 1, 2021
    Posts:
    5
    Now I set my camera sort mode to NoDistanceSort and the instancing worked again.
    Thanks a lot for your help!
    But now I'm in a dilemma of choosing less draw call or better ZCull …
     
  5. cosmochristo

    cosmochristo

    Joined:
    Sep 24, 2018
    Posts:
    250
    you are welcome, glad to have helped.
    There is another option.
    Your different tests showed that the World design and calculations you use all vary in results depending on where you place them: positional variance. So you could choose to remove positional variance by never moving the camera from the origin. I.e. real floating origin, not origin shifting (that many incorrectly call floating origin). However, you probably think that I am biased .... and it's true!
    Then you can use either culling method and always have consistent results.