Pārlūkot izejas kodu

Cleanup

customisations
alemart 1 gadu atpakaļ
vecāks
revīzija
e14ff1f47f
2 mainītis faili ar 0 papildinājumiem un 392 dzēšanām
  1. 0
    236
      demos/assets/aframe-with-encantar.js
  2. 0
    156
      demos/assets/three-with-encantar.js

+ 0
- 236
demos/assets/aframe-with-encantar.js Parādīt failu

@@ -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
-}

+ 0
- 156
demos/assets/three-with-encantar.js Parādīt failu

@@ -1,156 +0,0 @@
1
-/**
2
- * @file three.js 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.1' },
10
-       'three.js': { version: '147' }
11
-});
12
-
13
-/**
14
- * Use this object to create your augmented scene
15
- * @typedef {object} ARSystem
16
- * @property {Session} session AR Session
17
- * @property {Frame} frame current Frame
18
- * @property {ReferenceImage | null} referenceImage corresponds to the target being tracked (if any)
19
- * @property {THREE.Scene} scene three.js Scene
20
- * @property {THREE.Group} root a 3D object that is automatically aligned with the physical target
21
- * @property {THREE.Camera} camera a camera adjusted for AR
22
- * @property {THREE.WebGLRenderer} renderer three.js renderer
23
- */
24
-
25
-/**
26
- * Do magic to connect encantar.js to three.js
27
- * @param {() => Promise<Session> | SpeedyPromise<Session>} startARSession
28
- * @param {(ar: ARSystem) => void} [animateVirtualScene] animation callback
29
- * @param {(ar: ARSystem) => void | Promise<void> | SpeedyPromise<Session>} [initializeVirtualScene] initialization callback
30
- * @returns {Promise<ARSystem> | SpeedyPromise<ARSystem>}
31
- */
32
-function encantar(startARSession, animateVirtualScene, initializeVirtualScene)
33
-{
34
-    const ar = /** @type {ARSystem} */ ({
35
-        session: null,
36
-        frame: null,
37
-        referenceImage: null,
38
-        scene: null,
39
-        root: null,
40
-        camera: null,
41
-        renderer: null,
42
-    });
43
-
44
-    function mix(frame)
45
-    {
46
-        ar.root.visible = false;
47
-        ar.referenceImage = null;
48
-
49
-        for(const result of frame.results) {
50
-            if(result.tracker.type == 'image-tracker') {
51
-                if(result.trackables.length > 0) {
52
-                    const trackable = result.trackables[0];
53
-                    const projectionMatrix = result.viewer.view.projectionMatrix;
54
-                    const viewMatrixInverse = result.viewer.pose.transform.matrix;
55
-                    const modelMatrix = trackable.pose.transform.matrix;
56
-
57
-                    ar.root.visible = true;
58
-                    ar.referenceImage = trackable.referenceImage;
59
-
60
-                    align(projectionMatrix, viewMatrixInverse, modelMatrix);
61
-                }
62
-            }
63
-        }
64
-    }
65
-
66
-    function align(projectionMatrix, viewMatrixInverse, modelMatrix)
67
-    {
68
-        ar.camera.projectionMatrix.fromArray(projectionMatrix.read());
69
-        ar.camera.projectionMatrixInverse.copy(ar.camera.projectionMatrix).invert();
70
-        ar.camera.matrix.fromArray(viewMatrixInverse.read());
71
-        ar.camera.updateMatrixWorld(true);
72
-        ar.root.matrix.fromArray(modelMatrix.read());
73
-        ar.root.updateMatrixWorld(true);
74
-    }
75
-
76
-    function animate(time, frame)
77
-    {
78
-        ar.frame = frame;
79
-        mix(ar.frame);
80
-
81
-        animateVirtualScene.call(undefined, ar);
82
-
83
-        ar.renderer.render(ar.scene, ar.camera);
84
-        ar.session.requestAnimationFrame(animate);
85
-    }
86
-
87
-
88
-
89
-
90
-    if(typeof animateVirtualScene !== 'function')
91
-        animateVirtualScene = (ar => void 0);
92
-
93
-    if(typeof initializeVirtualScene !== 'function')
94
-        initializeVirtualScene = (ar => void 0);
95
-
96
-    return startARSession().then(session => {
97
-
98
-        ar.session = session;
99
-
100
-        ar.scene = new THREE.Scene();
101
-        ar.camera = new THREE.PerspectiveCamera();
102
-        ar.camera.matrixAutoUpdate = false;
103
-
104
-        ar.root = new THREE.Group();
105
-        ar.root.matrixAutoUpdate = false;
106
-        ar.scene.add(ar.root);
107
-
108
-        ar.renderer = new THREE.WebGLRenderer({
109
-            canvas: ar.session.viewport.canvas,
110
-            alpha: true,
111
-        });
112
-
113
-        ar.session.addEventListener('end', event => {
114
-            ar.root.visible = false;
115
-            ar.frame = null;
116
-            ar.referenceImage = null;
117
-        });
118
-
119
-        ar.session.viewport.addEventListener('resize', event => {
120
-            const size = ar.session.viewport.virtualSize;
121
-            ar.renderer.setPixelRatio(1.0);
122
-            ar.renderer.setSize(size.width, size.height, false);
123
-        });
124
-
125
-        let init = initializeVirtualScene.call(undefined, ar);
126
-        if(!(typeof init === 'object' && 'then' in init))
127
-            init = Promise.resolve();
128
-
129
-        return init.then(() => {
130
-            ar.session.requestAnimationFrame(animate);
131
-            return ar;
132
-        });
133
-
134
-    }).catch(error => {
135
-        
136
-        console.error(error);
137
-        alert(error.message);
138
-        return ar;
139
-   
140
-    });
141
-}
142
-
143
-/**
144
- * Version check
145
- * @param {object} json
146
- */
147
-function __THIS_PLUGIN_HAS_BEEN_TESTED_WITH__(json)
148
-{
149
-    window.addEventListener('load', () => {
150
-    try { AR, __THREE__;
151
-    const versionOf = { 'encantar.js': AR.version.replace(/-.*$/, ''), 'three.js': __THREE__ };
152
-    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;
153
-    for(const [x, expected] of Object.entries(json)) check(x, expected.version, versionOf[x]);
154
-    } catch(e) { alert(e.message); }
155
-    });
156
-}

Notiek ielāde…
Atcelt
Saglabāt