Has anyone created a mesh, based on another meshInstance? The minimum you need to do it is the graphics device and a list of positions. That list of positions must be somewhere in the meshInstance, but I can’t find it. There’s meshInstance.mesh.vertexBuffer.storage which is an ArrayBuffer, basically holding four arrays of different type of numbers. There are 768 values in each array, which would be 256 positions. Sounds good but none of those arrays are working, when I try them.
I’m not even sure about those arrays – this is a mesh of a simple cube. I can’t understand why you would need more than 8 positions (vertices).
It seems that you already have a mesh. That’s the object with positions, normals, uv coordinates and similar. This can be used by multiple mesh instances.
So you just need to create a new mesh instance with this existing mesh. No need to re-create mesh as well.
What I’m trying to do, in code, is the same thing you do in the Editor when you add a collision component to an entity, set the type on this component as “mesh”, and then select the entity in the editor as the source of the mesh. I’m bringing in a model with 175 meshes on it (all very differently shaped) and I’m trying to avoid having to export each one of those meshes out of Blender and then bring them into PC and use the asset IDs in my code.
Essentially what I’m doing is decomposing the imported entity into separate entities so that raycasting for entity picking will work. So far, I’m able to iterate through the meshInstances, create the new entities, assign the models and materials to them, and position them correctly. But the whole point is to get separate collision components on each one. But I can’t see how to get a meshInstance.mesh into a new collision component. I’m still trying!
I think most of the related code is here
and this is about as far as my experience with this part ends.
The collision component can take a model resource, so if we can create a model from a meshInstance, that can be passed to the collision component.
I’ve done a quick example of tinkering. The visible mesh is offset from where the collision is but you can get the general idea on what needs to be done for the collision component https://playcanvas.com/editor/scene/1101639
I still have some issues but hope that gets you on the right track!
Success!! Thanks so much to @yaustar and @Albertos and @mvaligursky and @Leonidas for helping me figure this out. Here’s my solution to decomposing an entity into new entities using the meshInstances and materials from the source entity. The key to figuring out the last part (using the model mesh) was the fact that you can supply an actual model object to the “model” property of the collision component. I was surprised when that worked.
This solution is not recursive. It examines only the first level down from the root of the hierarchy, but that could changed if necessary. To see it in action, run the app, use the arrow keys to move the original object out of the way and you will see the decomposed sub-entities. Open the console and try clicking on the different cubes. If you click cubes from the old object, you will see they are all the same entity. If you click the new cubes, you can see they are each separate.
Niiiice! Thanks for sharing the solution!
And how do you get the hierarchy view like that? Some kind of developer tool?