I’m new to shaders and made a Shader Graph that inflates a mesh along its normals, using the Lit shader Master Stack. I noticed that the code generated from the Lit-shader-based Shader Graph has multiple passes and it seems that each pass recomputes the vertex displacement.
If each pass does recompute the vertex displacement, is there a way to avoid this? I would like to compute the vertex displacement once and then have each pass use the result.
Here’s the Shader Graph:
Here’s the Scene view result with a sphere collider for reference:
Here’s the generated code corresponding to the vertex displacement:
VertexDescription VertexDescriptionFunction(VertexDescriptionInputs IN)
{
VertexDescription description = (VertexDescription)0;
float _Float_ea07e58a9c3e49a59d9d8f1223cb4d31_Out_0 = 0.5;
float3 _Multiply_23975ed8aacd4318aafbf99ccf9222ee_Out_2;
Unity_Multiply_float(IN.ObjectSpaceNormal, (_Float_ea07e58a9c3e49a59d9d8f1223cb4d31_Out_0.xxx), _Multiply_23975ed8aacd4318aafbf99ccf9222ee_Out_2);
float3 _Add_a1315b18917244fea035f817669fbddc_Out_2;
Unity_Add_float3(IN.ObjectSpacePosition, _Multiply_23975ed8aacd4318aafbf99ccf9222ee_Out_2, _Add_a1315b18917244fea035f817669fbddc_Out_2);
description.Position = _Add_a1315b18917244fea035f817669fbddc_Out_2;
description.Normal = IN.ObjectSpaceNormal;
description.Tangent = IN.ObjectSpaceTangent;
return description;
}
The names are obscure but you can see that the Add, Multiply, and so on correspond to the Shader Graph above.
Here’s the Find results, showing 16 calls to the vertex displacement code:
The pass named “ShadowCaster” calls it, as well as the passes named “META” and “ScenePickingPass”, and so on.
In my use case I’ll be displacing 28,000 vertices per frame and modifying 28,000 normals per frame.
I’m not advanced enough yet to know how to profile this to answer my own question, but I’m working on it. Thanks in advance for any help.