Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

PMREMGenerator: Add flipEnvMap. #23158

Merged
merged 4 commits into from
Jan 6, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 13 additions & 14 deletions examples/webgl_materials_cubemap_dynamic.html
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,9 @@

import * as THREE from '../build/three.module.js';

let camera, scene, renderer;
import Stats from './jsm/libs/stats.module.js';

let camera, scene, renderer, stats;
let cube, sphere, torus, material;

let count = 0, cubeCamera1, cubeCamera2, cubeRenderTarget1, cubeRenderTarget2;
Expand Down Expand Up @@ -49,35 +51,30 @@
renderer.outputEncoding = THREE.sRGBEncoding;
document.body.appendChild( renderer.domElement );

stats = new Stats();
document.body.appendChild( stats.dom );

scene = new THREE.Scene();
scene.background = texture;

camera = new THREE.PerspectiveCamera( 60, window.innerWidth / window.innerHeight, 1, 1000 );

//

cubeRenderTarget1 = new THREE.WebGLCubeRenderTarget( 256, {
format: THREE.RGBFormat,
generateMipmaps: true,
minFilter: THREE.LinearMipmapLinearFilter
} );
cubeRenderTarget1 = new THREE.WebGLCubeRenderTarget( 256 );

cubeCamera1 = new THREE.CubeCamera( 1, 1000, cubeRenderTarget1 );

cubeRenderTarget2 = new THREE.WebGLCubeRenderTarget( 256, {
format: THREE.RGBFormat,
generateMipmaps: true,
minFilter: THREE.LinearMipmapLinearFilter
} );
cubeRenderTarget2 = new THREE.WebGLCubeRenderTarget( 256 );

cubeCamera2 = new THREE.CubeCamera( 1, 1000, cubeRenderTarget2 );

//

material = new THREE.MeshBasicMaterial( {
material = new THREE.MeshStandardMaterial( {
Mugen87 marked this conversation as resolved.
Show resolved Hide resolved
envMap: cubeRenderTarget2.texture,
combine: THREE.MultiplyOperation,
reflectivity: 1
roughness: 0,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would also recommend using a roughness value > 0 (or several values, or adjustable values). The example only worked before for mirrored reflections; now that it uses actual PMREM, it will also work for other roughness values, so we should demonstrate that to help ensure it does not regress.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

metalness: 1
} );

sphere = new THREE.Mesh( new THREE.IcosahedronGeometry( 20, 8 ), material );
Expand Down Expand Up @@ -201,6 +198,8 @@

renderer.render( scene, camera );

stats.update();

}

</script>
Expand Down
9 changes: 7 additions & 2 deletions src/extras/PMREMGenerator.js
Original file line number Diff line number Diff line change
Expand Up @@ -342,6 +342,8 @@ class PMREMGenerator {

}

this._cubemapShader.uniforms.flipEnvMap.value = ( texture.isRenderTargetTexture === false ) ? - 1 : 1;

} else {

if ( this._equirectShader == null ) {
Expand Down Expand Up @@ -763,7 +765,8 @@ function _getCubemapShader() {
name: 'CubemapToCubeUV',

uniforms: {
'envMap': { value: null }
'envMap': { value: null },
'flipEnvMap': { value: - 1 }
},

vertexShader: _getCommonVertexShader(),
Expand All @@ -773,13 +776,15 @@ function _getCubemapShader() {
precision mediump float;
precision mediump int;

uniform float flipEnvMap;

varying vec3 vOutputDirection;

uniform samplerCube envMap;

void main() {

gl_FragColor = textureCube( envMap, vec3( - vOutputDirection.x, vOutputDirection.yz ) );
gl_FragColor = textureCube( envMap, vec3( flipEnvMap * vOutputDirection.x, vOutputDirection.yz ) );
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

❤️


}
`,
Expand Down
2 changes: 0 additions & 2 deletions src/renderers/WebGLCubeRenderTarget.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,6 @@ class WebGLCubeRenderTarget extends WebGLRenderTarget {
this.texture.generateMipmaps = options.generateMipmaps !== undefined ? options.generateMipmaps : false;
this.texture.minFilter = options.minFilter !== undefined ? options.minFilter : LinearFilter;

this.texture._needsFlipEnvMap = false;
Mugen87 marked this conversation as resolved.
Show resolved Hide resolved

}

fromEquirectangularTexture( renderer, texture ) {
Expand Down