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 Going down an expensive branch only occasionally

Discussion in 'Shader Graph' started by dgoyette, Jul 1, 2019.

  1. dgoyette

    dgoyette

    Joined:
    Jul 1, 2016
    Posts:
    4,193
    I created a shader about a year ago in shaderlab/HLSL/Cg, which most of the time was just a standard PBR shader, with a switch I could flip to get the shader to do something interesting (and expensive) as the object was being destroyed. In order to avoid the expensive part of the shader being run all the time, I used shader_feature along with mat.EnableKeyword() to conditionally enable/disable portions of the shader at runtime. So, when it was time to destroy some object spectacularly, I set EnableKeywork to the correct value, and now the shader started doing the expensive stuff.

    With shadergraph, it seems the only thing I have to work with is the Branch node. However, per the documentation on the Branch node, "Both sides of the branch will be calculated in the shader, even if one is never output." This means that I can't conditionally execute the expensive stuff only when necessary; the expensive stuff is always executing.

    Are there any options to avoid this? I really don't want the expensive portion of my shader running at all times for all objects. The only option I can think of is to make two separate materials, one which does the expensive stuff and one that does not, and swap the material's shader at timetime. I haven't tried that yet, so I don't know if that will come with a performance hit when swapping, but either way: it would be preferable not to have to duplicate most of the shader and have to maintain two similar copies.