|
@@ -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
|
+})();
|