Adding geo at runtime redux

I’m working with the API adding assets at runtime. I got a model downloaded from PlayCanvas to programmatically load and display (see code at bottom).

When I download a model from PlayCanvas it gives me a nice package that includes all the textures and materials along with the geo. It includes a mapping file which describes the location of the relevant materials. In the back of my mind I was sorta hoping that when the asset was displayed the textures and stuff would ‘programagically’ already be referenced. But no, the only thing displayed was the raw geo.

Is there built-in functionality in the API which parses the mapping file, loads all the resources, and attaches the materials to the geo or do I have to parse the mapping file manually and load/map all the resources individually?

function loadObject(){
var pc_cube_entity, pc_cube_asset;

  //create asset
  pc_cube_asset = new pc.Asset("pc_cube", "model", {
      url: "./assets/playcanvas_cube/playcanvas.json"
  });
  app.assets.add(pc_cube_asset);

  //add asset to registry
  pc_cube_asset.on("load", function (err, model) {
      //add entity to world
      pc_cube_entity = new pc.Entity();
      pc_cube_entity.addComponent("model", {
          type: "asset",
          asset: this
      });
      app.root.addChild(pc_cube_entity);
  });
  app.assets.load(pc_cube_asset);

}

This is how I’ve done it in the past: Load 3D models at run time from a PlayCanvas application

var Sandbox = pc.createScript('sandbox');

Sandbox.URL = 'https://raw.githubusercontent.com/yaustar/3dmodel-Playcanvas/master/Gas%20Station.json';

// initialize code called once per entity
Sandbox.prototype.initialize = function() {
    this.app.assets.loadFromUrl(Sandbox.URL, "model", function (err, asset) {
        var entity = new pc.Entity();
        entity.addComponent("model");
        entity.model.model = asset.resource;
        pc.app.root.addChild(entity);
    });
};

// swap method called for script hot-reloading
// inherit your script state here
// Sandbox.prototype.swap = function(old) { };

// to learn more about script anatomy, please read:
// http://developer.playcanvas.com/en/user-manual/scripting/

It looks like that code will load the geo but not all the materials and textures. I was hoping there was something built into the API that would load all the textures and materials based on the mapping files that come when you download an asset from PlayCanvas.

Are you guys aware of anything like that or do I have to load every texture and material manually and then attach them to the newly created entity?

It should load. Are there any errors in the console? The assets will have to uploaded in the correct folder names which are GUID based. eg 6836758

eg: GitHub - yaustar/3dmodel-Playcanvas

Ah, you are awesome! That worked…thanks a lot.