How to use resource handler for loading a json asset?

Hi everyone, in our project we want to load multiple assets from S3 but I got an error while running the script, it says :
No resource handler found for: function( modelAsset )

Can anyone suggest me a way to add a resource handler in this case? Here is the script I’m using:


var StartActions = pc.createScript('startActions');

// initialize code called once per entity
StartActions.prototype.initialize = function() 
{
    //this.setAssetSizes();
    this.s3Assets = [
        /*
         EntityName:
         { url/json:, filename:, position:, size-x:, size-y:, size-z:, entityName:, fatherEntity:, scaleX}
         * 
        */
        {
            'filename': 'Pantalla.json',
            'url': ' https://3dsvent.s3.amazonaws.com/assets/Pantalla.json',
            'posX': -71,
            'posY': 0,
            'posZ': 0,
            'scaleX': '',
            'scaleY': '',
            'scaleZ': '',
            'scale': 4.68,
            'fatherEntity': 'Decoracion',
            'entityName': 'PantalaTest'
         }
    ];
    this.loadAssets();
};

/* Set size of all objects by tag */
StartActions.prototype.setAssetSizes = function()
{
    var models = this.app.root.findByTag("modelSize");
    for (var i = 0; i < models.length; i++)
    {
        models[i].setLocalScale(0.9, 0.9, 0.9);
    }
};

StartActions.prototype.loadAssets = function()
{
    var assets = this.s3Assets;
    
    for (var asset in assets)
    {
        var fatherEnt = this.app.root.findByName(asset.fatherEntity);
        var file = { url: asset.url, filename: asset.filename };
        var assetS3 = new pc.Asset('load', function( modelAsset ) 
        {
            var entity = new pc.Entity(asset.entityName);
            entity.addComponent("model");
            entity.model.asset = modelAsset;
            entity.collision.asset = modelAsset;
            entity.collision.enabled = true;
            entity.setPosition(asset.posX, asset.posY, asset.posZ);
            if (!asset.scale !== null || !asset.scale !== undefined) { entity.setLocalScale(asset.scale, asset.scale, asset.scale); } else { entity.setLocalScale(asset.scaleX, asset.scaleY, asset.scaleZ); }
            fatherEnt.addChild(entity);
        });
        
        this.app.assets.add(assetS3);
        this.app.assets.load(assetS3);
    }
};

Hi @Axel_Saucedo,

If you are trying to load Playcanvas .json model assets, you don’t have to add a new resource loader.

Try using the loadFromUrl method and specify the type of asset you are loading (model):

this.app.assets.loadFromUrl("https://remote.url.to/asset.json", "model", function (err, asset) {
    var model = asset.resource;
});

https://developer.playcanvas.com/en/api/pc.AssetRegistry.html#loadFromUrl

2 Likes

Sorry to bother you again Leonidas, it’s due to I had this problem while I was running this script:

This is the test project:
https://playcanvas.com/editor/scene/976175

Which files should we be looking at?

Just scripts (startActions JS) due to the asset will be loaded from the url.

You have an error in your code here:
image

The for loop isn’t getting the object in the array but rather the key/index. Looks like you wanted forEach rather than for … in


1 Like