-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsquare.js
More file actions
323 lines (271 loc) · 8.53 KB
/
square.js
File metadata and controls
323 lines (271 loc) · 8.53 KB
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
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
// contexts
let container;
let camera;
let renderer;
let scene;
let foreLight;
let clock = new THREE.Clock();
let columns = []
// stats
let stats = new Stats();
stats.showPanel(0); // 0: fps, 1: ms, 2: mb, 3+: custom
document.body.appendChild( stats.dom );
// initials
let cameraPositionInitial = {x: 0, y: 160, z:0}
function createLights() {
// Create a directional light
// const light = new THREE.DirectionalLight(0x5a5a5a, 10.0);
const light = new THREE.DirectionalLight(0xFFFFFF, 10.0);
light.position.set(0, 100, 0);
scene.add(light);
}
function addForelight() {
// foreLight = new THREE.SpotLight(0xFFFFFF, 1.0);
// foreLight.position.set(0, 5, 25);
// foreLight.target.position.set(0, 0, 10);
// scene.add(foreLight);
}
function removeForelight() {
scene.remove(foreLight)
}
function init(quant) {
// Get a reference to the container element that will hold our scene
container = document.querySelector("#scene-container");
// create scenes
backgroundScene = new THREE.Scene();
backgroundScene.background = new THREE.Color(0x000000);
scene = new THREE.Scene();
// set up the options for a perspective camera
const fov = 35; // fov = Field Of View
const aspect = container.clientWidth / container.clientHeight;
const near = 0.1;
const far = 3000;
createLights()
camera = new THREE.PerspectiveCamera(fov, aspect, near, far);
// every object is initially created at ( 0, 0, 0 )
// we'll move the camera back a 4bit so that we can view the scene
camera.position.set(cameraPositionInitial.x, cameraPositionInitial.y, cameraPositionInitial.z);
camera.lookAt( new THREE.Vector3(0, 0, 0));
// create a WebGLRenderer and set its width and height
renderer = new THREE.WebGLRenderer();
renderer.setSize(container.clientWidth, container.clientHeight);
renderer.setPixelRatio(window.devicePixelRatio);
// add the automatically created <canvas> element to the page
columns = []
container.appendChild(renderer.domElement);
for (var x = -quant; x <= quant; x +=0.1) {
for (var z = -quant; z <= quant; z += 0.1) {
columns.push({
mesh: column(x, 1, z),
position: {'x': x, 'y': 1, 'z': z}
})
}
}
}
function animate() {
setTimeout( function() {
stats.begin();
requestAnimationFrame( animate );
start();
stats.end();
}, 1000 / 60 );
renderer.render(scene, camera);
}
let order = [];
let halt = false
let routineItem = 0;
function start() {
if (halt) return;
let zoom1 = 1
let move1 = 1
let zoom2 = 1
let move2 = 1.5
return zoomIn((zoom1 - (zoom1 - clock.getElapsedTime())) / zoom1, {start: cameraPositionInitial.y, end: 25}, function() {
return moveColumn((move1 - (move1 - clock.getElapsedTime() + zoom1)) / move1, {x: 0.05, y: 0, z: 0.05}, function() {
return zoomIn((zoom2 - (zoom2 - clock.getElapsedTime() + zoom1+move1)) / zoom2, {start: 25, end: 5}, function() {
return moveColumn((move2 - (move2 - clock.getElapsedTime() + zoom1+move1+zoom2)) / move2, {x: 0.1, y: 0, z: 0.1}, function() {
resetCamera(true, false, true)
resetColumns()
clock.start()
})
})
})
})
}
function zoomIn(progress, options, callback) {
if (progress > 1) {
return callback();
}
camera.position.y = options['start'] - (progress * (options['start'] - options['end']))
}
function moveColumn(progress, options, callback) {
if (progress > 1) {
return callback();
}
moveColumns((progress)*options['x'], (progress)*options['y'], (progress)*options['z'])
}
function routine() {
if (halt) return
// if (order.length == 0) order = [0, 1, 2, 3, 1, 2, 3, 1, 2, 3, 1, 2, 3, 1, 2, 3, 1, -1]
if (order.length == 0) order = [0, 1, 2, 1, 2, 1, 2, 1, -1]
// if (order.length == 0) order = [1]
resetCamera(true, false, true)
if (order[0] == -1) {
itemCount = cameraPositionInitial.y
camera.position.y = cameraPositionInitial.y
resetColumns()
order.shift()
}
// initial zoom
if (order[0] == 0) {
if (itemCount > 100) {
itemCount -= 10
ifReady(0.01, function() { camera.position.y = itemCount })
return
}
order.shift()
itemCount = 0
}
// explode
if (order[0] == 1) {
moveColumns(0.03, 0, 0.03)
if (itemCount > 30) {
order.shift()
itemCount = 0
return
}
itemCount++
}
// camera rotate on axis
if (order[0] == 2) {
rotations = 45
if (itemCount <= rotations) {
camera.rotation.z += 2*Math.PI / 180
itemCount += 1
}
if (itemCount >= rotations) {
camera.rotation.z = roundUpToNearest(camera.rotation.z, Math.PI/2)
itemCount = 0
order.shift()
return
}
}
// implode
if (order[0] == 3) {
moveColumns(-0.01, 0, -0.01)
if (itemCount > 30) {
order.shift()
itemCount = 0
return
}
itemCount++
}
// camera rotate back on axis
if (order[0] == 4) {
rotations = 45
if (itemCount <= rotations) {
camera.rotation.z += 2 * Math.PI / 180
itemCount += 1
}
if (itemCount >= rotations) {
camera.rotation.z = roundUpToNearest(camera.rotation.z, Math.PI/2)
if (camera.rotation.z == 2*Math.PI) camera.rotation.z = 0
itemCount = 0
order.shift()
return
}
}
// make some pink
if (order[0] == 5) {
colors(0)
order.shift()
}
// make some pink
if (order[0] == 6) {
columns.forEach(function (c, i) {
if (c.position.x > 0) {
c.mesh.material.color.setHex(0xFC427B)
}
})
order.shift()
}
// perspective
if (routineItem == 'a') {
if (itemCount < 2) {
if (lastTime > 0.1 && itemCount == 0) {
addForelight()
lastTime = 0
itemCount = 1
}
if (lastTime > 0.5) {
removeForelight()
lastTime = 0
itemCount = 2
}
lastTime += clock.getDelta()
}
routineItem = 0
}
}
let c = new THREE.Clock(true)
let since = 0;
function ifReady(milliseconds, callback) {
since += c.getDelta()
if (since > milliseconds) {
since = 0
return callback()
}
}
function roundUpToNearest(number, multiple) {
var half = multiple/2
return Math.max(multiple, number+half - (number+half) % multiple)
}
let material = new THREE.MeshLambertMaterial({color: 0xFFFFFF, transparent: true});
let geometry = new THREE.BoxBufferGeometry(0.1, 1, 0.1);
function column(x, y, z) {
let innerMesh = new THREE.Mesh(geometry, material);
innerMesh.position.set(x, y, z);
scene.add(innerMesh);
return innerMesh;
}
function moveColumns(x, y, z) {
ifReady(0.005, () => columns.forEach(function (c, i) {
c.mesh.position.x += x * c.mesh.position.x
c.mesh.position.y += y * c.mesh.position.y
c.mesh.position.z += z * c.mesh.position.z
}))
}
function resetColumns() {
columns.forEach(function (c, i) {
c.mesh.position.x = c.position.x
c.mesh.position.y = c.position.y
c.mesh.position.z = c.position.z
})
}
function resetCamera(x, y, z) {
camera.position.x = cameraPositionInitial.x
camera.position.y = cameraPositionInitial.y
camera.position.z = cameraPositionInitial.z
return;
// if we had odd numbers, set camera to center col
if (columns.length % 2 != 0) {
var col = columns[Math.round(columns.length/2)-1].mesh.position
if (x) camera.position.x = col.x
if (y) camera.position.y = col.y
if (z) camera.position.z = col.z
return
}
let avgPosition = {x: 0, y: 0, z: 0}
columns.forEach(function (c, i) {
avgPosition.x += c.mesh.position.x
avgPosition.y += c.mesh.position.y
avgPosition.z += c.mesh.position.z
})
camera.position.x = avgPosition.x / columns.length + cameraPositionInitial.x
camera.position.y = (avgPosition.y / columns.length) + cameraPositionInitial.y
camera.position.z = avgPosition.z / columns.length + cameraPositionInitial.z
}
init(0.35)
resetCamera()
animate()
addForelight()