Search Unity

Best material to "hide" a submesh?

Discussion in 'General Graphics' started by yoonitee, Sep 19, 2018.

  1. yoonitee

    yoonitee

    Joined:
    Jun 27, 2013
    Posts:
    2,363
    Say you have a 3d model with several materials and you want to turn off part of the mesh with a particular material. You could replace it with a fully transparent mesh.

    Is this the most efficient way of doing it? Will it still use draw-calls to draw an invisible mesh? What's the alternative?
     
  2. LennartJohansen

    LennartJohansen

    Joined:
    Dec 1, 2014
    Posts:
    2,394
    Best way would be to skip it totally. Make your own mesh renderer that uses Graphics.Drawmesh and just skip the submesh. Or better drawmeshInstanced if there is a lot of these objects
     
  3. bgolus

    bgolus

    Joined:
    Dec 7, 2012
    Posts:
    12,342
    A fully transparent material, whether it be using alpha blending or alpha test, can be just as expensive (and in some case more expensive) than just rendering the object normally. The "best" way, at least if you're going to use a material to do it, is to have a shader that simply collapses the geometry to nothing.

    Code (csharp):
    1. Shader "Hide Geometry"
    2. {
    3.     SubShader
    4.     {
    5.         Pass
    6.         {
    7.             Tags { "Queue"="Transparent" "RenderType"="None" "IgnoreProjector"="True" }
    8.             ColorMask 0
    9.             ZWrite Off
    10.          
    11.             CGPROGRAM
    12.             #pragma vertex vert
    13.             #pragma fragment frag
    14.  
    15.             float4 vert () : SV_POSITION { return float4(0,0,0,0); }
    16.             fixed4 frag() : SV_Target { return fixed4(0,0,0,0); }
    17.             ENDCG
    18.         }
    19.     }
    20. }
    But, this will be a draw call still. It's not going to actually draw anything, that shader guarantees twice over that no pixels will be written to, but the CPU doesn't know that and will still issue the draw call to the GPU. To avoid that you have to draw the object manually like @LennartJohansen suggested.
     
  4. yoonitee

    yoonitee

    Joined:
    Jun 27, 2013
    Posts:
    2,363
    Thanks. That will probably be a lot quicker. I wonder isn't there a shader program that simply tells the GPU to not draw anything at all?

    @Lennart Do you have an example of how to create your own Mesh Renderer and will it be as efficient as the Unity renderer in terms of batching etc.?

    I think the above shader solution is good enough for most purposes. If the object just has one submesh with this material I can just hide the whole object.