[SOLVED] Change Render component asset at runtime

Considering my transition from Model + Animation components, how would I assign the asset of a .glb file to a Render component?

Heres what I have so far:

Function it resides in:

I’ve also attempted changing the material in a similar manner with no success, utilizing Loading glTF GLBs for setup.

Hi @khaelou,

Check this example:

https://developer.playcanvas.com/en/tutorials/loading-gltf-glbs/

Basically from the asset you can instantiate a render entity, which you can append as a child to your hierarchy:

    utils.loadGlbContainerFromAsset(this.glbAsset, null, this.glbAsset.name, function (err, asset) {
        var renderRootEntity = asset.resource.instantiateRenderEntity();
        self.entity.addChild(renderRootEntity);
    });

If you don’t want to add a new entity, you can inspect the asset and grab the mesh instances directly. From there you can easily add them to any render component using this property:

https://developer.playcanvas.com/api/pc.RenderComponent.html#meshInstances

I’ve referenced this resource as stated in the thread, this way of doing so does not add the animations or detect the animations from Anim component.

In other words, it doesn’t work out of the box like the Model component method.

Idle Anim = Model Component
TPose = Render Component

Yes, for the animations you need some additional steps.

This may be of help:

1 Like

Thanks for guiding me to this thread.

Yet I’m using the add-Animations.js script from Animation without State Graph on my Character entity, how would I take those animations and use instead?

Something like this?

Press 5 to load a new GLB in place but use the same anim component (I’ve modified the example to load a GLB from a binary asset):

https://playcanvas.com/editor/scene/1325648

3 Likes

Exactly, this is the method I further progressed with as it does not require use of the StateGraph component and supports existing FBX animations.

A post was merged into an existing topic: Can’t found the entity in editor