Transparent objects between camera and player using RayCast and set_albedo

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By nu-fulla

I’m trying to make a script that makes all objects between the camera and the player transparent. My current script (not mentioned here) swaps the material file for an alternate transparent material file (then back again when the object is not in the way). This works how I want it to but requires multiple scripts and alternate textures.

I’ve tried modifying this script that hides all “objects” that hit my camera’s RayCast:

if get_node("CamerarRayCast").is_colliding():
	var CollisionName = get_node("CameraRayCast").get_collider()
	CollisionName.hide()

But “CollisionName” is the “object” node (the parent that contains the mesh and the collision shape) not the “mesh”, how can I modify that to get access the objects child mesh? I also want to leave the albedo color unchanged and just change the alpha value.

You can change a materials albedo values in a GDScript like this:

MeshName.get_surface_material(0).set_albedo(Color(3, 4, 5, 0.5))

That’s:

MeshName.get_surface_material(0).set_albedo (Color(red, green, blue, alpha))

With those things in mind what I want is something like this:

if get_node("CameraRayCast").is_colliding():
	var CollisionName = get_node("CameraRayCast").get_collider()
	CollisonName/MeshInstance.get_surface_material(0).flags_transparent = true
	CollisonName/MeshInstance.get_surface_material(0).set_albedo(Color(, , , 0.5))
else:
	CollisonName/MeshInstance.get_surface_material(0).flags_transparent = false
	CollisionName/MeshInstance.get_surface_material(0).set_albedo(Color(, , , 1))

Any help would be much appreciated.

:bust_in_silhouette: Reply From: marty

Godot Spatial Materials have properties that you can set for Distance- and Proximity-based fading of any objects occluding the camera.

See the bottom of this page in the docs to get started: Spatial Material — Godot Engine (3.1) documentation in English.