|
@@ -1,236 +0,0 @@
|
1
|
|
-/**
|
2
|
|
- * @file aframe plugin for encantar.js
|
3
|
|
- * @author Alexandre Martins (https://github.com/alemart)
|
4
|
|
- * @license LGPL-3.0-or-later
|
5
|
|
- */
|
6
|
|
-
|
7
|
|
-/* Usage of the indicated versions is encouraged */
|
8
|
|
-__THIS_PLUGIN_HAS_BEEN_TESTED_WITH__({
|
9
|
|
- 'encantar.js': { version: '0.2.0' },
|
10
|
|
- 'aframe': { version: '1.4.2' }
|
11
|
|
-});
|
12
|
|
-
|
13
|
|
-/**
|
14
|
|
- * Do magic to connect encantar.js to aframe
|
15
|
|
- * @param {(canvas: HTMLCanvasElement) => Promise<Session> | SpeedyPromise<Session>} startARSession
|
16
|
|
- */
|
17
|
|
-function encantar(startARSession)
|
18
|
|
-{
|
19
|
|
- AFRAME.registerSystem('ar-system', {
|
20
|
|
- init()
|
21
|
|
- {
|
22
|
|
- this.state = {
|
23
|
|
- isTracking: false,
|
24
|
|
- frame: null,
|
25
|
|
- referenceImage: null,
|
26
|
|
- projectionMatrix: null,
|
27
|
|
- viewMatrixInverse: null,
|
28
|
|
- modelMatrix: null,
|
29
|
|
- };
|
30
|
|
- this.session = null;
|
31
|
|
-
|
32
|
|
- return startARSession(this.el.canvas).then(session => {
|
33
|
|
-
|
34
|
|
- this.session = session;
|
35
|
|
-
|
36
|
|
- session.addEventListener('end', event => {
|
37
|
|
- this.state.isTracking = false;
|
38
|
|
- });
|
39
|
|
- session.viewport.addEventListener('resize', event => {
|
40
|
|
- this.updateRenderer(session.viewport.virtualSize);
|
41
|
|
- });
|
42
|
|
-
|
43
|
|
- if(session.viewport.canvas !== this.el.canvas) {
|
44
|
|
- session.end();
|
45
|
|
- throw new Error('Invalid AFRAME <canvas>');
|
46
|
|
- }
|
47
|
|
-
|
48
|
|
- const animate = (time, frame) => {
|
49
|
|
- this.updateState(frame);
|
50
|
|
- this.renderVirtualScene();
|
51
|
|
- session.requestAnimationFrame(animate);
|
52
|
|
- };
|
53
|
|
- session.requestAnimationFrame(animate);
|
54
|
|
-
|
55
|
|
- }).catch(error => {
|
56
|
|
-
|
57
|
|
- console.error(error);
|
58
|
|
- alert(error.message);
|
59
|
|
-
|
60
|
|
- });
|
61
|
|
- },
|
62
|
|
-
|
63
|
|
- updateState(frame)
|
64
|
|
- {
|
65
|
|
- const wasTracking = this.state.isTracking;
|
66
|
|
-
|
67
|
|
- this.state.frame = frame;
|
68
|
|
- this.state.isTracking = false;
|
69
|
|
- this.state.referenceImage = null;
|
70
|
|
-
|
71
|
|
- for(const result of frame.results) {
|
72
|
|
- if(result.tracker.type == 'image-tracker') {
|
73
|
|
- if(result.trackables.length > 0) {
|
74
|
|
- const trackable = result.trackables[0];
|
75
|
|
- this.state.projectionMatrix = result.viewer.view.projectionMatrix;
|
76
|
|
- this.state.viewMatrixInverse = result.viewer.pose.transform.matrix;
|
77
|
|
- this.state.modelMatrix = trackable.pose.transform.matrix;
|
78
|
|
- this.state.referenceImage = trackable.referenceImage;
|
79
|
|
- this.state.isTracking = true;
|
80
|
|
- }
|
81
|
|
- }
|
82
|
|
- }
|
83
|
|
-
|
84
|
|
- if(this.state.isTracking && !wasTracking)
|
85
|
|
- this.updateRenderer(frame.session.viewport.virtualSize);
|
86
|
|
- },
|
87
|
|
-
|
88
|
|
- updateRenderer(size)
|
89
|
|
- {
|
90
|
|
- const renderer = this.el.renderer;
|
91
|
|
- const resize = () => {
|
92
|
|
- renderer.setPixelRatio(1.0);
|
93
|
|
- renderer.setSize(size.width, size.height, false);
|
94
|
|
- };
|
95
|
|
-
|
96
|
|
- resize();
|
97
|
|
- setTimeout(resize, 200); // internals of AFRAME (a-scene.js)
|
98
|
|
- },
|
99
|
|
-
|
100
|
|
- renderVirtualScene()
|
101
|
|
- {
|
102
|
|
- const scene = this.el;
|
103
|
|
- if(!scene.camera || !scene.object3D)
|
104
|
|
- return;
|
105
|
|
-
|
106
|
|
- scene.delta = scene.clock.getDelta() * 1000;
|
107
|
|
- scene.time = scene.clock.elapsedTime * 1000;
|
108
|
|
- if(scene.isPlaying)
|
109
|
|
- scene.tick(scene.time, scene.delta);
|
110
|
|
-
|
111
|
|
- scene.object3D.background = null;
|
112
|
|
- scene.renderer.render(scene.object3D, scene.camera);
|
113
|
|
- scene.renderer.setAnimationLoop(null);
|
114
|
|
- },
|
115
|
|
- });
|
116
|
|
-
|
117
|
|
- AFRAME.registerComponent('ar-root', {
|
118
|
|
- schema: {
|
119
|
|
- 'image-target': { type: 'string', default: '' },
|
120
|
|
- },
|
121
|
|
-
|
122
|
|
- init()
|
123
|
|
- {
|
124
|
|
- this.arSystem = this.el.sceneEl.systems['ar-system'];
|
125
|
|
-
|
126
|
|
- this.el.object3D.matrixAutoUpdate = false;
|
127
|
|
- this.el.object3D.visible = false;
|
128
|
|
- },
|
129
|
|
-
|
130
|
|
- remove()
|
131
|
|
- {
|
132
|
|
- const session = this.arSystem.session;
|
133
|
|
- session.end();
|
134
|
|
- },
|
135
|
|
-
|
136
|
|
- tick()
|
137
|
|
- {
|
138
|
|
- const ANY = '', target = this.data['image-target'];
|
139
|
|
- const state = this.arSystem.state;
|
140
|
|
-
|
141
|
|
- if(state.isTracking && (target === ANY || target === state.referenceImage.name)) {
|
142
|
|
- this.alignVirtualScene(state.modelMatrix);
|
143
|
|
- this.el.object3D.visible = true;
|
144
|
|
- }
|
145
|
|
- else
|
146
|
|
- this.el.object3D.visible = false;
|
147
|
|
- },
|
148
|
|
-
|
149
|
|
- alignVirtualScene(modelMatrix)
|
150
|
|
- {
|
151
|
|
- const arRoot = this.el.object3D;
|
152
|
|
-
|
153
|
|
- arRoot.matrix.fromArray(modelMatrix.read());
|
154
|
|
- arRoot.updateMatrixWorld(true);
|
155
|
|
- }
|
156
|
|
- });
|
157
|
|
-
|
158
|
|
- AFRAME.registerPrimitive('ar-root', AFRAME.utils.extendDeep({}, AFRAME.primitives.getMeshMixin(), {
|
159
|
|
- defaultComponents: {
|
160
|
|
- 'ar-root': {}
|
161
|
|
- },
|
162
|
|
- mappings: {
|
163
|
|
- 'image-target': 'ar-root.image-target'
|
164
|
|
- }
|
165
|
|
- }));
|
166
|
|
-
|
167
|
|
- AFRAME.registerComponent('ar-camera', {
|
168
|
|
- init()
|
169
|
|
- {
|
170
|
|
- this.arSystem = this.el.sceneEl.systems['ar-system'];
|
171
|
|
- this.arCamera = this.el.getObject3D('camera');
|
172
|
|
-
|
173
|
|
- this.arCamera.matrixAutoUpdate = false;
|
174
|
|
-
|
175
|
|
- this.el.setAttribute('camera', { active: true });
|
176
|
|
- this.el.setAttribute('wasd-controls', { enabled: false });
|
177
|
|
- this.el.setAttribute('look-controls', { enabled: false });
|
178
|
|
- this.el.setAttribute('position', { x: 0, y: 0, z: 0 }); // AFRAME sets y = 1.6m for VR
|
179
|
|
- },
|
180
|
|
-
|
181
|
|
- tick()
|
182
|
|
- {
|
183
|
|
- const state = this.arSystem.state;
|
184
|
|
-
|
185
|
|
- if(state.isTracking)
|
186
|
|
- this.updateCamera(state.projectionMatrix, state.viewMatrixInverse);
|
187
|
|
- },
|
188
|
|
-
|
189
|
|
- updateCamera(projectionMatrix, viewMatrixInverse)
|
190
|
|
- {
|
191
|
|
- const arCamera = this.arCamera;
|
192
|
|
-
|
193
|
|
- arCamera.projectionMatrix.fromArray(projectionMatrix.read());
|
194
|
|
- arCamera.projectionMatrixInverse.copy(arCamera.projectionMatrix).invert();
|
195
|
|
- arCamera.matrix.fromArray(viewMatrixInverse.read());
|
196
|
|
- arCamera.updateMatrixWorld(true);
|
197
|
|
- }
|
198
|
|
- });
|
199
|
|
-
|
200
|
|
- /*
|
201
|
|
- // AFRAME won't catch this in setupInitialCamera()
|
202
|
|
- AFRAME.registerPrimitive('ar-camera', AFRAME.utils.extendDeep({}, AFRAME.primitives.getMeshMixin(), {
|
203
|
|
- defaultComponents: {
|
204
|
|
- 'ar-camera': {}
|
205
|
|
- }
|
206
|
|
- }));
|
207
|
|
- */
|
208
|
|
-
|
209
|
|
- AFRAME.registerComponent('ar-scene', {
|
210
|
|
- init()
|
211
|
|
- {
|
212
|
|
- this.el.setAttribute('vr-mode-ui', { enabled: false });
|
213
|
|
- this.el.setAttribute('embedded', true);
|
214
|
|
- this.el.setAttribute('renderer', { alpha: true });
|
215
|
|
- }
|
216
|
|
- });
|
217
|
|
-};
|
218
|
|
-
|
219
|
|
-// Start automatically
|
220
|
|
-if(typeof startARSession === 'function')
|
221
|
|
- encantar(startARSession);
|
222
|
|
-else
|
223
|
|
- alert('Missing startARSession');
|
224
|
|
-
|
225
|
|
-/**
|
226
|
|
- * Version check
|
227
|
|
- * @param {object} json
|
228
|
|
- */
|
229
|
|
-function __THIS_PLUGIN_HAS_BEEN_TESTED_WITH__(json)
|
230
|
|
-{
|
231
|
|
- try { AR, AFRAME;
|
232
|
|
- const versionOf = { 'encantar.js': AR.version.replace(/-.*$/, ''), 'aframe': AFRAME.version };
|
233
|
|
- const check = (x,v,w) => v !== w ? console.warn(`\n\n\nWARNING\n\nThis plugin has been tested with ${x} version ${v}. The version in use is ${w}. Usage of ${x} version ${v} is recommended instead.\n\n\n`) : void 0;
|
234
|
|
- for(const [x, expected] of Object.entries(json)) check(x, expected.version, versionOf[x]);
|
235
|
|
- } catch(e) { alert(e.message); }
|
236
|
|
-}
|