-
Notifications
You must be signed in to change notification settings - Fork 2
/
newscript.js
371 lines (305 loc) · 9.08 KB
/
newscript.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
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
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
window.onload = init;
function init() {
var root = new THREERoot({
createCameraControls:!true,
antialias:(window.devicePixelRatio === 1),
fov:60
});
root.renderer.setClearColor(0x000000);
root.renderer.setPixelRatio(window.devicePixelRatio || 1);
root.camera.position.set(0, 0, 600);
var textAnimation = createTextAnimation();
root.scene.add(textAnimation);
var light = new THREE.DirectionalLight();
light.position.set(0, 0, 1);
root.scene.add(light);
var tl = new TimelineMax({
repeat:-1,
repeatDelay:0.5,
yoyo:true
});
tl.fromTo(textAnimation, 4,
{animationProgress:0.0},
{animationProgress:0.6, ease:Power1.easeInOut},
0
);
tl.fromTo(textAnimation.rotation, 4, {y:0}, {y:Math.PI * 2, ease:Power1.easeInOut}, 0);
createTweenScrubber(tl);
}
function createTextAnimation() {
var geometry = generateTextGeometry('WDCT CORNER', {
size:40,
height:12,
font:'droid sans',
weight:'bold',
style:'normal',
curveSegments:24,
bevelSize:2,
bevelThickness:2,
bevelEnabled:true,
anchor:{x:0.5, y:0.5, z:0.0}
});
THREE.BAS.Utils.tessellateRepeat(geometry, 1.0, 2);
THREE.BAS.Utils.separateFaces(geometry);
return new TextAnimation(geometry);
}
function generateTextGeometry(text, params) {
var geometry = new THREE.TextGeometry(text, params);
geometry.computeBoundingBox();
var size = geometry.boundingBox.size();
var anchorX = size.x * -params.anchor.x;
var anchorY = size.y * -params.anchor.y;
var anchorZ = size.z * -params.anchor.z;
var matrix = new THREE.Matrix4().makeTranslation(anchorX, anchorY, anchorZ);
geometry.applyMatrix(matrix);
return geometry;
}
////////////////////
// CLASSES
////////////////////
function TextAnimation(textGeometry) {
var bufferGeometry = new THREE.BAS.ModelBufferGeometry(textGeometry);
var aAnimation = bufferGeometry.createAttribute('aAnimation', 2);
var aEndPosition = bufferGeometry.createAttribute('aEndPosition', 3);
var aAxisAngle = bufferGeometry.createAttribute('aAxisAngle', 4);
var faceCount = bufferGeometry.faceCount;
var i, i2, i3, i4, v;
var maxDelay = 0.0;
var minDuration = 1.0;
var maxDuration = 1.0;
var stretch = 0.05;
var lengthFactor = 0.001;
var maxLength = textGeometry.boundingBox.max.length();
this.animationDuration = maxDuration + maxDelay + stretch + lengthFactor * maxLength;
this._animationProgress = 0;
var axis = new THREE.Vector3();
var angle;
for (i = 0, i2 = 0, i3 = 0, i4 = 0; i < faceCount; i++, i2 += 6, i3 += 9, i4 += 12) {
var face = textGeometry.faces[i];
var centroid = THREE.BAS.Utils.computeCentroid(textGeometry, face);
var centroidN = new THREE.Vector3().copy(centroid).normalize();
// animation
var delay = (maxLength - centroid.length()) * lengthFactor;
var duration = THREE.Math.randFloat(minDuration, maxDuration);
for (v = 0; v < 6; v += 2) {
aAnimation.array[i2 + v ] = delay + stretch * Math.random();
aAnimation.array[i2 + v + 1] = duration;
}
// end position
var point = utils.fibSpherePoint(i, faceCount, 200);
for (v = 0; v < 9; v += 3) {
aEndPosition.array[i3 + v ] = point.x;
aEndPosition.array[i3 + v + 1] = point.y;
aEndPosition.array[i3 + v + 2] = point.z;
}
// axis angle
axis.x = centroidN.x;
axis.y = -centroidN.y;
axis.z = -centroidN.z;
axis.normalize();
angle = Math.PI * THREE.Math.randFloat(0.5, 2.0);
for (v = 0; v < 12; v += 4) {
aAxisAngle.array[i4 + v ] = axis.x;
aAxisAngle.array[i4 + v + 1] = axis.y;
aAxisAngle.array[i4 + v + 2] = axis.z;
aAxisAngle.array[i4 + v + 3] = angle;
}
}
var material = new THREE.BAS.PhongAnimationMaterial({
shading: THREE.FlatShading,
side: THREE.DoubleSide,
transparent: true,
uniforms: {
uTime: {type: 'f', value: 0}
},
shaderFunctions: [
THREE.BAS.ShaderChunk['cubic_bezier'],
THREE.BAS.ShaderChunk['ease_out_cubic'],
THREE.BAS.ShaderChunk['quaternion_rotation']
],
shaderParameters: [
'uniform float uTime;',
'uniform vec3 uAxis;',
'uniform float uAngle;',
'attribute vec2 aAnimation;',
'attribute vec3 aEndPosition;',
'attribute vec4 aAxisAngle;'
],
shaderVertexInit: [
'float tDelay = aAnimation.x;',
'float tDuration = aAnimation.y;',
'float tTime = clamp(uTime - tDelay, 0.0, tDuration);',
'float tProgress = ease(tTime, 0.0, 1.0, tDuration);'
// 'float tProgress = tTime / tDuration;'
],
shaderTransformPosition: [
'transformed = mix(transformed, aEndPosition, tProgress);',
'float angle = aAxisAngle.w * tProgress;',
'vec4 tQuat = quatFromAxisAngle(aAxisAngle.xyz, angle);',
'transformed = rotateVector(tQuat, transformed);',
]
},
{
diffuse: 0x444444,
specular: 0xcccccc,
shininess: 4
//emissive:0xffffff
}
);
THREE.Mesh.call(this, bufferGeometry, material);
this.frustumCulled = false;
}
TextAnimation.prototype = Object.create(THREE.Mesh.prototype);
TextAnimation.prototype.constructor = TextAnimation;
Object.defineProperty(TextAnimation.prototype, 'animationProgress', {
get: function() {
return this._animationProgress;
},
set: function(v) {
this._animationProgress = v;
this.material.uniforms['uTime'].value = this.animationDuration * v;
}
});
function THREERoot(params) {
params = utils.extend({
fov:60,
zNear:10,
zFar:100000,
createCameraControls:true
}, params);
this.renderer = new THREE.WebGLRenderer({
antialias:params.antialias
});
this.renderer.setPixelRatio(Math.min(2, window.devicePixelRatio || 1));
document.getElementById('three-container').appendChild(this.renderer.domElement);
this.camera = new THREE.PerspectiveCamera(
params.fov,
window.innerWidth / window.innerHeight,
params.zNear,
params.zfar
);
this.scene = new THREE.Scene();
if (params.createCameraControls) {
this.controls = new THREE.OrbitControls(this.camera, this.renderer.domElement);
}
this.resize = this.resize.bind(this);
this.tick = this.tick.bind(this);
this.resize();
this.tick();
window.addEventListener('resize', this.resize, false);
}
THREERoot.prototype = {
tick: function() {
this.update();
this.render();
requestAnimationFrame(this.tick);
},
update: function() {
this.controls && this.controls.update();
},
render: function() {
this.renderer.render(this.scene, this.camera);
},
resize: function() {
this.camera.aspect = window.innerWidth / window.innerHeight;
this.camera.updateProjectionMatrix();
this.renderer.setSize(window.innerWidth, window.innerHeight);
}
};
////////////////////
// UTILS
////////////////////
var utils = {
extend:function(dst, src) {
for (var key in src) {
dst[key] = src[key];
}
return dst;
},
randSign: function() {
return Math.random() > 0.5 ? 1 : -1;
},
ease:function(ease, t, b, c, d) {
return b + ease.getRatio(t / d) * c;
},
// mapEase:function(ease, v, x1, y1, x2, y2) {
// var t = v;
// var b = x2;
// var c = y2 - x2;
// var d = y1 - x1;
//
// return utils.ease(ease, t, b, c, d);
// },
fibSpherePoint: (function() {
var v = {x:0, y:0, z:0};
var G = Math.PI * (3 - Math.sqrt(5));
return function(i, n, radius) {
var step = 2.0 / n;
var r, phi;
v.y = i * step - 1 + (step * 0.5);
r = Math.sqrt(1 - v.y * v.y);
phi = i * G;
v.x = Math.cos(phi) * r;
v.z = Math.sin(phi) * r;
radius = radius || 1;
v.x *= radius;
v.y *= radius;
v.z *= radius;
return v;
}
})()
};
function createTweenScrubber(tween, seekSpeed) {
seekSpeed = seekSpeed || 0.001;
function stop() {
TweenMax.to(tween, 1, {timeScale:0});
}
function resume() {
TweenMax.to(tween, 1, {timeScale:1});
}
function seek(dx) {
var progress = tween.progress();
var p = THREE.Math.clamp((progress + (dx * seekSpeed)), 0, 1);
tween.progress(p);
}
var _cx = 0;
// desktop
var mouseDown = false;
document.body.style.cursor = 'pointer';
window.addEventListener('mousedown', function(e) {
mouseDown = true;
document.body.style.cursor = 'ew-resize';
_cx = e.clientX;
stop();
});
window.addEventListener('mouseup', function(e) {
mouseDown = false;
document.body.style.cursor = 'pointer';
resume();
});
window.addEventListener('mousemove', function(e) {
if (mouseDown === true) {
var cx = e.clientX;
var dx = cx - _cx;
_cx = cx;
seek(dx);
}
});
/*mobile*/
// window.addEventListener('touchstart', function(e) {
// _cx = e.touches[0].clientX;
// stop();
// e.preventDefault();
// },{passive: false});
// window.addEventListener('touchend', function(e) {
// resume();
// e.preventDefault();
// });
// window.addEventListener('touchmove', function(e) {
// var cx = e.touches[0].clientX;
// var dx = cx - _cx;
// _cx = cx;
// seek(dx);
// e.preventDefault();
// });
}