-
Notifications
You must be signed in to change notification settings - Fork 10
/
example30.html
150 lines (121 loc) · 4.37 KB
/
example30.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
<!DOCTYPE html>
<html>
<head>
<title>Example 30 - Merge objects</title>
<style>
body{
margin: 0;
overflow: hidden;
}
#stats { /* Align stats top-left */
position: absolute;
left: 0px;
top: 0px;
}
</style>
</head>
<body>
<!-- JavaScript libraries -->
<script src="http://cdnjs.cloudflare.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="http://cdnjs.cloudflare.com/ajax/libs/three.js/r67/three.min.js"></script>
<script src="http://cdnjs.cloudflare.com/ajax/libs/stats.js/r11/Stats.min.js"></script>
<script src="http://cdnjs.cloudflare.com/ajax/libs/dat-gui/0.5/dat.gui.min.js"></script>
<script src="assets/libs/TrackballControls.js"></script>
<!-- Javascript code that runs our Three.js examples -->
<script>
// once everything is loaded, we run our Three.js stuff.
$(function () {
var stats = initStats();
// create a scene, that will hold all our elements such as objects, cameras and lights.
var scene = new THREE.Scene();
// create a camera, which defines where we're looking at.
var camera = new THREE.PerspectiveCamera(45, window.innerWidth / window.innerHeight, 1, 500);
// create a render and set the size
var renderer = new THREE.WebGLRenderer();
renderer.setClearColor(new THREE.Color(0x00000, 1.0));
renderer.setSize(window.innerWidth, window.innerHeight);
renderer.shadowMapEnabled = true;
// position and point the camera to the center of the scene
camera.position.set(0,40,50);
camera.lookAt(scene.position);
// add the output of the renderer to the html element
$('body').append(renderer.domElement);
// call the render function
var step = 0;
var cubeMaterial = new THREE.MeshNormalMaterial({color: 0x00ff00, transparent: true, opacity: 0.5 });
var controls = new function () {
this.rotationSpeed = 0.02;
this.combined = false;
this.numberOfObjects = 500;
this.redraw = function () {
var toRemove = [];
scene.traverse(function (e) {
if (e instanceof THREE.Mesh) {
toRemove.push(e);
}
});
toRemove.forEach(function (e) {
scene.remove(e)
});
// add a large number of cubes
if (controls.combined) {
var geometry = new THREE.Geometry();
for (var i = 0; i < controls.numberOfObjects; i++) {
geometry.merge(controls.addCube());
}
scene.add(new THREE.Mesh(geometry, cubeMaterial));
} else {
for (var i = 0; i < controls.numberOfObjects; i++) {
scene.add(new THREE.Mesh(controls.addCube(), cubeMaterial));
}
}
}
this.addCube = addcube;
this.outputObjects = function () {
console.log(scene.children);
}
}
var gui = new dat.GUI();
gui.add(controls, 'numberOfObjects', 0, 20000);
gui.add(controls, 'combined').onChange(controls.redraw);
gui.add(controls, 'redraw');
controls.redraw();
render();
var rotation = 0;
function translateGeometry (geometry, vector) {
geometry.vertices.forEach(function (v) {
v.add(vector);
});
geometry.verticesNeedUpdate.true;
}
function addcube() {
var cubeSize = 1.0;
var cubeGeometry = new THREE.BoxGeometry(cubeSize, cubeSize, cubeSize);
var x = -60 + Math.round((Math.random() * 100));
var y = Math.round((Math.random() * 10));
var z = -150 + Math.round((Math.random() * 175));
var v = new THREE.Vector3(x,y,z);
translateGeometry(cubeGeometry, v);
return cubeGeometry;
};
function render() {
rotation += 0.005;
stats.update();
camera.position.x = Math.sin(rotation) * 50;
// camera.position.y = Math.sin(rotation) * 40;
camera.position.z = Math.cos(rotation) * 50;
camera.lookAt(scene.position);
// render using requestAnimationFrame
requestAnimationFrame(render);
renderer.render(scene, camera);
}
function initStats() {
var stats = new Stats();
stats.setMode(0); // 0: fps, 1: ms
$('body').append(stats.domElement);
return stats;
}
});
</script>
</body>
</html>