-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtplayer.html
286 lines (233 loc) · 7.42 KB
/
tplayer.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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>three.js</title>
<meta name="viewport" content="width=device-width, user-scalable=no, minimum-scale=1.0, maximum-scale=1.0">
<meta name="description" content="展示一些样例,写一些书籍,gy的网站">
<meta name="keywords" content="three.js,demos,canvas,webgl,tricks,前端,guoyang,gy134340,web">
<link rel="icon" href="img/icon/favicon.png">
<link rel="stylesheet" href="css/reset.css">
<style>
.container{
width: 100%;
height: 100%;
position: relative;
}
.message{
position: absolute;
z-index: 999;
color:#fff;
font-size: 18px;
}
.title{
color:#fff;
position: absolute;
font-size: 20px;
top:10px;
-webkit-transform:translateX(-50%);
transform: translateX(-50%);
left:50%;
width: auto;
margin: 0 auto;
}
</style>
</head>
<body>
<div class="container">
<p class="title">three.js audio 播放音乐和动画</p>
</div>
<script src="js/three.min.js"></script>
<script src="js/renderers/Projector.js"></script>
<script src="js/renderers/CanvasRenderer.js"></script>
<script src='js/TrackballControls.js'></script>
<script src="js/stats.min.js"></script>
<script src='js/Tween.js'></script>
<script src="js/RequestAnimationFrame.js"></script>
<script type="text/javascript">
/**
* 这是一个用three 播放音乐的啥啥啥
*
* @author gy 2017
*
*/
var container,stats;
var canavs;
var camera,scene,renderer,controls;
var raycaster; // 点击发出的一条射线
var mouse = {
};
var meshs = [];
var winWidth = window.innerWidth;
var winHeight = window.innerHeight;
var INTERSECTED = []; // 鼠标点击的第一个点,以防快速点击,所以用数组保存
var PI2 = Math.PI * 2;
// 初始化
function init(){
container = document.querySelectorAll('.container')[0];
raycaster = new THREE.Raycaster(); // 射线
mouse = new THREE.Vector2(); // 点向量
mouse.x = 0;
mouse.y = 0;
// 添加scene
scene = new THREE.Scene();
// 添加相机
camera = new THREE.PerspectiveCamera(75, winWidth / winHeight, 1, 10000);
camera.position.set(0, -2400, 1200);
camera.lookAt(scene.position);
// 添加光源
var light = new THREE.AmbientLight(0x404040);
scene.add(light);
var lightDir = new THREE.DirectionalLight( 0xffffff );
light.position.set( 0, 1, 1 ).normalize();
scene.add(light);
// 添加音乐
var listener = new THREE.AudioListener();
camera.add(listener);
var sound = new THREE.Audio(listener);
var audioLoader = new THREE.AudioLoader();
audioLoader.load( 'audio/hwa.mp3', function( buffer ) {
sound.setBuffer( buffer );
sound.setLoop(true);
sound.setVolume(0.5);
sound.play();
});
// 放两个音箱
var material = new THREE.MeshBasicMaterial({
map: new THREE.TextureLoader().load('textures/bg.jpg')
});
var geometry = new THREE.BoxGeometry(500,1000,500);
var cubeLeft = new THREE.Mesh(geometry, material);
var cubeRight = new THREE.Mesh(geometry,material);
cubeLeft.position.set(540,150,250);
cubeRight.position.set(-540,150,250);
scene.add( cubeLeft );
scene.add( cubeRight );
// 添加一堆光的栗子栗子
// 这里是用一个应用一个别的纹理
var spriteMap = new THREE.TextureLoader().load( 'textures/lensflare2.jpg' );
var particlaMaterial = new THREE.SpriteMaterial( {
// map: new THREE.CanvasTexture( generateSprite() ),
map: spriteMap,
blending: THREE.AdditiveBlending
} );
// var poiLeft = new THREE.Vector3(-500, -600, 400);
// var poiRight = new THREE.Vector3(500, -600, 400);
// console.log(poiLeft);
for ( var i = 0; i < 1000; i++ ) { // i也是延迟的时间
particleLeft = new THREE.Sprite( particlaMaterial );
initLeftParticle( particleLeft, i * 10 );
scene.add( particleLeft );
particleRight = new THREE.Sprite( particlaMaterial );
initRightParticle( particleRight, i * 10 );
scene.add( particleRight );
}
var torusGeometry = new THREE.TorusGeometry( 140, 50, 16, 100 );
var torusMaterial = new THREE.MeshBasicMaterial( { map: new THREE.TextureLoader().load('textures/lava/cloud.png') } );
var torusLeft = new THREE.Mesh( torusGeometry, torusMaterial );
var torusRight = new THREE.Mesh( torusGeometry, torusMaterial );
torusLeft.position.set(540,-450,250);
torusRight.position.set(-540,-450,250);
torusLeft.rotation.x = Math.PI / 2;
torusRight.rotation.x = Math.PI / 2;
scene.add( torusLeft );
scene.add( torusRight );
// 渲染
renderer = new THREE.CanvasRenderer();
renderer.setClearColor(0x000000);
renderer.setPixelRatio( window.devicePixelRatio );
renderer.setSize(winWidth, winHeight);
container.append(renderer.domElement);
// 统计
stats = new Stats();
container.appendChild(stats.dom);
drawLine();
controls = new THREE.TrackballControls( camera, renderer.domElement );
controls.speed = 5;
controls.noPan = false;
controls.noZoom = false;
}
// 初始化左边光粒,delay等于传入i的大小
// @todo 两个函数优化成一个
//
function initLeftParticle( particle, delay) {
var particle = this instanceof THREE.Sprite ? this : particle; // 循环到底的判断
var delay = delay !== undefined ? delay : 0;
particle.position.set(-500, -600, 400);
particle.scale.x = particle.scale.y = Math.random() * 32 + 16;
new TWEEN.Tween( particle ) // 此处是有循环的,单个particle的循环
.delay( delay )
.to( {}, 10000 )
.onComplete( initLeftParticle )
.start();
new TWEEN.Tween( particle.position ) // tween particle的位置
.delay( delay )
.to( { x: Math.random() * 4000 - 2000, y: Math.random() * 1000 - 500, z: Math.random() * 4000 - 2000 }, 10000 )
.start();
new TWEEN.Tween( particle.scale ) // 变大呀
.delay( delay )
.to( { x: 0.01, y: 0.01 }, 10000 )
.start();
}
// 初始化光粒,delay等于传入i的大小
function initRightParticle( particle, delay) {
var particle = this instanceof THREE.Sprite ? this : particle; // 循环到底的判断
var delay = delay !== undefined ? delay : 0;
particle.position.set( 500, -600, 400);
particle.scale.x = particle.scale.y = Math.random() * 32 + 16;
new TWEEN.Tween( particle ) // 此处是有循环的,单个particle的循环
.delay( delay )
.to( {}, 10000 )
.onComplete( initRightParticle )
.start();
new TWEEN.Tween( particle.position ) // tween particle的位置
.delay( delay )
.to( { x: Math.random() * 4000 - 2000, y: Math.random() * 1000 - 500, z: Math.random() * 4000 - 2000 }, 10000 )
.start();
new TWEEN.Tween( particle.scale ) // 变大呀
.delay( delay )
.to( { x: 0.01, y: 0.01 }, 10000 )
.start();
}
function render(){
TWEEN.update();
camera.lookAt(scene.position);
camera.updateMatrixWorld();
renderer.render(scene, camera);
}
function animate(){
requestAnimationFrame( animate );
render();
stats.update(); // fps
controls.update(); // control 位置
}
// // 画一个网格
function drawLine() {
var material = new THREE.LineBasicMaterial({
color: 0xCCCCCC
});
for ( var i = -40; i < 40; i++ ){
var geometry = new THREE.Geometry();
geometry.vertices.push(
new THREE.Vector3( 50*i, -1000, 0 ),
new THREE.Vector3( 50*i, 1000, 0 )
);
var line = new THREE.Line( geometry, material );
scene.add( line );
}
for ( var j = -40; j < 40; j++ ){
var geometry = new THREE.Geometry();
geometry.vertices.push(
new THREE.Vector3( -1000, 50*j, 0 ),
new THREE.Vector3( 1000, 50*j, 0 )
);
var line = new THREE.Line( geometry, material );
scene.add( line );
}
}
// start here
init();
animate();
</script>
</body>
</html>