Skip to content

Commit f924198

Browse files
authored
Respect the original camera view offset inside SSAARenderPass. (#21740)
* Respect the original camera view offset inside SSAARenderPass. Add a viewOffsetX GUI param to the webgl_postprocessing_ssaa example. * Refine the camera view preservation inside SSAARenderPass. Fix a viewOffsetX param typo inside the webgl_postprocessing_ssaa example. * Convert spaces to tabs inside the webgl_postprocessing_ssaa example and SSAARenderPass. * Update SSAARenderPass to always preserve the original camera view. * Update the SSARenderPass to handle the original view offset through camera methods instead of direct access of camera.view. * Remove an unnecessary conditional inside SSAARenderPass. * Refine the originalViewOffset conditional logic further inside SSAARenderPass. Co-authored-by: Matt Simpson <[email protected]>
1 parent 08b9537 commit f924198

File tree

2 files changed

+54
-6
lines changed

2 files changed

+54
-6
lines changed

examples/jsm/postprocessing/SSAARenderPass.js

Lines changed: 48 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,25 @@ class SSAARenderPass extends Pass {
9595
const roundingRange = 1 / 32;
9696
this.copyUniforms[ 'tDiffuse' ].value = this.sampleRenderTarget.texture;
9797

98-
const width = readBuffer.width, height = readBuffer.height;
98+
let viewOffset = {
99+
100+
fullWidth: readBuffer.width,
101+
102+
fullHeight: readBuffer.height,
103+
104+
offsetX: 0,
105+
106+
offsetY: 0,
107+
108+
width: readBuffer.width,
109+
110+
height: readBuffer.height
111+
112+
};
113+
114+
let originalViewOffset = Object.assign( {}, this.camera.view );
115+
116+
if ( originalViewOffset.enabled ) Object.assign( viewOffset, originalViewOffset );
99117

100118
// render the scene multiple times, each slightly jitter offset from the last and accumulate the results.
101119
for ( let i = 0; i < jitterOffsets.length; i ++ ) {
@@ -104,9 +122,15 @@ class SSAARenderPass extends Pass {
104122

105123
if ( this.camera.setViewOffset ) {
106124

107-
this.camera.setViewOffset( width, height,
108-
jitterOffset[ 0 ] * 0.0625, jitterOffset[ 1 ] * 0.0625, // 0.0625 = 1 / 16
109-
width, height );
125+
this.camera.setViewOffset(
126+
127+
viewOffset.fullWidth, viewOffset.fullHeight,
128+
129+
viewOffset.offsetX + jitterOffset[ 0 ] * 0.0625, viewOffset.offsetY + jitterOffset[ 1 ] * 0.0625, // 0.0625 = 1 / 16
130+
131+
viewOffset.width, viewOffset.height
132+
133+
);
110134

111135
}
112136

@@ -141,8 +165,26 @@ class SSAARenderPass extends Pass {
141165
this.fsQuad.render( renderer );
142166

143167
}
144-
145-
if ( this.camera.clearViewOffset ) this.camera.clearViewOffset();
168+
169+
if ( this.camera.setViewOffset && originalViewOffset.enabled ) {
170+
171+
this.camera.setViewOffset(
172+
173+
originalViewOffset.fullWidth, originalViewOffset.fullHeight,
174+
175+
originalViewOffset.offsetX, originalViewOffset.offsetY,
176+
177+
originalViewOffset.width, originalViewOffset.height
178+
179+
);
180+
181+
}
182+
183+
else if ( this.camera.clearViewOffset ) {
184+
185+
this.camera.clearViewOffset();
186+
187+
}
146188

147189
renderer.autoClear = autoClear;
148190
renderer.setClearColor( this._oldClearColor, oldClearAlpha );

examples/webgl_postprocessing_ssaa.html

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@
3939
camera: 'perspective',
4040
clearColor: 'black',
4141
clearAlpha: 1.0,
42+
viewOffsetX: 0,
4243
autoRotate: true
4344

4445
};
@@ -67,6 +68,7 @@
6768
gui.add( params, 'camera', [ 'perspective', 'orthographic' ] );
6869
gui.add( params, "clearColor", [ 'black', 'white', 'blue', 'green', 'red' ] );
6970
gui.add( params, "clearAlpha", 0, 1 );
71+
gui.add( params, "viewOffsetX", -100, 100 );
7072
gui.add( params, "autoRotate" );
7173

7274
gui.open();
@@ -92,6 +94,7 @@
9294

9395
cameraP = new THREE.PerspectiveCamera( 65, aspect, 3, 10 );
9496
cameraP.position.z = 7;
97+
cameraP.setViewOffset( width, height, params.viewOffsetX, 0, width, height );
9598

9699
cameraO = new THREE.OrthographicCamera( width / - 2, width / 2, height / 2, height / - 2, 3, 10 );
97100
cameraO.position.z = 7;
@@ -172,6 +175,7 @@
172175
const aspect = width / height;
173176

174177
cameraP.aspect = aspect;
178+
cameraP.setViewOffset( width, height, params.viewOffset, 0, width, height );
175179
cameraO.updateProjectionMatrix();
176180

177181
cameraO.left = - height * aspect;
@@ -226,6 +230,8 @@
226230
ssaaRenderPassO.enabled = ( params.camera === 'orthographic' );
227231

228232
copyPass.enabled = ! params.renderToScreen;
233+
234+
cameraP.view.offsetX = params.viewOffsetX;
229235

230236
composer.render();
231237

0 commit comments

Comments
 (0)