How I can enable cossilion on GLB model

collisions don’t work with this setup and I cant find intelligible api about this case

public static LoadFromUrl(app:pc.Application, url:string, collision: boolean) {
    log.pc_shooter('URL', url)
    return new Promise<pc.Entity>((resolve) => {
      app.assets.loadFromUrl(url, 'container', function(err: any, asset: any) {
        const entity:pc.Entity = asset.resource.instantiateRenderEntity({
          castShadows: true
        })
        // entity.addComponent('collision', {
        //   type: 'mesh',
        //   renderAsset: asset.resource.model
        // })
        log.pc_shooter(asset.resource.model, 'asset.resource.model')
        entity.addComponent('rigidbody', {
          type: 'kinematic',
          restitution: .5,
          friction: .5
        })
        entity.addComponent('collision', {
          type: 'mesh',
          renderAsset: asset.resource.model
        })
        // app.root.addChild(entity)
        // entity.setPosition(pos)
        resolve(entity)
      })
    })
  }

Hi @pblhelka and welcome,

Loading a GLB results in a hierarchy of entities added to your scene. That means there isn’t a single model but potentially multiple.

You can search through the entity hierarchy that the instantiateRenderEntity() returns to find any render components created, and add collision and rigidbody components to each.

Something like this:

const renders = entity.findComponents('render');

renders.forEach(render => {

        render.entity.addComponent('rigidbody', {
          type: 'kinematic',
          restitution: .5,
          friction: .5
        });
        render.entity.addComponent('collision', {
          type: 'mesh',
          renderAsset: render.asset
        });
});

Note collisions between mesh to mesh colliders aren’t supported. Mesh colliders are usually used on static rigid bodies.

Hope that helps.

2 Likes

You can also do it with the model asset too if you just want one collision component

entity.addComponent('collision', {
          type: 'mesh',
          asset: asset.resource.model
        })
1 Like

thank you)