|
@@ -0,0 +1,273 @@
|
|
1
|
+/**
|
|
2
|
+ * Augmented Reality demo using the babylon.js plugin for encantar.js
|
|
3
|
+ * @author Alexandre Martins <alemartf(at)gmail.com> (https://github.com/alemart/encantar-js)
|
|
4
|
+ */
|
|
5
|
+
|
|
6
|
+(function() {
|
|
7
|
+
|
|
8
|
+/**
|
|
9
|
+ * Augmented Reality Demo
|
|
10
|
+ */
|
|
11
|
+class EnchantedDemo extends ARDemo
|
|
12
|
+{
|
|
13
|
+ /**
|
|
14
|
+ * Constructor
|
|
15
|
+ */
|
|
16
|
+ constructor()
|
|
17
|
+ {
|
|
18
|
+ super();
|
|
19
|
+
|
|
20
|
+ this._objects = { };
|
|
21
|
+ }
|
|
22
|
+
|
|
23
|
+ /**
|
|
24
|
+ * Start the AR session
|
|
25
|
+ * @returns {Promise<Session>}
|
|
26
|
+ */
|
|
27
|
+ async startSession()
|
|
28
|
+ {
|
|
29
|
+ if(!AR.isSupported()) {
|
|
30
|
+ throw new Error(
|
|
31
|
+ 'This device is not compatible with this AR experience.\n\n' +
|
|
32
|
+ 'User agent: ' + navigator.userAgent
|
|
33
|
+ );
|
|
34
|
+ }
|
|
35
|
+
|
|
36
|
+ const tracker = AR.Tracker.ImageTracker();
|
|
37
|
+ await tracker.database.add([
|
|
38
|
+ {
|
|
39
|
+ name: 'mage',
|
|
40
|
+ image: document.getElementById('mage')
|
|
41
|
+ },
|
|
42
|
+ {
|
|
43
|
+ name: 'cat',
|
|
44
|
+ image: document.getElementById('cat')
|
|
45
|
+ }
|
|
46
|
+ ]);
|
|
47
|
+
|
|
48
|
+ const viewport = AR.Viewport({
|
|
49
|
+ container: document.getElementById('ar-viewport'),
|
|
50
|
+ hudContainer: document.getElementById('ar-hud')
|
|
51
|
+ });
|
|
52
|
+
|
|
53
|
+ const video = document.getElementById('my-video');
|
|
54
|
+ const useWebcam = (video === null);
|
|
55
|
+ const source = useWebcam ? AR.Source.Camera() : AR.Source.Video(video);
|
|
56
|
+
|
|
57
|
+ const session = await AR.startSession({
|
|
58
|
+ mode: 'immersive',
|
|
59
|
+ viewport: viewport,
|
|
60
|
+ trackers: [ tracker ],
|
|
61
|
+ sources: [ source ],
|
|
62
|
+ stats: true,
|
|
63
|
+ gizmos: true,
|
|
64
|
+ });
|
|
65
|
+
|
|
66
|
+ const scan = document.getElementById('scan');
|
|
67
|
+
|
|
68
|
+ tracker.addEventListener('targetfound', event => {
|
|
69
|
+ session.gizmos.visible = false;
|
|
70
|
+ if(scan)
|
|
71
|
+ scan.hidden = true;
|
|
72
|
+
|
|
73
|
+ this._onTargetFound(event.referenceImage);
|
|
74
|
+ });
|
|
75
|
+
|
|
76
|
+ tracker.addEventListener('targetlost', event => {
|
|
77
|
+ session.gizmos.visible = true;
|
|
78
|
+ if(scan)
|
|
79
|
+ scan.hidden = false;
|
|
80
|
+
|
|
81
|
+ this._onTargetLost(event.referenceImage);
|
|
82
|
+ });
|
|
83
|
+
|
|
84
|
+ return session;
|
|
85
|
+ }
|
|
86
|
+
|
|
87
|
+ /**
|
|
88
|
+ * Initialization
|
|
89
|
+ * @param {ARSystem} ar
|
|
90
|
+ * @returns {Promise<void>}
|
|
91
|
+ */
|
|
92
|
+ async init(ar)
|
|
93
|
+ {
|
|
94
|
+ // Do not automatically play an animation when loading GLTF models
|
|
95
|
+ BABYLON.SceneLoader.OnPluginActivatedObservable.add(loader => {
|
|
96
|
+ if(loader.name == 'gltf') {
|
|
97
|
+ loader.animationStartMode = BABYLON.GLTFLoaderAnimationStartMode.NONE;
|
|
98
|
+ }
|
|
99
|
+ });
|
|
100
|
+
|
|
101
|
+ // Change the point of view - slightly
|
|
102
|
+ ar.root.position.y = -0.5;
|
|
103
|
+
|
|
104
|
+ // Initialize objects
|
|
105
|
+ this._initLight(ar);
|
|
106
|
+ this._initText(ar);
|
|
107
|
+ this._initMagicCircle(ar);
|
|
108
|
+
|
|
109
|
+ await Promise.all([
|
|
110
|
+ this._initMage(ar),
|
|
111
|
+ this._initCat(ar),
|
|
112
|
+ ]);
|
|
113
|
+ }
|
|
114
|
+
|
|
115
|
+ /**
|
|
116
|
+ * Animation loop
|
|
117
|
+ * @param {ARSystem} ar
|
|
118
|
+ * @returns {void}
|
|
119
|
+ */
|
|
120
|
+ update(ar)
|
|
121
|
+ {
|
|
122
|
+ const delta = ar.session.time.delta; // given in seconds
|
|
123
|
+
|
|
124
|
+ this._animateMagicCircle(delta);
|
|
125
|
+ }
|
|
126
|
+
|
|
127
|
+
|
|
128
|
+ // ------------------------------------------------------------------------
|
|
129
|
+
|
|
130
|
+
|
|
131
|
+ _initLight(ar)
|
|
132
|
+ {
|
|
133
|
+ const light = new BABYLON.HemisphericLight('light', BABYLON.Vector3.Down());
|
|
134
|
+ light.intensity = 1.0;
|
|
135
|
+ light.diffuse.set(1, 1, 0.9);
|
|
136
|
+ light.specular.set(0, 0, 0);
|
|
137
|
+ }
|
|
138
|
+
|
|
139
|
+ _initMagicCircle(ar)
|
|
140
|
+ {
|
|
141
|
+ // create a magic circle
|
|
142
|
+ const magicCircle = BABYLON.MeshBuilder.CreatePlane('magic-circle', {
|
|
143
|
+ width: 1,
|
|
144
|
+ height: 1,
|
|
145
|
+ sideOrientation: BABYLON.Mesh.DOUBLESIDE
|
|
146
|
+ });
|
|
147
|
+
|
|
148
|
+ magicCircle.material = new BABYLON.StandardMaterial('magic-circle-material');
|
|
149
|
+ magicCircle.material.diffuseTexture = new BABYLON.Texture('../assets/magic-circle.png');
|
|
150
|
+ magicCircle.material.diffuseTexture.hasAlpha = true;
|
|
151
|
+ magicCircle.material.useAlphaFromDiffuseTexture = true;
|
|
152
|
+ magicCircle.material.diffuseColor.set(0, 0, 0);
|
|
153
|
+ magicCircle.material.emissiveColor.set(1, 1, 1);
|
|
154
|
+ magicCircle.material.unlit = true;
|
|
155
|
+ magicCircle.rotation.set(-Math.PI / 2, 0, 0);
|
|
156
|
+ magicCircle.scaling.set(4, 4, 1);
|
|
157
|
+
|
|
158
|
+ // make it a child of ar.root
|
|
159
|
+ magicCircle.parent = ar.root;
|
|
160
|
+
|
|
161
|
+ // save a reference
|
|
162
|
+ this._objects.magicCircle = magicCircle;
|
|
163
|
+ }
|
|
164
|
+
|
|
165
|
+ _initText(ar)
|
|
166
|
+ {
|
|
167
|
+ const text = BABYLON.MeshBuilder.CreatePlane('text', {
|
|
168
|
+ width: 1,
|
|
169
|
+ height: 1,
|
|
170
|
+ sideOrientation: BABYLON.Mesh.DOUBLESIDE
|
|
171
|
+ });
|
|
172
|
+
|
|
173
|
+ text.material = new BABYLON.StandardMaterial('text-material');
|
|
174
|
+ text.material.diffuseTexture = new BABYLON.Texture('../assets/it-works.png');
|
|
175
|
+ text.material.diffuseTexture.hasAlpha = true;
|
|
176
|
+ text.material.useAlphaFromDiffuseTexture = true;
|
|
177
|
+ text.material.diffuseColor.set(0, 0, 0);
|
|
178
|
+ text.material.emissiveColor.set(1, 1, 1);
|
|
179
|
+ text.material.unlit = true;
|
|
180
|
+ text.position.set(0, 2, 0.5);
|
|
181
|
+ text.scaling.set(3, 1.5, 1);
|
|
182
|
+
|
|
183
|
+ text.parent = ar.root;
|
|
184
|
+
|
|
185
|
+ this._objects.text = text;
|
|
186
|
+ }
|
|
187
|
+
|
|
188
|
+ async _initMage(ar)
|
|
189
|
+ {
|
|
190
|
+ // load the mage
|
|
191
|
+ const gltf = await BABYLON.SceneLoader.ImportMeshAsync('', '../assets/', 'mage.glb');
|
|
192
|
+ const mage = gltf.meshes[0];
|
|
193
|
+ mage.scaling.set(0.7, 0.7, 0.7);
|
|
194
|
+
|
|
195
|
+ // play an animation
|
|
196
|
+ const anim = gltf.animationGroups.find(anim => anim.name == 'Idle');
|
|
197
|
+ if(anim)
|
|
198
|
+ anim.play(true);
|
|
199
|
+
|
|
200
|
+ // make the mage a child of ar.root
|
|
201
|
+ mage.parent = ar.root;
|
|
202
|
+
|
|
203
|
+ // save a reference
|
|
204
|
+ this._objects.mage = mage;
|
|
205
|
+ }
|
|
206
|
+
|
|
207
|
+ async _initCat(ar)
|
|
208
|
+ {
|
|
209
|
+ const gltf = await BABYLON.SceneLoader.ImportMeshAsync('', '../assets/', 'cat.glb');
|
|
210
|
+ const cat = gltf.meshes[0];
|
|
211
|
+ cat.scaling.set(0.7, 0.7, 0.7);
|
|
212
|
+
|
|
213
|
+ const anim = gltf.animationGroups.find(anim => anim.name == 'Cheer');
|
|
214
|
+ if(anim)
|
|
215
|
+ anim.play(true);
|
|
216
|
+
|
|
217
|
+ cat.parent = ar.root;
|
|
218
|
+
|
|
219
|
+ this._objects.cat = cat;
|
|
220
|
+ }
|
|
221
|
+
|
|
222
|
+ _animateMagicCircle(delta)
|
|
223
|
+ {
|
|
224
|
+ const TWO_PI = 2.0 * Math.PI;
|
|
225
|
+ const ROTATIONS_PER_SECOND = 1.0 / 8.0;
|
|
226
|
+
|
|
227
|
+ this._objects.magicCircle.rotate(BABYLON.Axis.Z, -TWO_PI * ROTATIONS_PER_SECOND * delta);
|
|
228
|
+ }
|
|
229
|
+
|
|
230
|
+ _onTargetFound(referenceImage)
|
|
231
|
+ {
|
|
232
|
+ // change the scene based on the tracked image
|
|
233
|
+ switch(referenceImage.name) {
|
|
234
|
+ case 'mage':
|
|
235
|
+ this._objects.mage.setEnabled(true);
|
|
236
|
+ this._objects.cat.setEnabled(false);
|
|
237
|
+ this._objects.text.setEnabled(false);
|
|
238
|
+ this._objects.magicCircle.material.emissiveColor.fromHexString('#beefff');
|
|
239
|
+ break;
|
|
240
|
+
|
|
241
|
+ case 'cat':
|
|
242
|
+ this._objects.mage.setEnabled(false);
|
|
243
|
+ this._objects.cat.setEnabled(true);
|
|
244
|
+ this._objects.text.setEnabled(true);
|
|
245
|
+ this._objects.magicCircle.material.emissiveColor.fromHexString('#ffffaa');
|
|
246
|
+ break;
|
|
247
|
+ }
|
|
248
|
+ }
|
|
249
|
+
|
|
250
|
+ _onTargetLost(referenceImage)
|
|
251
|
+ {
|
|
252
|
+ }
|
|
253
|
+}
|
|
254
|
+
|
|
255
|
+/**
|
|
256
|
+ * Start the Demo
|
|
257
|
+ * @returns {void}
|
|
258
|
+ */
|
|
259
|
+function main()
|
|
260
|
+{
|
|
261
|
+ const demo = new EnchantedDemo();
|
|
262
|
+
|
|
263
|
+ if(typeof encantar === 'undefined')
|
|
264
|
+ throw new Error(`Can't find the babylon.js plugin for encantar.js`);
|
|
265
|
+
|
|
266
|
+ encantar(demo).catch(error => {
|
|
267
|
+ alert(error.message);
|
|
268
|
+ });
|
|
269
|
+}
|
|
270
|
+
|
|
271
|
+document.addEventListener('DOMContentLoaded', main);
|
|
272
|
+
|
|
273
|
+})();
|