How to add mesh instance to collision component

Ok, I generated a terrain procedurally and now I am having a problem with adding a collision component to the generated terrain.

The collision needs to be of type ‘mesh’ and renderAsset needs to be ‘meshInstance.mesh’.

GenerateTerrain.prototype.createEntity = function (meshInstance) {
    let newEntity = new pc.Entity();
    newEntity.addComponent('render', {
        meshInstances: [meshInstance]
    });

    // This is the problematic one
    newEntity.addComponent('collision', {
        type: 'mesh',
        mesh: meshInstance.mesh
    });

    newEntity.addComponent('rigidbody', {
        type: 'static'
    });

    console.log(newEntity);
    this.entity.addChild(newEntity);
};

Image shows that raycast is null. It is properly implemented since I tested it on manual made object with static rigid body and mesh collider with render asset.


Also, wondering if I should create for each box a box collider but I think that is not cool for CPU. Also considered using “compound” collider to combine all of the small colliders but I’d still need to generate 1000 + primitive colliders…

Think that one, mesh collider, for static world is the best choice.

Nevermind, decided to not go with complex mesh but did create a custom raycaster. Needs a bit more work but it will get there. If you want to see it, you can find it here:

Kingdom-of-Quadrilatera

1 Like