-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.html
121 lines (93 loc) · 3.23 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
<!DOCTYPE html>
<html>
<head>
<title>Rigid Simulation</title>
<script src="src/three.js"></script>
<script src="src/stats.min.js"></script>
</head>
<body>
<script type="text/javascript">
var scene, camera, renderer, floor, stats;
var time = 0;
init();
animate();
function init(){
scene = new THREE.Scene();
camera = new THREE.PerspectiveCamera(50, window.innerWidth / window.innerHeight, 0.1, 100000);
camera.position.set(0, 0, 2000);
scene.add(camera);
// floor
var light = new THREE.HemisphereLight( 0xffffff, 0x444444 );
light.position.set( 1, 1, 1 );
scene.add( light );
createBoxMesh();
renderer = new THREE.WebGLRenderer( { antialias: true } );
renderer.setPixelRatio( window.devicePixelRatio );
renderer.setSize( window.innerWidth, window.innerHeight );
document.body.appendChild( renderer.domElement );
stats = new Stats();
document.body.appendChild( stats.dom );
window.addEventListener( 'resize', onWindowResize, false );
}
function createBoxMesh(){
let material = new THREE.MeshLambertMaterial( { color: Math.random() * 0xffffff } )
let object = new THREE.Mesh( new THREE.BoxGeometry( 100, 100, 100 ), material );
object.position.set(0, 70, 0);
object.userData.velocity = new THREE.Vector3();
object.userData.velocity.y = Math.random() * 30;
object.userData.velocityAng = createRotation();
scene.add(object);
}
function createTorusMesh(){
let geometry = new THREE.TorusGeometry( 100, 20, 6, 10 );
let material = new THREE.MeshBasicMaterial( { color: Math.random() * 0xffffff } );
let torus = new THREE.Mesh( geometry, material );
torus.position.set(0, 70, 0);
torus.userData.velocity = new THREE.Vector3();
torus.userData.velocity.y = Math.random() * 30;
torus.userData.velocityAng = createRotation();
scene.add( torus );
}
function createRotation(){
let rotation = new THREE.Matrix4();
//rotation.makeRotationX(Math.PI * (Math.random() - 0.5) * 0.2);
//rotation.makeRotationY(Math.PI * (Math.random() - 0.5) * 0.2);
//rotation.makeRotationZ(Math.PI * (Math.random() - 0.5) * 0.1);
let rotateEuler = new THREE.Euler( Math.PI * (Math.random() - 0.5) * 0.1, Math.PI * (Math.random() - 0.5) * 0.1, Math.PI * (Math.random() - 0.5) * 0.1, 'XYZ' )
rotation.makeRotationFromEuler(rotateEuler);
return rotation
}
function onWindowResize() {
camera.aspect = window.innerWidth / window.innerHeight;
camera.updateProjectionMatrix();
renderer.setSize( window.innerWidth, window.innerHeight );
}
function animate() {
requestAnimationFrame( animate );
render();
stats.update();
}
function render() {
//createMesh();
for (let i = scene.children.length - 1; i >= 0; i--) {
if(scene.children[i].type == 'Mesh' ){
let object = scene.children[i];
object.userData.velocity.y -= 0.59 ;
object.position.add(object.userData.velocity);
object.geometry.applyMatrix(object.userData.velocityAng);
if( object.position.y < -1000 ){
scene.remove(object);
if(Math.random() > 0.5 ){
createBoxMesh();
}
else{
createTorusMesh();
}
}
}
}
renderer.render( scene, camera );
}
</script>
</body>
</html>