# Pls tell me 1.0 subtraction and sqrt call why important in this shader code below?

Attention Topic was automatically imported from the old Question2Answer platform.
``````    void fragment() {
float fresnel = sqrt(1.0 - dot(NORMAL, VIEW));
RIM = 0.2;
METALLIC = 0.0;
ROUGHNESS = 0.01 * (1.0 - fresnel);
ALBEDO = vec3(0.1, 0.3, 0.5) + (0.1 * fresnel);
}
``````

I learn shading but I don’t understand these both line:
float fresnel = sqrt(1.0 - dot(NORMAL, VIEW));
ROUGHNESS = 0.01 * (1.0 - fresnel);

1.0 subtraction why necessary and sqrt call too?

As I’m sure you know, roughness is a normalised value with 0.0 being perfectly reflective and 1.0 without reflection/diffuse. A mirror vs a rock.

This code calculates the fresnel effect, it’s that “hot spot” light effect you get on the sea or on crockery.

The most important part is the dot product. That, when dealing with normalised vectors (magnitude/length of one), is a test of how similar they are. So if two vectors pointing the same way the dot product is 1, opposite directions it’s -1 and orthogonal the dot product is 0. I’ll skip the linear algebra behind it but it’s very simple.

So, the two vectors here are the normal (that’s a line perpendicular to the surface of the object and the view, the object camera vector. So, this is testing whether the surface normal is pointing towards the camera.

Let’s say the output is exactly 1.0 on a given pixel, the normal is exactly facing the camera. Well, if you used that to set the roughness then it would be set to one. But in fresnel you want the opposite, the bit that’s pointing towards you to be reflective. Easy, just `1.0 - dot(NORMAL, VIEW)`

Now, the sqrt. At the moment this is a linear fall off, if you imagine a plane spinning slowly on its axis then the roughness would decrease as it points away in a way proportional to the rotation. With fresnel, that’s not really what you want. You want an area to be very bright in the centre but for it to die away quickly. So, you need a curved graph. The sqrt looks like this:

Very bright in the centre but falls off quickly. Aka, fresnel.