engine icon indicating copy to clipboard operation
engine copied to clipboard

Broken environment reflections when using render to texture

Open MAG-AdrianMeredith opened this issue 1 year ago • 10 comments

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

  1. load the examples app locally
  2. load up the render to texture example
  3. 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

MAG-AdrianMeredith avatar Dec 08 '24 21:12 MAG-AdrianMeredith

Interestingly this seems to work on WebGPU for me, only the WebGL has the issue.

mvaligursky avatar Dec 09 '24 10:12 mvaligursky

If I comment out this line from the example, all is fine. Need to investigte:

        // flipY: !app.graphicsDevice.isWebGPU,

mvaligursky avatar Dec 09 '24 12:12 mvaligursky

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.

mvaligursky avatar Dec 09 '24 15:12 mvaligursky

passing -litArgs_worldNormal to that function instead of litArgs_worldNormal solves this. Just need to figure out a generic solution, other implications.

mvaligursky avatar Dec 09 '24 15:12 mvaligursky

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)

MAG-AdrianMeredith avatar Jan 21 '25 14:01 MAG-AdrianMeredith

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???

MAG-AdrianMeredith avatar Jan 21 '25 20:01 MAG-AdrianMeredith

yep, that's where I concluded there is something not right with the model perhaps.

mvaligursky avatar Jan 21 '25 20:01 mvaligursky

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?

MAG-AdrianMeredith avatar Jan 23 '25 11:01 MAG-AdrianMeredith

not really. maybe flip along two axis, that should avoid normals issue

mvaligursky avatar Jan 23 '25 12:01 mvaligursky

In the end I've removed the flip and am fixing it in post (which isn't ideal) but it still an issue

MAG-AdrianMeredith avatar May 12 '25 07:05 MAG-AdrianMeredith