forked from github/game-off-2013
-
Notifications
You must be signed in to change notification settings - Fork 1
/
ardetector.js
110 lines (96 loc) · 3.27 KB
/
ardetector.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
"use strict";
define(['settings','JSARToolKit.min'],function(settings) {
var create = function(sourceCanvas) {
var JSARRaster = new NyARRgbRaster_Canvas2D(sourceCanvas.getElement());
var JSARParameters = new FLARParam(sourceCanvas.getWidth(), sourceCanvas.getHeight());
var JSARDetector = new FLARMultiIdMarkerDetector(JSARParameters, 120);
JSARDetector.setContinueMode(true);
var getMarkerNumber = function(idx) {
var data = JSARDetector.getIdMarkerData(idx);
if (data.packetLength > 4) {
return -1;
}
var result=0;
for (var i = 0; i < data.packetLength; i++ ) {
result = (result << 8) | data.getPacketData(i);
}
return result;
};
var getTransformMatrix = function(idx) {
var mat = new NyARTransMatResult();
JSARDetector.getTransformMatrix(idx, mat);
var cm = new Float32Array(16);
cm[0] = mat.m00;
cm[1] = -mat.m10;
cm[2] = mat.m20;
cm[3] = 0;
cm[4] = mat.m01;
cm[5] = -mat.m11;
cm[6] = mat.m21;
cm[7] = 0;
cm[8] = -mat.m02;
cm[9] = mat.m12;
cm[10] = -mat.m22;
cm[11] = 0;
cm[12] = mat.m03;
cm[13] = -mat.m13;
cm[14] = mat.m23;
cm[15] = 1;
return cm;
};
var getCameraMatrix = function(zNear, zFar) {
var result = new Float32Array(16);
JSARParameters.copyCameraMatrix(result, zNear, zFar);
return result;
};
var newMarker = function(id, matrix) {
return {
id: id,
matrix: matrix,
age: settings.arMarkerPersist,
};
};
var markers = {};
var detect = function( onCreate, onUpdate, onDestroy ) {
if( !sourceCanvas.getElement().changed ) {
return;
}
var markerCount = JSARDetector.detectMarkerLite(JSARRaster, settings.arThreshold);
var marker;
for( var index = 0; index < markerCount; index++ ) {
var id = getMarkerNumber(index);
marker = markers[id];
if( marker === undefined ) {
marker = newMarker(id, getTransformMatrix(index));
markers[id] = marker;
onCreate( marker );
}
else {
marker.matrix = getTransformMatrix(index);
marker.age = settings.arMarkerPersist;
onUpdate( marker );
}
}
for( var id in markers ) {
marker = markers[id];
if( marker ) {
if( marker.age-- === 0 ) {
onDestroy( marker );
delete markers[id];
}
}
}
};
function reset() {
markers = {};
}
return {
detect: detect,
reset: reset,
getCameraMatrix: getCameraMatrix,
};
};
return {
create: create,
};
});