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

RTT texture flip flag #3346

Merged
merged 5 commits into from
Jul 20, 2021
Merged
Show file tree
Hide file tree
Changes from 4 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
6 changes: 4 additions & 2 deletions examples/src/examples/graphics/render-to-texture.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,9 @@ class RenderToTextureExample extends Example {
});
const renderTarget = new pc.RenderTarget({
colorBuffer: texture,
depth: true
depth: true,
// @ts-ignore
flipY: true
});

// create a layer for object that do not render into texture
Expand Down Expand Up @@ -130,7 +132,7 @@ class RenderToTextureExample extends Example {
// create a plane called tv which we use to display rendered texture
// this is only added to excluded Layer, so it does not render into texture
const tv = createPrimitive("plane", new pc.Vec3(6, 8, -5), new pc.Vec3(20, 10, 10), pc.Color.BLACK, [excludedLayer.id]);
tv.setLocalEulerAngles(90, 0, 180);
tv.setLocalEulerAngles(90, 0, 0);
tv.render.castShadows = false;
tv.render.receiveShadows = false;
// @ts-ignore engine-tsd
Expand Down
5 changes: 5 additions & 0 deletions src/graphics/render-target.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ const defaultOptions = {
* * {@link CUBEFACE_POSZ}
* * {@link CUBEFACE_NEGZ}
*
* @param {boolean} [options.flipY] - When set to true the image will be flipped in Y. Default is false.
*
* Defaults to {@link CUBEFACE_POSX}.
* @example
* // Create a 512x512x24-bit render target with a depth buffer
Expand Down Expand Up @@ -126,6 +128,9 @@ class RenderTarget {
if (!this.name) {
this.name = "Untitled";
}

// render image flipped in Y
this.flipY = !!options.flipY;
}

/**
Expand Down
25 changes: 21 additions & 4 deletions src/scene/renderer/forward-renderer.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,10 @@ var viewMat3 = new Mat3();
var viewProjMat = new Mat4();
var projMat;

var flipYMat = new Mat4().setScale(1, -1, 1);
var flippedViewProjMat = new Mat4();
var flippedSkyboxProjMat = new Mat4();

var viewInvL = new Mat4();
var viewInvR = new Mat4();
var viewL = new Mat4();
Expand Down Expand Up @@ -434,7 +438,17 @@ class ForwardRenderer {

// ViewProjection Matrix
viewProjMat.mul2(projMat, viewMat);
this.viewProjId.setValue(viewProjMat.data);

if (target && target.flipY) {
flippedViewProjMat.mul2(flipYMat, viewProjMat);
flippedSkyboxProjMat.mul2(flipYMat, camera.getProjectionMatrixSkybox());

this.viewProjId.setValue(flippedViewProjMat.data);
this.projSkyboxId.setValue(flippedSkyboxProjMat.data);
} else {
this.viewProjId.setValue(viewProjMat.data);
this.projSkyboxId.setValue(camera.getProjectionMatrixSkybox().data);
}

// View Position (world space)
this.dispatchViewPos(camera._node.getPosition());
Expand Down Expand Up @@ -1235,7 +1249,7 @@ class ForwardRenderer {
this.viewPosId.setValue(vp);
}

renderForward(camera, drawCalls, drawCallsCount, sortedLights, pass, cullingMask, drawCallback, layer) {
renderForward(camera, drawCalls, drawCallsCount, sortedLights, pass, cullingMask, drawCallback, layer, rtFlipY) {
var device = this.device;
var scene = this.scene;
var vrDisplay = camera.vrDisplay;
Expand Down Expand Up @@ -1363,7 +1377,9 @@ class ForwardRenderer {
}
}

this.setCullMode(camera._cullFaces, camera._flipFaces, drawCall);
// enable cull inversion if either the camera has flipFaces enabled or the render target
// has flipY enabled (but disable inversion if both are true or false).
this.setCullMode(camera._cullFaces, !!(camera._flipFaces ^ rtFlipY), drawCall);

stencilFront = drawCall.stencilFront || material.stencilFront;
stencilBack = drawCall.stencilBack || material.stencilBack;
Expand Down Expand Up @@ -2291,7 +2307,8 @@ class ForwardRenderer {
layer.shaderPass,
layer.cullingMask,
layer.onDrawCall,
layer);
layer,
!!(renderAction?.renderTarget?.flipY));
layer._forwardDrawCalls += this._forwardDrawCalls - draws;

// Revert temp frame stuff
Expand Down