godot icon indicating copy to clipboard operation
godot copied to clipboard

Implement CAMERA_VISIBLE_LAYERS as built-in shader variable

Open NumbuhFour opened this issue 2 years ago • 3 comments

Adds a built-in variable for shaders which exposes the cull mask of the camera rendering the current pass.

Godot_VisibleLayers_Shader_example_pr In this example, each camera has a different cull mask and the cube's shader outputs a different albedo for each.

Implements godotengine/godot-proposals#5563 Tested with render methods forward_plus, gl_compatibility, and mobile

NumbuhFour avatar Oct 14 '22 08:10 NumbuhFour

uints should be used in place of ints. In my mind it is preferable to be consistent and use uint32_ts throughout instead of switching to ints when passing the data to the renderer. uints are supported in both GLSL 330 and 450 so using them should be fine in all shaders.

@clayjohn I originally made that decision because I thought godot's shader translation couldn't handle uints as I was getting errors, but I have discovered that it was just that uints have their own literal suffix, so all your suggested changes have been implemented. Thank you for the review!

An example shader now:

uint layers = CAMERA_VISIBLE_LAYERS;
if ((layers & 3u) == 3u) { // Both layers 1 and 2, render blue
	ALBEDO = vec3(0.0,0.0,1.0);
}
else if ((layers & 1u) == 1u) { // Only layer 1, render red
	ALBEDO = vec3(1.0,0.0,0.0);
}
else if ((layers & 2u) == 2u) { // Only layer 2, render green
	ALBEDO = vec3(0.0,1.0,0.0);
}

NumbuhFour avatar Nov 02 '22 01:11 NumbuhFour

Approved with respect to implementation

clayjohn avatar Nov 04 '22 16:11 clayjohn

This looks ok to me.

reduz avatar Nov 28 '22 14:11 reduz

Thanks! And congrats for your first merged Godot contribution :tada:

akien-mga avatar Dec 04 '22 23:12 akien-mga