Applying Spatial Shader to all meshes in a scene

Godot Version

Godot 4.2.1

Question

I have a spatial shader that I want to apply to all MeshInstance3Ds in every scene (like a global shading effect), but I cannot figure out a practical way of doing it. Applying the shader manually to each mesh would take forever, and a loop to apply it at runtime would cause a massive lag spike. Is there a way to make a MeshInstance3D have this shader applied by default, or is there a better method I’m missing?

Add the MeshInstance3D to a group and then use SceneTree.set_group() to set the GeometryInstance3D.material_override or GeometryInstance3D.material_overlay

If you don’t want going one by one adding them to a group you can use the SceneTree.node_added signal in a singleton (autoload) and add them to that group in there.

Like:

extends Node


func _enter_tree() -> void:
	get_tree().node_added.connect(func(node:Node):
		if node is GeometryInstance3D:
			node.add_to_group('to_appy_material')
	)
	
	
func change_material(material:Material) -> void:
	get_tree().set_group('to_appy_material', 'material_override', material)

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.