can't transmit custom data to mesh instances in multimesh.

Godot Version

v4.4.stable.official [4c311cbee]

Question

I’m trying to pass the INSTANCE_CUSTOM parameter to the spatial shader for the mesh in a MultiMesh, but I can’t seem to make it work. I’m hoping someone familiar with this can take a look. Thanks!
Notes:
I’ve successfully displayed the MultiMesh and changed the mesh color via the shader.
A
On line 33 of main.gd, I passed custom data to each mesh in the MultiMesh. However, the shader reads it as the default VEC4(0.0) value.
——————
main.gd:

extends Node3D

var ps = PhysicsServer3D
var rs = RenderingServer

var muiltiMeshNode: MultiMeshInstance3D

func _ready() → void:
muiltiMeshNode = MultiMeshInstance3D.new()
add_child(muiltiMeshNode)
muiltiMeshNode.visible = true

var mm: MultiMesh = MultiMesh.new()

#add mesh to multi mesh
var mesh: BoxMesh = BoxMesh.new()
var mate1 = ShaderMaterial.new()
mate1.shader = load(“res://t1.gdshader”)
mesh.surface_set_material(0, mate1)
mm.mesh = mesh

mm.transform_format = MultiMesh.TRANSFORM_3D
mm.instance_count = 5
mm.visible_instance_count = -1
#mm.use_colors = true
mm.use_custom_data = true

for i in mm.instance_count:
mm.set_instance_transform(i, Transform3D(Vector3(1.0, 0.0, 0.0), Vector3(0.0, 1.0, 0.0), Vector3(0.0, 0.0, 1.0), Vector3(i * 3.0, 0.0, 0.0)))
#mm.set_instance_color(i, Color(0.0, 0.0, 1.0, 0.0))
mm.set_instance_custom_data(i, Color.BLUE)

muiltiMeshNode.multimesh = mm

func _process(_delta: float) → void:
pass

——————
ti.gdshader:

shader_type spatial;

void vertex(){
//COLOR = vec4(0.0,1.0,0.0,1.0);
COLOR = INSTANCE_CUSTOM;
}

void fragment() {
ALBEDO = COLOR.rgb;
}
——————
A

Try to use a varying to pass info from the vertex() function to the fragment() function. Use the flat qualifier if you don’t want any interpolation.

yeah, thank for your helping; tried, but still can’t work; oh god the problem
A1

thsnk u a lot; and i found where the problem is;
i swaped the order to be like this:

	mm.transform_format = MultiMesh.TRANSFORM_3D
	mm.use_colors = true
	mm.use_custom_data = true
	mm.instance_count = 5
	mm.visible_instance_count = -1

and it workded;
it was because, like the content in godot doc:

bool use_custom_data
If true, the MultiMesh will use custom data (see set_instance_custom_data). Can only be set when [member instance_count] is 0 or less. This means that you need to call this method before setting the instance count, or temporarily reset it to 0.

so i just need to set “mm.use_custom_data = true” before
“mm.instance_count = 5”

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