Skip to content

Commit d448b18

Browse files
committed
Implement viewport/scissor
Addresses #570
1 parent a88158f commit d448b18

11 files changed

Lines changed: 299 additions & 15 deletions

‎src/core/Pass.ts‎

Lines changed: 218 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,8 @@ import { BaseEventMap } from "./BaseEventMap.js";
2828
import { Disposable } from "./Disposable.js";
2929
import { Identifiable } from "./Identifiable.js";
3030
import { Renderable } from "./Renderable.js";
31+
import { Viewport } from "../utils/Viewport.js";
32+
import { Scissor } from "../utils/Scissor.js";
3133

3234
/**
3335
* Pass events.
@@ -192,6 +194,22 @@ export abstract class Pass<TMaterial extends Material | null = null>
192194

193195
readonly resolution: Resolution;
194196

197+
/**
198+
* The viewport.
199+
*
200+
* @see {@link Viewport.enabled} to enable the viewport.
201+
*/
202+
203+
readonly viewport: Viewport;
204+
205+
/**
206+
* A rectangular area inside the viewport. Fragments that are outside the area will not be rendered.
207+
*
208+
* @see {@link Scissor.enabled} to enable the scissor.
209+
*/
210+
211+
readonly scissor: Scissor;
212+
195213
/**
196214
* The input resources of this pass.
197215
*/
@@ -234,7 +252,11 @@ export abstract class Pass<TMaterial extends Material | null = null>
234252
this.disposables = new Set<Disposable>();
235253

236254
this.resolution = new Resolution();
255+
this.viewport = new Viewport();
256+
this.scissor = new Scissor();
237257
this.resolution.addEventListener(Resolution.EVENT_CHANGE, (e) => this.handleResolutionEvent(e));
258+
this.viewport.addEventListener(Viewport.EVENT_CHANGE, (e) => this.handleViewportEvent(e));
259+
this.scissor.addEventListener(Scissor.EVENT_CHANGE, (e) => this.handleScissorEvent(e));
238260

239261
this.input = new Input();
240262
this.output = new Output();
@@ -516,6 +538,42 @@ export abstract class Pass<TMaterial extends Material | null = null>
516538

517539
}
518540

541+
/**
542+
* Updates the viewport of all subpasses.
543+
*/
544+
545+
private updateSubpassViewport(): void {
546+
547+
const { enabled, offsetX, offsetY, baseWidth, baseHeight } = this.viewport;
548+
549+
for(const pass of this.subpasses) {
550+
551+
pass.viewport.enabled = enabled;
552+
pass.viewport.setOffset(offsetX, offsetY);
553+
pass.viewport.setBaseSize(baseWidth, baseHeight);
554+
555+
}
556+
557+
}
558+
559+
/**
560+
* Updates the scissor of all subpasses.
561+
*/
562+
563+
private updateSubpassScissor(): void {
564+
565+
const { enabled, offsetX, offsetY, baseWidth, baseHeight } = this.scissor;
566+
567+
for(const pass of this.subpasses) {
568+
569+
pass.scissor.enabled = enabled;
570+
pass.scissor.setOffset(offsetX, offsetY);
571+
pass.scissor.setBaseSize(baseWidth, baseHeight);
572+
573+
}
574+
575+
}
576+
519577
/**
520578
* Updates the size of the default output buffer, if it exists.
521579
*/
@@ -687,6 +745,22 @@ export abstract class Pass<TMaterial extends Material | null = null>
687745

688746
protected onResolutionChange(): void {}
689747

