The value of the Custom Interpolator is incorrect

I am attempting to move calculations that occur in the Fragment Shader to the Vertex Shader for optimization. However, when I move them to the Vertex Shader, the calculations become incorrect.

To identify the cause, I have created a simple test ShaderGraph.

  • TestShader1: Incorrect. UV + Step.

  • TestShader2: Incorrect. UV + Custom Node(Out = saturate(Value * 3.402823466e+38)). Is Step Node is faster than Ceiling Node? (NO!)

  • TestShader3: Correct. CalcInVertex On/Off has same result. UV + Multiply.

  • CalcInVertex On: The value calculated in the Vertex Shader is used in the Fragment Shader via a Custom Interpolator.

  • CalcInVertex Off: Everything is calculated only in the Fragment Shader.

Even though it is a simple shader, the results differ between On and Off. However, this is not always the case. As shown in the screenshot below, sometimes the values are correct.

I am confused whether this is a bug or if it is due to my misunderstanding of the characteristics of the Vertex Shader and Custom Interpolator. If it appears to be a bug, I would like to report it with a reproducible project.

I have written the bug report for now. I will share the response here as soon as I receive it.
I am also attaching a reproducible project.

Playground.zip (72.3 KB)

Working as intended.

When calculating values in the vertex shader, the value is calculated only at each individual vertex, and then linearly interpolated across the triangle between the vertices. And for a quad, there are only 4 vertices.

In your first example, the value of the UV is 0,0 in the bottom left, 0,1 in the top left, 1,1 in the top right, and 1,0 in the bottom right. Those are the only values the vertices sees of the UV, not any of the values in between it shows in the preview. When you apply the step function they don’t change and you get the same values you started with. Those values are then interpolated across the triangles and you get what looks like nothing has happened, because nothing really did. Hence why the results look identical to the original UVs; they are identical to the original UVs.

Same with multiplying the UVs by a very large number and then saturating it. The 4 UV values the vertex shader sees are not changed, so you see no difference. But that one you can make work by moving the saturate() out of the Calc function and instead do it after the branch so it happens in the fragment shader stage.

The last example works because you’re actually changing the values on each vertex to something different, but the value is being interpolated linearly the same way.

1 Like

Thank you for the detailed explanation.
When I changed the Quad to a Plane, the calculations worked as intended.

My question has been completely resolved!

By the way, I often find your answers on the Unity Forums and Discussions very helpful.
(Including the one about saturate(Value * 3.402823466e+38).)

Thank you so much! :smile: