0 votes

Hi i try to create a Multimesh like a wall with size of (1,1,0.2) as a single mesh and use them to draw walls in different directions.

    var multiMesh = MultiMesh.new()
    multiMesh.transform_format = MultiMesh.TRANSFORM_3D
    multiMesh.mesh = CubeMesh.new()
    multiMesh.mesh.size = Vector3(1,1,.2)
    multiMesh.color_format = MultiMesh.COLOR_FLOAT
    multiMesh.mesh.material = SpatialMaterial.new()
    multiMesh.mesh.material.vertex_color_use_as_albedo = true
    multiMesh.set_instance_color(0, Color.red)
    multiMesh.set_instance_color(1, Color.blue)
    multiMesh.set_instance_color(2, Color(.5,0,0,1))
    multiMesh.set_instance_color(3, Color(0,0,.5, 1))

    var mmi = MultiMeshInstance.new()
    mmi.multimesh = multiMesh;

    var t0 = Transform().rotated(Vector3(0,1,0), deg2rad(0))
    t0.origin = Vector3(0, 0, 0)
    var b = Basis().rotated(Vector3(0,1,0), deg2rad(90))
    var t1 = Transform(b,Vector3(0, 0, 0) )
    multiMesh.set_instance_transform(0, t0)
    multiMesh.set_instance_transform(1, t1)

The problem is original mesh position needs to fixed by mmi.translate(Vector3(.5,.5,.1)) to place the wall in the top line of the space (1,1)

When i try to reuse with the multimesh i apply the rotation related to the wall direction deg 0 for top horizontal
deg 90 for right vertical
deg 180 for bottom horizontal
deg 270 for left vertical

When i apply the transform to the mesh instance the mesh is wrong placed.
The picture shows the problem, on the left the blue wall is missplaced and should be placed like the example on the right side.
enter image description here
Looks like i need to set the center of the original mesh back to 0,5, 0.5 to get correct result.
The wall is just an example i have different mesh types.
What i doing wrong?

in Engine by (47 points)
edited by

1 Answer

–1 vote

Hi all, after reading and understanding the Godot documentation i found my own solution

func set_instance_transform( meshIndex, origin:Vector3, degrees:int):
    var v:Vector3 =  assert_size/2 - translation
    var b = transform.basis.rotated(Vector3(0,1,0), deg2rad(degrees))
    var t = Transform(b, v).translated(v)
    t.origin += origin
    multimesh.set_instance_transform(meshIndex, t)
by (47 points)
Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read Frequently asked questions and How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to [email protected] with your username.