How can i load model on run time loading fine on static url

Given that the callback is in the initialise function of a script, I would listen for the first render event to send the message.

1 Like

Ah, I forgot this was in an iframe. In which case, I would have some code on the PlayCanvas app that would post a message to the parent to tell it when itā€™s ready.

Iā€™ve updated the project and JSFiddle to load the model when the PlayCanvas app is ready:

https://jsfiddle.net/v31cxktr/9/
https://playcanvas.com/editor/scene/974566

1 Like

@yaustar
thank you for your help really appreciated, i have some questions , let me know how we can focus on that entity we have load from outside or if we want to change materials or any other properties ?

What do you mean by focus?

You can keep the reference to the Entity that is created on the load process and from there you can access the model and materials to change/modify.

Camera focus

The Model Viewer Starter Kit does this in the Orbit camera: https://playcanvas.com/project/446385/overview/starter-kit-model-viewer

TLDR, It gets the AABB of all the models that you want to have in focus and works out roughly how far back it needs to be to have it all in frame.

can you implement it on example ? i am not able to integrate that

Sorry, Iā€™m afraid Iā€™m pretty rammed with work at the moment.

This is some code that Iā€™m using to focus on one object, simply loop over all your other models and keep adding to the bounds for multiple, I guess.

var bounds = this.targetEntity.model.meshInstances[0].aabb;
// .. add another loop around this for all your other models or so
for (var mesh = 1; mesh < this.targetEntity.model.meshInstances.length; mesh++) {
  bounds.add(this.targetEntity.model.meshInstances[mesh].aabb);
}

If you also have them moved the entities around by transform, youā€™ll need to manually modify the bounds of them too I think.
Then you can easily get the biggest axis (either x, y or z)

var size = bounds.halfExtents;
var biggest = size.x;
if (biggest < size.y) biggest = size.y;
if (biggest < size.z) biggest = size.z;

and then just use that value multiplied by some zoom factor as your start zoom (I donā€™t use their starter kit anymore so youā€™ll have to try something yourself for this).


if you need to change stuff like materials you should check the API reference, as yaustar said you should keep references to entities and then go from there: https://developer.playcanvas.com/en/api/pc.Entity.html. For a material swap try this:

// Sets a new StandardMaterial on each mesh
// a model is built out of meshInstances, each has a material slot
entity.model.meshInstances.forEach(function(mesh) {
  mesh.material = new pc.StandardMaterial();
});
1 Like

if you check our above example you can see that we are load model dynamically

In which case, perform the code after all the models have finished loading :slight_smile:

1 Like

@yaustar
@Ivolutio

how to rotate this loaded model from code when get post message ?

Depends what you mean with ā€˜rotateā€™, just once or continuously?
If you just want to orient it after it spawned: entity.setLocalEulerAngles(x, y, z)
If you want to apply continuous rotation my approach would be to create and add a ā€˜rotateā€™ script with an update method calling entity.rotate(x, y, z).

I suggest you dig through some more of the documentation and API reference.
https://developer.playcanvas.com/en/api/pc.Entity.html#setLocalRotation
https://developer.playcanvas.com/en/api/pc.Entity.html#rotate
https://developer.playcanvas.com/en/user-manual/scripting/creating-new/

1 Like

like Xflip Yflip

@Ivolutio

have used this code in load model but nothing is working !

    var r = new pc.Vec3(0, 90, 0);
    this.entity.model.model.rotate(r);
    console.log('asd');

Rotate only works on entity, not the model component

1 Like

also not working on this !

    var r = new pc.Vec3(0, 90, 0);
     this.entity.rotate(r);

That will rotate it once per call by 90 degrees on the Y across. If you want to rotate it continuously, you need to call it per frame by the amount you want to rotate each frame.