-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsystem-arjs.js
234 lines (203 loc) · 8.66 KB
/
system-arjs.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
AFRAME.registerSystem('arjs', {
schema: {
trackingMethod: {
type: 'string',
default: 'best',
},
debugUIEnabled: {
type: 'boolean',
default: true,
},
areaLearningButton: {
type: 'boolean',
default: true,
},
performanceProfile: {
type: 'string',
default: 'default',
},
// old parameters
debug: {
type: 'boolean',
default: false
},
detectionMode: {
type: 'string',
default: '',
},
matrixCodeType: {
type: 'string',
default: '',
},
patternRatio: {
type: 'number',
default: -1,
},
labelingMode: {
type: 'string',
default: '',
},
cameraParametersUrl: {
type: 'string',
default: '',
},
maxDetectionRate: {
type: 'number',
default: -1
},
sourceType: {
type: 'string',
default: '',
},
sourceUrl: {
type: 'string',
default: '',
},
sourceWidth: {
type: 'number',
default: -1
},
sourceHeight: {
type: 'number',
default: -1
},
deviceId: {
type: 'string',
default: ''
},
displayWidth: {
type: 'number',
default: -1
},
displayHeight: {
type: 'number',
default: -1
},
canvasWidth: {
type: 'number',
default: -1
},
canvasHeight: {
type: 'number',
default: -1
},
},
//////////////////////////////////////////////////////////////////////////////
// Code Separator
//////////////////////////////////////////////////////////////////////////////
init: function () {
var _this = this
//////////////////////////////////////////////////////////////////////////////
// setup arProfile
//////////////////////////////////////////////////////////////////////////////
var arProfile = this._arProfile = new ARjs.Profile()
.trackingMethod(this.data.trackingMethod)
.performance(this.data.performanceProfile)
.defaultMarker()
//////////////////////////////////////////////////////////////////////////////
// honor this.data and setup arProfile with it
//////////////////////////////////////////////////////////////////////////////
// honor this.data and push what has been modified into arProfile
if (this.data.debug !== false) arProfile.contextParameters.debug = this.data.debug
if (this.data.detectionMode !== '') arProfile.contextParameters.detectionMode = this.data.detectionMode
if (this.data.matrixCodeType !== '') arProfile.contextParameters.matrixCodeType = this.data.matrixCodeType
if (this.data.patternRatio !== -1) arProfile.contextParameters.patternRatio = this.data.patternRatio
if (this.data.labelingMode !== '') arProfile.contextParameters.labelingMode = this.data.labelingMode
if (this.data.cameraParametersUrl !== '') arProfile.contextParameters.cameraParametersUrl = this.data.cameraParametersUrl
if (this.data.maxDetectionRate !== -1) arProfile.contextParameters.maxDetectionRate = this.data.maxDetectionRate
if (this.data.canvasWidth !== -1) arProfile.contextParameters.canvasWidth = this.data.canvasWidth
if (this.data.canvasHeight !== -1) arProfile.contextParameters.canvasHeight = this.data.canvasHeight
if (this.data.sourceType !== '') arProfile.sourceParameters.sourceType = this.data.sourceType
if (this.data.sourceUrl !== '') arProfile.sourceParameters.sourceUrl = this.data.sourceUrl
if (this.data.sourceWidth !== -1) arProfile.sourceParameters.sourceWidth = this.data.sourceWidth
if (this.data.sourceHeight !== -1) arProfile.sourceParameters.sourceHeight = this.data.sourceHeight
if (this.data.deviceId !== '') arProfile.sourceParameters.deviceId = this.data.deviceId
if (this.data.displayWidth !== -1) arProfile.sourceParameters.displayWidth = this.data.displayWidth
if (this.data.displayHeight !== -1) arProfile.sourceParameters.displayHeight = this.data.displayHeight
arProfile.checkIfValid()
//////////////////////////////////////////////////////////////////////////////
// Code Separator
//////////////////////////////////////////////////////////////////////////////
this._arSession = null
_this.isReady = false
_this.needsOverride = true
// wait until the renderer is isReady
this.el.sceneEl.addEventListener('renderstart', function () {
var scene = _this.el.sceneEl.object3D
var camera = _this.el.sceneEl.camera
var renderer = _this.el.sceneEl.renderer
//////////////////////////////////////////////////////////////////////////////
// build ARjs.Session
//////////////////////////////////////////////////////////////////////////////
var arSession = _this._arSession = new ARjs.Session({
scene: scene,
renderer: renderer,
camera: camera,
sourceParameters: arProfile.sourceParameters,
contextParameters: arProfile.contextParameters
})
//////////////////////////////////////////////////////////////////////////////
// Code Separator
//////////////////////////////////////////////////////////////////////////////
_this.isReady = true
//////////////////////////////////////////////////////////////////////////////
// awful resize trick
//////////////////////////////////////////////////////////////////////////////
// KLUDGE
window.addEventListener('resize', onResize)
function onResize() {
var arSource = _this._arSession.arSource
// ugly kludge to get resize on aframe... not even sure it works
if (arProfile.contextParameters.trackingBackend !== 'tango') {
arSource.copyElementSizeTo(document.body)
}
// fixing a-frame css
var buttonElement = document.querySelector('.a-enter-vr')
if (buttonElement) {
buttonElement.style.position = 'fixed'
}
}
//////////////////////////////////////////////////////////////////////////////
// honor .debugUIEnabled
//////////////////////////////////////////////////////////////////////////////
if (_this.data.debugUIEnabled) initDebugUI()
function initDebugUI() {
// get or create containerElement
var containerElement = document.querySelector('#arjsDebugUIContainer')
if (containerElement === null) {
containerElement = document.createElement('div')
containerElement.id = 'arjsDebugUIContainer'
containerElement.setAttribute('style', 'position: fixed; bottom: 10px; width:100%; text-align: center; z-index: 1;color: grey;')
document.body.appendChild(containerElement)
}
// create sessionDebugUI
var sessionDebugUI = new ARjs.SessionDebugUI(arSession)
containerElement.appendChild(sessionDebugUI.domElement)
}
})
//////////////////////////////////////////////////////////////////////////////
// Code Separator
//////////////////////////////////////////////////////////////////////////////
// TODO this is crappy - code an exponential backoff - max 1 seconds
// KLUDGE: kludge to write a 'resize' event
var startedAt = Date.now()
var timerId = setInterval(function () {
if (Date.now() - startedAt > 10000 * 1000) {
clearInterval(timerId)
return
}
// onResize()
window.dispatchEvent(new Event('resize'));
}, 1000 / 30)
},
tick: function (now, delta) {
var _this = this
// skip it if not yet isInitialised
if (this.isReady === false) return
var arSession = this._arSession
// update arSession
this._arSession.update()
// copy projection matrix to camera
this._arSession.onResize()
},
})