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

Reflector/Refractor: Change render target setups. #24386

Merged
merged 1 commit into from
Aug 3, 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
17 changes: 12 additions & 5 deletions examples/jsm/objects/Reflector.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,10 @@ import {
UniformsUtils,
Vector3,
Vector4,
WebGLRenderTarget
WebGLRenderTarget,
HalfFloatType,
NoToneMapping,
LinearEncoding
} from 'three';

class Reflector extends Mesh {
Expand Down Expand Up @@ -48,7 +51,7 @@ class Reflector extends Mesh {
const textureMatrix = new Matrix4();
const virtualCamera = this.camera;

const renderTarget = new WebGLRenderTarget( textureWidth, textureHeight, { samples: multisample } );
const renderTarget = new WebGLRenderTarget( textureWidth, textureHeight, { samples: multisample, type: HalfFloatType } );

const material = new ShaderMaterial( {
uniforms: UniformsUtils.clone( shader.uniforms ),
Expand Down Expand Up @@ -137,18 +140,19 @@ class Reflector extends Mesh {
projectionMatrix.elements[ 14 ] = clipPlane.w;

// Render

renderTarget.texture.encoding = renderer.outputEncoding;

scope.visible = false;

const currentRenderTarget = renderer.getRenderTarget();

const currentXrEnabled = renderer.xr.enabled;
const currentShadowAutoUpdate = renderer.shadowMap.autoUpdate;
const currentOutputEncoding = renderer.outputEncoding;
const currentToneMapping = renderer.toneMapping;

renderer.xr.enabled = false; // Avoid camera modification
renderer.shadowMap.autoUpdate = false; // Avoid re-computing shadows
renderer.outputEncoding = LinearEncoding;
renderer.toneMapping = NoToneMapping;

renderer.setRenderTarget( renderTarget );

Expand All @@ -159,6 +163,8 @@ class Reflector extends Mesh {

renderer.xr.enabled = currentXrEnabled;
renderer.shadowMap.autoUpdate = currentShadowAutoUpdate;
renderer.outputEncoding = currentOutputEncoding;
renderer.toneMapping = currentToneMapping;

renderer.setRenderTarget( currentRenderTarget );

Expand Down Expand Up @@ -254,6 +260,7 @@ Reflector.ReflectorShader = {
vec4 base = texture2DProj( tDiffuse, vUv );
gl_FragColor = vec4( blendOverlay( base.rgb, color ), 1.0 );

#include <tonemapping_fragment>
#include <encodings_fragment>

}`
Expand Down
8 changes: 3 additions & 5 deletions examples/jsm/objects/ReflectorForSSRPass.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@ import {
DepthTexture,
UnsignedShortType,
NearestFilter,
Plane
Plane,
HalfFloatType
} from 'three';

class ReflectorForSSRPass extends Mesh {
Expand Down Expand Up @@ -104,6 +105,7 @@ class ReflectorForSSRPass extends Mesh {

const parameters = {
depthTexture: useDepthTexture ? depthTexture : null,
type: HalfFloatType
};

const renderTarget = new WebGLRenderTarget( textureWidth, textureHeight, parameters );
Expand Down Expand Up @@ -198,10 +200,6 @@ class ReflectorForSSRPass extends Mesh {
textureMatrix.multiply( virtualCamera.matrixWorldInverse );
textureMatrix.multiply( scope.matrixWorld );

// Render

renderTarget.texture.encoding = renderer.outputEncoding;

// scope.visible = false;

const currentRenderTarget = renderer.getRenderTarget();
Expand Down
18 changes: 12 additions & 6 deletions examples/jsm/objects/Refractor.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,10 @@ import {
UniformsUtils,
Vector3,
Vector4,
WebGLRenderTarget
WebGLRenderTarget,
LinearEncoding,
NoToneMapping,
HalfFloatType
} from 'three';

class Refractor extends Mesh {
Expand Down Expand Up @@ -45,7 +48,7 @@ class Refractor extends Mesh {

// render target

const renderTarget = new WebGLRenderTarget( textureWidth, textureHeight, { samples: multisample } );
const renderTarget = new WebGLRenderTarget( textureWidth, textureHeight, { samples: multisample, type: HalfFloatType } );

// material

Expand Down Expand Up @@ -191,16 +194,22 @@ class Refractor extends Mesh {
const currentRenderTarget = renderer.getRenderTarget();
const currentXrEnabled = renderer.xr.enabled;
const currentShadowAutoUpdate = renderer.shadowMap.autoUpdate;
const currentOutputEncoding = renderer.outputEncoding;
const currentToneMapping = renderer.toneMapping;

renderer.xr.enabled = false; // avoid camera modification
renderer.shadowMap.autoUpdate = false; // avoid re-computing shadows
renderer.outputEncoding = LinearEncoding;
renderer.toneMapping = NoToneMapping;

renderer.setRenderTarget( renderTarget );
if ( renderer.autoClear === false ) renderer.clear();
renderer.render( scene, virtualCamera );

renderer.xr.enabled = currentXrEnabled;
renderer.shadowMap.autoUpdate = currentShadowAutoUpdate;
renderer.outputEncoding = currentOutputEncoding;
renderer.toneMapping = currentToneMapping;
renderer.setRenderTarget( currentRenderTarget );

// restore viewport
Expand All @@ -221,10 +230,6 @@ class Refractor extends Mesh {

this.onBeforeRender = function ( renderer, scene, camera ) {

// Render

renderTarget.texture.encoding = renderer.outputEncoding;

// ensure refractors are rendered only once per frame

if ( camera.userData.refractor === true ) return;
Expand Down Expand Up @@ -317,6 +322,7 @@ Refractor.RefractorShader = {
vec4 base = texture2DProj( tDiffuse, vUv );
gl_FragColor = vec4( blendOverlay( base.rgb, color ), 1.0 );

#include <tonemapping_fragment>
#include <encodings_fragment>

}`
Expand Down