My Godot 4 project has split-screen multiplayer. Each player gets a Camera3D and Viewport. Some 3D meshes need to appear semitransparent for one player, but solid for another - and vise versa.
When I had just one camera, I could change the opacity of the Material's albedo. But that affects every camera at once. How can I accomplish having 2 different appearances for the same 3D mesh, depending on which camera is being used to view it?
I could duplicate the mesh, make the copy transparent, and give it a different cull_mask
. But duplicating every single mesh in the game isn't feasible. I need to keep one mesh per object, and instead have different cameras see it differently. (Such as giving the mesh two materials and having each camera choose which material it sees. Or handling the effect via a shader.)
Use a ShaderMaterial that makes use of the CAMERA_VISIBLE_LAYERS
keyword.
Let's say Camera 1 should only see through Blue objects, and Camera 2 only see through Red. Here are the steps:
cull_mask
parameter. Set the parameter for red meshes to the binary value for Red Layer. Do the same for blue meshes.This line in the ShaderMaterial checks CAMERA_VISIBLE_LAYERS
. If Red Layer is included, red meshes will appear solid. If not, they will be semitransparent.
void fragment() {
ALPHA = (int(CAMERA_VISIBLE_LAYERS) & cull_mask) == cull_mask ? 1. : .125;
}
To switch which meshes a Camera can see through on the fly, just change the cull_mask
on the Camera itself.