I just started using babylon js and I was trying to add physics to my 3d glb model but I get these errors
Tried to create a MeshImpostor for an object without vertices. This will fail.
It was not possible to create a physics body for this object.
I created the model in blender and exported it as a glb file
this is my code
async function add3d(path, addPhysics = false, mass = 0, friction = 0.1, restitution = 0.1) {
return new Promise((resolve) => {
var name = path.split("/").pop();
var directory = path.replace(name, "");
BABYLON.SceneLoader.ImportMesh("", directory, name, scene, (meshes) => {
if (addPhysics) {
meshes.forEach((mesh) => {
// Apply physics impostor to child meshes first
mesh.getChildMeshes().forEach((child) => {
child.setParent(null);
child.physicsImpostor = new BABYLON.PhysicsImpostor(child, BABYLON.PhysicsImpostor.MeshImpostor, {
mass: mass,
restitution: restitution,
friction: friction,
}, scene);
});
mesh.physicsImpostor = new BABYLON.PhysicsImpostor(mesh, BABYLON.PhysicsImpostor.MeshImpostor, {
mass: mass,
restitution: restitution,
friction: friction,
}, scene);
});
}
resolve(meshes[0]);
}, undefined, (error) => {
console.error(`Error loading ${path}: ${error.message}`);
});
});
}
Instead of assuming only the root has no vertices, I use this:
// "importedObjects" is returned by SceneLoader.ImportMesh directly or
// ImportMeshAsync through a Promise (in which case, the following is used as the last function in the ".then" Promise chain since it doesn't return anything)
importedObjects.meshes.filter((mfilter)=>
mfilter.getVerticesDataKinds().length >0).forEach((m)=>{
// code to process each mesh "m"
}
This is useful when doing a MergeMeshes() as well (for my purpose here, I don’t care about materials, constraints, or separate meshes):
That works when you control the model (as in OP). My use case is importing mesh files I don’t control, then cleaning them up as best I can.
Background detail:
Just playing around creating rollable dice from obj and stl files other people created. The four samples here are “pass the pigs” and “knuckle” from Thingiverse, “mini” and “teddy” I thought were from BABYLON examples (but I can’t seem to find them now). The pink d20 “Glimmergrove” has inset numbers that are difficult to discern with the current material.
Pass The Pigs by tmone is licensed under the Creative Commons - Attribution - Share Alike license.
Knucklebone Dice by MishaT is licensed under the Creative Commons - Attribution - Non-Commercial license.
Tales of The Glimmergrove d20 by Kazrak is licensed under the Creative Commons - Attribution - Share Alike license.