Can't get set_instance_color to work on my MultiMesh instances

Godot Version

v4.2.1.stable.official [b09f793f5]

Question

I’m looking to set the instances to different colors within my MultiMesh, but no matter what Color I pass the set_instance_color function, my instances display as white and have a color value of (0, 0, 0, 1).

I’ve tried different values for Color, adding the material to the material_override for the MultiMeshInstance3D, different default colors. I’m assuming this is my setup, but can’t figure out what I’m doing wrong. Any help would be greatly appreciated!

I tried this setup as well as this example (this partially worked, but using the same scene I wasn’t able to reproduce the results of simply copy and pasting the example code.)

code snippet:

    var multiMesh = MultiMesh.new()
	multiMesh.transform_format = MultiMesh.TRANSFORM_3D
	multiMesh.instance_count = colors.size() 
	multiMesh.use_colors = true
	
	var sphereMesh = SphereMesh.new()
	var material = StandardMaterial3D.new()
	material.vertex_color_use_as_albedo = true
	material.albedo_color = Color(1, 1, 1)
	sphereMesh.material = material
	multiMesh.mesh = sphereMesh
	
	for i in multiMesh.instance_count:
		multiMesh.set_instance_color(i, Color(colors[i]))
               # example value for colors[i] = "ff7f00"
               # I have tried hardcoding values, but nothing changed the color
		var instance_position = Vector3(
			randf_range(-max_x_distance, max_x_distance),
			randf_range(-max_y_distance, max_y_distance),
			randf_range(-max_z_distance, max_z_distance)
		)
		
		multiMesh.set_instance_transform(i, Transform3D(Basis(), instance_position))
	
	var multiMeshInstance3d = MultiMeshInstance3D.new()
	multiMeshInstance3d.multimesh = multiMesh
	add_child(multiMeshInstance3d)

I’t could be that you set multiMesh.instance_count = colors.size() so early in the code.

The docs say: “This clears and (re)sizes the buffers. Setting data format or flags afterwards will have no effect.”

So, move that down to just before you assign the multimesh to the mmesh instance 3d node.

1 Like

I tested it and it works fine:

extends Node


func _ready() -> void:
	var colors = []
	for i in 50:
		var c = Color.from_hsv(randf(), 1.0, randf_range(0.5, 1.0))
		colors.push_back(c.to_html())

	var multimesh = MultiMesh.new()
	multimesh.transform_format = MultiMesh.TRANSFORM_3D
	multimesh.use_colors = true
	multimesh.instance_count = colors.size()

	var sphere_mesh = SphereMesh.new()
	var material = StandardMaterial3D.new()
	material.vertex_color_use_as_albedo = true
	material.albedo_color = Color.WHITE
	sphere_mesh.material = material

	multimesh.mesh = sphere_mesh

	var transform = Transform3D()

	for i in multimesh.instance_count:
		transform.origin = Vector3(randf_range(-10, 10), randf_range(-10, 10), randf_range(-10, 10))
		multimesh.set_instance_color(i, Color(colors[i]))
		multimesh.set_instance_transform(i, transform)

	var multimesh_instance = MultiMeshInstance3D.new()
	multimesh_instance.multimesh = multimesh
	add_child(multimesh_instance)

1 Like

Thank you @dbat and @mrcdk ! Looks like the only thing I needed to do was to ensure that I set use_colors = true before setting the instance_count. :man_facepalming: so simple… but so frustrating lol.

Wrong (old):

    var multiMesh = MultiMesh.new()
	multiMesh.transform_format = MultiMesh.TRANSFORM_3D
	multiMesh.instance_count = colors.size() 
	multiMesh.use_colors = true

Right (new thanks to the suggestion and code example):

    var multiMesh = MultiMesh.new()
	multiMesh.transform_format = MultiMesh.TRANSFORM_3D
	multiMesh.use_colors = true
	multiMesh.instance_count = colors.size() 

Thanks again for the help!

2 Likes

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