Search code examples
three.jsaframewebvr

How to add a mesh collider to imported 3D object in A-Frame?


I am working on an aframe project where 3D objects are loaded from .obj files. However the raycaster wouldn't work with the imported objects. I suspect the reason is that unlike built-in geometries, these imported objects don't have proper collider set up. Is it possible to add mesh colliders to a generic 3D object, like in Unity? Or is it for some other possible reasons that the raycaster won't work on these objects?


Solution

  • Meshes do work with the raycaster so if you have issues 2 quick solutions:

    • make sure the mesh is indeed properly size and centered. Chances are it's not and the bounding box is incorrect so you have to look in specific parts of the mesh to make it work. Using the inspector allows to see the bounding box. You can then use a 3D modeling software like Blender to fix the mesh.
    • the bounding box might feel too small for natural interaction, you can then add an invisible transparent object e.g. a sphere or box that will then change with they raycaster. This solution only makes sense if don't mind imperfection on the volume.

    Also make sure there is no object between the origin of the raycaster and the mesh! It's a silly problem but sometimes we forget that we add/remove object by making them transparent and... they prevent the raycaster to interact with the object behind.

    PS: if you want a collider in addition to the raycaster there is the aabb-collider component.