☑ I have some problem when I change the material

This is my script

var FadeIn=function(entity){
var opacity=0;
var mat=entity.model.model.meshInstances[0].material;

return (function(){
    setTimeout(function(){
        opacity+=1/100;
        mat.opacity=opacity;
        mat.update();
        if(opacity<=1){
            setTimeout(arguments.callee)
        }
    },16)
}())
}

I want to make a fade in animation with this script.

So I give a model clone object as a arguments.

    var e=this.edge.clone();
    FadeIn(e);

However,I clone too many edge model.

Once upon I use FadeIn(e) function.

I will found that all edge clone object would change the opacity.

I think the reason is:

I change the opacity with material,so all the entity who use this material would change together.

So,I want to know how can I solve this problem?

I just want to change the only one edge clone object not all the edge clone object.

Thank you.

You can use setParameter method on specific mesh you want change opacity of. Change material_opacity property on mesh to desired opacity, when using this method, uniform is set directly, so no need to call material.update.

Dear max

I use setParameter like

opacity+=1/100;
//             mat.opacity=opacity;
//             mat.update();
mat.setParameter('material_opacity',opacity)

However,the problem don’t solve.

This is my project.

https://playcanvas.com/project/421458/overview/question

You can see that there is some problem when the edge create.

Thank you.

Use setParameter on meshInstance directly, something like:
entity.model.meshInstances[0].setParameter.

This is mesh scope parameter overriding.

Dear max

I am so happy to tell you that I succeed in setting the only one opacity of the model.

I find that the setParameter API is so convenient.

So,I want to know more parameter of the setParameter API such as ‘material_opacity’ ‘uDiffuseMap’

‘texture_emissiveMapTransform’.

However,I can’t find any way to do this.

Could you tell me some tip?

1 Like

Hey, that’s cool you’ve figured it out.

There is a concept called “shader chunks” - those are pieces of shader that implement specific bits. Based on material parameters shader is generate using specific set of chunks. So different materials might have different shaders.
You can inspect material shader if you console.log or use dev tools with breakpoints to inspect material object, it has shader property, which has definition in which you can find fshader and vshader - those are shader strings.
uniform - is what you can set using setParameter.

Additionally there is a global object pc.shaderChunks that contains all chunks, or simply check out source: https://github.com/playcanvas/engine/tree/master/src/graphics/program-lib/chunks of all chunks.

1 Like