How can I modify material's properties via Script?

Hi.

I want to change the properties, like opacity, diffuse and so on of material by Script.

But I tried every way I known and it still doesn’t work.

One of way I tried is

    this.material = new pc.StandardMaterial();
    this.material.diffuse.set(110/255, 193/255, 165/255);
    this.material.update();
    this.asset = new pc.Asset('BgColor02', 'material');
    this.asset.resource = this.material;
    this.entity.model.materialAsset = this.asset;

Also, I used context.assets.get(id) to get the material and set its parameters. Did not work, either.

Is there any way to set the material’s properties, like opacity, diffuse and so on ?

Thank you for your time !

You can set properties on the material like you do and then when you set all of them, you call material.update() (like you do). Then to set the material on a model you need to set it on all its mesh instances. So something like this:

var meshInstances = this.entity.model.meshInstances;
for (var i = 0; i < meshInstances.length; i++) {
    meshInstances[i].material = material;
}

That’s only needed initially when you want to assign a new material to a model. In general if you’re creating stuff like materials programmatically you don’t need to mess with assets.

PS: The context does not exist anymore it’s legacy code. If you are using the new scripts you should use this.app instead - so if you want to access the asset registry you’d do this.app.assets instead of context.assets .