Broken environment reflections when using render to texture
Description
When rendering a model with a secondary camera into a renderTarget, the reflections are broken.
I was able to reproduce this using a totally clean checkout against latest main and v2.1.0. Weirdly enough the same code running on the examples website seems to work???
Steps to Reproduce
- load the examples app locally
- load up the render to texture example
- load a model with shiny parts
and add ContainerHandler + Basis 4. See that the model in the render to textures is super shiny and totally broken
Interestingly this seems to work on WebGPU for me, only the WebGL has the issue.
If I comment out this line from the example, all is fine. Need to investigte:
// flipY: !app.graphicsDevice.isWebGPU,
Learnings as I investigate this:
- it happens on both WebGL and WebGPU, if we set flipY to true on the render target. The issue is related to the flipping. We currently do the flipping by mirroring the projection matrix along the Y.
- The issue comes from fresnel calculation. We do this:
dReflection.rgb *= getFresnel(
dot(dViewDirW, litArgs_worldNormal), litArgs_gloss, litArgs_specularity
);
If I comment out this block, both rendering's match. So the problem is the fresnel term is incorrect in the texture. Could be either problem with viewDir, or with worldNormal.
passing -litArgs_worldNormal to that function instead of litArgs_worldNormal solves this. Just need to figure out a generic solution, other implications.
Bump, I'm running into this again. In that I'll need to add another workaround to flip the texture separate (I previously did this with the uvs). worst case scenario I could just knockup a little render pass to do it specifically for this camera and remove the other workarounds (bit of a waste of performance/memory though)
I had a go at a variant of what you were working on and at first it appeared to work. It fixed the original example but then others were now inverted???
yep, that's where I concluded there is something not right with the model perhaps.
It was more than one model in the original example so its not just that model. Something about the workflow of the normals that just doesn't like the world scale being inverted. Any other alternatives to implementing a yFlip?
not really. maybe flip along two axis, that should avoid normals issue
In the end I've removed the flip and am fixing it in post (which isn't ideal) but it still an issue