I've gota Spatial shader attached to a terrain object.

I don't understand how this code works:

ALBEDO = vec3(length(VERTEX)/float(100), 1.0, 1.0);

It appears to change the color of the parts of my terrain based on my camera's position in space.

The code does what I want, but it doesn't make sense to me. Isn't ALBEDO the color of the entire terrain object? Shouldn't the above code change the color of all terrain based on where I move my camera?

I'm working towards lowering the alpha of all parts of terrain that are above the player. But I can't do that if I dont understand how ALBEDO can selectively alter only parts of the landscape...

ALBEDO is an output channel. You are assigning your value to it. Yes it's the whole mesh object. But what you are assigning to it might perhaps have some negative values too. They may or may not get clamped.

Yes the VERTEX input there is dependent on camera/view transform.

@Megalomaniak I see, so VERTEX is the vertex where the ALBEDO thread is working at the time. So I can picture the code iterating over each vertex in parallel in the object and assigning that vertex a color based on its position in space?

if you want the color to be world space(so static to the world) then you have to transform it by matrix multiplying the vertex input value with the camera/view transform input value iirc. Been a while so maybe I misrecall tho.

As @Megalomaniak mentioned, if you want the color to be in world space, then you will need to do some additional calculations. VERTEX is relative to the mesh origin, so the positions in the VERTEX function are where the vertices are in 3D space relative to the mesh, not where the mesh is in 3D space relative to the scene. Without converting it to world space, moving the node around in the Godot editor shouldn't change anything, since the vertices haven't move any.

To convert a local space to world space in a shader, you will need to multiply the vertex by the CAMERA_MATRIX, as explained in this Godot QA post. There is also a comment showing how to take a world position in a shader and convert it back to local space, which could be helpful for certain effects.

ABLEDO in the shader, it's the ALBEDO for that specific pixel. They use keyword ALBEDO in 3d shaders, rather than COLOR. COLOR is for the canvas shaders. ALBEDO is for the spatial. Changing the ALBEDO color only affects the one specific pixel rendering. To change the alpha value, use ALPHA.

a year later