Skip to content

Commit

Permalink
Add Scene#opacityObjects
Browse files Browse the repository at this point in the history
  • Loading branch information
xeolabs committed Jun 10, 2020
1 parent 5436074 commit 37dc831
Show file tree
Hide file tree
Showing 5 changed files with 78 additions and 13 deletions.
6 changes: 4 additions & 2 deletions examples/picking_canvas_mesh_opacity.html
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ <h1>Changing Opacity with the Pointer</h1>
if (!lastEntity || hit.entity.id !== lastEntity.id) {

if (lastEntity) {
lastEntity.opacity = lastOpacity;
lastEntity.opacity = null;
}

lastEntity = hit.entity;
Expand All @@ -87,11 +87,13 @@ <h1>Changing Opacity with the Pointer</h1>
} else {

if (lastEntity) {
lastEntity.opacity = lastOpacity;
lastEntity.opacity = null;
lastEntity = null;
}
}
});

window.viewer = viewer;

</script>
</html>
32 changes: 22 additions & 10 deletions src/viewer/scene/PerformanceModel/lib/PerformanceNode.js
Original file line number Diff line number Diff line change
Expand Up @@ -477,19 +477,27 @@ class PerformanceNode {
if (this.meshes.length === 0) {
return;
}
if (opacity < 0) {
opacity = 0;
} else if (opacity > 1) {
opacity = 1;
}
opacity = Math.floor(opacity * 255.0); // Quantize
var lastOpacity = (this.meshes[0]._colorize[3] / 255.0);
if (lastOpacity === opacity) {
return;
const opacityUpdated = (opacity !== null && opacity !== undefined);
if (opacityUpdated) {
if (opacity < 0) {
opacity = 0;
} else if (opacity > 1) {
opacity = 1;
}
opacity = Math.floor(opacity * 255.0); // Quantize
var lastOpacity = (this.meshes[0]._colorize[3] / 255.0);
if (lastOpacity === opacity) {
return;
}
} else {
opacity = 255.0;
}
for (var i = 0, len = this.meshes.length; i < len; i++) {
for (let i = 0, len = this.meshes.length; i < len; i++) {
this.meshes[i]._setOpacity(opacity);
}
if (this._isObject) {
this.scene._objectOpacityUpdated(this, opacityUpdated);
}
this.model.glRedraw();
}

Expand Down Expand Up @@ -597,6 +605,10 @@ class PerformanceNode {
const colorized = false;
this.scene._objectColorizeUpdated(this, colorized);
}
if (this._isObject) {
const opacityUpdated = false;
this.scene._objectOpacityUpdated(this, opacityUpdated);
}
}
for (var i = 0, len = this.meshes.length; i < len; i++) {
this.meshes[i]._destroy();
Expand Down
6 changes: 5 additions & 1 deletion src/viewer/scene/mesh/Mesh.js
Original file line number Diff line number Diff line change
Expand Up @@ -1316,7 +1316,9 @@ class Mesh extends Component {
colorize[1] = 1;
colorize[2] = 1;
}
colorize[3] = opacity !== null && opacity !== undefined ? opacity : 1.0;
const opacityUpdated = (opacity !== null && opacity !== undefined);
colorize[3] = opacityUpdated ? opacity : 1.0;
this.scene._objectOpacityUpdated(this, opacityUpdated);
this.glRedraw();
}

Expand Down Expand Up @@ -1756,6 +1758,8 @@ class Mesh extends Component {
}
const colorized = false;
this.scene._objectColorizeUpdated(this, colorized);
const opacityUpdated = false;
this.scene._objectOpacityUpdated(this, opacityUpdated);
}
if (this._isModel) {
this.scene._deregisterModel(this);
Expand Down
8 changes: 8 additions & 0 deletions src/viewer/scene/nodes/Node.js
Original file line number Diff line number Diff line change
Expand Up @@ -648,6 +648,10 @@ class Node extends Component {
for (let i = 0, len = this._children.length; i < len; i++) {
this._children[i].opacity = opacity;
}
if (this._isObject) {
const opacityUpdated = (opacity !== null && opacity !== undefined);
this.scene._objectOpacityUpdated(this, opacityUpdated);
}
}

/**
Expand Down Expand Up @@ -1253,6 +1257,10 @@ class Node extends Component {
const colorized = false;
this.scene._objectColorizeUpdated(this, colorized);
}
if (this._isObject) {
const opacityUpdated = (opacity !== null && opacity !== undefined);
this.scene._objectOpacityUpdated(this, opacityUpdated);
}
}
if (this._isModel) {
this.scene._deregisterModel(this);
Expand Down
39 changes: 39 additions & 0 deletions src/viewer/scene/scene/Scene.js
Original file line number Diff line number Diff line change
Expand Up @@ -462,6 +462,20 @@ class Scene extends Component {
this.colorizedObjects = {};
this._numColorizedObjects = 0;

/**
* Map of {@link Entity}s that represent objects whose opacity was updated.
*
* An Entity represents an object if {@link Entity#isObject} is ````true````.
*
* Each {@link Entity} is mapped here by {@link Entity#id}.
*
* @property opacityObjects
* @final
* @type {{String:Object}}
*/
this.opacityObjects = {};
this._numOpacityObjects = 0;

// Cached ID arrays, lazy-rebuilt as needed when stale after map updates

/**
Expand All @@ -474,6 +488,7 @@ class Scene extends Component {
this._highlightedObjectIds = null;
this._selectedObjectIds = null;
this._colorizedObjectIds = null;
this._opacityObjectIds = null;

this._collidables = {}; // Components that contribute to the Scene AABB
this._compilables = {}; // Components that require shader compilation
Expand Down Expand Up @@ -1044,6 +1059,17 @@ class Scene extends Component {
this._colorizedObjectIds = null; // Lazy regenerate
}

_objectOpacityUpdated(entity, opacityUpdated) {
if (opacityUpdated) {
this.opacityObjects[entity.id] = entity;
this._numOpacityObjects++;
} else {
delete this.opacityObjects[entity.id];
this._numOpacityObjects--;
}
this._opacityObjectIds = null; // Lazy regenerate
}

_webglContextLost() {
// this.loading++;
this.canvas.spinner.processes++;
Expand Down Expand Up @@ -1317,6 +1343,18 @@ class Scene extends Component {
return this._colorizedObjectIds;
}

/**
* Gets the IDs of the {@link Entity}s in {@link Scene#opacityObjects}.
*
* @type {String[]}
*/
get opacityObjectIds() {
if (!this._opacityObjectIds) {
this._opacityObjectIds = Object.keys(this.opacityObjects);
}
return this._opacityObjectIds;
}

/**
* Sets the number of "ticks" that happen between each render or this Scene.
*
Expand Down Expand Up @@ -2221,6 +2259,7 @@ class Scene extends Component {
this.highlightedObjects = null;
this.selectedObjects = null;
this.colorizedObjects = null;
this.opacityObjects = null;
this.sectionPlanes = null;
this.lights = null;
this.lightMaps = null;
Expand Down

0 comments on commit 37dc831

Please sign in to comment.