forked from anvaka/unrender
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
203 lines (162 loc) · 4.71 KB
/
index.js
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
var THREE = require('three');
var TWEEN = require('tween.js');
var combineOptions = require('./options.js');
var createParticleView = require('./lib/particle-view.js');
var createLineView = require('./lib/line-view.js');
var createHitTest = require('./lib/hit-test.js');
var createAutoPilot = require('./lib/auto-pilot.js');
var flyControls = require('three.fly');
var normalizeColor = require('./lib/normalize-color.js');
// Expose three.js as well, so simple clients do not have to require it
unrender.THREE = THREE;
unrender.TWEEN = TWEEN;
module.exports = unrender;
function unrender(container, options) {
var api = {
destroy: destroy,
scene: getScene,
camera: getCamera,
input: getInput,
renderer: getRenderer,
// todo: this should all be refactored into single particles class.
particles: particles,
getParticleView: getParicleView,
hitTest: getHitTest,
lines: drawLines,
onFrame: onFrame,
offFrame: offFrame,
lookAt: lookAt,
around: around,
getContainer: getContainer
};
options = combineOptions(options);
var lastFrame;
var rafCallbacks = [];
var scene = createScene();
var camera = createCamera();
var renderer = createRenderer();
var particleView = createParticleView(scene);
var lineView = createLineView(scene);
var input = createInputHandler();
var autoPilot = createAutoPilot(camera);
// TODO: This doesn't seem to belong here... Not sure where to put it
var hitTest = createHitTest(particleView, container, input);
var updateTween = window.performance ? highResTimer : dateTimer;
startEventsListening();
frame();
return api;
function getHitTest() {
return hitTest;
}
function createInputHandler() {
var controls = flyControls(camera, container, THREE);
controls.movementSpeed = 200;
controls.rollSpeed = 0.065;
return controls;
}
function frame(time) {
lastFrame = requestAnimationFrame(frame);
renderer.render(scene, camera);
hitTest.update(scene, camera);
input.update(0.1);
updateTween(time);
for (var i = 0; i < rafCallbacks.length; ++i) {
rafCallbacks[i](time);
}
}
function getParicleView() {
return particleView;
}
function particles(coordinates) {
// todo: this should go away when we refactor this into single view
if (coordinates === undefined) {
return particleView.coordinates();
}
particleView.initWithNewCoordinates(coordinates);
if (hitTest) hitTest.destroy();
hitTest = createHitTest(particleView, container, input);
return api;
}
function getContainer() {
return container;
}
function destroy() {
hitTest.destroy();
input.destroy();
stopEventsListening();
container.removeChild(renderer.domElement);
}
function createScene() {
var scene = new THREE.Scene();
scene.sortObjects = false;
return scene;
}
function getScene() {
return scene;
}
function createCamera() {
var camera = new THREE.PerspectiveCamera(45, container.clientWidth / container.clientHeight, 1, 20000);
scene.add(camera);
return camera;
}
function getCamera() {
return camera;
}
function getInput() {
return input;
}
function onFrame(callback) {
rafCallbacks.push(callback)
}
function offFrame(callback) {
var idx = rafCallbacks.indexOf(callback);
if (idx < 0) return;
rafCallbacks.splice(idx, 1);
}
function createRenderer() {
var renderer = new THREE.WebGLRenderer({
antialias: false
});
renderer.setClearColor(options.clearColor, 1);
renderer.setSize(container.clientWidth, container.clientHeight);
container.appendChild(renderer.domElement);
return renderer;
}
function getRenderer() {
return renderer;
}
function startEventsListening() {
window.addEventListener('resize', onWindowResize, false);
}
function stopEventsListening() {
window.removeEventListener('resize', onWindowResize, false);
cancelAnimationFrame(lastFrame);
}
function onWindowResize() {
camera.aspect = container.clientWidth / container.clientHeight;
camera.updateProjectionMatrix();
renderer.setSize(container.clientWidth, container.clientHeight);
}
function drawLines(lines, color) {
lineView.draw(lines, color);
}
function around(r, x, y, z) {
autoPilot.around(r, x, y, z);
}
function lookAt(index, done, distanceFromTarget) {
// todo: this should tak x,y,z instead
var points = particleView.coordinates()
var pos = {
x: points[index],
y: points[index + 1],
z: points[index + 2]
};
autoPilot.flyTo(pos, done, distanceFromTarget);
}
function highResTimer(time) {
TWEEN.update(time);
}
function dateTimer(time) {
TWEEN.update(+new Date());
}
}