- Home /
World/screen space gradient overlay in surface shader
I'm trying to make a surface shader that lays a gradient blending two colors over the mesh. The gradient would always work in the vertical direction, from the cottom vertex to the top either in world or screen space, maintaining it's direction regardless of the of the transformations done to the mesh.
This mockup shows more or less what I mean:
I've tried with a vertex shader:
struct v2f {
float4 pos : SV_POSITION;
fixed4 color : COLOR;
}
v2f vert (appdata_full v) {
v2f o;
o.pos = mul (UNITY_MATRIX_MVP, v.vertex);
o.col = lerp(_Color1,_Color2, v.vertex.y);
return o;
}
float4 frag (v2f i) : COLOR {
float4 c = i.color;
return c;
}
but the vertex colors remain painted the same color regardless of how the mesh is transformed. So if the cube in the picture above was turned upside down, the gold part would face bottom, which is not what I'm looking for. Furthermore I was unable to pass the functions to a surface shader, and I would like to combine the snippet with other stuff.
I'd be grateful for any directions on how to achieve this.
Answer by gnp89 · May 27, 2014 at 08:44 PM
You are using de vertex.y in model space, thats why if you make some transformations the color is always the same. For the lerp, you should use the vertex.y but in world space. So try
o.col = lerp(_Color1,_Color2, o.pos.y);
Note: the third parameter in lerp must should be between 0 and 1. If its not, it will probably clamp it and the color won't be the one you want. To achieve this you should calculate the lerp weight as follows:
lerpWeight = (o.pos.y - lowestVertex.y) / (highestVertex.y - lowestVertex.y)
and then
o.col = lerp(_Color1,_Color2, lerpWeight);
The challenge here is to calculate the lowest and highest (using their 'y' value) vertices for that model in world space, for every frame. I'm not sure if you can do that.
$$anonymous$$aybe you can calc the highest and lowest in a first pass and in a second pass set the color. Anyone knows if this can be done?
Your answer
![](https://koobas.hobune.stream/wayback/20220613141902im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Error in shadow received by vertex displacement shader 1 Answer
Why the shader responds differently to Tri-color ambient light vs. Skybox lighting? 0 Answers
Vertex program max instructions limit 2 Answers
Gradient over multiple tiled objects 1 Answer
Additional blending when using properties rather than hard-coded values for colors 0 Answers