748+
/**
749+
* Performs tasks when the {@link viewport} has changed.
750+
*
751+
* Override this empty method to handle viewport changes.
752+
*/
753+
754+
protected onViewportChange(): void {}
755+
756+
/**
757+
* Performs tasks when the {@link scissor} has changed.
758+
*
759+
* Override this empty method to handle scissor changes.
760+
*/
761+
762+
protected onScissorChange(): void {}
763+
690764
/**
691765
* Creates a framebuffer.
692766
*
@@ -710,6 +784,100 @@ export abstract class Pass<TMaterial extends Material | null = null>
710784

711785
}
712786

787+
/**
788+
* Applies the viewport of this pass to the current render target.
789+
*/
790+
791+
protected applyViewport(): void {
792+
793+
if(this.renderer === null) {
794+
795+
return;
796+
797+
}
798+
799+
const renderTarget = this.renderer.getRenderTarget();
800+
const viewport = this.viewport;
801+
802+
if(renderTarget === null) {
803+
804+
if(viewport.enabled) {
805+
806+
this.renderer.setViewport(viewport.x, viewport.y, viewport.z, viewport.w);
807+
808+
} else {
809+
810+
const { baseWidth, baseHeight } = this.resolution;
811+
this.renderer.setViewport(0, 0, baseWidth, baseHeight);
812+
813+
}
814+
815+
} else {
816+
817+
if(viewport.enabled) {
818+
819+
renderTarget.viewport.copy(viewport);
820+
821+
} else {
822+
823+
const { width, height } = renderTarget;
824+
renderTarget.viewport.set(0, 0, width, height);
825+
826+
}
827+
828+
}
829+
830+
}
831+
832+
/**
833+
* Applies the scissor region of this pass to the current render target.
834+
*/
835+
836+
protected applyScissor(): void {
837+
838+
if(this.renderer === null) {
839+
840+
return;
841+
842+
}
843+
844+
const renderTarget = this.renderer.getRenderTarget();
845+
const scissor = this.scissor;
846+
847+
if(renderTarget === null) {
848+
849+
if(scissor.enabled) {
850+
851+
this.renderer.setScissor(scissor.x, scissor.y, scissor.z, scissor.w);
852+
this.renderer.setScissorTest(true);
853+
854+
} else {
855+
856+
const { baseWidth, baseHeight } = this.resolution;
857+
this.renderer.setScissor(0, 0, baseWidth, baseHeight);
858+
this.renderer.setScissorTest(false);
859+
860+
}
861+
862+
} else {
863+
864+
if(scissor.enabled) {
865+
866+
renderTarget.scissor.copy(scissor);
867+
renderTarget.scissorTest = true;
868+
869+
} else {
870+
871+
const { baseWidth, baseHeight } = this.resolution;
872+
renderTarget.scissor.set(0, 0, baseWidth, baseHeight);
873+
renderTarget.scissorTest = false;
874+
875+
}
876+
877+
}
878+
879+
}
880+
713881
/**
714882
* Sets the active render target.
715883
*
@@ -769,6 +937,56 @@ export abstract class Pass<TMaterial extends Material | null = null>
769937

770938
}
771939

940+
/**
941+
* Handles {@link viewport} events.
942+
*
943+
* @param event - A viewport event.
944+
*/
945+
946+
private handleViewportEvent(event: Event): void {
947+
948+
if(!this.attached) {
949+
950+
return;
951+
952+
}
953+
954+
switch(event.type) {
955+
956+
case "change":
957+
this.onViewportChange();
958+
this.updateSubpassViewport();
959+
break;
960+
961+
}
962+
963+
}
964+
965+
/**
966+
* Handles {@link scissor} events.
967+
*
968+
* @param event - A scissor event.
969+
*/
970+
971+
private handleScissorEvent(event: Event): void {
972+
973+
if(!this.attached) {
974+
975+
return;
976+
977+
}
978+
979+
switch(event.type) {
980+
981+
case "change":
982+
this.onScissorChange();
983+
this.updateSubpassScissor();
984+
break;
985+
986+
}
987+
988+
}
989+
772990
/**
773991
* Handles {@link input} events.
774992
*

‎src/core/RenderPipeline.ts‎

Lines changed: 69 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { Material, Vector2, WebGLRenderer } from "three";
1+
import { Material, Vector2, Vector4, WebGLRenderer } from "three";
22
import { Timer } from "three/examples/jsm/misc/Timer.js";
33
import { ShaderChunkExtensions } from "../shader-chunks/ShaderChunkExtensions.js";
44
import { ImmutableTimer } from "../utils/ImmutableTimer.js";
@@ -198,6 +198,8 @@ export class RenderPipeline implements Disposable, Renderable, Resizable {
198198

199199
this.renderer.getDrawingBufferSize(v);
200200
pass.resolution.setBaseSize(v.width, v.height);
201+
pass.viewport.copyBaseSize(this.resolution);
202+
pass.scissor.copyBaseSize(this.resolution);
201203

202204
}
203205

@@ -363,6 +365,8 @@ export class RenderPipeline implements Disposable, Renderable, Resizable {
363365
for(const pass of this.passes) {
364366

365367
pass.resolution.setBaseSize(effectiveSize.width, effectiveSize.height);
368+
pass.viewport.copyBaseSize(resolution);
369+
pass.scissor.copyBaseSize(resolution);
366370

367371
}
368372

@@ -383,6 +387,70 @@ export class RenderPipeline implements Disposable, Renderable, Resizable {
383387

384388
}
385389

390+
/**
391+
* Sets the viewport for all passes.
392+
*
393+
* Please note that viewport settings need to be enabled on a per-pass basis to take effect.
394+
*
395+
* @param x - The X-offset starting in the lower left corner, or a vector that describes the viewport.
396+
* @param y - The Y-offset starting in the lower left corner.
397+
* @param width - The width in logical pixels (before pixel ratio).
398+
* @param height - The height in logical pixels (before pixel ratio).
399+
*/
400+
401+
setViewport(x: number | Vector4, y = 0, width = 0, height = 0): void {
402+
403+
if(x instanceof Vector4) {
404+
405+
const v = x;
406+
x = v.x;
407+
y = v.y;
408+
width = v.z;
409+
height = v.w;
410+
411+
}
412+
413+
for(const pass of this.passes) {
414+
415+
pass.viewport.setOffset(x, y);
416+
pass.viewport.setBaseSize(width, height);
417+
418+
}
419+
420+
}
421+
422+
/**
423+
* Sets the scissor region for all passes.
424+
*
425+
* Please note that scissor settings need to be enabled on a per-pass basis to take effect.
426+
*
427+
* @param x - The X-offset, or a vector that describes the scissor region.
428+
* @param y - The Y-offset starting in the lower left corner.
429+
* @param width - The width in logical pixels (before pixel ratio).
430+
* @param height - The height in logical pixels (before pixel ratio).
431+
*/
432+
433+
setScissor(x: number | Vector4, y = 0, width = 0, height = 0): void {
434+
435+
if(x instanceof Vector4) {
436+
437+
const v = x;
438+
x = v.x;
439+
y = v.y;
440+
width = v.z;
441+
height = v.w;
442+
443+
}
444+
445+
for(const pass of this.passes) {
446+
447+
pass.scissor.setOffset(x, y);
448+
pass.scissor.setBaseSize(width, height);
449+
450+
}
451+
452+
}
453+
386454
dispose(): void {
387455

388456
RenderPipeline.ioManager.removePipeline(this);

‎src/passes/BufferDebugPass.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -260,7 +260,7 @@ export class BufferDebugPass extends Pass<BufferDebugMaterial> {
260260

261261
override render(): void {
262262

263-
this.renderer?.setRenderTarget(this.output.defaultBuffer?.value ?? null);
263+
this.setRenderTarget(this.output.defaultBuffer?.value ?? null);
264264
this.renderFullscreen();
265265
this.renderer?.render(this.debugScene, this.debugCamera);
266266

‎src/passes/ClearPass.ts‎

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -147,7 +147,6 @@ export class ClearPass extends Pass {
147147

148148
}
149149

150-
renderer.setRenderTarget(this.output.defaultBuffer?.value ?? null);
151150
renderer.clear(flags.color, flags.depth, flags.stencil);
152151

153152
if(hasOverrideClearColor) {
@@ -175,8 +174,6 @@ export class ClearPass extends Pass {
175174
const renderer = this.renderer!;
176175
const flags = this.clearFlags;
177176

178-
renderer.setRenderTarget(this.output.defaultBuffer?.value ?? null);
179-
180177
if(scene.background instanceof Color) {
181178

182179
this.clear(scene.background);
@@ -214,6 +211,8 @@ export class ClearPass extends Pass {
214211
const background = this.scene?.background ?? null;
215212
const hasOverrideClearColor = this.clearValues.color !== null;
216213

214+
this.setRenderTarget(this.output.defaultBuffer?.value ?? null);
215+
217216
if(!hasOverrideClearColor && this.camera !== null && background !== null) {
218217

219218
this.clearWithBackground();

0 commit comments

Comments
 (0)