0 votes
    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?

Godot version 3.4
in Engine by (224 points)

1 Answer

+3 votes
Best answer

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.

by (2,156 points)
selected by
Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read Frequently asked questions and How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to [email protected] with your username.