-
-
Notifications
You must be signed in to change notification settings - Fork 35.7k
/
Copy pathLayers.js
121 lines (91 loc) · 2.21 KB
/
Layers.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
/**
* A layers object assigns an 3D object to 1 or more of 32
* layers numbered `0` to `31` - internally the layers are stored as a
* bit mask], and by default all 3D objects are a member of layer `0`.
*
* This can be used to control visibility - an object must share a layer with
* a camera to be visible when that camera's view is
* rendered.
*
* All classes that inherit from {@link Object3D} have an `layers` property which
* is an instance of this class.
*/
class Layers {
/**
* Constructs a new layers instance, with membership
* initially set to layer `0`.
*/
constructor() {
/**
* A bit mask storing which of the 32 layers this layers object is currently
* a member of.
*
* @type {number}
*/
this.mask = 1 | 0;
}
/**
* Sets membership to the given layer, and remove membership all other layers.
*
* @param {number} layer - The layer to set.
*/
set( layer ) {
this.mask = ( 1 << layer | 0 ) >>> 0;
}
/**
* Adds membership of the given layer.
*
* @param {number} layer - The layer to enable.
*/
enable( layer ) {
this.mask |= 1 << layer | 0;
}
/**
* Adds membership to all layers.
*/
enableAll() {
this.mask = 0xffffffff | 0;
}
/**
* Toggles the membership of the given layer.
*
* @param {number} layer - The layer to toggle.
*/
toggle( layer ) {
this.mask ^= 1 << layer | 0;
}
/**
* Removes membership of the given layer.
*
* @param {number} layer - The layer to enable.
*/
disable( layer ) {
this.mask &= ~ ( 1 << layer | 0 );
}
/**
* Removes the membership from all layers.
*/
disableAll() {
this.mask = 0;
}
/**
* Returns `true` if this and the given layers object have at least one
* layer in common.
*
* @param {Layers} layers - The layers to test.
* @return {boolean } Whether this and the given layers object have at least one layer in common or not.
*/
test( layers ) {
return ( this.mask & layers.mask ) !== 0;
}
/**
* Returns `true` if the given layer is enabled.
*
* @param {number} layer - The layer to test.
* @return {boolean } Whether the given layer is enabled or not.
*/
isEnabled( layer ) {
return ( this.mask & ( 1 << layer | 0 ) ) !== 0;
}
}
export { Layers };