Raycast against mesh with baked vertices

Hello, and I have been working on a scene whereby I would like to align some mesh instances to an imported GLTF model. The mesh was created in Blender, has had the transformations applied, and was packed using gltfpack (which creates a separate transform and mesh nodes in the output GLTF).

After importing the mesh I create instances of a cube, and position them at locations along the mesh (like pins on a map). In order to better align the instances with the mesh I am casting a ray towards the GLTF mesh and using the hit distance and normal to offset and rotate the mesh. Without baking the transform into the mesh vertices (bakeCurrentTransformIntoVertices) the normal information I get back from the ray hit is incorrect, after baking the vertices the ray no longer hits the surface.

The following playground shows the issue I am having:

https://www.babylonjs-playground.com/#MQGJBY#1
(toggle the enableBakeVertices boolean to show the error)

I have included some logic to display the surface normals of the mesh I want to align with, and some more to show the rays being cast. The parent transform has been removed from the root hierarchy of the imported GLTF, and then it’s position is reset; this was necessary to retain the original position of the child mesh after baking the vertices.

The issue, simply, is that my ray intersection tests fail after baking the vertices, despite the normals drawing in the correct positions. Using the same code on a rotated plane (MeshBuilder.CreatePlane) works, although I do not need to do the same work to detach the plane from a parent hierarchy or to reset a parent TransformNode’s rotation and position.

Thanks for any advice you might be able to lend, and please let me know if I am doing anything glaringly wrong,
Robert

The problem lies with the fact that the glb files describe the geometry in a right-handed coordinate system and Babylon is left handed, so we have to add a root transform to convert from right to left and it’s always a pain to work with…

You would simplify your life greatly by setting scene.useRightHandedSystem=true; because in that case the transform is the identify transform and everything is working as expected:

https://playground.babylonjs.com/#MQGJBY#3

If you don’t want to set useRightHandedSystem to true, you can create the cube under the same parent than the floor so that they inherit the same transformation and the picking will also work:

https://www.babylonjs-playground.com/#MQGJBY#4

2 Likes

Thanks.

Do I need to modify the DragBehavior object to accommodate this change?

r

I don’t know the DragBehavior object, I guess the best way to know is to test :slight_smile:

1 Like

Roger.