- Home /
Shader: Normal Angle
Is it possible to blend between two colors based on the normal's angle?
How would I go about getting the angle of the normal in a surface shader?
Could you precise what angle do you want. The angle between the normal and what ?
Vector3.up would do just fine, but I don't know what the options are when using a shader.
Answer by MaT227 · Sep 26, 2015 at 08:17 AM
If you want to blend between two colors according to the angle between the normal and a vector this is how you can do but first take a look at the documentation about Surface Shaders.
// You need to add the worldNormal vector
struct Input
{
...
float3 worldNormal;
INTERNAL_DATA
};
// Here in the surface function I assume that you already calculated the normals.
// This is your normals converted into world space.
float3 worldNormal = WorldNormalVector(IN, o.Normal);
// This is the "angle" between a vector (in your case it can be half3(0, 1, 0)) and the world normals.
half w = dot(worldNormal, normalize(_Vector));
// Here is the blending between red and green according to the dot product which is a between 0 and 1.
half3 color = lerp(half3(1, 0, 0), half3(0, 1, 0), w);
Tell me if this helps.
Just want to add that the Dot product can also be negative (in between -1 and 1 for normalized vectors). So if the normal points outwards or downwards it's always "red" since -1 is clamped to "0".
edit
You also might want to take a look at the surface shader examples page. It shows most commonly used parameters and properties in action with example code and screenshots.
Your answer
![](https://koobas.hobune.stream/wayback/20220612035044im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
colorize mesh based on dot product 1 Answer
how can i add color blend to Madfingers shader (making character flashing red when damaged) 0 Answers
Material doesn't have a color property '_Color' 4 Answers
Transparent shader with color property and no lighting help 1 Answer
How to calculate light from behind a quad? (and other light positions) 1 Answer