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
| (function(){
'use strict';
var tau = Math.PI * 2;
var width, height;
var mode;
var scene, camera, renderer, pointCloud;
THREE.ImageUtils.crossOrigin = '';
var SETTINGS = [{
particleCount: 100000,
material: new THREE.PointCloudMaterial({
size: 0.1,
color: 0xfff,
transparent: true,
opacity: 0.5
}),
initialize: null,
spawnBehavior: function(index){
var x, y, z;
x = (Math.random() * 2000) - 1000;
y = (Math.random() * 2000) - 1000;
z = (Math.random() * 2000) - 1000;
return new THREE.Vector3(x, y, z);
},
frameBehavior: null,
sceneFrameBehavior: function(){
camera.rotation.x -= 0.000015;
camera.rotation.y += 0.00005;
camera.rotation.z += 0.00005;
camera.translateZ(-1);
}
}]
function onDocumentReady(){
initialize();
}
function addModeButtons(){
var buttons = document.getElementById('buttons');
_.forEach(SETTINGS, function(mode){
var button = document.createElement('button');
button.textContent = mode.name;
button.addEventListener('click', function(){
setMode(mode);
});
buttons.appendChild(button);
});
}
function initialize(){
scene = new THREE.Scene();
camera = new THREE.PerspectiveCamera(120, width / height, 1, 1000);
renderer = new THREE.WebGLRenderer();
document.body.appendChild(renderer.domElement);
window.addEventListener('resize', onWindowResize);
onWindowResize();
addModeButtons();
scene.fog = new THREE.Fog(0, 1);
setMode(SETTINGS[SETTINGS.length-1]);
var controls = new THREE.OrbitControls( camera, renderer.domElement );
controls.enableDamping = true;
controls.dampingFactor = 0.25;
controls.enableZoom = false;
AnimationController.registerAnimation(render);
}
function setMode(_mode){
mode = _mode;
scene.remove(pointCloud);
resetCamera();
var points = createPoints(mode.spawnBehavior);
var material = mode.material;
pointCloud = new THREE.PointCloud(points, material);
if(mode.initialize && typeof mode.initialize === 'function'){
mode.initialize();
}
scene.add(pointCloud);
}
function createPoints(spawnBehavior){
var ret = new THREE.Geometry();
_.times(mode.particleCount, function(index){
ret.vertices.push(spawnBehavior(index));
})
return ret;
}
function onWindowResize(){
width = window.innerWidth;
height = window.innerHeight;
updateRendererSize();
}
function resetCamera(){
camera.position.x = camera.position.y = camera.position.z = camera.rotation.x = camera.rotation.y = camera.rotation.z = 0;
}
function updateRendererSize(){
renderer.setSize(window.innerWidth, window.innerHeight);
camera.aspect = window.innerWidth / window.innerHeight;
camera.updateProjectionMatrix();
}
function updateParticles(){
if(mode.sceneFrameBehavior && typeof mode.sceneFrameBehavior === 'function'){
mode.sceneFrameBehavior();
}
if(mode.frameBehavior && typeof mode.frameBehavior === 'function'){
_.forEach(pointCloud.geometry.vertices, mode.frameBehavior);
pointCloud.geometry.verticesNeedUpdate = true;
pointCloud.geometry.colorsNeedUpdate = true;
}
}
function render(){
updateParticles();
renderer.render(scene, camera);
}
document.addEventListener('DOMContentLoaded', onDocumentReady);
})(); |