-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.js
139 lines (108 loc) · 4.34 KB
/
main.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
import { GLTFLoader} from "./libs/GLTFLoader.js";
const THREE = window.MINDAR.IMAGE.THREE;
import {loadGLTF, loadAudio} from "./libs/loader.js";
document.addEventListener('DOMContentLoaded', () => {
const start = async() => {
const mindarThree = new window.MINDAR.IMAGE.MindARThree({
container: document.body,
imageTargetSrc: './abcd.mind',
maxTrack: 4,
});
const {renderer, scene, camera} = mindarThree;
const light = new THREE.HemisphereLight(0xffffbb, 0x080820, 1);
scene.add(light);
const airplane = await loadGLTF("./airplane/scene.gltf");
airplane.scene.scale.set(0.5, 0.5, 0.5);
const airplaneMixer = new THREE.AnimationMixer(airplane.scene);
const airplaneAction = airplaneMixer.clipAction(airplane.animations[0]);
airplaneAction.play();
// calling airplaneClip and we are loading the audio from our hard disk
const airplaneAclip = await loadAudio("./sound/airplane.mp3");
// we instantiated the THREE listener component using airListener variable
const airListener = new THREE.AudioListener();
// instantiated a speaker positional audio as airplaneAudio
const airplaneAudio = new THREE.PositionalAudio(airListener);
const ball = await loadGLTF("./ball/scene.gltf");
ball.scene.scale.set(0.2, 0.2, 0.2);
const ballMixer = new THREE.AnimationMixer(ball.scene);
const ballAction = ballMixer.clipAction(ball.animations[0]);
ballAction.play();
const ballAclip = await loadAudio("./sound/ball.mp3");
const ballListener = new THREE.AudioListener();
const ballAudio = new THREE.PositionalAudio(ballListener);
const car = await loadGLTF("./car/scene.gltf");
car.scene.scale.set(0.3, 0.3, 0.3);
car.scene.position.set(0, -0.1, 0);
const carMixer = new THREE.AnimationMixer(car.scene);
const carAction = carMixer.clipAction(car.animations[0]);
carAction.play();
const carAclip = await loadAudio("./sound/car.mp3");
const carListener = new THREE.AudioListener();
const carAudio = new THREE.PositionalAudio(carListener);
const dog = await loadGLTF("./dog/scene.gltf");
dog.scene.scale.set(0.5, 0.5, 0.5);
const dogMixer = new THREE.AnimationMixer(dog.scene);
const dogAction = dogMixer.clipAction(dog.animations[0]);
dogAction.play();
const airplaneAnchor = mindarThree.addAnchor(0);
airplaneAnchor.group.add(airplane.scene);
// added listener to the camera
camera.add(airListener);
// we set the referal distance from which the audio should fade out
airplaneAudio.setRefDistance(100);
// set the buffer of audio to stream
airplaneAudio.setBuffer(airplaneAclip);
// we sset the audio to loop
airplaneAudio.setLoop(true);
// we added the audio to the anchor of airplane which will be activated on seeing the airplane image
airplaneAnchor.group.add(airplaneAudio)
// make airplane audio play only when the target of airplane image is detected
airplaneAnchor.onTargetFound = () => {
airplaneAudio.play();
}
// make airplane audio pause then the target image is lost in the camera
airplaneAnchor.onTargetLost = () => {
airplaneAudio.pause();
}
const ballAnchor = mindarThree.addAnchor(1);
ballAnchor.group.add(ball.scene);
camera.add(ballListener);
ballAudio.setRefDistance(100);
ballAudio.setBuffer(ballAclip);
ballAudio.setLoop(true);
ballAnchor.group.add(ballAudio)
ballAnchor.onTargetFound = () => {
ballAudio.play();
}
ballAnchor.onTargetLost = () => {
ballAudio.pause();
}
const carAnchor = mindarThree.addAnchor(2);
carAnchor.group.add(car.scene);
camera.add(carListener);
carAudio.setRefDistance(100);
carAudio.setBuffer(carAclip);
carAudio.setLoop(true);
carAnchor.group.add(carAudio)
carAnchor.onTargetFound = () => {
carAudio.play();
}
carAnchor.onTargetLost = () => {
carAudio.pause();
}
const dogAnchor = mindarThree.addAnchor(3);
dogAnchor.group.add(dog.scene);
const clock = new THREE.Clock();
await mindarThree.start();
renderer.setAnimationLoop(() => {
const delta = clock.getDelta();
airplaneMixer.update(delta);
ballMixer.update(delta);
carMixer.update(delta);
car.scene.rotation.set(0, car.scene.rotation.y + delta, 0);
dogMixer.update(delta);
renderer.render(scene, camera);
});
}
start();
});