Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Question Discard instance in Instanced Setup

Discussion in 'Shaders' started by Guedez, Jun 18, 2020.

  1. Guedez

    Guedez

    Joined:
    Jun 1, 2012
    Posts:
    827
    Is there a way to cancel a instance of a Graphics.DrawMeshInstancedIndirect?
    Or to stop it from drawing a shadow?

    Currently I draw a whole lot of instances in 3 levels of detail. No shadows, only receive shadows, cast two sided shadows.
    The issue lies on the edge of these levels of detail, and I wanted to make the objects near the 'lod border' downgrade through shader before the actual change, so it would look smooth.

    Collapsing all triangles into a single point is my current way of 'canceling' a instance, but I am not sure how to do the same for shadows

    Edit: Encasing the 'collapsing all triangles to zero' code in
    #ifdef UNITY_PASS_SHADOWCASTER 
    makes the shadows disappear, but also makes it transparent...
     
    Last edited: Jun 18, 2020
  2. dukenukem4d

    dukenukem4d

    Joined:
    Jul 30, 2019
    Posts:
    14
    I'd like to know the same thing, since no asnwers, should we assume it's not possible?
     
  3. StuwuStudio

    StuwuStudio

    Joined:
    Feb 4, 2015
    Posts:
    165
  4. bgolus

    bgolus

    Joined:
    Dec 7, 2012
    Posts:
    12,329
    Collapsing the vertices in the vertex shader is the correct solution. If it wasn't working for the OP, I suspect the issue was with how they were determining when to collapse instances.
     
  5. StuwuStudio

    StuwuStudio

    Joined:
    Feb 4, 2015
    Posts:
    165
    Collapsing as in using a 0 matrix for the local->world matrix?
    Would that still make the vertex program run for every vertex or is the instance discarded before that?
     
  6. bgolus

    bgolus

    Joined:
    Dec 7, 2012
    Posts:
    12,329
    There's no way to skip an instance before the vertex shader runs. Either you don't render that instance to begin with (remove it from the array and reduce the instance count on the CPU or with a compute shader), or you run the vertex shader for every vertex.

    The idea is to use a dynamic branch to output a zeroed out vertex position early so you skip most of the calculations.
     
  7. StuwuStudio

    StuwuStudio

    Joined:
    Feb 4, 2015
    Posts:
    165
    Alright, I see. So it's sort of a trade-off, you have to benchmark to find which way to choose. If packing the instances in a compute shader ends up being faster than setting the area to 0, I try that.