-
Notifications
You must be signed in to change notification settings - Fork 0
/
09-1声音设计 - 音频集成.html
72 lines (61 loc) · 2.03 KB
/
09-1声音设计 - 音频集成.html
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
<!DOCTYPE html>
<html lang="en">
<head>
<title>three.js webgl - geometry - cube</title>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, user-scalable=no, minimum-scale=1.0, maximum-scale=1.0" />
<link type="text/css" rel="stylesheet" href="https://threejs.org/examples/main.css" />
<style>
html,
body {
width: 100%;
height: 100%;
margin: 0;
padding: 0;
}
</style>
</head>
<body>
<script type="importmap">
{
"imports": {
"three": "https://threejs.org/build/three.module.js",
"three/addons/": "https://threejs.org/examples/jsm/"
}
}
</script>
<script type="module">
import * as THREE from "three";
import { OrbitControls } from "three/addons/controls/OrbitControls.js";
const scene = new THREE.Scene();
const camera = new THREE.PerspectiveCamera(75, window.innerWidth / window.innerHeight, 0.1, 1000);
const renderer = new THREE.WebGLRenderer();
renderer.setSize(window.innerWidth, window.innerHeight);
document.body.appendChild(renderer.domElement);
const controls = new OrbitControls(camera, renderer.domElement);
// 创建音频上下文
const audioCtx = new window.AudioContext;
// 加载音频文件并播放
const audioLoader = new THREE.AudioLoader();
audioLoader.load('https://threejs.org/examples/sounds/358232_j_s_song.mp3', function (buffer) {
const audioSource = audioCtx.createBufferSource();
audioSource.buffer = buffer;
audioSource.connect(audioCtx.destination);
audioSource.start();
});
// 将音频监听器添加到相机
const listener = new THREE.AudioListener();
camera.add(listener);
// 创建一个声音对象并设置其在场景中的位置
const sound = new THREE.Audio(listener);
sound.position.set(5, 0, 0);
// 动画循环
function animate() {
requestAnimationFrame(animate);
controls.update();
renderer.render(scene, camera);
}
animate();
</script>
</body>
</html>