Godot Version
4.3
Question
My Scene has:
- Node3D_DebugRoom
-
- Tank
-
- Tank
The Child Tank Scene has:
- Tank
-
- MeshInstance3D_for_Tank_Model
-
- MeshInstance3D_for_Team_Color
The MeshInstance3D_for_Tank_Model is the Tank Vehicle model, while MeshInstance3D_for_Team_Color is the MeshInstance3D on certain parts of the MeshInstance3D_for_Tank_Model for color coding. Think RTS game. Some parts of the RTS units will be color coded for each team. The MeshInstance3D_for_Team_Color has a Shader Material with 1 Instance Shader Parameter which is just albedo. The whole MeshInstance3D will only have 1 simple color.
Now, the tank also an @export var team_number: int
, and I could assign the team number 0 and 1 to each tank, and I can get different color with the following code in the tank’s gd script:
func _ready() -> void:
var mi3d:MeshInstance3D = get_node("MeshInstance3D_for_Team_Color")
mi3d.set_instance_shader_parameter("albedo", globalSingleton.TEAM_RGB[team_number])
The globalSingleton.TEAM_RGB is just a static var for BLUE and RED:
static var TEAM_RGB = [
Vector3( 0.0, 0.0, 1.0 ),
Vector3( 1.0, 0.0, 0.0 )
]
With 2 tanks from above, one tank is assigned team_number 0 and the other team_number 1. When I run the game, I could already have a BLUE tank and a RED tank correctly.
HOWEVER, the _ready()
constructor above only runs when the game runs. This means in the editor, my tanks will not be colored accordingly. So here is the question: in this circumstance, is it possible to make it so that the Godot Editor could change the Instance Shader Parameter with respect to the @export var team_number?
As the Tank’s Scene has MeshInstance3D_for_Team_Color as its child, I am making all Tanks in the Node3D_DebugRoom to have Editable Children, and from there, I am manually changing each Tank to have the correct color via Instance Shader Parameter right now.
Is there a better way to do what I am doing? I want the shader albedo of MeshInstance3D_for_Team_Color to changed with respect to team_number
in the editor.