forked from c-frame/aframe-extras
-
Notifications
You must be signed in to change notification settings - Fork 0
/
sphere-collider.js
147 lines (124 loc) · 4.13 KB
/
sphere-collider.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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
/**
* Based on aframe/examples/showcase/tracked-controls.
*
* Implement bounding sphere collision detection for entities with a mesh.
* Sets the specified state on the intersected entities.
*
* @property {string} objects - Selector of the entities to test for collision.
* @property {string} state - State to set on collided entities.
*
*/
module.exports = AFRAME.registerComponent('sphere-collider', {
schema: {
objects: {default: ''},
state: {default: 'collided'},
radius: {default: 0.05},
watch: {default: true}
},
init: function () {
/** @type {MutationObserver} */
this.observer = null;
/** @type {Array<Element>} Elements to watch for collisions. */
this.els = [];
/** @type {Array<Element>} Elements currently in collision state. */
this.collisions = [];
this.handleHit = this.handleHit.bind(this);
this.handleHitEnd = this.handleHitEnd.bind(this);
},
remove: function () {
this.pause();
},
play: function () {
const sceneEl = this.el.sceneEl;
if (this.data.watch) {
this.observer = new MutationObserver(this.update.bind(this, null));
this.observer.observe(sceneEl, {childList: true, subtree: true});
}
},
pause: function () {
if (this.observer) {
this.observer.disconnect();
this.observer = null;
}
},
/**
* Update list of entities to test for collision.
*/
update: function () {
const data = this.data;
let objectEls;
// Push entities into list of els to intersect.
if (data.objects) {
objectEls = this.el.sceneEl.querySelectorAll(data.objects);
} else {
// If objects not defined, intersect with everything.
objectEls = this.el.sceneEl.children;
}
// Convert from NodeList to Array
this.els = Array.prototype.slice.call(objectEls);
},
tick: (function () {
const position = new THREE.Vector3(),
meshPosition = new THREE.Vector3(),
colliderScale = new THREE.Vector3(),
distanceMap = new Map();
return function () {
const el = this.el,
data = this.data,
mesh = el.getObject3D('mesh'),
collisions = [];
let colliderRadius;
if (!mesh) { return; }
distanceMap.clear();
position.copy(el.object3D.getWorldPosition());
el.object3D.getWorldScale(colliderScale);
colliderRadius = data.radius * scaleFactor(colliderScale);
// Update collision list.
this.els.forEach(intersect);
// Emit events and add collision states, in order of distance.
collisions
.sort((a, b) => distanceMap.get(a) > distanceMap.get(b) ? 1 : -1)
.forEach(this.handleHit);
// Remove collision state from current element.
if (collisions.length === 0) { el.emit('hit', {el: null}); }
// Remove collision state from other elements.
this.collisions
.filter((el) => !distanceMap.has(el))
.forEach(this.handleHitEnd);
// Store new collisions
this.collisions = collisions;
// Bounding sphere collision detection
function intersect (el) {
let radius, mesh, distance, box, extent, size;
if (!el.isEntity) { return; }
mesh = el.getObject3D('mesh');
if (!mesh) { return; }
box = new THREE.Box3().setFromObject(mesh);
size = box.getSize();
extent = Math.max(size.x, size.y, size.z) / 2;
radius = Math.sqrt(2 * extent * extent);
box.getCenter(meshPosition);
if (!radius) { return; }
distance = position.distanceTo(meshPosition);
if (distance < radius + colliderRadius) {
collisions.push(el);
distanceMap.set(el, distance);
}
}
// use max of scale factors to maintain bounding sphere collision
function scaleFactor (scaleVec) {
return Math.max.apply(null, scaleVec.toArray());
}
};
})(),
handleHit: function (targetEl) {
targetEl.emit('hit');
targetEl.addState(this.data.state);
this.el.emit('hit', {el: targetEl});
},
handleHitEnd: function (targetEl) {
targetEl.emit('hitend');
targetEl.removeState(this.data.state);
this.el.emit('hitend', {el: targetEl});
}
});