How to use multimeshinstance?

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By abhaskumarsinha
:warning: Old Version Published before Godot 3 was released.

How to use multi mesh instance to generate a large number of straight and upright tree spraid randomly…??

:bust_in_silhouette: Reply From: Zylann

Here is an example that creates a MultiMesh with 4 cubes:

extends MultiMeshInstance
func _ready():
	# Create multimesh resource that will contain 4 cubes
	var mm = MultiMesh.new()
	mm.transform_format = MultiMesh.TRANSFORM_3D
	mm.instance_count = 4
	mm.mesh = preload("res://cube.obj")
	
	# Set position of all cubes at random
	for i in range(mm.instance_count):
		var pos = Vector3(rand_range(-2, 2), rand_range(-2, 2), rand_range(-2, 2))
		var t = Transform(Basis(), pos)
		mm.set_instance_transform(i, t)
	
	# Assign multimesh to be rendered by the MultiMeshInstance
	self.multimesh = mm

WARNING: In 3.0 alpha 2 I found transform_format must be assigned BEFORE instance_count, otherwise it triggers errors.

And if you want to use colors, set color_format before instance_count. And give mm.mesh a spatial material.

garyo | 2018-03-14 22:28