forked from AR-js-org/AR.js
-
Notifications
You must be signed in to change notification settings - Fork 0
/
component-anchor.js
271 lines (244 loc) · 10.6 KB
/
component-anchor.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
//////////////////////////////////////////////////////////////////////////////
// arjs-anchor
//////////////////////////////////////////////////////////////////////////////
AFRAME.registerComponent('arjs-anchor', {
dependencies: ['arjs', 'artoolkit'],
schema: {
preset: {
type: 'string',
},
markerhelpers: { // IIF preset === 'area'
type: 'boolean',
default: false,
},
// controls parameters
size: {
type: 'number',
default: 1
},
type: {
type: 'string',
},
patternUrl: {
type: 'string',
},
barcodeValue: {
type: 'number'
},
changeMatrixMode: {
type: 'string',
default: 'modelViewMatrix',
},
minConfidence: {
type: 'number',
default: 0.6,
},
smooth: {
type: 'boolean',
default: false,
},
smoothCount: {
type: 'number',
default: 5,
},
smoothTolerance: {
type: 'number',
default: 0.01,
},
smoothThreshold: {
type: 'number',
default: 2,
},
},
init: function () {
var _this = this
// get arjsSystem
var arjsSystem = this.el.sceneEl.systems.arjs || this.el.sceneEl.systems.artoolkit
//////////////////////////////////////////////////////////////////////////////
// Code Separator
//////////////////////////////////////////////////////////////////////////////
_this.isReady = false
_this._arAnchor = null
// honor object visibility
if (_this.data.changeMatrixMode === 'modelViewMatrix') {
_this.el.object3D.visible = false
} else if (_this.data.changeMatrixMode === 'cameraTransformMatrix') {
_this.el.sceneEl.object3D.visible = false
} else console.assert(false)
// trick to wait until arjsSystem is isReady
var startedAt = Date.now()
var timerId = setInterval(function () {
// wait until the system is isReady
if (arjsSystem.isReady === false) return
clearInterval(timerId)
//////////////////////////////////////////////////////////////////////////////
// update arProfile
//////////////////////////////////////////////////////////////////////////////
var arProfile = arjsSystem._arProfile
// arProfile.changeMatrixMode('modelViewMatrix')
arProfile.changeMatrixMode(_this.data.changeMatrixMode)
// honor this.data.preset
var markerParameters = Object.assign({}, arProfile.defaultMarkerParameters)
if (_this.data.preset === 'hiro') {
markerParameters.type = 'pattern'
markerParameters.patternUrl = THREEx.ArToolkitContext.baseURL + 'examples/marker-training/examples/pattern-files/pattern-hiro.patt'
markerParameters.markersAreaEnabled = false
} else if (_this.data.preset === 'kanji') {
markerParameters.type = 'pattern'
markerParameters.patternUrl = THREEx.ArToolkitContext.baseURL + 'examples/marker-training/examples/pattern-files/pattern-kanji.patt'
markerParameters.markersAreaEnabled = false
} else if (_this.data.preset === 'area') {
markerParameters.type = 'barcode'
markerParameters.barcodeValue = 1001
markerParameters.markersAreaEnabled = true
} else if (_this.data.type === 'barcode') {
markerParameters = {
type: _this.data.type,
changeMatrixMode: 'modelViewMatrix',
barcodeValue: _this.data.barcodeValue,
markersAreaEnabled: false
}
} else if (_this.data.type === 'pattern') {
markerParameters.type = _this.data.type
markerParameters.patternUrl = _this.data.patternUrl;
markerParameters.markersAreaEnabled = false
}
markerParameters.smooth = _this.data.smooth;
markerParameters.smoothCount = _this.data.smoothCount;
markerParameters.smoothTolerance = _this.data.smoothTolerance;
markerParameters.smoothThreshold = _this.data.smoothThreshold;
//////////////////////////////////////////////////////////////////////////////
// create arAnchor
//////////////////////////////////////////////////////////////////////////////
var arSession = arjsSystem._arSession
var arAnchor = _this._arAnchor = new ARjs.Anchor(arSession, markerParameters)
// it is now considered isReady
_this.isReady = true
//////////////////////////////////////////////////////////////////////////////
// honor .debugUIEnabled
//////////////////////////////////////////////////////////////////////////////
if (arjsSystem.data.debugUIEnabled) {
// 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 anchorDebugUI
var anchorDebugUI = new ARjs.AnchorDebugUI(arAnchor)
containerElement.appendChild(anchorDebugUI.domElement)
}
}, 1000 / 60)
},
remove: function () {
},
update: function () {
},
tick: function () {
var _this = this
// if not yet isReady, do nothing
if (this.isReady === false) return
//////////////////////////////////////////////////////////////////////////////
// update arAnchor
//////////////////////////////////////////////////////////////////////////////
var arjsSystem = this.el.sceneEl.systems.arjs || this.el.sceneEl.systems.artoolkit
this._arAnchor.update()
//////////////////////////////////////////////////////////////////////////////
// honor pose
//////////////////////////////////////////////////////////////////////////////
var arWorldRoot = this._arAnchor.object3d
arWorldRoot.updateMatrixWorld(true)
arWorldRoot.matrixWorld.decompose(this.el.object3D.position, this.el.object3D.quaternion, this.el.object3D.scale)
//////////////////////////////////////////////////////////////////////////////
// honor visibility
//////////////////////////////////////////////////////////////////////////////
if (_this._arAnchor.parameters.changeMatrixMode === 'modelViewMatrix') {
var wasVisible = _this.el.object3D.visible
_this.el.object3D.visible = this._arAnchor.object3d.visible
} else if (_this._arAnchor.parameters.changeMatrixMode === 'cameraTransformMatrix') {
var wasVisible = _this.el.sceneEl.object3D.visible
_this.el.sceneEl.object3D.visible = this._arAnchor.object3d.visible
} else console.assert(false)
// emit markerFound markerLost
if (_this._arAnchor.object3d.visible === true && wasVisible === false) {
_this.el.emit('markerFound')
} else if (_this._arAnchor.object3d.visible === false && wasVisible === true) {
_this.el.emit('markerLost')
}
}
})
//////////////////////////////////////////////////////////////////////////////
// define some primitives shortcuts
//////////////////////////////////////////////////////////////////////////////
AFRAME.registerPrimitive('a-anchor', AFRAME.utils.extendDeep({}, AFRAME.primitives.getMeshMixin(), {
defaultComponents: {
'arjs-anchor': {},
'arjs-hit-testing': {},
},
mappings: {
'type': 'arjs-anchor.type',
'size': 'arjs-anchor.size',
'url': 'arjs-anchor.patternUrl',
'value': 'arjs-anchor.barcodeValue',
'preset': 'arjs-anchor.preset',
'min-confidence': 'arjs-anchor.minConfidence',
'marker-helpers': 'arjs-anchor.markerhelpers',
'smooth': 'arjs-anchor.smooth',
'smooth-count': 'arjs-anchor.smoothCount',
'smooth-tolerance': 'arjs-anchor.smoothTolerance',
'smooth-threshold': 'arjs-anchor.smoothThreshold',
'hit-testing-render-debug': 'arjs-hit-testing.renderDebug',
'hit-testing-enabled': 'arjs-hit-testing.enabled',
}
}))
AFRAME.registerPrimitive('a-camera-static', AFRAME.utils.extendDeep({}, AFRAME.primitives.getMeshMixin(), {
defaultComponents: {
'camera': {},
},
mappings: {
}
}))
//////////////////////////////////////////////////////////////////////////////
// backward compatibility
//////////////////////////////////////////////////////////////////////////////
// FIXME
AFRAME.registerPrimitive('a-marker', AFRAME.utils.extendDeep({}, AFRAME.primitives.getMeshMixin(), {
defaultComponents: {
'arjs-anchor': {},
'arjs-hit-testing': {},
},
mappings: {
'type': 'arjs-anchor.type',
'size': 'arjs-anchor.size',
'url': 'arjs-anchor.patternUrl',
'value': 'arjs-anchor.barcodeValue',
'preset': 'arjs-anchor.preset',
'min-confidence': 'arjs-anchor.minConfidence',
'marker-helpers': 'arjs-anchor.markerhelpers',
'smooth': 'arjs-anchor.smooth',
'smooth-count': 'arjs-anchor.smoothCount',
'smooth-tolerance': 'arjs-anchor.smoothTolerance',
'smooth-threshold': 'arjs-anchor.smoothThreshold',
'hit-testing-render-debug': 'arjs-hit-testing.renderDebug',
'hit-testing-enabled': 'arjs-hit-testing.enabled',
}
}))
AFRAME.registerPrimitive('a-marker-camera', AFRAME.utils.extendDeep({}, AFRAME.primitives.getMeshMixin(), {
defaultComponents: {
'arjs-anchor': {
changeMatrixMode: 'cameraTransformMatrix'
},
'camera': {},
},
mappings: {
'type': 'arjs-anchor.type',
'size': 'arjs-anchor.size',
'url': 'arjs-anchor.patternUrl',
'value': 'arjs-anchor.barcodeValue',
'preset': 'arjs-anchor.preset',
'min-confidence': 'arjs-anchor.minConfidence',
'marker-helpers': 'arjs-anchor.markerhelpers',
}
}))