-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.js
470 lines (403 loc) · 17.7 KB
/
main.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
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
var video, canvas, context;
var pastResults = {};
var currId;
var MARParser = function(node) {
var doc = document.createElement('html');
doc.innerHTML = node;
this.parseNode = $(doc);
this.textNode = node;
this.scene = this.parseNode.find('scene')[0];
this.sensor = this.parseNode.find('sensor')[0];
this.background = this.parseNode.find('background')[0];
this.aobject = function() {
return this.parseNode.find('aobject').map(function(idx, m) {
return {
aobject: m,
aobject_reference: this.parseNode.find($(m).attr('placeholder-for'))
}
}.bind(this));
};
this.robject = function() {
return this.parseNode.find('robject').map(function(idx, m) {
return {
robject: m,
robject_reference: this.parseNode.find($(m).attr('placeholder')),
referenece_transform: this.parseNode.find('Matrix' + this.parseNode.find($(m).attr('placeholder')).attr('transform'))[0]
}
}.bind(this));
};
this.MAREvent = function() {
return this.parseNode.find('MAREvent').map(function(idx, m) {
return {
event: m,
event_object: this.parseNode.find('aobject, robject').find($(m).attr('object')),
event_callback: this.parseNode.find('MARBehavior[id="#' + $(m).attr('id') + '"]')
}
}.bind(this));
};
this.MARBehavior = function() {
return this.parseNode.find('MARBehavior').map(function(idx, m) {
return {
callback: m,
callback_handler: this.parseNode.find('MAREvent').find($(m).attr('event')),
callback_target: this.parseNode.find('aobject, robject').find($(m).attr('object'))
}
}.bind(this));
};
this.matrix = function() {
return this.parseNode.find('matrix').map(function(idx, m) {
return {
matrix: m
}
}.bind(this));
};
this.findEvent = function(elem) {
//elem == robject
return this.parseNode.find('MAREvent[object="#' + elem.robject.getAttribute('id') + '"]')[0];
}
};
// I'm going to use a glMatrix-style matrix as an intermediary.
// So the first step is to create a function to convert a glMatrix matrix into a Three.js Matrix4.
THREE.Matrix4.prototype.setFromArray = function(m) {
return this.set(
m[0], m[4], m[8], m[12],
m[1], m[5], m[9], m[13],
m[2], m[6], m[10], m[14],
m[3], m[7], m[11], m[15]
);
};
function MAREngine(parseContext) {
// 2. PREPARING
this.parseContext = parseContext;
this.setParseContext = function (context) {
this.parseContext = context;
switch (context.scene.getAttribute('type')) {
case 'ar':
{
bgTexture = new THREE.Texture(canvas);
bgTexture.minFilter = THREE.LinearFilter;
glPlane = new THREE.Mesh(new THREE.PlaneGeometry(2, 2, 0), new THREE.MeshBasicMaterial({
map: bgTexture,
depthTest: false,
depthWrite: false
}));
bgScene = new THREE.Scene();
bgCamera = new THREE.Camera();
bgScene.add(glPlane);
bgScene.add(bgCamera);
break;
}
case 'avr':
{
bgCamera = new THREE.PerspectiveCamera(70, canvas.width / canvas.height, 1, 1000);
bgCamera.position.z = 400;
bgScene = new THREE.Scene();
var geometry = new THREE.PlaneGeometry(600, 400);
var material = new THREE.MeshBasicMaterial({
color: 0x555555
});
var plane = new THREE.Mesh(geometry, material);
plane.rotation.x = -0.7;
bgScene.add(plane);
bgScene.remove(glPlane);
break;
}
}
}
var video = document.getElementById('video'),
canvas = document.getElementById("test"),
canvasContext = canvas.getContext("2d"),
param, markerRoots = {}, meshList = {}, clock = new THREE.Clock(), animMixer, action = {}, actionList = {};
var glRenderer, glScene, glCamera, glPlane;
var bgCamera, bgScene, bgTexture;
var __webrtc_init = (function() {
navigator.getUserMedia = navigator.getUserMedia || navigator.webkitGetUserMedia || navigator.mozGetUserMedia;
if (navigator.getUserMedia) {
navigator.getUserMedia({
video: true
},
function(stream) {
if (window.webkitURL) {
video.src = window.webkitURL.createObjectURL(stream);
} else if (video.mozSrcObject !== undefined) {
video.mozSrcObject = stream;
} else {
video.src = stream;
}
},
function(error) {
console.log(error);
}
);
}
})();
var __threejs_init = (function() {
glRenderer = new THREE.WebGLRenderer({
antialias: true
});
glRenderer.setSize(canvas.width, canvas.height);
document.getElementById("container").appendChild(glRenderer.domElement);
// create the scene
glScene = new THREE.Scene();
// Create a camera and a marker root object for your Three.js scene.
glCamera = new THREE.Camera();
glScene.add(glCamera);
var light = new THREE.DirectionalLight( 0xffffff , 5);
light.position.set( -10, -15, -1 ).normalize();
glScene.add(light);
switch (this.parseContext.scene.getAttribute('type')) {
case 'ar':
{
bgTexture = new THREE.Texture(canvas);
bgTexture.minFilter = THREE.LinearFilter;
glPlane = new THREE.Mesh(new THREE.PlaneGeometry(2, 2, 0), new THREE.MeshBasicMaterial({
map: bgTexture,
depthTest: false,
depthWrite: false
}));
bgScene = new THREE.Scene();
bgCamera = new THREE.Camera();
bgScene.add(glPlane);
bgScene.add(bgCamera);
break;
}
case 'avr':
{
bgCamera = new THREE.PerspectiveCamera(70, canvas.width / canvas.height, 1, 1000);
bgCamera.position.z = 400;
bgScene = new THREE.Scene();
var geometry = new THREE.PlaneGeometry(600, 400);
var material = new THREE.MeshBasicMaterial({
color: 0x555555
});
var plane = new THREE.Mesh(geometry, material);
plane.rotation.x = -0.7;
bgScene.add(plane);
break;
}
}
this.parseContext.aobject().each(function(idx, item) {
if (item.aobject.getAttribute('object') == 'model') {
var loader = new THREE.JSONLoader();
loader.load( item.aobject.getAttribute('model-data'), function(geo, mat) { loadJsonModel(geo, mat, item.aobject.getAttribute('id'));}, function(a) { console.log(a); });
}
});
})();
var copyMarkerMatrix = function(arMat, glMat) {
glMat[0] = arMat.m00;
glMat[1] = -arMat.m10;
glMat[2] = arMat.m20;
glMat[3] = 0;
glMat[4] = arMat.m01;
glMat[5] = -arMat.m11;
glMat[6] = arMat.m21;
glMat[7] = 0;
glMat[8] = -arMat.m02;
glMat[9] = arMat.m12;
glMat[10] = -arMat.m22;
glMat[11] = 0;
glMat[12] = arMat.m03;
glMat[13] = -arMat.m13;
glMat[14] = arMat.m23;
glMat[15] = 1;
}
var trackingMarker = function() {
var raster = new NyARRgbRaster_Canvas2D(canvas);
var param = new FLARParam(canvas.width, canvas.height);
var detector = new FLARMultiIdMarkerDetector(param, 120);
detector.setContinueMode(true);
// glMatrix matrices are flat arrays.
var tmp = new Float32Array(16);
// Next we need to make the Three.js camera use the FLARParam matrix.
param.copyCameraMatrix(tmp, 10, 10000);
glCamera.projectionMatrix.setFromArray(tmp);
// Create a NyARTransMatResult object for getting the marker translation matrices.
var resultMat = new NyARTransMatResult();
var markers = {};
var emptyFloatArray = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0];
canvas.changed = true;
// Update the video texture.
switch (this.parseContext.scene.getAttribute('type')) {
case 'ar':
{
bgTexture.needsUpdate = true;
break;
}
}
//move all marker roots to origin so that they will disappear when not tracked
Object.keys(markerRoots).forEach(function(key) {
markerRoots[key].matrix.setFromArray(emptyFloatArray);
markerRoots[key].matrixWorldNeedsUpdate = true;
});
var markerCount = detector.detectMarkerLite(raster, 120);
// Go through the detected markers and get their IDs and transformation matrices.
for (var i = 0; i < markerCount; i++) {
// Get the ID marker data for the current marker.
// ID markers are special kind of markers that encode a number.
// The bytes for the number are in the ID marker data.
var id = detector.getIdMarkerData(i);
// Read bytes from the id packet.
var currId = -1,
currItem;
// This code handles only 32-bit numbers or shorter.
if (id.packetLength <= 4) {
currId = 0;
for (var j = 0; j < id.packetLength; j++) {
currId = (currId << 8) | id.getPacketData(j);
}
}
// If this is a new id, let's start tracking it.
if (markers[currId] == null) {
//create new object for the marker
markers[currId] = {};
this.parseContext.robject().each(function(idx, item) {
if (item.robject.getAttribute('marker-id') == currId) {
currItem = item;
return false;
}
});
var event_item = this.parseContext.findEvent(currItem);
//create a new Three.js object as marker root
var markerRoot = augmentObject(currItem);
if (event_item.getAttribute('value') == 'true') {
// Add the marker root to your scene
glScene.add(markerRoot);
}
markerRoots[currId] = markerRoot;
}
// Get the transformation matrix for the detected marker.
detector.getTransformMatrix(i, resultMat);
// Copy the marker matrix to the tmp matrix.
copyMarkerMatrix(resultMat, tmp);
// Copy the marker matrix over to your marker root object.
markerRoots[currId].matrix.setFromArray(tmp);
markerRoots[currId].matrixWorldNeedsUpdate = true;
}
return markerRoots;
}
var augmentObject = function(robject_class) {
var markerRoot = new THREE.Object3D();
markerRoot.matrixAutoUpdate = false;
if (robject_class.robject_reference[0].getAttribute('object') == "cube") {
// cube
var cube = new THREE.Mesh(
new THREE.CubeGeometry(120, 120, 120),
new THREE.MeshBasicMaterial({
color: 0xff00ff,
wireframe: true
})
);
cube.position.x = parseFloat(robject_class.referenece_transform.getAttribute('x'));
cube.position.y = parseFloat(robject_class.referenece_transform.getAttribute('y'));
cube.position.z = parseFloat(robject_class.referenece_transform.getAttribute('z'));
markerRoot.add(cube);
} else if (robject_class.robject_reference[0].getAttribute('object') == "sphere") {
// sphere
var sphere = new THREE.Mesh(
new THREE.SphereGeometry(80, 40, 80),
new THREE.MeshBasicMaterial({
color: 0x0000FF,
wireframe: true
})
);
//console.log(robject_class)
sphere.position.x = parseFloat(robject_class.referenece_transform.getAttribute('x'));
sphere.position.y = parseFloat(robject_class.referenece_transform.getAttribute('y'));
sphere.position.z = parseFloat(robject_class.referenece_transform.getAttribute('z'));
markerRoot.add(sphere);
} else if (robject_class.robject_reference[0].getAttribute('object') == "model") {
// model
var customModel = meshList[robject_class.robject_reference[0].getAttribute('id')];
customModel.position.x = parseFloat(robject_class.referenece_transform.getAttribute('x'));
customModel.position.y = parseFloat(robject_class.referenece_transform.getAttribute('y'));
customModel.position.z = parseFloat(robject_class.referenece_transform.getAttribute('z'));
actionList[robject_class.robject_reference[0].getAttribute('id')].play();
markerRoot.add(customModel);
}
return markerRoot;
}
function loadJsonModel(geometry, materials, modelID) {
materials.forEach(function (material) {
material.skinning = true;
material.opacity = 1.0;
material.side = THREE.DoubleSide;
});
var object = new THREE.SkinnedMesh(
geometry,
new THREE.MeshFaceMaterial(materials)
);
object.scale.x = object.scale.y = object.scale.z = 100;
object.rotation.y = 180;
object.rotation.x = 0;
object.rotation.z = 90;
animMixer = new THREE.AnimationMixer(object);
meshList[modelID] = object;
actionList[modelID] = animMixer.clipAction( geometry.animations[ 0 ] );
console.log('loaded');
}
function renderer(interval, update_context) {
if (update_context !== undefined)
this.parseContext = update_context;
requestAnimFrame(renderer);
if (animMixer) {
animMixer.update(clock.getDelta());
}
canvasContext.drawImage(video, 0, 0, canvas.width, canvas.height);
trackingMarker();
// Render the scene.
glRenderer.autoClear = false;
glRenderer.clear();
glRenderer.render(bgScene, bgCamera);
glRenderer.render(glScene, glCamera);
}
window.requestAnimFrame = (function() {
return window.requestAnimationFrame ||
window.webkitRequestAnimationFrame ||
window.mozRequestAnimationFrame ||
window.oRequestAnimationFrame ||
window.msRequestAnimationFrame ||
function(callback, element) {
window.setTimeout(callback, 1000 / 60);
};
})();
return {
'renderer': renderer,
'engineContext': this
};
}
document.addEventListener("DOMContentLoaded", function() {
var editor = ace.edit("editor");
editor.setTheme("ace/theme/tomorrow");
editor.session.setMode("ace/mode/html");
editor.setAutoScrollEditorIntoView(true);
editor.setOption("maxLines", 100);
editor.setOption("fontSize", "20px");
editor.setValue('\
<scene id="wld" type="ar"></scene>\n\
<sensor id="cam_1" calibration="default"></sensor>\n\
<background id="cam_based" sensor="cam_1" placeholer="default"></background>\n\
<viewpoint id="arview" parent="default"></viewpoint>\n\
<calibration id="cal_1"></calibration>\n\n\
<Matrix id="forAobj1" x="0" y="0" z="-80"></Matrix>\n\
<Matrix id="forAobj2" x="50" y="0" z="0"></Matrix>\n\n\
<robject id="robj1" type="marker" marker-id="64" object="marker64" placeholder="#aobj4"></robject>\n\
<robject id="robj2" type="marker" marker-id="88" object="marker88" placeholder="#aobj3"></robject>\n\
<aobject id="aobj1" object="sphere" placeholder-for="#robj1" transform="#forAobj1" onclick ="default"></aobject>\n\
<aobject id="aobj2" object="cube" placeholder-for="#robj2" transform="#forAobj2" onclick ="default"></aobject>\n\
<aobject id="aobj3" object="model" placeholder-for="#robj2" model-data="./model/sj.json" transform="#forAobj2" onclick ="default"></aobject>\n\
<aobject id="aobj4" object="model" placeholder-for="#robj1" model-data="./model/miku.min.json" transform="#forAobj2" onclick ="default"></aobject>\n\n\
<MAREvent id="evt1" event="object_presence" object="#robj1" value="true"></MAREvent>\n\
<MAREvent id="evt2" event="object_presence" object="#robj2" value="true"></MAREvent>\n\
<MARBehavior id="bhv1" behavior="show" event="evt1.value" object="#aobj1"></MARBehavior>\n\
<MARBehavior id="bhv2" behavior="show" event="evt2.value" object="#aobj2"></MARBehavior>');
// 1. PARSING
var app = new MARParser(editor.getValue());
// 2. EXECUTING
var engine = MAREngine(app);
console.log(engine);
engine.renderer();
$('#eee').click(function() {
app = new MARParser(editor.getValue());
engine.engineContext.setParseContext(app);
});
});