0 votes

I'm trying to figure out how to get the world space normal with the light shader. I know there is documentation about obtaining the world space normal in the fragment shader, but there doesn't seem to be a way to do that easily in the light shader. Anybody now?

in Engine by (169 points)
edited by

I see you can pass parameters between vertex and fragment shaders using VAR1 and VAR2, however I don't see such variables in LightMaterial...

1 Answer

+1 vote
Best answer

Do you need normal in view space? If not, you can compute world space normal in vertex shader, pass it to fragment shader using VAR1 or VAR2 and pass it to LIGHT part in NORMAL. Do you need specular color? If not, you can use SPECULAR to pass the normal. Another way would be to calculate inverse camera matrix and world matrix inside the script and use uniform matrix in LIGHT. Using them, you could compute world normal from view space normal.

by (820 points)
selected by

Awesome! Combined with the example in the docs:

vec4 invcamx = INV_CAMERA_MATRIX.x;
vec4 invcamy = INV_CAMERA_MATRIX.y;
vec4 invcamz = INV_CAMERA_MATRIX.z;
vec4 invcamw = INV_CAMERA_MATRIX.w;

mat3 invcam = mat3(invcamx.xyz, invcamy.xyz, invcamz.xyz);

vec3 world_normal = NORMAL * invcam;
vec3 world_pos = (VERTEX - invcamw.xyz) * invcam;

And the knowledge that setting the NORMAL variable carries to the Lighting shader helped to make a shader that casts light on top of the model regardless of the orientation of the camera:

NORMAL = world_normal;

and the Lighting shader:

LIGHT = vec3(dot(NORMAL, vec3(0,1,0)));
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 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 webmaster@godotengine.org with your username.