Skip to content

Commit

Permalink
use EventHandle for scene and layers events for improved performance
Browse files Browse the repository at this point in the history
  • Loading branch information
Maksims committed Nov 25, 2024
1 parent 9f6df89 commit b002a00
Show file tree
Hide file tree
Showing 8 changed files with 310 additions and 68 deletions.
49 changes: 37 additions & 12 deletions src/framework/components/camera/component.js
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,24 @@ class CameraComponent extends Component {
/** @private */
_camera = new Camera();

/**
* @type {import('../../../core/event-handle.js').EventHandle|null}
* @private
*/
_evtLayersChanged = null;

/**
* @type {import('../../../core/event-handle.js').EventHandle|null}
* @private
*/
_evtLayerAdded = null;

/**
* @type {import('../../../core/event-handle.js').EventHandle|null}
* @private
*/
_evtLayerRemoved = null;

/**
* Create a new CameraComponent instance.
*
Expand Down Expand Up @@ -1020,16 +1038,20 @@ class CameraComponent extends Component {
}

onEnable() {
const system = this.system;
const scene = system.app.scene;
const scene = this.system.app.scene;
const layers = scene.layers;

system.addCamera(this);
this.system.addCamera(this);

this._evtLayersChanged?.off();
this._evtLayersChanged = scene.on('set:layers', this.onLayersChanged, this);

scene.on('set:layers', this.onLayersChanged, this);
if (layers) {
layers.on('add', this.onLayerAdded, this);
layers.on('remove', this.onLayerRemoved, this);
this._evtLayerAdded?.off();
this._evtLayerAdded = layers.on('add', this.onLayerAdded, this);

this._evtLayerRemoved?.off();
this._evtLayerRemoved = layers.on('remove', this.onLayerRemoved, this);
}

if (this.enabled && this.entity.enabled) {
Expand All @@ -1040,21 +1062,24 @@ class CameraComponent extends Component {
}

onDisable() {
const system = this.system;
const scene = system.app.scene;
const scene = this.system.app.scene;
const layers = scene.layers;

this.postEffects.disable();

this.removeCameraFromLayers();

scene.off('set:layers', this.onLayersChanged, this);
this._evtLayersChanged?.off();
this._evtLayersChanged = null;

if (layers) {
layers.off('add', this.onLayerAdded, this);
layers.off('remove', this.onLayerRemoved, this);
this._evtLayerAdded?.off();
this._evtLayerAdded = null;
this._evtLayerRemoved?.off();
this._evtLayerRemoved = null;
}

system.removeCamera(this);
this.system.removeCamera(this);
}

onRemove() {
Expand Down
49 changes: 41 additions & 8 deletions src/framework/components/element/component.js
Original file line number Diff line number Diff line change
Expand Up @@ -212,6 +212,24 @@ class ElementComponent extends Component {
*/
static EVENT_TOUCHCANCEL = 'touchcancel';

/**
* @type {import('../../../core/event-handle.js').EventHandle|null}
* @private
*/
_evtLayersChanged = null;

/**
* @type {import('../../../core/event-handle.js').EventHandle|null}
* @private
*/
_evtLayerAdded = null;

/**
* @type {import('../../../core/event-handle.js').EventHandle|null}
* @private
*/
_evtLayerRemoved = null;

/**
* Create a new ElementComponent instance.
*
Expand Down Expand Up @@ -2525,6 +2543,9 @@ class ElementComponent extends Component {
}

onEnable() {
const scene = this.system.app.scene;
const layers = scene.layers;

if (this._image) {
this._image.onEnable();
}
Expand All @@ -2539,10 +2560,15 @@ class ElementComponent extends Component {
this.system.app.elementInput.addElement(this);
}

this.system.app.scene.on('set:layers', this.onLayersChanged, this);
if (this.system.app.scene.layers) {
this.system.app.scene.layers.on('add', this.onLayerAdded, this);
this.system.app.scene.layers.on('remove', this.onLayerRemoved, this);
this._evtLayersChanged?.off();
this._evtLayersChanged = scene.on('set:layers', this.onLayersChanged, this);

if (layers) {
this._evtLayerAdded?.off();
this._evtLayerAdded = layers.on('add', this.onLayerAdded, this);

this._evtLayerRemoved?.off();
this._evtLayerRemoved = layers.on('remove', this.onLayerRemoved, this);
}

if (this._batchGroupId >= 0) {
Expand All @@ -2553,10 +2579,17 @@ class ElementComponent extends Component {
}

onDisable() {
this.system.app.scene.off('set:layers', this.onLayersChanged, this);
if (this.system.app.scene.layers) {
this.system.app.scene.layers.off('add', this.onLayerAdded, this);
this.system.app.scene.layers.off('remove', this.onLayerRemoved, this);
const scene = this.system.app.scene;
const layers = scene.layers;

this._evtLayersChanged?.off();
this._evtLayersChanged = null;

if (layers) {
this._evtLayerAdded?.off();
this._evtLayerAdded = null;
this._evtLayerRemoved?.off();
this._evtLayerRemoved = null;
}

if (this._image) this._image.onDisable();
Expand Down
47 changes: 39 additions & 8 deletions src/framework/components/gsplat/component.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,24 @@ class GSplatComponent extends Component {
*/
_materialOptions = null;

/**
* @type {import('../../../core/event-handle.js').EventHandle|null}
* @private
*/
_evtLayersChanged = null;

/**
* @type {import('../../../core/event-handle.js').EventHandle|null}
* @private
*/
_evtLayerAdded = null;

/**
* @type {import('../../../core/event-handle.js').EventHandle|null}
* @private
*/
_evtLayerRemoved = null;

/**
* Create a new GSplatComponent.
*
Expand Down Expand Up @@ -317,10 +335,17 @@ class GSplatComponent extends Component {

onEnable() {
const scene = this.system.app.scene;
scene.on('set:layers', this.onLayersChanged, this);
if (scene.layers) {
scene.layers.on('add', this.onLayerAdded, this);
scene.layers.on('remove', this.onLayerRemoved, this);
const layers = scene.layers;

this._evtLayersChanged?.off();
this._evtLayersChanged = scene.on('set:layers', this.onLayersChanged, this);

if (layers) {
this._evtLayerAdded?.off();
this._evtLayerAdded = layers.on('add', this.onLayerAdded, this);

this._evtLayerRemoved?.off();
this._evtLayerRemoved = layers.on('remove', this.onLayerRemoved, this);
}

if (this._instance) {
Expand All @@ -332,10 +357,16 @@ class GSplatComponent extends Component {

onDisable() {
const scene = this.system.app.scene;
scene.off('set:layers', this.onLayersChanged, this);
if (scene.layers) {
scene.layers.off('add', this.onLayerAdded, this);
scene.layers.off('remove', this.onLayerRemoved, this);
const layers = scene.layers;

this._evtLayersChanged?.off();
this._evtLayersChanged = null;

if (layers) {
this._evtLayerAdded?.off();
this._evtLayerAdded = null;
this._evtLayerRemoved?.off();
this._evtLayerRemoved = null;
}

this.removeFromLayers();
Expand Down
49 changes: 41 additions & 8 deletions src/framework/components/light/component.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,24 @@ import { properties } from './data.js';
* @category Graphics
*/
class LightComponent extends Component {
/**
* @type {import('../../../core/event-handle.js').EventHandle|null}
* @private
*/
_evtLayersChanged = null;

/**
* @type {import('../../../core/event-handle.js').EventHandle|null}
* @private
*/
_evtLayerAdded = null;

/**
* @type {import('../../../core/event-handle.js').EventHandle|null}
* @private
*/
_evtLayerRemoved = null;

/**
* Creates a new LightComponent instance.
*
Expand Down Expand Up @@ -1224,12 +1242,20 @@ class LightComponent extends Component {
}

onEnable() {
const scene = this.system.app.scene;
const layers = scene.layers;

this.light.enabled = true;

this.system.app.scene.on('set:layers', this.onLayersChanged, this);
if (this.system.app.scene.layers) {
this.system.app.scene.layers.on('add', this.onLayerAdded, this);
this.system.app.scene.layers.on('remove', this.onLayerRemoved, this);
this._evtLayersChanged?.off();
this._evtLayersChanged = scene.on('set:layers', this.onLayersChanged, this);

if (layers) {
this._evtLayerAdded?.off();
this._evtLayerAdded = layers.on('add', this.onLayerAdded, this);

this._evtLayerRemoved?.off();
this._evtLayerRemoved = layers.on('remove', this.onLayerRemoved, this);
}

if (this.enabled && this.entity.enabled) {
Expand All @@ -1242,12 +1268,19 @@ class LightComponent extends Component {
}

onDisable() {
const scene = this.system.app.scene;
const layers = scene.layers;

this.light.enabled = false;

this.system.app.scene.off('set:layers', this.onLayersChanged, this);
if (this.system.app.scene.layers) {
this.system.app.scene.layers.off('add', this.onLayerAdded, this);
this.system.app.scene.layers.off('remove', this.onLayerRemoved, this);
this._evtLayersChanged?.off();
this._evtLayersChanged = null;

if (layers) {
this._evtLayerAdded?.off();
this._evtLayerAdded = null;
this._evtLayerRemoved?.off();
this._evtLayerRemoved = null;
}

this.removeLightFromLayers();
Expand Down
45 changes: 37 additions & 8 deletions src/framework/components/model/component.js
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,24 @@ class ModelComponent extends Component {
_batchGroup = null;
// #endif

/**
* @type {import('../../../core/event-handle.js').EventHandle|null}
* @private
*/
_evtLayersChanged = null;

/**
* @type {import('../../../core/event-handle.js').EventHandle|null}
* @private
*/
_evtLayerAdded = null;

/**
* @type {import('../../../core/event-handle.js').EventHandle|null}
* @private
*/
_evtLayerRemoved = null;

/**
* Create a new ModelComponent instance.
*
Expand Down Expand Up @@ -933,11 +951,17 @@ class ModelComponent extends Component {
onEnable() {
const app = this.system.app;
const scene = app.scene;
const layers = scene?.layers;

scene.on('set:layers', this.onLayersChanged, this);
if (scene.layers) {
scene.layers.on('add', this.onLayerAdded, this);
scene.layers.on('remove', this.onLayerRemoved, this);
this._evtLayersChanged?.off();
this._evtLayersChanged = scene.on('set:layers', this.onLayersChanged, this);

if (layers) {
this._evtLayerAdded?.off();
this._evtLayerAdded = layers.on('add', this.onLayerAdded, this);

this._evtLayerRemoved?.off();
this._evtLayerRemoved = layers.on('remove', this.onLayerRemoved, this);
}

const isAsset = (this._type === 'asset');
Expand Down Expand Up @@ -984,11 +1008,16 @@ class ModelComponent extends Component {
onDisable() {
const app = this.system.app;
const scene = app.scene;
const layers = scene.layers;

this._evtLayersChanged?.off();
this._evtLayersChanged = null;

scene.off('set:layers', this.onLayersChanged, this);
if (scene.layers) {
scene.layers.off('add', this.onLayerAdded, this);
scene.layers.off('remove', this.onLayerRemoved, this);
if (layers) {
this._evtLayerAdded?.off();
this._evtLayerAdded = null;
this._evtLayerRemoved?.off();
this._evtLayerRemoved = null;
}

if (this._batchGroupId >= 0) {
Expand Down
Loading

0 comments on commit b002a00

Please sign in to comment.