Skip to content

Commit c944d38

Browse files
authored
RTT texture flip flag (playcanvas#3346)
* add flipY option to render target * slightly cleaner
1 parent b4fac34 commit c944d38

File tree

3 files changed

+32
-6
lines changed

3 files changed

+32
-6
lines changed

examples/src/examples/graphics/render-to-texture.tsx

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,9 @@ class RenderToTextureExample extends Example {
6969
});
7070
const renderTarget = new pc.RenderTarget({
7171
colorBuffer: texture,
72-
depth: true
72+
depth: true,
73+
// @ts-ignore
74+
flipY: true
7375
});
7476

7577
// create a layer for object that do not render into texture
@@ -130,7 +132,7 @@ class RenderToTextureExample extends Example {
130132
// create a plane called tv which we use to display rendered texture
131133
// this is only added to excluded Layer, so it does not render into texture
132134
const tv = createPrimitive("plane", new pc.Vec3(6, 8, -5), new pc.Vec3(20, 10, 10), pc.Color.BLACK, [excludedLayer.id]);
133-
tv.setLocalEulerAngles(90, 0, 180);
135+
tv.setLocalEulerAngles(90, 0, 0);
134136
tv.render.castShadows = false;
135137
tv.render.receiveShadows = false;
136138
// @ts-ignore engine-tsd

src/graphics/render-target.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,8 @@ const defaultOptions = {
3232
* * {@link CUBEFACE_POSZ}
3333
* * {@link CUBEFACE_NEGZ}
3434
*
35+
* @param {boolean} [options.flipY] - When set to true the image will be flipped in Y. Default is false.
36+
*
3537
* Defaults to {@link CUBEFACE_POSX}.
3638
* @example
3739
* // Create a 512x512x24-bit render target with a depth buffer
@@ -126,6 +128,9 @@ class RenderTarget {
126128
if (!this.name) {
127129
this.name = "Untitled";
128130
}
131+
132+
// render image flipped in Y
133+
this.flipY = !!options.flipY;
129134
}
130135

131136
/**

src/scene/renderer/forward-renderer.js

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,10 @@ var viewMat3 = new Mat3();
4848
var viewProjMat = new Mat4();
4949
var projMat;
5050

51+
var flipYMat = new Mat4().setScale(1, -1, 1);
52+
var flippedViewProjMat = new Mat4();
53+
var flippedSkyboxProjMat = new Mat4();
54+
5155
var viewInvL = new Mat4();
5256
var viewInvR = new Mat4();
5357
var viewL = new Mat4();
@@ -434,7 +438,17 @@ class ForwardRenderer {
434438

435439
// ViewProjection Matrix
436440
viewProjMat.mul2(projMat, viewMat);
437-
this.viewProjId.setValue(viewProjMat.data);
441+
442+
if (target && target.flipY) {
443+
flippedViewProjMat.mul2(flipYMat, viewProjMat);
444+
flippedSkyboxProjMat.mul2(flipYMat, camera.getProjectionMatrixSkybox());
445+
446+
this.viewProjId.setValue(flippedViewProjMat.data);
447+
this.projSkyboxId.setValue(flippedSkyboxProjMat.data);
448+
} else {
449+
this.viewProjId.setValue(viewProjMat.data);
450+
this.projSkyboxId.setValue(camera.getProjectionMatrixSkybox().data);
451+
}
438452

439453
// View Position (world space)
440454
this.dispatchViewPos(camera._node.getPosition());
@@ -1235,7 +1249,7 @@ class ForwardRenderer {
12351249
this.viewPosId.setValue(vp);
12361250
}
12371251

1238-
renderForward(camera, drawCalls, drawCallsCount, sortedLights, pass, cullingMask, drawCallback, layer) {
1252+
renderForward(camera, drawCalls, drawCallsCount, sortedLights, pass, cullingMask, drawCallback, layer, flipFaces) {
12391253
var device = this.device;
12401254
var scene = this.scene;
12411255
var vrDisplay = camera.vrDisplay;
@@ -1363,7 +1377,7 @@ class ForwardRenderer {
13631377
}
13641378
}
13651379

1366-
this.setCullMode(camera._cullFaces, camera._flipFaces, drawCall);
1380+
this.setCullMode(camera._cullFaces, flipFaces, drawCall);
13671381

13681382
stencilFront = drawCall.stencilFront || material.stencilFront;
13691383
stencilBack = drawCall.stencilBack || material.stencilBack;
@@ -2283,6 +2297,10 @@ class ForwardRenderer {
22832297
renderAction.lightClusters.activate();
22842298
}
22852299

2300+
// enable flip faces if either the camera has _flipFaces enabled or the render target
2301+
// has flipY enabled
2302+
const flipFaces = !!(camera._flipFaces ^ renderAction?.renderTarget?.flipY);
2303+
22862304
const draws = this._forwardDrawCalls;
22872305
this.renderForward(camera.camera,
22882306
visible.list,
@@ -2291,7 +2309,8 @@ class ForwardRenderer {
22912309
layer.shaderPass,
22922310
layer.cullingMask,
22932311
layer.onDrawCall,
2294-
layer);
2312+
layer,
2313+
flipFaces);
22952314
layer._forwardDrawCalls += this._forwardDrawCalls - draws;
22962315

22972316
// Revert temp frame stuff

0 commit comments

Comments
 (0)