-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwebgl-sqlstream.js
363 lines (272 loc) · 10.3 KB
/
webgl-sqlstream.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
'use strict';
function WebGL (){
var that = this;
var dotsArray = [];
var stats;
var gui = new dat.GUI();
this.torusCreated = false;
this.defaultParams = {
width: window.innerWidth,
height: window.innerHeight,
container: "#webgl-container",
models: {
landscape: "./models/landscape.json"
},
torus: {
color: "#AAAAAA",
color1: "#00cea0",
color2: "#fd8925",
model: {},
initVertices: [],
centroid: new THREE.Vector3(),
height: 0.04,
frequency: 100,
amplifier: 2,
radius: 0.8,
thickness: 0.13,
radialSegments: 96,
tubularSegments: 96,
distance: -2.4
},
landscape: {
model: {},
initVertices: [],
animate: true,
width: 6,
height: 3,
widthSegments: 96,
heightSegments: 96
},
renderer: {
alpha: true,
antialias: true
}
};
this.Utils = {
onWindowResize: function(){
that._WIDTH = window.innerWidth;
that._HEIGHT = window.innerHeight;
that._CAMERA.aspect = that._WIDTH/that._HEIGHT;
that._CAMERA.updateProjectionMatrix();
that._RENDERER.setSize( that._WIDTH, that._HEIGHT );
},
checkValue: function(vector, value){
var v;
if ( vector <= 0 ){
v = vector - value;
} else {
v = vector + value;
}
return v;
},
calcSin: function(vector, altitude){
var sin = vector + Math.sin(altitude * that.torus.height );
return sin;
}
};
this.config = function(options) {
var container = options.container || that.defaultParmas.container;
var rendererOptions = options.renderer || that.defaultParmas.renderer;
// Config Landscape
that.torus = this.defaultParams.torus;
that.torus.color = options.torus.color;
that.torus.color1 = options.torus.color1;
that.torus.color2 = options.torus.color2;
that.torus.height = options.torus.height;
that.torus.frequency = options.torus.frequency;
that.torus.amplifier = options.torus.amplifier;
that.torus.radius = options.torus.radius;
that.torus.thickness = options.torus.thickness;
that.torus.radialSegments = options.torus.radialSegments;
that.torus.tubularSegments = options.torus.tubularSegments;
// Config Landscape
that.landscape = that.defaultParams.landscape;
that.landscape.animate = options.landscape.animate;
// DAT GUI Hooks
var generalFolder = gui.addFolder("Shared Options");
generalFolder.open();
generalFolder.addColor(that.torus, "color").onChange(updateView);
generalFolder.addColor(that.torus, "color1").onChange(updateView);
generalFolder.addColor(that.torus, "color2").onChange(updateView);
generalFolder.add(that.torus, "frequency", 1, 1000).onFinishChange(updateView);
generalFolder.add(that.torus, "amplifier", 0.1, 16).onFinishChange(updateView);
var landscapeFolder = gui.addFolder("Landscape");
landscapeFolder.open();
landscapeFolder.add( that.landscape, "width", 1, 24).onFinishChange(updateView);
landscapeFolder.add( that.landscape, "height", 1, 24).onFinishChange(updateView);
landscapeFolder.add( that.landscape, "widthSegments", 16, 256).onFinishChange(updateView);
landscapeFolder.add( that.landscape, "heightSegments", 16, 256).onFinishChange(updateView);
var torusFolder = gui.addFolder("Torus");
torusFolder.add( that.torus, "radius", 0.1, 2).onFinishChange(updateView);
torusFolder.add( that.torus, "thickness", 0.01, 1).onFinishChange(updateView);
torusFolder.add( that.torus, "radialSegments", 1, 256).onFinishChange(updateView);
torusFolder.add( that.torus, "tubularSegments", 1, 256).onFinishChange(updateView);
// Remove this when in production
stats = new Stats();
stats.showPanel( 0 ); // 0: fps, 1: ms, 2: mb, 3+: custom
stats.domElement.style.position = "absolute";
stats.domElement.style.top = 0;
document.body.appendChild( stats.domElement );
this._WIDTH = options.width || that.defaultParmas.width;
this._HEIGHT = options.height || that.defaultParmas.height;;
this._CONTAINER = document.querySelector( container );
this.isChanging = false;
this._RENDERER = new THREE.WebGLRenderer({
alpha: rendererOptions.alpha,
antialias: rendererOptions.antialias
});
this._RENDERER.setSize( this._WIDTH, this._HEIGHT );
this._RENDERER.getPixelRatio( window.devicePixelRatio || 1 );
this._CONTAINER.appendChild( this._RENDERER.domElement );
this._SCENE = new THREE.Scene();
this._CAMERA = new THREE.PerspectiveCamera( 65, this._WIDTH/this._HEIGHT, 0.01, 1000 );
this._CAMERA.position.z = 0.1;
init();
};
// Setup perlin noise
var counter = 0;
noise.seed(Math.random());
// Animate objects
this.setTorusPosition = function(x,y,z){
that.torus.model.position.set( x, y, z);
};
this.animateTorusPosition = function(x,y,z, duration){
TweenLite.to(that.torus.model.position, duration, { x: x, y: y, z: z });
};
this.updateTorus = function(){
var speedT = counter/that.torus.frequency;
var vertex, vertex2, vertex3, value, altitudeX, altitudeY, altitudeZ;
if ( that.torus.model.geometry === undefined ){
return;
}
var initArray = that.torus.initVertices,
position = that.torus.model.geometry.attributes.position || [];
for ( var i = 0, lenT = position.count; i < lenT; i++){
vertex = initArray[i*3];
vertex2 = initArray[i*3+1];
vertex3 = initArray[i*3+2];
value = noise.simplex3( vertex * that.torus.amplifier, vertex2 * that.torus.amplifier, speedT );
altitudeX = that.Utils.calcSin(vertex, that.Utils.checkValue(vertex, value));
altitudeY = that.Utils.calcSin(vertex2, that.Utils.checkValue(vertex2, value));
altitudeZ = that.Utils.calcSin(vertex3, that.Utils.checkValue(vertex3, value));
position.array[i*3] = altitudeX;
position.array[i*3+1] = altitudeY;
position.array[i*3+2] = altitudeZ;
};
position.needsUpdate = true;
};
this.updateLandscape = function(){
var speedL = counter/that.torus.frequency*0.6;
var vertex, vertex2, vertex3, value;
if ( that.landscape.model.geometry === undefined ){
return;
}
var initArray = that.landscape.initVertices,
position = that.landscape.model.geometry.attributes.position;
for ( var i = 0, lenL = position.count; i < lenL; i++ ){
vertex = initArray[i*3];
vertex2 = initArray[i*3+1];
value = noise.perlin3( vertex, vertex2, speedL ) * 0.8;
position.array[i*3+2] = initArray[i*3+2] + value;
}
position.needsUpdate = true;
}
this.createTorus = function(){
// Create Torus
console.log("torus created");
var geometry = new THREE.TorusBufferGeometry( that.torus.radius, that.torus.thickness, that.torus.radialSegments, that.torus.tubularSegments );
var position = geometry.attributes.position;
for ( var i = 0, lenT = position.count; i < lenT; i++){
position.array[i*3+1] += Math.random() * 0.02;
}
position.needsUpdate = true;
var position = geometry.attributes.position;
for ( var i = 0, lenT = position.array; i < lenT; i++){
position.array[i] += Math.random();
}
that.torus.model = new THREE.Mesh( geometry, new THREE.MeshBasicMaterial({ color: that.torus.color, wireframe: true, transparent: true, opacity: 0.4 }) );
var torusSolid =new THREE.Mesh( geometry, new THREE.MeshBasicMaterial({ color: 0xFFFFFF }) );
var torus2 = new THREE.Mesh( geometry,
new THREE.MeshBasicMaterial({ color: new THREE.Color(that.torus.color1), wireframe: true, transparent: true, opacity: 0.16 })
);
var torus3 = new THREE.Mesh( geometry,
new THREE.MeshBasicMaterial({ color: new THREE.Color(that.torus.color2), wireframe: true, transparent: true, opacity: 0.16 })
);
that.torus.model.position.z = that.torus.distance;
that.torus.model.add( torusSolid );
that.torus.model.add( torus2 );
that.torus.model.add( torus3 );
torus2.rotation.x = Math.PI;
torus3.rotation.y = Math.PI;
that.torus.initVertices = new Float32Array(that.torus.model.geometry.attributes.position.array);
that._SCENE.add( that.torus.model );
};
this.removeTorus = function(){
that._SCENE.remove( that.torus.model );
}
this.createLandscape = function(){
// Create Torus
console.log("landscape created");
var geometry = new THREE.PlaneBufferGeometry(that.landscape.width,that.landscape.height, that.landscape.widthSegments, that.landscape.heightSegments);
var position = geometry.attributes.position;
for ( var i = 0, lenT = position.count; i < lenT; i++){
position.array[i*3+2] += Math.random() * 0.02;
}
position.needsUpdate = true;
that.landscape.model = new THREE.Mesh( geometry, new THREE.MeshBasicMaterial({ color: that.torus.color, wireframe: true, transparent: true, opacity: 0.8 }));
var landscapeSolid = new THREE.Mesh( geometry, new THREE.MeshBasicMaterial({ color: 0xFFFFFF, side: THREE.DoubleSide }));
var landscape2 = new THREE.Mesh( geometry, new THREE.MeshBasicMaterial({ color: new THREE.Color(that.torus.color1), wireframe: true, transparent: true, opacity: 0.06 }));
var landscape3 = new THREE.Mesh( geometry, new THREE.MeshBasicMaterial({ color: new THREE.Color(that.torus.color2), wireframe: true, transparent: true, opacity: 0.06 }));
that.landscape.model.add( landscape2 );
that.landscape.model.add( landscape3 );
that.landscape.model.add( landscapeSolid );
landscape2.rotation.x = Math.PI;
landscape3.rotation.z = Math.PI;
that.landscape.model.position.z = -1.6;
that.landscape.model.position.y = -0.8;
that.landscape.model.rotation.x = Math.PI/2;
that.landscape.initVertices = new Float32Array(that.landscape.model.geometry.attributes.position.array);
that._SCENE.add( that.landscape.model );
}
this.removeLandscape = function(){
that._SCENE.remove( that.landscape.model );
}
function updateView(){
if ( that.torusCreated ){
that.removeTorus();
that.createTorus();
} else {
that.removeLandscape();
that.createLandscape();
};
};
function init(){
that.createLandscape();
that.animate();
that.bindEventListeneres();
};
this.render = function(){
that._RENDERER.render( this._SCENE, this._CAMERA );
};
var elapsedTime = 0;
this.update = function(){
counter++;
if ( that.landscape.animate ){
that.updateLandscape();
};
if ( that.torus.animate ){
that.updateTorus();
};
};
this.animate = function(){
stats.begin(); // Remove this when in production
that.update();
that.render();
stats.end(); // Remove this when in production
requestAnimationFrame( that.animate );
};
this.bindEventListeneres = function(){
window.addEventListener("resize", that.Utils.onWindowResize, false);
};
return this;
};