-
Notifications
You must be signed in to change notification settings - Fork 0
/
particle.html
283 lines (233 loc) · 6.8 KB
/
particle.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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<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: 16px;
-webkit-transform:translate(-50%, -50%);
transform: translate(-50%, -50%);
}
.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">粒子与geometry</p>
</div>
</body>
<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.min.js'></script>
<script src="js/RequestAnimationFrame.js"></script>
<script type="text/javascript">
// about three.points shader geometry
// @author gy 2017.2.11
var container,stats;
var canvas;
var meshs = [];
var camera,scene,renderer,controls;
var raycaster; // 点击发出的一条射线
var mouse = new THREE.Vector2(); // 点向量
var winWidth = window.innerWidth;
var winHeight = window.innerHeight;
mouse.x = 0;
mouse.y = 0;
function init () {
container = document.querySelectorAll('.container')[0];
scene = new THREE.Scene();
// 相机
camera = new THREE.PerspectiveCamera(60, winWidth / winHeight, 1, 10000);
camera.position.set(600, 200, 600);
camera.lookAt(0, 0, 0);
// 光
var light = new THREE.AmbientLight(0x404040);
scene.add(light);
var pointLight = new THREE.PointLight( 0xff00ff, 1, 1000 );
pointLight.position.set( 0, 800, 0 );
scene.add( pointLight );
var axisHelper = new THREE.AxisHelper(1000);
scene.add( axisHelper );
// geometry
// var text = 't';
// var textFont = new Promise(function(resolve,reject){
// new THREE.FontLoader().load('fonts/optimer_bold.typeface.json',function(res){
// font = res;
// resolve();
// });
// });
// textFont.then(function(){
// var geometry = new THREE.TextGeometry(text,{
// font: font,
// size: 70,
// height: 20,
// curveSegments: 4,
// bevelThickness: 2,
// bevelSize: 1.5,
// bevelEnabled: true,
// material: 0,
// extrudeMaterial: 1
// });
// var wireframe = new THREE.WireframeGeometry( geometry );
// var line = new THREE.LineSegments( wireframe );
// line.material.depthTest = false;
// line.material.opacity = 0.25;
// line.material.transparent = true;
// scene.add( line );
// }).catch(function(error){
// console.log('font error');
// });
var gridHelper = new THREE.GridHelper( 400, 100 );
scene.add( gridHelper );
// var sprite = new THREE.TextureLoader().load( "textures/sprites/ball.png" );
var geometry = new THREE.SphereGeometry( 50, 60, 60 );
// points
var material = new THREE.PointsMaterial({color: 0x00ffff,transparent: true});
var points = new THREE.Points( geometry, material );
points.position.set(0,40,0)
// console.log('points',points.geometry.vertices);
scene.add( points );
var len = points.geometry.vertices.length;
var geometry = new THREE.BoxBufferGeometry( 2, 2, 2 );
var material = new THREE.MeshBasicMaterial( { color: 0xff0000 } );
var mesh = new THREE.Mesh( geometry, material );
scene.add( mesh );
for(var i = 0; i < len; i+=2){
var poi = points.geometry.vertices[i];
var mesh = new THREE.Mesh( geometry, material );
scene.add( mesh);
mesh.position.set(Math.round(poi.x),Math.round(poi.y)+40,Math.round(poi.z));
mesh.userData.originY = mesh.position.y;
mesh.userData.originX = mesh.position.x;
mesh.userData.originZ = mesh.position.z;
meshs.push(mesh);
// console.log(mesh, poi);
}
startFlow();
setInterval(function(){
startFlow();
},6000);
// var down = flowDown();
// var Up = flowUp();
// flowDown();
// down.then(Up);
// Up.then(down);
// renderer
renderer = new THREE.WebGLRenderer( {antialias: true} );
renderer.setClearColor(0x000000);
renderer.setPixelRatio(window.devivePixelRatio);
renderer.setSize(window.innerWidth, window.innerHeight);
container.append(renderer.domElement);
// 统计
stats = new Stats();
container.appendChild(stats.dom);
// 控制器
controls = new THREE.TrackballControls( camera, renderer.domElement );
controls.speed = 5;
controls.noPan = false;
controls.noZoom = false;
controls.rotateSpeed = 1.0;
controls.zoomSpeed = 1.2;
controls.panSpeed = 0.8;
controls.staticMoving = true;
controls.dynamicDampingFactor = 0.3;
window.addEventListener( 'resize', onWindowResize, false );
}
function startFlow(){
flowDown().then(function(){
return flowUp();
}).catch(function(error){
console.log('down up error');
});
}
function flowDown(){
for(var i = 0, len = meshs.length; i < len; i++){
var cube = meshs[i];
new TWEEN.Tween(cube.position)
.to({y:0},Math.sqrt(cube.position.y) * 100)
// .easing( TWEEN.Easing.Elastic.In )
.start();
}
return new Promise(function(resolve,reject){
setTimeout(function(){
resolve();
},3000);
})
}
function flowUp(){
var rand = 400 * Math.random() - 400;
for(var i = 0, len = meshs.length; i < len; i++){
var cube = meshs[i];
var originY = cube.userData.originY;
var originX = cube.userData.originX;
var originZ = cube.userData.originZ;
new TWEEN.Tween(cube.position)
.to({x:originX+rand,y:originY,z:originZ+rand},Math.sqrt(originY) * 150)
// .easing( TWEEN.Easing.Elastic.Out )
.start();
}
return new Promise(function(resolve,reject){
setTimeout(function(){
resolve();
},3000);
})
}
var theta = 0;
var radius = 600;
function render(){
theta += 0.3; // 自动渲染
camera.position.x = radius * Math.sin( THREE.Math.degToRad( theta ) );
camera.position.z = radius * Math.cos( THREE.Math.degToRad( theta ) );
camera.lookAt(scene.position);
camera.updateProjectionMatrix();
renderer.render(scene, camera);
}
function animate () {
requestAnimationFrame(animate);
render();
TWEEN.update();
stats.update();
controls.update();
}
init();
animate();
function onWindowResize(){
renderer.setPixelRatio( window.devicePixelRatio );
camera.aspect = window.innerWidth / window.innerHeight;
camera.updateProjectionMatrix();
renderer.setSize( window.innerWidth, window.innerHeight );
}
function fastAbs(value){
return (value ^ (value >> 31)) - (value >> 31);
}
function threshold(value){
return (value > 0x15) ? 0xFF : 0;
}
</script>
</html>