Making part of a 3D object invisible based on where the camera is looking

Godot Version

4.5 - Mobile rendering.

Question

I’m looking for advice on which way to start approaching this.

I’m trying to make an xray vision style effect where my first person character can see through SOME 3d object, but not others. I don’t want the entire object to turn invisible or fade, but rather just the part that is being looked at. For example:

The camera is looking at the top corner of the green cube, and you can see the purple cube on the other side. If the camera moves to the left, the see-through part will move with it.

I feel like perhaps an ALPHA material position parameter could be moved around based on a raycast, or maybe trying to get the camera direction into the shader etc.. (but not sure if these are possible at all)

Advice appreciated

In shader’s fragment() function take a dot product of vertex direction and -z axis, smoothstep it to some range and assign to ALPHA.

Thanks - so it is definitely working in terms of hiding parts of the object so I can see through it, which is brilliant, but I’m not sure I’ve got the correct inputs in terms of “vertex direction” and “-z axis”

Which input names would I use specifically?

-z is just vec3(0.0, 0.0, -1.0) and vertex direction is normalized VERTEX.
In fragment function, VERTEX is always in camera space and look direction will always be vec3(0.0, 0.0, -1.0). Taking the dot product between those two unit vectors will give you inclination of the pixel from the viewing direction.

1 Like