n5ro / aframe-physics-system

Physics system for A-Frame VR, built on CANNON.js.
https://n5ro.github.io/aframe-physics-system/
MIT License
505 stars 136 forks source link

gltf ammo-shape loading before object3DMap.mesh information in A-frame #192

Open icurtis1 opened 3 years ago

icurtis1 commented 3 years ago

When adding ammo-body and ammo-shape components to a loaded gltf-model I get error "Cannot use FIT.ALL without object3DMap.mesh" it seems that the ammo is trying to create a ammo-shape before model loaded.

This is what currently causes the error:

I've created a workaround for now in my A-frame project for now with an A-frame component like so:

const gltfPhysicsObjectComponent = {
  schema: {
    model: {default: ''},
    body: {type: 'string', default: 'dynamic'}, //dynamic: A freely-moving object.
    shape: {type: 'string', default: 'hull'}, // hull: Wraps a model in a convex hull, like a shrink-wrap
  },
  init() {
    const gltfmodel = document.createElement('a-entity')
    this.el.appendChild(gltfmodel)
    gltfmodel.setAttribute('gltf-model', this.data.model)
    gltfmodel.setAttribute('shadow', {receive: false})

    // Specify what type of ammo-body (dynamic, static, kinematic)
    gltfmodel.setAttribute('ammo-body', {type: this.data.body})

    // Waiting for model to load before adding ammo-shape (box, cylinder, sphere, capsule, cone, hull)
    this.el.addEventListener('model-loaded', () => {
      gltfmodel.setAttribute('ammo-shape', {type: this.data.shape})
    })
  },
}

export {gltfPhysicsObjectComponent}

The ammo component as of now works great with primitives but not with gltf models. This is my current workaround for now...

diarmidmackenzie commented 2 years ago

Thanks - hit the same problem, and this was very helpful.