[ENGINE ONLY] Terrain generation with heightmap

Hi, I try to figure out how to bring in a terrain using a height map using engine only.
I found this here: https://developer.playcanvas.com/en/tutorials/terrain-generation/ and try to adapt, it seems that the terrain script itself is working, but
how do I actually bring in the Terrain class to the root app?

Thanks!

Which part are you having trouble with? You can ask use the script as is too and add the script type to the entity.

@yaustar this is the code I am struggeling.
“this” is not available. I created the Terrain so far I guess now the final step is to add it
to app.root as a child, am I wrong?

/*
this.entity.addComponent(‘model’);
this.entity.model.model = renderModel;
this.entity.addComponent(‘collision’, {
type: ‘mesh’,
});
this.entity.collision.model = collisionModel;
this.entity.addComponent(‘rigidbody’, {
friction: 0.5,
type: ‘static’,
});
*/

Hi @iso74,

Are you adding the script to an entity of yours in code? If you are just using the methods, then you need to create a new pc.Entity and add that to app.root.

Like this:

    var entity = new pc.Entity();
    entity.addComponent('model');
    entity.model.model = renderModel;

    entity.addComponent('collision', {
        type: 'mesh'
    });
    entity.collision.model = collisionModel;

    entity.addComponent('rigidbody', {
        friction: 0.5,
        type: 'static'
    });

    app.root.addChild(entity);

Nice, thanks @Leonidas that did the trick and bring in the Heightmap!

1 Like