-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathalongpath.js
326 lines (256 loc) · 11.4 KB
/
alongpath.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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
/******/ (function(modules) { // webpackBootstrap
/******/ // The module cache
/******/ var installedModules = {};
/******/ // The require function
/******/ function __webpack_require__(moduleId) {
/******/ // Check if module is in cache
/******/ if(installedModules[moduleId])
/******/ return installedModules[moduleId].exports;
/******/ // Create a new module (and put it into the cache)
/******/ var module = installedModules[moduleId] = {
/******/ exports: {},
/******/ id: moduleId,
/******/ loaded: false
/******/ };
/******/ // Execute the module function
/******/ modules[moduleId].call(module.exports, module, module.exports, __webpack_require__);
/******/ // Flag the module as loaded
/******/ module.loaded = true;
/******/ // Return the exports of the module
/******/ return module.exports;
/******/ }
/******/ // expose the modules object (__webpack_modules__)
/******/ __webpack_require__.m = modules;
/******/ // expose the module cache
/******/ __webpack_require__.c = installedModules;
/******/ // __webpack_public_path__
/******/ __webpack_require__.p = "";
/******/ // Load entry module and return exports
/******/ return __webpack_require__(0);
/******/ })
/************************************************************************/
/******/ ([
/* 0 */
/***/ function(module, exports) {
if (typeof AFRAME === 'undefined') {
throw new Error('Component attempted to register before AFRAME was available.');
}
/**
* Alongpath component for A-Frame.
* Move Entities along a predefined path
*/
AFRAME.registerComponent('alongpath', {
schema: {
// TODO: Parse path-points directly here
path: {default: ''},
closed: {default: false},
dur: {default: 1000},
delay: {default: 0},
loop: {default: false},
rotate: {default: false},
inspect: {default: false}
},
init: function () {
this.initialPosition = this.el.getAttribute("position");
},
update: function (oldData) {
// Only restart following the path when
// Path-Data has been changed.
if (!oldData.path || oldData.path != this.data.path) {
this.curve = null;
// Create Curve from Path
this.createCurve();
}
// Create/Update Debug-Visuals when needed or
// remove Debug-Visuals when disabled
if (this.data.inspect && this.curve) {
if (!oldData.inspect || oldData.inspect === false) {
this.inspectorElementChanged = this.inspectorElementChanged.bind(this);
// Add Elements to visualize the Path
// and allow for path editing in the
// A-Frame Inspector
this.createInspectorElements();
}
// Update the Debug-Visuals
this.drawCurveLine();
this.synchInspectorElementPosition();
} else if (oldData.inspect === true) {
this.removeInspectorElements();
}
},
createCurve: function () {
// TODO: Parse path-points in schema function
this.pathpoints = this.data.path.split(' ').map(function (p) {
p = p.split(',');
return new THREE.Vector3(
parseFloat(p[0]),
parseFloat(p[1]),
parseFloat(p[2])
);
});
// Only create curve when there are more than 2 pathpoints
if (this.pathpoints.length >= 2) {
var curve = new THREE.CatmullRomCurve3(this.pathpoints);
curve.closed = this.data.closed;
this.curve = curve;
} else {
console.warn("The path needs at least 2 path-points!");
}
// Reset to initial state
this.interval = 0;
this.el.removeState("endofpath");
},
tick: function (time, timeDelta) {
if (this.curve) {
// Only update position if we didn't reach
// the end of the path
if (!this.el.is("endofpath")) {
this.interval = this.interval + timeDelta;
var i = 0;
if (this.interval - this.data.delay >= this.data.dur) {
// Time is up, we should be at the end of the path
i = 1;
} else if ((this.interval - this.data.delay < 0)) {
// We are still waiting for the delay-time to finish
// so keep entity at the beginning of the path
i = 0;
} else {
// Update path position based on timing
i = (this.interval - this.data.delay) / this.data.dur;
}
if ((this.data.loop === false) && i >= 1) {
// We have reached the end of the path and are not going
// to loop back to the beginning therefore set final state
this.el.removeState("moveonpath");
this.el.addState("endofpath");
this.el.emit("movingended");
// Set the end-position
if (this.data.closed) {
this.el.setAttribute('position', this.curve.points[0]);
} else {
this.el.setAttribute('position', this.curve.points[this.curve.points.length - 1]);
}
} else if ((this.data.loop === true) && i >= 1) {
// We have reached the end of the path
// but we are looping through the curve,
// so restart here.
this.el.emit("movingended");
this.interval = this.data.delay;
} else {
// We are starting to move or somewhere in the middle of the path…
if (!this.el.is("moveonpath")) {
this.el.addState("moveonpath");
this.el.emit("movingstarted");
}
// …updating position
var p = this.curve.getPoint(i);
this.el.setAttribute('position', p);
}
// Update Rotation of Entity
// Based on http://jsfiddle.net/qGPTT/133/
if (this.data.rotate === true) {
var axis = new THREE.Vector3();
var up = new THREE.Vector3(0, 1, 0);
var tangent = this.curve.getTangentAt(i).normalize();
axis.crossVectors(up, tangent).normalize();
var radians = Math.acos(up.dot(tangent));
this.el.object3D.quaternion.setFromAxisAngle(axis, radians);
}
}
}
},
remove: function () {
this.el.setAttribute("position", this.initialPosition);
this.removeInspectorElements();
},
createInspectorElements: function() {
this.removeInspectorElements();
var debugRootElement = document.querySelector(".alongpath-debug-root");
if (!debugRootElement) {
var debugRoot = document.createElement("a-entity");
debugRootElement = this.el.sceneEl.appendChild(debugRoot);
debugRootElement.setAttribute("class", "alongpath-debug-root");
}
this.inspectorRootElement = debugRootElement;
this.inspectorElements = new Array();
for (var i = 0; i < this.pathpoints.length; i++) {
var pathPoint = document.createElement("a-box");
var pathPointEl = this.inspectorRootElement.appendChild(pathPoint);
AFRAME.utils.entity.setComponentProperty(pathPointEl, "position", AFRAME.utils.coordinates.stringify(this.pathpoints[i]));
AFRAME.utils.entity.setComponentProperty(pathPointEl, "width", 0.1);
AFRAME.utils.entity.setComponentProperty(pathPointEl, "height", 0.1);
AFRAME.utils.entity.setComponentProperty(pathPointEl, "depth", 0.1);
AFRAME.utils.entity.setComponentProperty(pathPointEl, "color", "red");
pathPointEl.setAttribute("class", "alongpath-debug");
pathPointEl.setAttribute("visible", true);
pathPointEl.addEventListener("componentchanged", this.inspectorElementChanged);
this.inspectorElements.push(pathPointEl);
}
// First add a line
this.drawCurveLine();
},
drawCurveLine: function() {
if (this.inspectorCurve) {
this.inspectorCurve.parentNode.removeChild(this.inspectorCurve);
}
var lineEntity = document.createElement("a-entity");
var lineEntityEl = this.inspectorRootElement.appendChild(lineEntity);
var lineMaterial = new THREE.LineBasicMaterial({
color: "red"
});
var lineGeometry = new THREE.Geometry();
lineGeometry.vertices = this.curve.getPoints(this.pathpoints.length * 10);
lineEntityEl.setObject3D('mesh', new THREE.Line(lineGeometry, lineMaterial));
lineEntityEl.setAttribute("className", "alongpath-debug");
lineEntityEl.setAttribute("visible", true);
this.inspectorCurve = lineEntityEl;
},
removeInspectorElements: function() {
// Remove the Inspector-Boxes
if(this.inspectorElements) {
for (var i = 0; i < this.inspectorElements.length; i++) {
this.inspectorElements[i].parentNode.removeChild(this.inspectorElements[i]);
}
}
// Remove the curve
if(this.inspectorCurve) {
this.inspectorCurve.parentNode.removeChild(this.inspectorCurve);
}
// Remove also the Root Element if it has
// no more children
if (this.inspectorRootElement) {
if(!this.inspectorRootElement.childNodes || this.inspectorRootElement.childNodes.length == 0) {
if (this.inspectorRootElement.parentNode) {
this.inspectorRootElement.parentNode.removeChild(this.inspectorRootElement);
}
this.inspectorRootElement = null;
}
}
this.inspectorElements = null;
this.inspectorCurve = null;
},
synchInspectorElementPosition: function() {
for (var i = 0; i < this.pathpoints.length; i++) {
if (this.inspectorElements[i]) {
AFRAME.utils.entity.setComponentProperty(this.inspectorElements[i], "position", AFRAME.utils.coordinates.stringify(this.pathpoints[i]));
}
}
},
inspectorElementChanged: function(e) {
var newPathPoints = new Array();
for (var i = 0; i < this.inspectorElements.length; i++) {
if (this.inspectorElements[i].components.position.attrValue) {
newPathPoints.push(this.inspectorElements[i].components.position.attrValue.x
+ "," + this.inspectorElements[i].components.position.attrValue.y
+ "," + this.inspectorElements[i].components.position.attrValue.z
);
}
}
var newPath = newPathPoints.join(" ");
if (newPath != "" && newPath != this.data.path) {
AFRAME.utils.entity.setComponentProperty(this.el, "alongpath.path", newPath);
}
}
});
/***/ }
/******/ ]);