When PBRMaterial sets MirrorTexture, this material is not affected by .env?

Hello, in my scene, I used env as scene.environmentTexture

I have a PBR material with a mirror texture, but now I have some problems. The meshes in the renderList are fine, but the rest is black

The material shown in this PG is StandardMaterial

So like my title, when I set MirrorTexture to PBRMaterial, this material is not affected by .env?

If so, how do I solve this problem?

Re-create a plane for creating a mirror , and set transparency, and the original material to do overlay, can solve?

Or is there an easier way?

Below is a screenshot of my scene, sorry I can’t reproduce it with PG
before setting mirrorTexture:

after setting mirrorTexture:


The reflection texture of a PBR material is an IBL texture, which takes precedence over the scene.environmentTexture when defined: both can’t be applied at the same time.

One way to be able to use a mirror texture with a PBR material would be to use a material plugin. You would pass the mirror texture to the plugin, and you would compute the final color yourself. cc @sebavan in case I’m missing something.

I have encountered some problems using the material plugin according to what you said

there is my plugin code:

getCustomCode(_shaderType: string, shaderLanguage: ShaderLanguage): { [pointName: string]: string } {
        if (shaderLanguage === ShaderLanguage.GLSL) {
            if (_shaderType === 'fragment') {
                return {
                    "CUSTOM_FRAGMENT_DEFINITIONS": `
                        uniform sampler2D mirrorSampler;
                    `,
                    "CUSTOM_FRAGMENT_MAIN_END": `
                        vec4 mirrorColor = texture2D(mirrorSampler, vUV);
                        vec4 myColor = finalColor + mirrorColor;
                        gl_FragColor = mirrorColor;
                    `
                }
            }
        }
        return {};
    }
      const mountain = this.scene.getMeshByName('Scene_SuperLink_Day_primitive0') as AbstractMesh

      const ground = this.scene.getMeshByName('Scene_SuperLink_Day_primitive1') as AbstractMesh
      const mat = ground.material as PBRMaterial

      this.mirrorTexture.renderList?.push(mountain)
      
      const mirrorPlug = new MirrorPluginMaterial(mat, this.mirrorTexture)
         
      //  mirrorPlug.isEnabled = true
      // pass the mirrorTexture to reflectionTexture channel
      mat.reflectionTexture = this.mirrorTexture;

when I passed the mirrorTexture to reflectionTexture channel, I was able to see the effect, which assured me that my mirrorTexture was well generated!

But When I remove the reflectionTexture and enable the material plugin, and set gl_FragColor = mirrorColor; It was all white, which again made me wonder if my mirrorTexture was wrong

when I set this code:

  vec4 myColor = finalColor + mirrorColor;
  gl_FragColor = mirrorColor;

just like this:

Just like you said, how should it be computed with finalColor?
Thank you for your help

@Evgeni_Popov

I realize that MirrorTexture should be CubeTexture and samplerCube should be used for sampling, but I still don’t know how to calculate the color of mirrorTexture and finalColor
@Evgeni_Popov
@sebavan

Do you have a skybox visible in your miror texture ?

No, just like in my code, just mountain

this.mirrorTexture.renderList?.push(mountain)

Does skybox need to be added?

But my lighting information is provided by.env

Yes, if you want to see it in the miror or with probes, you need a skybox so that there is something to reflect. Think about it like currently you are reflecting the void so there are nothing to show. The skybox would display the env and your mirror would then have this surface to reflect.

Ok, I will try it later, and whether it fails or succeeds, I will come back here to give feedback
Thank you

You can use a 2D texture, but you must use the right projection mode to read from the texture in the shader code:

1 Like

It’s so perfect. Thank you so much

On version 134.0.6998.166 of Mac chrome, an error is reported

‘view’ : undeclared identifier

error:

I modified the code a bit to make it work on Mac chrome

Thank you very much, hope this feature will be added to the main version of babylonjs

1 Like