Переглянути джерело

Update hello-world demo

customisations
alemart 11 місяці тому
джерело
коміт
4e1665e16c

+ 2
- 2
demos/hello-world/README.html Переглянути файл

@@ -47,8 +47,8 @@
47 47
         </style>
48 48
     </head>
49 49
     <body>
50
-        <h1>encatAR.js template: Hello, world!</h1>
51
-        <p>Scan the QR code to launch the web-based Augmented Reality experience. Next, scan <a href="../assets/my-reference-image.webp" target="_blank">this cartoon</a>.</p>
50
+        <h1>encantAR.js WebAR template</h1>
51
+        <p>Scan the QR code to launch the web-based Augmented Reality experience. Next, scan <a href="../assets/mage.webp" target="_blank">this cartoon</a> or <a href="../assets/cat.webp" target="_blank">this picture</a>.</p>
52 52
 
53 53
         <h2>Menu</h2>
54 54
         <ul>

+ 140
- 60
demos/hello-world/demo.js Переглянути файл

@@ -1,67 +1,147 @@
1
-window.addEventListener('load', async function() {
2
-    try {
3
-        const session = await startARSession();
1
+/**
2
+ * Augmented Reality template using encantar.js
3
+ * @author Alexandre Martins <alemartf(at)gmail.com> (https://github.com/alemart/encantar-js)
4
+ */
4 5
 
5
-        function animate(time, frame)
6
-        {
7
-            session.requestAnimationFrame(animate);
8
-        }
6
+(function() {
9 7
 
10
-        session.requestAnimationFrame(animate);
11
-    }
12
-    catch(error) {
13
-        alert(error.message);
8
+/**
9
+ * Start the AR session
10
+ * @returns {Promise<Session>}
11
+ */
12
+async function startARSession()
13
+{
14
+    if(!AR.isSupported()) {
15
+        throw new Error(
16
+            'This device is not compatible with this AR experience.\n\n' +
17
+            'User agent: ' + navigator.userAgent
18
+        );
14 19
     }
15 20
 
16
-    async function startARSession()
21
+    const tracker = AR.Tracker.ImageTracker();
22
+    await tracker.database.add([
17 23
     {
18
-        if(!AR.isSupported()) {
19
-            throw new Error(
20
-                'This device is not compatible with this AR experience.\n\n' +
21
-                'User agent: ' + navigator.userAgent
22
-            );
23
-        }
24
+        name: 'mage',
25
+        image: document.getElementById('mage')
26
+    },
27
+    {
28
+        name: 'cat',
29
+        image: document.getElementById('cat')
30
+    }
31
+    ]);
32
+
33
+    const viewport = AR.Viewport({
34
+        container: document.getElementById('ar-viewport'),
35
+        hudContainer: document.getElementById('ar-hud')
36
+    });
37
+
38
+    const video = document.getElementById('my-video');
39
+    const useWebcam = (video === null);
40
+    const source = useWebcam ? AR.Source.Camera() : AR.Source.Video(video);
41
+
42
+    const session = await AR.startSession({
43
+        mode: 'immersive',
44
+        viewport: viewport,
45
+        trackers: [ tracker ],
46
+        sources: [ source ],
47
+        stats: true,
48
+        gizmos: true,
49
+    });
50
+
51
+    const scan = document.getElementById('scan');
52
+
53
+    tracker.addEventListener('targetfound', event => {
54
+        if(scan)
55
+            scan.hidden = true;
56
+
57
+        console.log('Target found: ' + event.referenceImage.name);
58
+    });
59
+
60
+    tracker.addEventListener('targetlost', event => {
61
+        if(scan)
62
+            scan.hidden = false;
63
+
64
+        console.log('Target lost: ' + event.referenceImage.name);
65
+    });
66
+
67
+    return session;
68
+}
69
+
70
+/**
71
+ * Animation loop
72
+ * @param {number} time
73
+ * @param {Frame} frame
74
+ * @returns {void}
75
+ */
76
+function animate(time, frame)
77
+{
78
+    const session = frame.session;
79
+    const deltaTime = session.time.delta; // given in seconds
24 80
 
25
-        //AR.Settings.powerPreference = 'low-power';
26
-
27
-        const tracker = AR.Tracker.ImageTracker();
28
-        await tracker.database.add([{
29
-            name: 'my-reference-image',
30
-            image: document.getElementById('my-reference-image')
31
-        }]);
32
-
33
-        const viewport = AR.Viewport({
34
-            container: document.getElementById('ar-viewport'),
35
-            hudContainer: document.getElementById('ar-hud')
36
-        });
37
-
38
-        const video = document.getElementById('my-video');
39
-        const useWebcam = (video === null);
40
-        const source = useWebcam ?
41
-            AR.Source.Camera({ resolution: 'md' }) :
42
-            AR.Source.Video(video);
43
-
44
-        const session = await AR.startSession({
45
-            mode: 'immersive',
46
-            viewport: viewport,
47
-            trackers: [ tracker ],
48
-            sources: [ source ],
49
-            stats: true,
50
-            gizmos: true,
51
-        });
52
-
53
-        const scan = document.getElementById('scan');
54
-
55
-        tracker.addEventListener('targetfound', event => {
56
-            if(scan)
57
-                scan.hidden = true;
58
-        });
59
-
60
-        tracker.addEventListener('targetlost', event => {
61
-            if(scan)
62
-                scan.hidden = false;
63
-        });
64
-
65
-        return session;
81
+    mix(frame);
82
+
83
+    session.requestAnimationFrame(animate);
84
+}
85
+
86
+/**
87
+ * Blend the physical and the virtual scenes
88
+ * @param {Frame} frame
89
+ * @returns {boolean} true if an image is being tracked
90
+ */
91
+function mix(frame)
92
+{
93
+    for(const result of frame.results) {
94
+        if(result.tracker.type == 'image-tracker') {
95
+            if(result.trackables.length > 0) {
96
+                const trackable = result.trackables[0];
97
+                const projectionMatrix = result.viewer.view.projectionMatrix;
98
+                const viewMatrix = result.viewer.pose.viewMatrix;
99
+                const modelMatrix = trackable.pose.transform.matrix;
100
+
101
+                doSomethingWith(projectionMatrix, viewMatrix, modelMatrix);
102
+                return true;
103
+            }
104
+        }
66 105
     }
67
-});
106
+
107
+    return false;
108
+}
109
+
110
+/**
111
+ * Template function
112
+ * @param {SpeedyMatrix} projectionMatrix
113
+ * @param {SpeedyMatrix} viewMatrix
114
+ * @param {SpeedyMatrix} modelMatrix
115
+ * @returns {void}
116
+ */
117
+function doSomethingWith(projectionMatrix, viewMatrix, modelMatrix)
118
+{
119
+    /*
120
+    console.log('projectionMatrix', projectionMatrix.toString());
121
+    console.log('viewMatrix', viewMatrix.toString());
122
+    console.log('modelMatrix', modelMatrix.toString());
123
+    */
124
+}
125
+
126
+/**
127
+ * Start the Demo
128
+ * @returns {void}
129
+ */
130
+function main()
131
+{
132
+    startARSession().then(session => {
133
+
134
+        const canvas = session.viewport.canvas; // render your virtual scene on this <canvas>
135
+
136
+        session.requestAnimationFrame(animate); // start the animation loop
137
+
138
+    }).catch(error => {
139
+
140
+        alert(error.message);
141
+
142
+    });
143
+}
144
+
145
+document.addEventListener('DOMContentLoaded', main);
146
+
147
+})();

+ 2
- 1
demos/hello-world/index.html Переглянути файл

@@ -15,6 +15,7 @@
15 15
                 <a id="about" href="NOTICE.html"></a>
16 16
             </div>
17 17
         </div>
18
-        <img id="my-reference-image" src="../assets/my-reference-image.webp" hidden>
18
+        <img id="mage" src="../assets/mage.webp" hidden>
19
+        <img id="cat" src="../assets/cat.webp" hidden>
19 20
     </body>
20 21
 </html>

+ 2
- 1
demos/hello-world/video.html Переглянути файл

@@ -15,7 +15,8 @@
15 15
                 <a id="about" href="NOTICE.html"></a>
16 16
             </div>
17 17
         </div>
18
-        <img id="my-reference-image" src="../assets/my-reference-image.webp" hidden>
18
+        <img id="mage" src="../assets/mage.webp" hidden>
19
+        <img id="cat" src="../assets/cat.webp" hidden>
19 20
         <video id="my-video" hidden muted loop playsinline autoplay>
20 21
             <source src="../assets/my-video.webm" type="video/webm" />
21 22
             <source src="../assets/my-video.mp4" type="video/mp4" />

+ 6
- 6
docs/demos.md Переглянути файл

@@ -37,17 +37,17 @@ Create an augmented scene with [three.js](https://threejs.org){ ._blank }.
37 37
 
38 38
 [Launch demo](/encantar-js/demos/hello-three/README.html){ ._blank .md-button }
39 39
 
40
-### WebAR with WebGL only
40
+### WebAR template
41 41
 
42
-Create an augmented scene without additional libraries.
42
+A template to help you create an augmented scene with any 3D framework.
43 43
 
44
-[Launch demo](/encantar-js/demos/hello-webgl/README.html){ ._blank .md-button }
44
+[Launch demo](/encantar-js/demos/hello-world/README.html){ ._blank .md-button }
45 45
 
46
-### Hello, world!
46
+### WebAR with pure WebGL
47 47
 
48
-A basic template to help you get started.
48
+Create an augmented scene without a 3D framework.
49 49
 
50
-[Launch demo](/encantar-js/demos/hello-world/README.html){ ._blank .md-button }
50
+[Launch demo](/encantar-js/demos/hello-webgl/README.html){ ._blank .md-button }
51 51
 
52 52
 ## Try locally
53 53
 

Завантаження…
Відмінити
Зберегти