-
Notifications
You must be signed in to change notification settings - Fork 61
/
index.html
157 lines (129 loc) · 4.76 KB
/
index.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
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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>音乐频谱可视化</title>
<style>
body {
margin: 0;
overflow: hidden;
}
</style>
<script src="./js/three.js"></script>
<script src="./js/OrbitControls.js"></script>
</head>
<body>
<script>
let audioCtx = new AudioContext();
let source, analyser;
function getData() {
source = audioCtx.createBufferSource();
analyser = audioCtx.createAnalyser();
return fetch('./music/一路生花.mp3')
.then(function(response) {
if (!response.ok) {
throw new Error("HTTP error, status = " + response.status);
}
return response.arrayBuffer();
})
.then(function(arrayBuffer) {
audioCtx.decodeAudioData(arrayBuffer, function(decodedData) {
source.buffer = decodedData;
source.connect(analyser);
analyser.connect(audioCtx.destination);
});
});
};
function triggerHandler() {
getData().then(function() {
source.start(0);
create();
render();
});
document.removeEventListener('mousedown', triggerHandler)
}
document.addEventListener('mousedown', triggerHandler);
const STEP = 50;
const CUBE_NUM = Math.ceil(1024 / STEP);
const FLOWER_NUM = 400;
const width = window.innerWidth;
const height = window.innerHeight;
const scene = new THREE.Scene();
const camera = new THREE.PerspectiveCamera(45, width / height, 0.1, 1000);
const renderer = new THREE.WebGLRenderer();
/**
* 花瓣分组
*/
const petal = new THREE.Group();
/**
* 频谱立方体
*/
const cubes = new THREE.Group();
function create() {
const pointLight = new THREE.PointLight( 0xffffff );
pointLight.position.set(0, 300, 40);
scene.add(pointLight);
camera.position.set(0,300, 400);
camera.lookAt(scene.position);
renderer.setSize(width, height);
document.body.appendChild(renderer.domElement)
renderer.render(scene, camera)
for (let i = 0; i < CUBE_NUM; i ++ ) {
const geometry = new THREE.BoxGeometry( 10, 10, 10 );
const material = new THREE.MeshPhongMaterial({color: 'yellowgreen'});
const cube = new THREE.Mesh( geometry, material );
cube.translateX((10 + 10) * i);
cube.translateY(1);
cubes.add(cube);
}
cubes.translateX(- (10 +10) * CUBE_NUM / 2);
var flowerTexture1 = new THREE.TextureLoader().load("img/flower1.png");
var flowerTexture2 = new THREE.TextureLoader().load("img/flower2.png");
var flowerTexture3 = new THREE.TextureLoader().load("img/flower3.png");
var flowerTexture4 = new THREE.TextureLoader().load("img/flower4.png");
var flowerTexture5 = new THREE.TextureLoader().load("img/flower5.png");
var imageList = [flowerTexture1, flowerTexture2, flowerTexture3, flowerTexture4, flowerTexture5];
for (let i = 0; i < FLOWER_NUM; i++) {
var spriteMaterial = new THREE.SpriteMaterial({
map: imageList[Math.floor(Math.random() * imageList.length)],
});
var sprite = new THREE.Sprite(spriteMaterial);
petal.add(sprite);
sprite.scale.set(40, 50, 1);
sprite.position.set(2000 * (Math.random() - 0.5), 500 * Math.random(), 2000 * (Math.random() - 0.5))
}
scene.add(cubes);
scene.add(petal);
}
function render() {
petal.children.forEach(sprite => {
sprite.position.y -= 5;
sprite.position.x += 0.5;
if (sprite.position.y < - height / 2) {
sprite.position.y = height / 2;
}
if (sprite.position.x > 1000) {
sprite.position.x = -1000;
}
});
const frequencyData = new Uint8Array(analyser.frequencyBinCount);
analyser.getByteFrequencyData(frequencyData);
const averageFrequencyData = [];
for (let i = 0; i< frequencyData.length; i += STEP) {
let sum = 0;
for(let j = i; j < i + STEP; j++) {
sum += frequencyData[j];
}
averageFrequencyData.push(sum / STEP);
}
for (let i = 0; i < averageFrequencyData.length; i++) {
cubes.children[i].scale.y = Math.floor(averageFrequencyData[i] * 0.4);
}
scene.rotateX(0.005);
renderer.render(scene, camera);
requestAnimationFrame(render);
}
const controls = new THREE.OrbitControls(camera);
</script>
</body>
</html>