Skip to content

Commit

Permalink
Fix some issues with CameraFrame.enabled not working correctly (#7311)
Browse files Browse the repository at this point in the history
Co-authored-by: Martin Valigursky <[email protected]>
  • Loading branch information
mvaligursky and Martin Valigursky authored Jan 24, 2025
1 parent fb1ecb2 commit ef1d2e7
Show file tree
Hide file tree
Showing 5 changed files with 62 additions and 28 deletions.
9 changes: 9 additions & 0 deletions examples/src/examples/graphics/ambient-occlusion.controls.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,15 @@ import * as pc from 'playcanvas';
export const controls = ({ observer, ReactPCUI, React, jsx, fragment }) => {
const { BindingTwoWay, BooleanInput, LabelGroup, Panel, SelectInput, SliderInput } = ReactPCUI;
return fragment(
jsx(
LabelGroup,
{ text: 'enabled' },
jsx(BooleanInput, {
type: 'toggle',
binding: new BindingTwoWay(),
link: { observer, path: 'data.enabled' }
})
),
jsx(
Panel,
{ headerText: 'Ambient Occlusion' },
Expand Down
7 changes: 6 additions & 1 deletion examples/src/examples/graphics/ambient-occlusion.example.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,8 @@ assetListLoader.load(() => {
cameraEntity.addComponent('camera', {
clearColor: new pc.Color(0.4, 0.45, 0.5),
nearClip: 1,
farClip: 600
farClip: 600,
toneMapping: pc.TONEMAP_NEUTRAL
});

// add orbit camera script
Expand Down Expand Up @@ -175,6 +176,9 @@ assetListLoader.load(() => {

const applySettings = () => {

// enabled
cameraFrame.enabled = data.get('data.enabled');

cameraFrame.ssao.type = data.get('data.ssao.type');
cameraFrame.ssao.blurEnabled = data.get('data.ssao.blurEnabled');
cameraFrame.ssao.intensity = data.get('data.ssao.intensity');
Expand Down Expand Up @@ -215,6 +219,7 @@ assetListLoader.load(() => {

// initial settings
data.set('data', {
enabled: true,
ssao: {
type: pc.SSAOTYPE_LIGHTING,
blurEnabled: true,
Expand Down
9 changes: 9 additions & 0 deletions examples/src/examples/graphics/post-processing.controls.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,15 @@ import * as pc from 'playcanvas';
export const controls = ({ observer, ReactPCUI, React, jsx, fragment }) => {
const { BindingTwoWay, BooleanInput, LabelGroup, Panel, SelectInput, SliderInput } = ReactPCUI;
return fragment(
jsx(
LabelGroup,
{ text: 'enabled' },
jsx(BooleanInput, {
type: 'toggle',
binding: new BindingTwoWay(),
link: { observer, path: 'data.enabled' }
})
),
jsx(
Panel,
{ headerText: 'Scene Rendering' },
Expand Down
4 changes: 4 additions & 0 deletions examples/src/examples/graphics/post-processing.example.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -237,6 +237,9 @@ assetListLoader.load(() => {
material.update();
});

// enabled
cameraFrame.enabled = data.get('data.enabled');

// Scene
cameraFrame.rendering.renderTargetScale = data.get('data.scene.scale');
cameraFrame.rendering.toneMapping = data.get('data.scene.tonemapping');
Expand Down Expand Up @@ -283,6 +286,7 @@ assetListLoader.load(() => {

// set initial values
data.set('data', {
enabled: true,
scene: {
scale: 1.8,
background: 6,
Expand Down
61 changes: 34 additions & 27 deletions src/extras/render-passes/camera-frame.js
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,9 @@ import { CameraFrameOptions, RenderPassCameraFrame } from './render-pass-camera-
* @category Render Pass
*/
class CameraFrame {
/** @private */
_enabled = true;

/**
* Rendering settings.
*
Expand Down Expand Up @@ -303,7 +306,7 @@ class CameraFrame {
Debug.assert(cameraComponent, 'CameraFrame: cameraComponent must be defined');

this.updateOptions();
this.enabled = true;
this.enable();
}

/**
Expand All @@ -314,40 +317,44 @@ class CameraFrame {
}

enable() {
if (!this.renderPassCamera) {
const cameraComponent = this.cameraComponent;
this.renderPassCamera = new RenderPassCameraFrame(this.app, cameraComponent, this.options);
cameraComponent.renderPasses = [this.renderPassCamera];
}
Debug.assert(!this.renderPassCamera);

const cameraComponent = this.cameraComponent;
this.renderPassCamera = new RenderPassCameraFrame(this.app, cameraComponent, this.options);
cameraComponent.renderPasses = [this.renderPassCamera];
}

disable() {
if (this.renderPassCamera) {
const cameraComponent = this.cameraComponent;
cameraComponent.renderPasses?.forEach((renderPass) => {
renderPass.destroy();
});
cameraComponent.renderPasses = [];
cameraComponent.rendering = null;

cameraComponent.jitter = 0;

// no longer HDR rendering
cameraComponent.gammaCorrection = GAMMA_SRGB;
}
Debug.assert(this.renderPassCamera);

const cameraComponent = this.cameraComponent;
cameraComponent.renderPasses?.forEach((renderPass) => {
renderPass.destroy();
});
cameraComponent.renderPasses = [];
cameraComponent.rendering = null;

cameraComponent.jitter = 0;

// no longer HDR rendering
cameraComponent.gammaCorrection = GAMMA_SRGB;

this.renderPassCamera = null;
}

/**
* Sets the enabled state of the camera frame. This disabled the render passes, and releases
* any resources.
* Sets the enabled state of the camera frame. Passing false will release associated resources.
*
* @type {boolean}
*/
set enabled(value) {
if (value) {
this.enable();
} else {
this.disable();
if (this._enabled !== value) {
if (value) {
this.enable();
} else {
this.disable();
}
this._enabled = value;
}
}

Expand All @@ -357,7 +364,7 @@ class CameraFrame {
* @type {boolean}
*/
get enabled() {
return this.renderPassCamera !== null;
return this._enabled;
}

updateOptions() {
Expand All @@ -382,7 +389,7 @@ class CameraFrame {
*/
update() {

if (!this.enabled) return;
if (!this._enabled) return;

const cameraComponent = this.cameraComponent;
const { options, renderPassCamera, rendering, bloom, grading, vignette, fringing, taa, ssao } = this;
Expand Down

0 comments on commit ef1d2e7

Please sign in to comment.