Skip to content

Commit 906211f

Browse files
authored
PassNode: Provide viewport and scissor API. (#31390)
* PassNode: Provide viewport and scissor API. * PassNode: Remove `auto*` properties. * Update PassNode.js * PassNode: Improve wording. * PassNode: Honor resolution scale factor for viewport and scissor. * PassNode: Rephrase comment.
1 parent 7486544 commit 906211f

File tree

1 file changed

+121
-1
lines changed

1 file changed

+121
-1
lines changed

src/nodes/display/PassNode.js

Lines changed: 121 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import { viewZToOrthographicDepth, perspectiveDepthToViewZ } from './ViewportDep
77

88
import { HalfFloatType/*, FloatType*/ } from '../../constants.js';
99
import { Vector2 } from '../../math/Vector2.js';
10+
import { Vector4 } from '../../math/Vector4.js';
1011
import { DepthTexture } from '../../textures/DepthTexture.js';
1112
import { RenderTarget } from '../../core/RenderTarget.js';
1213

@@ -323,10 +324,43 @@ class PassNode extends TempNode {
323324
*/
324325
this._mrt = null;
325326

327+
/**
328+
* Layer object for configuring the camera that is used
329+
* to produce the pass.
330+
*
331+
* @private
332+
* @type {?Layers}
333+
* @default null
334+
*/
326335
this._layers = null;
327336

337+
/**
338+
* Scales the resolution of the internal render taregt.
339+
*
340+
* @private
341+
* @type {number}
342+
* @default 1
343+
*/
328344
this._resolution = 1;
329345

346+
/**
347+
* Custom viewport definition.
348+
*
349+
* @private
350+
* @type {?Vector4}
351+
* @default null
352+
*/
353+
this._customViewport = null;
354+
355+
/**
356+
* Custom scissor definition.
357+
*
358+
* @private
359+
* @type {?Vector4}
360+
* @default null
361+
*/
362+
this._customScissor = null;
363+
330364
/**
331365
* This flag can be used for type testing.
332366
*
@@ -374,14 +408,19 @@ class PassNode extends TempNode {
374408
* Gets the current resolution of the pass.
375409
*
376410
* @return {number} The current resolution. A value of `1` means full resolution.
377-
* @default 1
378411
*/
379412
getResolution() {
380413

381414
return this._resolution;
382415

383416
}
384417

418+
/**
419+
* Sets the layer configuration that should be used when rendering the pass.
420+
*
421+
* @param {Layers} layers - The layers object to set.
422+
* @return {PassNode} A reference to this pass.
423+
*/
385424
setLayers( layers ) {
386425

387426
this._layers = layers;
@@ -390,6 +429,11 @@ class PassNode extends TempNode {
390429

391430
}
392431

432+
/**
433+
* Gets the current layer configuration of the pass.
434+
*
435+
* @return {?Layers} .
436+
*/
393437
getLayers() {
394438

395439
return this._layers;
@@ -680,6 +724,82 @@ class PassNode extends TempNode {
680724

681725
this.renderTarget.setSize( effectiveWidth, effectiveHeight );
682726

727+
if ( this._customScissor !== null ) this.renderTarget.scissor.copy( this._customScissor );
728+
if ( this._customViewport !== null ) this.renderTarget.viewport.copy( this._customViewport );
729+
730+
}
731+
732+
/**
733+
* This method allows to define the pass's scissor rectangle. By default, the scissor rectangle is kept
734+
* in sync with the pass's dimensions. To reverse the process and use auto-sizing again, call the method
735+
* with `null` as the single argument.
736+
*
737+
* @param {?(number | Vector4)} x - The horizontal coordinate for the lower left corner of the box in logical pixel unit.
738+
* Instead of passing four arguments, the method also works with a single four-dimensional vector.
739+
* @param {number} y - The vertical coordinate for the lower left corner of the box in logical pixel unit.
740+
* @param {number} width - The width of the scissor box in logical pixel unit.
741+
* @param {number} height - The height of the scissor box in logical pixel unit.
742+
*/
743+
setScissor( x, y, width, height ) {
744+
745+
if ( x === null ) {
746+
747+
this._customScissor = null;
748+
749+
} else {
750+
751+
if ( this._customScissor === null ) this._customScissor = new Vector4();
752+
753+
if ( x.isVector4 ) {
754+
755+
this._customScissor.copy( x );
756+
757+
} else {
758+
759+
this._customScissor.set( x, y, width, height );
760+
761+
}
762+
763+
this._customScissor.multiplyScalar( this._pixelRatio * this._resolution ).floor();
764+
765+
}
766+
767+
}
768+
769+
/**
770+
* This method allows to define the pass's viewport. By default, the viewport is kept in sync
771+
* with the pass's dimensions. To reverse the process and use auto-sizing again, call the method
772+
* with `null` as the single argument.
773+
*
774+
* @param {number | Vector4} x - The horizontal coordinate for the lower left corner of the viewport origin in logical pixel unit.
775+
* @param {number} y - The vertical coordinate for the lower left corner of the viewport origin in logical pixel unit.
776+
* @param {number} width - The width of the viewport in logical pixel unit.
777+
* @param {number} height - The height of the viewport in logical pixel unit.
778+
*/
779+
setViewport( x, y, width, height ) {
780+
781+
if ( x === null ) {
782+
783+
this._customViewport = null;
784+
785+
} else {
786+
787+
if ( this._customViewport === null ) this._customViewport = new Vector4();
788+
789+
if ( x.isVector4 ) {
790+
791+
this._customViewport.copy( x );
792+
793+
} else {
794+
795+
this._customViewport.set( x, y, width, height );
796+
797+
}
798+
799+
this._customViewport.multiplyScalar( this._pixelRatio * this._resolution ).floor();
800+
801+
}
802+
683803
}
684804

685805
/**

0 commit comments

Comments
 (0)