|
@@ -1,187 +0,0 @@
|
1
|
|
-window.addEventListener('load', () => {
|
2
|
|
-
|
3
|
|
- const my = { };
|
4
|
|
-
|
5
|
|
- async function initialize(ar)
|
6
|
|
- {
|
7
|
|
- // add lights
|
8
|
|
- const ambientLight = new THREE.AmbientLight(0x808080);
|
9
|
|
- const directionalLight = new THREE.DirectionalLight(0xffffff, 0.75);
|
10
|
|
- directionalLight.position.set(0, 0, -1);
|
11
|
|
- ar.scene.add(ambientLight);
|
12
|
|
- ar.scene.add(directionalLight);
|
13
|
|
-
|
14
|
|
- // create cubes
|
15
|
|
- my.cubes = [
|
16
|
|
- createCube(ar, 1.0, -0.5, 0x00ff00),
|
17
|
|
- createCube(ar, -1.0, -0.5, 0xffff00),
|
18
|
|
- createCube(ar, 0.0, -0.5, 0x0077ff),
|
19
|
|
- ];
|
20
|
|
-
|
21
|
|
- // create text
|
22
|
|
- my.text = await createText(ar, 'Tap on any cube', 0x88ffee);
|
23
|
|
- my.text.position.set(-1.5, 0.25, 0.5);
|
24
|
|
-
|
25
|
|
- // setup interactivity
|
26
|
|
- my.pointer = createPointer(ar);
|
27
|
|
- my.raycaster = new THREE.Raycaster();
|
28
|
|
- }
|
29
|
|
-
|
30
|
|
- function createCube(ar, x, y, color, length = 0.7)
|
31
|
|
- {
|
32
|
|
- const geometry = new THREE.BoxGeometry(length, length, length);
|
33
|
|
- const material = new THREE.MeshPhongMaterial({ color });
|
34
|
|
- const cube = new THREE.Mesh(geometry, material);
|
35
|
|
-
|
36
|
|
- cube.position.x = x;
|
37
|
|
- cube.position.y = y;
|
38
|
|
- cube.position.z = length / 2;
|
39
|
|
- cube.userData.color = color;
|
40
|
|
-
|
41
|
|
- material.opacity = 1.0;
|
42
|
|
- material.transparent = true;
|
43
|
|
-
|
44
|
|
- ar.root.add(cube);
|
45
|
|
- return cube;
|
46
|
|
- }
|
47
|
|
-
|
48
|
|
- async function createText(ar, text, color = 0xffffff)
|
49
|
|
- {
|
50
|
|
- const loader = new THREE.FontLoader();
|
51
|
|
- const fontURL = '../assets/helvetiker_bold.typeface.json';
|
52
|
|
- const font = await loader.loadAsync(fontURL);
|
53
|
|
-
|
54
|
|
- const material = new THREE.MeshPhongMaterial({ color });
|
55
|
|
- const geometry = new THREE.TextGeometry(text, {
|
56
|
|
- font: font,
|
57
|
|
- size: 0.3,
|
58
|
|
- height: 0.05,
|
59
|
|
- });
|
60
|
|
- const mesh = new THREE.Mesh(geometry, material);
|
61
|
|
-
|
62
|
|
- ar.root.add(mesh);
|
63
|
|
- return mesh;
|
64
|
|
- }
|
65
|
|
-
|
66
|
|
- function createPointer(ar)
|
67
|
|
- {
|
68
|
|
- const pointer = {
|
69
|
|
- position: new THREE.Vector2(),
|
70
|
|
- down: false,
|
71
|
|
-
|
72
|
|
- get active()
|
73
|
|
- {
|
74
|
|
- return this.down && Math.max(Math.abs(this.position.x), Math.abs(this.position.y)) <= 1.0;
|
75
|
|
- }
|
76
|
|
- };
|
77
|
|
-
|
78
|
|
- function updatePosition(event)
|
79
|
|
- {
|
80
|
|
- const canvas = ar.renderer.domElement;
|
81
|
|
- const rect = canvas.getBoundingClientRect();
|
82
|
|
- const x = event.pageX - (rect.left + window.scrollX);
|
83
|
|
- const y = event.pageY - (rect.top + window.scrollY);
|
84
|
|
-
|
85
|
|
- // normalize to [-1,1] x [-1,1]
|
86
|
|
- pointer.position.x = 2.0 * x / rect.width - 1.0;
|
87
|
|
- pointer.position.y = -2.0 * y / rect.height + 1.0;
|
88
|
|
- }
|
89
|
|
-
|
90
|
|
- // setup pointer interactivity
|
91
|
|
- window.addEventListener('pointermove', event => {
|
92
|
|
- updatePosition(event);
|
93
|
|
- });
|
94
|
|
- window.addEventListener('pointerdown', event => {
|
95
|
|
- updatePosition(event);
|
96
|
|
- pointer.down = true;
|
97
|
|
- });
|
98
|
|
- window.addEventListener('pointerup', event => {
|
99
|
|
- pointer.down = false;
|
100
|
|
- });
|
101
|
|
-
|
102
|
|
- // done!
|
103
|
|
- return pointer;
|
104
|
|
- }
|
105
|
|
-
|
106
|
|
- function animate(ar)
|
107
|
|
- {
|
108
|
|
- // reset all cubes
|
109
|
|
- for(let i = 0; i < my.cubes.length; i++) {
|
110
|
|
- my.cubes[i].material.color.setHex(my.cubes[i].userData.color);
|
111
|
|
- my.cubes[i].scale.z = 1.0;
|
112
|
|
- }
|
113
|
|
-
|
114
|
|
- // interact with objects via raycasting
|
115
|
|
- if(my.pointer.active) {
|
116
|
|
- my.raycaster.setFromCamera(my.pointer.position, ar.camera);
|
117
|
|
-
|
118
|
|
- const intersections = my.raycaster.intersectObjects(my.cubes);
|
119
|
|
- for(let i = 0; i < intersections.length; i++) {
|
120
|
|
-
|
121
|
|
- // create the appearance of a pushed button
|
122
|
|
- const object = intersections[i].object;
|
123
|
|
- object.material.color.setHex(0xff3333);
|
124
|
|
- object.scale.z = 0.2;
|
125
|
|
-
|
126
|
|
- }
|
127
|
|
- }
|
128
|
|
- }
|
129
|
|
-
|
130
|
|
- async function startARSession()
|
131
|
|
- {
|
132
|
|
- if(!AR.isSupported()) {
|
133
|
|
- throw new Error(
|
134
|
|
- 'This device is not compatible with this AR experience.\n\n' +
|
135
|
|
- 'User agent: ' + navigator.userAgent
|
136
|
|
- );
|
137
|
|
- }
|
138
|
|
-
|
139
|
|
- //AR.Settings.powerPreference = 'low-power';
|
140
|
|
-
|
141
|
|
- const tracker = AR.Tracker.ImageTracker();
|
142
|
|
- await tracker.database.add([{
|
143
|
|
- name: 'my-reference-image',
|
144
|
|
- image: document.getElementById('my-reference-image')
|
145
|
|
- }]);
|
146
|
|
-
|
147
|
|
- const viewport = AR.Viewport({
|
148
|
|
- container: document.getElementById('ar-viewport'),
|
149
|
|
- hudContainer: document.getElementById('ar-hud')
|
150
|
|
- });
|
151
|
|
-
|
152
|
|
- const video = document.getElementById('my-video');
|
153
|
|
- const useWebcam = (video === null);
|
154
|
|
- const source = useWebcam ?
|
155
|
|
- AR.Source.Camera({ resolution: 'md' }) :
|
156
|
|
- AR.Source.Video(video);
|
157
|
|
-
|
158
|
|
- const session = await AR.startSession({
|
159
|
|
- mode: 'immersive',
|
160
|
|
- viewport: viewport,
|
161
|
|
- trackers: [ tracker ],
|
162
|
|
- sources: [ source ],
|
163
|
|
- stats: true,
|
164
|
|
- gizmos: true,
|
165
|
|
- });
|
166
|
|
-
|
167
|
|
- const scan = document.getElementById('scan');
|
168
|
|
-
|
169
|
|
- tracker.addEventListener('targetfound', event => {
|
170
|
|
- session.gizmos.visible = false;
|
171
|
|
- if(scan)
|
172
|
|
- scan.hidden = true;
|
173
|
|
- });
|
174
|
|
-
|
175
|
|
- tracker.addEventListener('targetlost', event => {
|
176
|
|
- session.gizmos.visible = true;
|
177
|
|
- if(scan)
|
178
|
|
- scan.hidden = false;
|
179
|
|
- });
|
180
|
|
-
|
181
|
|
- return session;
|
182
|
|
- }
|
183
|
|
-
|
184
|
|
- // enchant!
|
185
|
|
- encantar(startARSession, animate, initialize);
|
186
|
|
-
|
187
|
|
-});
|