-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathEASEHighlighter.js
130 lines (113 loc) · 3.68 KB
/
EASEHighlighter.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
/**
* A highlighter for 3D objects based on the ros3d mouseover
* highlighter:
* https://github.com/RobotWebTools/ros3djs/blob/develop/src/visualization/interaction/Highlighter.js
*
* @author Sascha Jongebloed
*/
/**
* A highlighter for 3D objects in the scene.
*
* @constructor
* @param options - object with following keys:
*
*/
EASEHighlighter = function(options) {
options = options || {};
this.hoverObjs = {};
this.highlightColor = options.color || 0xFFFFFF;
};
/**
* Render the highlights for all objects that are currently highlighted.
*
* This method should be executed after clearing the renderer and
* rendering the regular scene.
*
* @param scene - the current scene, which should contain the highlighted objects (among others)
* @param renderer - the renderer used to render the scene.
* @param camera - the scene's camera
*/
EASEHighlighter.prototype.renderHighlights = function(scene, renderer, camera) {
// Render highlights by making everything but the highlighted
// objects invisible...
this.makeEverythingInvisible(scene);
this.makeHighlightedVisible(scene);
// Providing a transparent overrideMaterial...
var originalOverrideMaterial = scene.overrideMaterial;
scene.overrideMaterial = new THREE.MeshBasicMaterial({
fog : false,
opacity : 0.5,
transparent : true,
depthTest : true,
depthWrite : false,
polygonOffset : true,
polygonOffsetUnits : -1,
side : THREE.DoubleSide,
color : new THREE.Color(this.highlightColor)
});
// And then rendering over the regular scene
renderer.render(scene, camera);
// Finally, restore the original overrideMaterial (if any) and
// object visibility.
scene.overrideMaterial = originalOverrideMaterial;
this.restoreVisibility(scene);
};
/**
* Traverses the given object and makes every object that's a Mesh,
* Line or Sprite invisible. Also saves the previous visibility state
* so we can restore it later.
*
* @param scene - the object to traverse
*/
EASEHighlighter.prototype.makeEverythingInvisible = function (scene) {
scene.traverse(function(currentObject) {
if ( currentObject.type === "Mesh" || currentObject.type === "Line"
|| currentObject.type === "Sprite" ) {
currentObject.previousVisibility = currentObject.visible;
currentObject.visible = false;
}
});
};
/**
* Make the objects in the scene that are currently highlighted (and
* all of their children!) visible.
*
* @param scene - the object to traverse
*/
EASEHighlighter.prototype.makeHighlightedVisible = function (scene) {
var makeVisible = function(currentObject) {
if ( currentObject.type === "Mesh" || currentObject.type === "Line"
|| currentObject.type === "Sprite" ) {
currentObject.visible = true;
}
};
for (var uuid in this.hoverObjs) {
var selectedObject = this.hoverObjs[uuid];
// Make each selected object and all of its children visible
selectedObject.visible = true;
selectedObject.traverse(makeVisible);
}
};
/**
* Restore the old visibility state that was saved by
* makeEverythinginvisible.
*
* @param scene - the object to traverse
*/
EASEHighlighter.prototype.restoreVisibility = function (scene) {
scene.traverse(function(currentObject) {
if (currentObject.hasOwnProperty('previousVisibility')) {
currentObject.visible = currentObject.previousVisibility;
}
}.bind(this));
};
EASEHighlighter.prototype.highlight = function(node) {
this.hoverObjs[node.uuid] = node;
};
EASEHighlighter.prototype.unhighlight = function(node) {
delete this.hoverObjs[node.uuid];
};
EASEHighlighter.prototype.clearHighlights = function() {
this.hoverObjs = {};
};
module.exports = EASEHighlighter;