[SOLVED] Engine only texture loading problems


#1

Hi guys,

i cant seem to find a solution for my following problem. Im struggling for 2-3 hours now and didnt find any solution on the internet that worked.

Im just trying to load a material on a cube model.

class PlayerEntity {

    constructor() {
        this.entity = new pc.Entity();
        // load component
        this.entity.addComponent("model", { type: "box" });
        this.entity.setName('Player')

        const asset = new pc.Asset("proto-orange", "texture", {
            url: "./assets/textures/proto_orange.png"
        });
        app.assets.add(asset)
       this.entity.model.meshInstances[0].material = asset.resource;  
    }
}

And im getting the following error:

playcanvas-engine.js:17526 Uncaught TypeError: Cannot read property 'blendType' of undefined
    at Layer.addMeshInstances (playcanvas-engine.js:17526)
    at ModelComponent.addModelToLayers (playcanvas-engine.js:24650)
    at ModelComponent.onEnable (playcanvas-engine.js:24773)
    at Entity._onHierarchyStateChanged (playcanvas-engine.js:38480)
    at Entity._notifyHierarchyStateChanged (playcanvas-engine.js:38452)
    at Entity._onInsertChild (playcanvas-engine.js:11694)
    at Entity.addChild (playcanvas-engine.js:11671)
    at main.js:26

i hope someone got a solution for this


#2

i just found a solution online, but its not showing in the assetRegistry. Maybe someone else got a better solution?

class PlayerEntity {

    constructor() {
        this.entity = new pc.Entity();
        // load component
        this.entity.addComponent("model", { type: "box" });
        this.entity.setName('Player')

        const entityMaterial = this.entity.model.meshInstances[0].material;
        entityMaterial.diffuseMap = this.setMaterial("./assets/textures/proto_orange.png");
        entityMaterial.update();
   
        console.log(pc.app.assets)
    }

      setMaterial(materialPath) {
        let texture = new pc.gfx.Texture(app.graphicsDevice);
        let img = new Image();
        img.onload = function () {
          texture.setSource(img);
        };
        img.src = materialPath;
        return texture;
      }
}

#3

From your first post, it looks like you are creating a texture asset but trying to assign it to the material property of the mesh instance. You will need to create a material asset and assign the texture to it.

See example here https://github.com/playcanvas/engine/blob/master/examples/spinning_cube/textured.html#L75


#4

thank you very much. It is that simple :grin: