Add texture to a box

I am new to coding in Playcanvas and would like to simply add an image to a box in runtime:

var asset_testImage = this.app.assets.find("testImages.png");
var asset_testImage_url = asset_testImage.getFileUrl();
// option 1
var asphaltTexture = new pc.Asset("asphaltTexture", "texture", {
        url: asset_testImage.getFileUrl()
    });

// option 2
var asphaltTexture2 = new pc.Texture(self.app.graphicsDevice);
    asphaltTexture2.setSource(asset_testImage_url);

box.addComponent("model", {
        type: "box"
    });

var materialista = new pc.StandardMaterial();
    materialista.diffuseMap = asphaltTexture; // or asphaltTexture2??

box.model.material.diffuseMap = asphaltTexture2;
    box.model.material.update();

this.app.root.addChild(box);

Is this the right way? I tried to solve it like the steps in the editor. What am I doing wrong?

Close, if the image is already the the assets, then it it is already a texture. Example: https://playcanvas.com/editor/scene/725585

1 Like

Thank you so much! I dont know how many hours I spent for this little thing