-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
377 lines (372 loc) · 12.2 KB
/
app.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
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
function App() {
var SHADOW_MAP_WIDTH = 2048
, SHADOW_MAP_HEIGHT = 2048;
var renderer = new THREE.WebGLRenderer({
canvas: canv,
//shadowMapEnabled: true
});
renderer.shadowMap.enabled = true;
renderer.shadowMap.soft = true;
renderer.shadowMap.type = THREE.PCFShadowMap;
var raycaster = new THREE.Raycaster();
renderer.setSize(window.innerWidth, window.innerHeight);
var scene = this.scene = new THREE.Scene();
var pi2 = this.pi2 = Math.PI * 2;
var camera = this.camera = new THREE.PerspectiveCamera(35,// Field of view
window.innerWidth / window.innerHeight,// Aspect ratio
0.1,// Near
10000 // Far
);
var hilightedMesh;
var selectedMesh;
var bgClicked;
var buttons = 0;
this.getSelectedMesh = function() {
return selectedMesh;
}
window.stopButton.onclick = function(e) {
puppeteer.send({
stop: true
});
}
window.exportButton.onclick = function(e) {
puppeteer.exportModel();
}
window.restartButton.onclick = function(e) {
puppeteer.send({
restartServer: true
});
setTimeout(function() {
location.reload();
}, 5000);
}
window.killButton.onclick = function(e) {
puppeteer.send({
killServer: true
});
setTimeout(function() {
location.reload();
}, 5000);
}
window.dumpButton.onclick = function(e) {
puppeteer.app.getJSON('bot-burn.json', function(err, data) {
if (err != null ) {
alert("Something went wrong: " + err);
} else {
console.log("Bot read.");
App.prototype.setPrefs(data);
Pane.syncAllStates();
}
})
}
window.burnButton.onclick = function(e) {
puppeteer.send({
burnToBot: {
path: "bot-burn.json",
data: JSON.stringify(puppeteer.app.getState())
}
});
}
function loadFileObjAsText(fileobj, loadedcb) {
var reader = new FileReader();
reader.onload = function(event) {
var data = event.target.result;
loadedcb(fileobj, data);
}
reader.readAsText(fileobj);
}
function handleFileSelect(evt) {
evt.stopPropagation();
evt.preventDefault();
var files = evt.dataTransfer.files;
for (var i = 0, f; (f = files[i]) ; i++) {
loadFileObjAsText(f, function(fileobj, txt) {
//puppeteer.importModel(JSON.parse(txt));
App.prototype.setPrefs(JSON.parse(txt));
Pane.syncAllStates();
})
}
}
function handleDragOver(evt) {
evt.stopPropagation();
evt.preventDefault();
evt.dataTransfer.dropEffect = 'link';
// Explicitly show this is a copy.
}
document.addEventListener('dragover', handleDragOver, false);
document.addEventListener('drop', handleFileSelect, false);
var prefs = this.getPrefs();
var camYaw = this.camYaw = new THREE.Object3D();
var camPitch = this.camPitch = new THREE.Object3D();
camPitch.rotation.x = prefs.camera.pitch ? prefs.camera.pitch : pi2 * 0.85;
camYaw.rotation.y = prefs.camera.yaw ? prefs.camera.yaw : pi2 * 0.85;
camera.position.set(0, 0, prefs.camera.zoom ? prefs.camera.zoom : 32);
scene.add(camYaw);
camYaw.add(camPitch);
camPitch.add(camera);
function showStatus(str) {
console.log(str);
statusText.innerHTML = str;
}
var ud = picklet.create('ui-div', 64, function(str, col) {
puppeteer.colorSelected(str, col);
}, [1, 1, 1]);
var light = new THREE.SpotLight(0xFFFFFF);
//var light = new THREE.DirectionalLight(0xffffff);
light.position.set(0, 20, 0);
light.penumbra = 0.5;
light.decay = 0.1;
scene.add(light);
//light0.lookAt(scene);
//light0.castShadow =
light.lookAt(scene);
light.castShadow = true;
var light0 = new THREE.AmbientLight(0x808080);
scene.add(light0);
/*
var light0 = new THREE.DirectionalLight(0xffffff);
light0.position.set(-9, 7, 9);
scene.add(light0);
light0.lookAt(scene);
*/
// light0.shadow = new THREE.LightShadow( new THREE.PerspectiveCamera( 50, 1, 1200, 2500 ) );
// light0.shadow.bias = 0.0001;
light.shadow.mapSize.width = SHADOW_MAP_WIDTH;
light.shadow.mapSize.height = SHADOW_MAP_HEIGHT;
//scene.add(new THREE.CameraHelper( light.shadow.camera ))
renderer.setClearColor(0x303030, 1);
//0x8d8ddd
var mouse = new THREE.Vector2();
function onMouseMove(event) {
// calculate mouse position in normalized device coordinates
// (-1 to +1) for both components
mouse.x = (event.clientX / window.innerWidth) * 2 - 1;
mouse.y = -(event.clientY / window.innerHeight) * 2 + 1;
}
window.addEventListener('mousemove', onMouseMove, false);
var loader = new THREE.STLLoader();
var activeMeshLoads = 0;
var loadsFinished = false;
function meshLoaded(geometry, id) {
function v3(x, y, z) {
return new vec3(x,y,z);
}
var mesh = new THREE.Mesh(geometry,material);
//mesh.position.set( 0, - 0.25, 0.6 );
//mesh.rotation.set( 0, - Math.PI / 2, 0 );
mesh.scale.set(0.1, 0.1, 0.1);
mesh.castShadow = true;
mesh.receiveShadow = true;
//scene.add( mesh );
meshDB[id] = mesh;
activeMeshLoads--;
}
this.getJSON = function(url, callback) {
var xhr = new XMLHttpRequest();
xhr.open("get", url, true);
xhr.responseType = "json";
xhr.onload = function() {
var status = xhr.status;
if (status == 200) {
callback(null , xhr.response);
} else {
callback(status);
}
}
;
xhr.send();
}
;
this.loadMeshSTL = function(path, id) {
activeMeshLoads++;
loader.load(path, function(_id) {
var id = _id;
return function(geometry) {
meshLoaded(geometry, id)
}
}(id));
}
var hilightedMesh;
var selectedMesh;
var hilightMaterial = new THREE.MeshBasicMaterial({
color: new THREE.Color(1,1,1,0.5),
transparent: true,
opacity: 0.5
});
var selectMaterial = new THREE.MeshBasicMaterial({
color: new THREE.Color(1,1,1,0.25),
transparent: true,
opacity: 0.5
});
var selectGhost;
var hilightGhost;
var material = new THREE.MeshPhongMaterial({
color: 0xFFFFFF
});
var selMaterial = new THREE.MeshPhongMaterial({
color: 0xFFFF00
});
var hiMaterial = new THREE.MeshPhongMaterial({
color: 0xFF0000
});
function mwheel(evt) {
camera.position.z += evt.wheelDelta * -0.01;
}
function mdown(event) {
if (event.target != canv){
//event.preventDefault();
return;//s true;
}
buttons |= 1 << event.button;
if (hilightedMesh) {
if (hilightedMesh != selectedMesh) {
if (selectedMesh) {
selectGhost.parent.remove(selectGhost);
selectedMesh = selectGhost = undefined;
}
selectedMesh = hilightedMesh;
selectGhost = selectedMesh.clone();
selectGhost.material = selectMaterial;
selectGhost.scale.multiplyScalar(1.0);
selectedMesh.parent.add(selectGhost);
puppeteer.doSelectMesh(selectedMesh);
}
} else {
if (selectedMesh) {
selectGhost.parent.remove(selectGhost);
selectedMesh = selectGhost = undefined;
}
if (buttons != 0)
bgClicked = true;
}
}
function mup(event) {
if (event.target != canv)
return;
buttons &= ~(1 << event.button);
bgClicked = false;
puppeteer.send({
stop: true
});
event.preventDefault();
return false;
}
function mmove(evt) {
if (bgClicked) {
camYaw.rotation.y += evt.movementX * 0.01;
camPitch.rotation.x += evt.movementY * 0.01;
} else
puppeteer.meshMoved(evt, buttons);
}
var pickables = this.pickables = [];
var meshDB = this.meshDB = {};
function getState() {
var prefs = App.prototype.getPrefs();
prefs.colors = {};
scene.traverse(function(mesh) {
if (mesh.userData.material) {
var col = mesh.material.color;
prefs.colors[mesh.id] = [col.r, col.g, col.b];
}
})
prefs.camera.yaw = camYaw.rotation.y;
prefs.camera.pitch = camPitch.rotation.x;
prefs.camera.zoom = camera.position.z;
puppeteer.saveState(prefs);
return prefs;
}
this.getState = getState;
function saveState() {
localStorage.exobot = JSON.stringify(getState());
return null ;
}
this.saveState = saveState;
var fadeDuration = 2000;
var fadeEndTime;
var fadeDone;
function render() {
requestAnimationFrame(render);
if (!loadsFinished && activeMeshLoads == 0) {
loadsFinished = true;
window.puppeteer.buildBot();
Pane.syncAllStates();
document.body.oncontextmenu=function(evt){return false;};
window.addEventListener('mousedown', mdown, false);
window.addEventListener('mouseup', mup, false);
window.addEventListener('mouseout', mup, false);
window.addEventListener('mousemove', mmove, false);
window.addEventListener('mousewheel', mwheel, false);
}else{
if(loadsFinished && !fadeDone){
var now = performance.now();
if(!fadeEndTime){
fadeEndTime = now+fadeDuration;
window.blocker.style.display = 'block';
}else if(now<fadeEndTime)
{
window.blocker.style.opacity = (fadeEndTime - now)/fadeDuration;
return;
}else{
window.blocker.style.display = 'none';
fadeDone=true;
}
}
}
renderer.setSize(window.innerWidth, window.innerHeight);
camera.aspect = window.innerWidth / window.innerHeight;
camera.updateProjectionMatrix();
// update the picking ray with the camera and mouse position
raycaster.setFromCamera(mouse, camera);
// calculate objects intersecting the picking ray
var intersects = raycaster.intersectObjects(pickables, true);
if (intersects.length > 0) {
if (hilightedMesh != intersects[0].object) {
if (hilightGhost) {
hilightGhost.parent.remove(hilightGhost);
hilightGhost = undefined;
}
hilightedMesh = intersects[0].object
hilightGhost = hilightedMesh.clone();
hilightGhost.scale.multiplyScalar(1.0);
hilightGhost.material = hilightMaterial;
hilightedMesh.parent.add(hilightGhost);
}
} else {
hilightedMesh = undefined;
if (hilightGhost) {
hilightGhost.parent.remove(hilightGhost);
hilightGhost = undefined;
}
}
renderer.render(scene, camera);
puppeteer.tick();
//body.rotation.y += 0.001;
}
requestAnimationFrame(render);
window.onbeforeunload = function(e) {
saveState();
return;
// "Haaaalps!"
}
}
App.prototype.setPrefs = function(prefs) {
App.prefs = prefs;
}
App.prototype.getPrefs = function() {
if (App.prefs)
return App.prefs;
var prefs = App.prefs = {}
try {
App.prefs = prefs = JSON.parse(localStorage.exobot);
} catch (err) {
App.prefs = prefs = {};
}
// App.prefs=prefs={};
if (!prefs.camera)
prefs.camera = {};
if (!prefs.colors)
prefs.colors = {};
if (!prefs.bones)
prefs.bones = {};
return prefs;
}