[SOLVED] Setting material opacity using setParameter

I’m trying to set the material opacity on specific model without affecting other models using the same material:

this.entity.model.model.meshInstances.forEach(mi => mi.setParameter("material_opacity", 0.5));

Unfortunately, it doesn’t work :frowning:
Setting another parameters work as expected, e.g:

this.entity.model.model.meshInstances.forEach(mi => mi.setParameter('material_diffuse', [1,0,0]));

What I’m doing wrong?

P.S. Is it possible to find the complete list of all the possible parameter like material_opacity and material_diffuse?

Hi @Igor,

What is the initial value of opacity in your material?

To have the opacity shader chunk included in the generated shader program usually you have to trick the engine by putting a non 1.0 value.

If it’s 1.0 initially, put 0.999 and try again.

1 Like

About that list, not sure if there is one but at any point you can check in the console the generated shader definition of the material under:

material.shader;

All the included uniforms in the vertex and pixel shaders can be adjusted using the setParameter method.

1 Like

Thanks @Leonidas, setting initial opacity to 0.999 solves the problem.

1 Like

I got tired of manually setting my material opacities to 0.999 (annoying if you are fading an entire object and it has a lot of materials). I also just never liked the idea that a fully opaque material was being rendered as being a tiny bit transparent. So I just check to see if the material opacity is undefined or not. If it is, I set it to 0.999999 using material.update(), and then set it back to its initial value with setParameter(). That establishes the shader chunk for the material’s transparency and I can just use setParameter() from this point forward for any other opacity changes.

if(this.entity.model.meshInstances[0].parameters.material_opacity === undefined){
   this.initialOpacity = this.entity.model.meshInstances[this.miStep].material.opacity;
   this.entity.model.meshInstances[this.miStep].material.opacity = (this.initialOpacity * 0.999999);
   this.entity.model.meshInstances[this.miStep].material.blendType = pc.BLEND_NORMAL;
   this.entity.model.meshInstances[this.miStep].material.update();
   this.entity.model.meshInstances[this.miStep].setParameter('material_opacity', ( this.initialOpacity));
}	
2 Likes