PMREMGenerator: FromScene behavior differs with renderer clear color vs scene.background
See original GitHub issueDescribe the bug
The resulting environment map from PMREMGenerator.fromScene differs when calling renderer.setClearColor( 0xffffff )
beforehand vs calling scene.background = new Color( 0xffffff );
.
To Reproduce
You can see the difference in behavior in this fiddle:
https://jsfiddle.net/0saxwg6n/
Uncomment this line to see the behavior difference:
// NOTE: Uncomment this line to make it work
// scene.background = new THREE.Color( 0xffffff );
Code
Setting clear color:
renderer.setClearColor( 0xffffff );
const generator = new THREE.PMREMGenerator( renderer );
const rt = generator.fromScene( scene );
scene.environment = rt.texture;
Setting scene background:
scene.background = new THREE.Color( 0xffffff );
const generator = new THREE.PMREMGenerator( renderer );
const rt = generator.fromScene( scene );
scene.environment = rt.texture;
Expected behavior
The look of the resulting map is the same regardless of whether or not the background color is set via scene.background or renderer clear color.
Screenshots
renderer.setClearColor( 0xffffff );
scene.background = new Color( 0xffffff );
(an aside but is it expected that the background become so much darker when rendering to the environment map?)
Platform:
- Device: Desktop
- OS: Windows 10
- Browser: Chrome
- Three.js version: dev
Issue Analytics
- State:
- Created 3 years ago
- Comments:9 (3 by maintainers)
Top GitHub Comments
What was getting me was that this section of code is specifically designed to be the inverse of the RGBEToLinear shader function which means that setting a white background should get converted to RGBE when writing to the cube map and get converted back to white when sampling but instead we’re getting the tinted gray color.
After some testing this seems to be related to the
premultipliedAlpha
setting on the renderer and the fact that the alpha component is being overloaded to represent the exponent in this case. Setting it tofalse
in the example above yields more expected results:PremultipliedAlpa : True
PremultipliedAlpha : False
This StackOverflow answer mentions that there can be quirks when the premultiplied alpha color used to clear is invalid (white to RGBE is
1.0, 1.0, 1.0, 0.5...
) and ultimately the result shouldn’t be dependent on the premultiplied alpha setting. I think this can be fixed and made resilient to the setting by clearing the background with a flat color mesh with opacity rather than using the renderer clear color but I have to do more testing.Heh I think it’s just an extremely blown out white, which is why we’re not seeing the red tinting from the plane in the example image above.
Hmm, should be fairly fixable actually… We should just take
renderer.outputEncoding
into account when rendering the background color?