Skip to content

Commit

Permalink
refactor(View): move View properties to private fields.
Browse files Browse the repository at this point in the history
  • Loading branch information
gchoqueux committed Mar 25, 2022
1 parent c317a8a commit 51f5508
Showing 1 changed file with 14 additions and 13 deletions.
27 changes: 14 additions & 13 deletions src/Core/View.js
Expand Up @@ -132,6 +132,9 @@ const viewers = [];
let screenMeters;

class View extends THREE.EventDispatcher {
#layers = [];
#pixelDepthBuffer = new Uint8Array(4);
#fullSizeDepthBuffer;
/**
* Constructs an Itowns View instance
*
Expand Down Expand Up @@ -201,7 +204,6 @@ class View extends THREE.EventDispatcher {
options.camera);

this._frameRequesters = { };
this._layers = [];

window.addEventListener('resize', () => this.resize(), false);

Expand Down Expand Up @@ -233,8 +235,7 @@ class View extends THREE.EventDispatcher {

this.addEventListener(VIEW_EVENTS.LAYERS_INITIALIZED, fn);

this._fullSizeDepthBuffer = new Uint8Array(4 * this.camera.width * this.camera.height);
this._pixelDepthBuffer = new Uint8Array(4);
this.#fullSizeDepthBuffer = new Uint8Array(4 * this.camera.width * this.camera.height);

// Indicates that view's domElement can be focused (the negative value indicates that domElement can't be
// focused sequentially using tab key). Focus is needed to capture some key events.
Expand Down Expand Up @@ -347,7 +348,7 @@ class View extends THREE.EventDispatcher {
return layer._reject(new Error('Cant add GeometryLayer: missing a preUpdate function'));
}

this._layers.push(layer);
this.#layers.push(layer);
}

if (layer.object3d && !layer.object3d.parent && layer.object3d !== this.scene) {
Expand Down Expand Up @@ -390,7 +391,7 @@ class View extends THREE.EventDispatcher {
throw new Error(`Error to detach ${layerId} from ${parentLayer.id}`);
} else if (parentLayer == undefined) {
// Remove layer from viewer
this._layers.splice(this._layers.findIndex(l => l.id == layerId), 1);
this.#layers.splice(this.#layers.findIndex(l => l.id == layerId), 1);
}
if (layer.isColorLayer) {
// Update color layers sequence
Expand Down Expand Up @@ -437,7 +438,7 @@ class View extends THREE.EventDispatcher {
if (changeSource) {
this._changeSources.add(changeSource);
if ((changeSource.isTileMesh || changeSource.isCamera)) {
this._fullSizeDepthBuffer.needsUpdate = true;
this.#fullSizeDepthBuffer.needsUpdate = true;
}
}
this.mainLoop.scheduleViewUpdate(this, needsRedraw);
Expand All @@ -464,7 +465,7 @@ class View extends THREE.EventDispatcher {
*/
getLayers(filter) {
const result = [];
for (const layer of this._layers) {
for (const layer of this.#layers) {
if (!filter || filter(layer)) {
result.push(layer);
}
Expand Down Expand Up @@ -993,14 +994,14 @@ class View extends THREE.EventDispatcher {
// Render/Read to buffer
let buffer;
if (viewPaused) {
if (this._fullSizeDepthBuffer.needsUpdate) {
this.readDepthBuffer(0, 0, dim.x, dim.y, this._fullSizeDepthBuffer);
this._fullSizeDepthBuffer.needsUpdate = false;
if (this.#fullSizeDepthBuffer.needsUpdate) {
this.readDepthBuffer(0, 0, dim.x, dim.y, this.#fullSizeDepthBuffer);
this.#fullSizeDepthBuffer.needsUpdate = false;
}
const id = ((dim.y - mouse.y - 1) * dim.x + mouse.x) * 4;
buffer = this._fullSizeDepthBuffer.slice(id, id + 4);
buffer = this.#fullSizeDepthBuffer.slice(id, id + 4);
} else {
buffer = this.readDepthBuffer(mouse.x, mouse.y, 1, 1, this._pixelDepthBuffer);
buffer = this.readDepthBuffer(mouse.x, mouse.y, 1, 1, this.#pixelDepthBuffer);
}

screen.x = (mouse.x / dim.x) * 2 - 1;
Expand Down Expand Up @@ -1089,7 +1090,7 @@ class View extends THREE.EventDispatcher {
height = this.domElement.clientHeight;
}

this._fullSizeDepthBuffer = new Uint8Array(4 * width * height);
this.#fullSizeDepthBuffer = new Uint8Array(4 * width * height);
this.mainLoop.gfxEngine.onWindowResize(width, height);
this.camera.resize(width, height);
this.notifyChange(this.camera.camera3D);
Expand Down

0 comments on commit 51f5508

Please sign in to comment.