[SOLVED] Created material is not assigning to MeshInstances / Entity

Hello everyone, so basically I’m trying to implement gallery, which fetches images from url.
I create all pictures (entities, boxes) in script in initialize().
Firsly i met a problem where all pictures in my gallery were the same - I found a solution in forum, which tells that all created entities reference the default material. So i could create new material, or clone the default one and assign to entity. And that’s the problem.

    for (let child of this.entity.children) { // this.entity references the abstract entity which parent is root and which doesn't contain anything except the script. In this script I create entities and make them children of the entity before this function
        this.app.assets.loadFromUrl(imageUrl, "texture", function (err, asset) {
            let material = new pc.StandardMaterial();
            material.diffuseMap = asset.resource;
            material.update();

            child.render.meshInstances[0].material = material;
        });
}

I tried a lot of ways including putting material.update everywhere, loading texture with app.assets.add, app.assets.load, cloning, creating StandardMaterial and BasicMaterial, updating in a loop like so

            var renders = entity.findComponents('render');
            for (var i = 0; i < renders.length; ++i) {
                var meshInstances = renders[i].meshInstances;
                for (var j = 0; j < meshInstances.length; j++) {
                    var material = meshInstances[j].material.clone();
                    material.diffuseMap = asset.resource;
                    material.update();
                    meshInstances[j].material = material; // material.resource gives a lot of infinite errors in debugger
                }
            }

And creating some placeholder material with another color in Editor and passing it to the script
But anyway entities have the default material (or placeholder from editor), like ignoring any attempts to reassign.
The project: PlayCanvas | HTML5 Game Engine
Script: PlayCanvas | HTML5 Game Engine
What am I doing wrong?

Hi @Dmitry,

All of your entity creation and asset loading code is correct. The problem is the following line:

entity.render.isStatic = true;

Here you set your render component to static, meaning no further changes to the mesh instances are allowed. If you comment this line out it will work:

2 Likes

Yeah, it works, thank you!

1 Like