When I use PBRCustomMaterial , what if I can guarantee that all the properties of the original PBR material on the model will be assigned to PBRCustomMaterial?

I wonder if there is a better way to assign the PBRCustomMaterial property?

  private copyMaterialProperties(source: PBRMaterial, target: PBRCustomMaterial) {
    target.uniqueId = source.uniqueId;
    target.albedoColor = source.albedoColor;
    target.albedoTexture = source.albedoTexture;
    target.metallic = source?.metallic;
    target.roughness = source?.roughness;
    target.metallicTexture = source?.metallicTexture;
    target.reflectionColor = source?.reflectionColor;

    target.reflectanceTexture = source?.reflectionTexture;
    target.reflectionColor = source?.reflectionColor;
    target.clearCoat.isEnabled = source.clearCoat?.isEnabled;
    target.clearCoat.intensity = source.clearCoat?.intensity;
    target.alpha = source?.alpha;
    target.alphaMode = source?.alphaMode;
    target.transparencyMode = source?.transparencyMode;
    target.useAlphaFromAlbedoTexture = source.useAlphaFromAlbedoTexture;
    if (target.albedoTexture) {
      target.albedoTexture.hasAlpha = source.albedoTexture!.hasAlpha;
    }
    target.needAlphaBlending = () => source.needAlphaBlending();
    target.needDepthPrePass = source.needDepthPrePass;

    ...

  }

As it turns out, I missed a lot of properties, because after replacing the material, the rendering is different than before!!

Any advice you give me will be helpful!!Thank you all

Hello :slight_smile:

Are you looking for such loop ?

Object.keys(source).forEach((key)=>{
    if(target[key]!=undefined && source[key]!=undefined){
        console.log("Assigning", key);
        target[key] = source[key];
    }
})

When I tried this, I still had problems, so I took the initiative to copy several important properties.

I don’t know if it’s because the PBRMaterial material contains a lot of internal references and circular references, or if it contains WebGL context-dependent attributes?

This black one is what the loop assignment looks like!
So I manually selected a few important attributes

What are you trying to achieve?

I want to achieve this effect:
The green car paint will gradually cover the car, but the car paint can not lose the original properties, so I want to use PBRCustomMaterial

shader-5

You could also rely on a material plugin instead Babylon.js docs