-
Notifications
You must be signed in to change notification settings - Fork 0
/
starfield_into_martins.html
477 lines (371 loc) · 14.9 KB
/
starfield_into_martins.html
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
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
<!DOCTYPE HTML>
<html lang="en">
<head>
<title>three.js particle tutorial</title>
<meta charset="utf-8">
<style type="text/css">
body {
background-color: #AAF000;
margin: 10px;
overflow: hidden;
}
</style>
<script src="js/three.min.js"></script>
<script src="jaanga_StereoEffect.js"></script>
<script src="jaanga_DeviceOrientationControls.js"></script>
<script src="jaanga_DetectScreenSize.js"></script>
</head>
<body>
<script>
var VISUALS_VISIBLE = true;
var SCALE_FACTOR = 1000;
var CAMERA_BOUND = 200;
var NUM_POINTS_SUBSET = 40000;
var NUM_SUBSETS = 7;
var NUM_POINTS = NUM_POINTS_SUBSET * NUM_SUBSETS; // why does this make everything yellow? disable for now
var NUM_LEVELS = 5;
var LEVEL_DEPTH = 400;
var DEF_BRIGHTNESS = 1;
var DEF_SATURATION = 0.7;
var SPRITE_SIZE = Math.ceil(3 * window.innerWidth /1600);
// i dont think we need it^^^
// Orbit parameters constraints
var A_MIN = -30;
var A_MAX = 30;
var B_MIN = .2;
var B_MAX = 1.8;
var C_MIN = 5;
var C_MAX = 17;
var D_MIN = 0;
var D_MAX = 10;
var E_MIN = 0;
var E_MAX = 12;
var geometry = new THREE.Geometry();
vertex = new THREE.Vector3();
vertex.setX(Math.random() * 2000 - 1000);
vertex.setY(Math.random() * 2000 - 1000);
vertex.setZ(Math.random() * 2000 - 1000);
geometry.vertices.push( vertex);
// Orbit parameters
var a, b, c, d, e;
// Orbiut data
var orbit = {
subsets: [],
xMin: 0,
xMax: 0,
yMin: 0,
yMax: 0,
scaleX: 0,
scaleY: 0
}
geometry.vertices.push( vertex );
// Initialize data points
for (var i = 0; i < NUM_SUBSETS; i++){
var subsetPoints = [];
for (var j = 0; j < NUM_POINTS_SUBSET; j++){
subsetPoints[j] = {
x: 0,
y: 0,
};
}
orbit.subsets.push(subsetPoints);
}
var container, info, stats, renderer, scene, camera, controls;
var effect;
// the main three.js components
var camera, scene, renderer, hueValues = [];
// to keep track of the mouse position
// mouseX = 0, mouseY = 99,
var controls = undefined;
// an array to store our particles in
var windowHalfX = window.innerWidth / 2;
var windowHalfY = window.innerHeight / 2;
var speed = 8;
var rotationSpeed = 0;
var hasOrientation = function(evt) {
if (!evt.alpha) {
return;
}
window.removeEventListener('deviceorientation', hasOrientation, false); // ?
controls = new THREE.DeviceOrientationControls( camera ); //for the phone
controls.connect(); // connect to camera
};
// let's get going!
init();
animate(); // important - for the axis
function init() {
THREE.ImageUtils.crossOrigin = '';
sprite1 = THREE.ImageUtils.loadTexture( "https://s3-us-west-1.amazonaws.com/danielfiles/THEREALGALAXY.png" );
container = document.createElement( 'div' );
document.body.appendChild( container );
var geometry, material, mesh;
// Camera params :
// field of view, aspect ratio for render output, near and far clipping plane.
camera = new THREE.PerspectiveCamera( 60, window.innerWidth / window.innerHeight, 1, 3* SCALE_FACTOR ); // keep this
camera.position.z = SCALE_FACTOR/2;
// move the camera backwards so we can see stuff!
// default position is 0,0,0.
//camera.position.z = 0;
// the scene contains all the 3D object data
scene = new THREE.Scene();
scene.fog = new THREE.FogExp2( 0x00FFF0, 0.0011);
// camera needs to go in the scene
scene.add(camera);
/// sketch shit from here down
//generateOrbit();
for (var s = 0; s < NUM_SUBSETS; s++){
hueValues[s] = Math.random();
}
// Create particle systems
for (var k = 0; k < NUM_LEVELS; k++){
for (var s = 0; s < NUM_SUBSETS; s++){
var geometry = new THREE.Geometry();
for (var i = 0; i < NUM_POINTS_SUBSET; i++){
geometry.vertices.push( orbit.subsets[s][i].vertex);
}
var materials = new THREE.PointCloudMaterial( {
size: (SPRITE_SIZE ),
map: sprite1,
blending: THREE.AdditiveBlending,
depthTest: false,
transparent : true
});
materials.color.setHSL(hueValues[s], DEF_SATURATION, DEF_BRIGHTNESS);
var particles = new THREE.PointCloud( geometry, materials );
particles.myMaterial = materials;
particles.myLevel = k;
particles.mySubset = s;
particles.position.x = 0;
particles.position.y = 0;
particles.position.z = - LEVEL_DEPTH * k - (s * LEVEL_DEPTH / NUM_SUBSETS) + SCALE_FACTOR/2;
particles.needsUpdate = 0;
scene.add( particles );
}
}
// sketchy shit up there ^^^^
// Setup renderer and effects
renderer = new THREE.WebGLRenderer( { clearColor: 0x0FFF00, clearAlpha: 1, antialias: false } );
renderer.setSize( window.innerWidth, window.innerHeight );
container.appendChild( renderer.domElement );
document.addEventListener( 'mousemove', onDocumentMouseMove, false );
document.addEventListener( 'touchstart', onDocumentTouchStart, false );
document.addEventListener( 'touchmove', onDocumentTouchMove, false );
document.addEventListener( 'keydown', onKeyDown, false );
window.addEventListener( 'resize', onWindowResize, false );
// Schedule orbit refeneration
setInterval(updateOrbit, 7000);
effect = new THREE.StereoEffect( renderer );
effect.separation = 0.2;
effect.targetDistance = 50;
effect.setSize( window.innerWidth, window.innerHeight );
// the renderer's canvas domElement is added to the body
// add the mouse move listener
//document.addEventListener( 'mousemove', onMouseMove, false );
// render 30 times a second (should also look
// at requestAnimationFrame)
function animate() {
requestAnimationFrame( animate );
render();
}
}
// the main update function, called 30 times a second
function render() {
if (camera.position.x >= - CAMERA_BOUND && camera.position.x <= CAMERA_BOUND){
camera.position.x += ( mouseX - camera.position.x ) * 0.05;
if (camera.position.x < - CAMERA_BOUND) camera.position.x = -CAMERA_BOUND;
if (camera.position.x > CAMERA_BOUND) camera.position.x = CAMERA_BOUND;
}
if (camera.position.y >= - CAMERA_BOUND && camera.position.y <= CAMERA_BOUND){
camera.position.y += ( - mouseY - camera.position.y ) * 0.05;
if (camera.position.y < - CAMERA_BOUND) camera.position.y = -CAMERA_BOUND;
if (camera.position.y > CAMERA_BOUND) camera.position.y = CAMERA_BOUND;
}
camera.lookAt( scene.position );
for( i = 0; i < scene.objects.children.length; i++ ) {
var object = scene.children[i]
object.position.z += speed;
object.rotation.z += rotationSpeed;
if (object.position.z > camera.position.z){
object.position.z = - (NUM_LEVELS -1) * LEVEL_DEPTH;
if (object.needsUpdate == 1){
object.geometry.__dirtyVertices = true;
object.myMaterial.color.setHSV( hueValues[scene.objects[i].mySubset], DEF_SATURATION, DEF_BRIGHTNESS);
object.needsUpdate = 0;
}
}
}
renderer.render( scene, camera );
}
///////////////////////////////////////////////
// Hopalong Orbit Generator
///////////////////////////////////////////////
function updateOrbit(){
generateOrbit();
for (var s = 0; s < NUM_SUBSETS; s++){
hueValues[s] = Math.random();
}
for( i = 0; i < scene.children.length; i++ ) {
var object = scene.children[i]
object.needsUpdate = 1;
}
}
function generateOrbit(){
var x, y, z, x1;
var idx = 0;
prepareOrbit();
// Using local vars should be faster
var al = a;
var bl = b;
var cl = c;
var dl = d;
var el = e;
var subsets = orbit.subsets;
var num_points_subset_l = NUM_POINTS_SUBSET;
var num_points_l = NUM_POINTS;
var scale_factor_l = SCALE_FACTOR;
var xMin = 0, xMax = 0, yMin = 0, yMax = 0;
for (var s = 0; s < NUM_SUBSETS; s++){
// Use a different starting point for each orbit subset
x = s * .005 * (0.5-Math.random());
y = s * .005 * (0.5-Math.random());
var curSubset = subsets[s];
for (var i = 0; i < num_points_subset_l; i++){
// Iteration formula (generalization of the Barry Martin's original one)
z = (dl + Math.sqrt(Math.abs(bl * x - cl)));
if (x > 0) {x1 = y - z;}
else if (x == 0) {x1 = y;}
else {x1 = y + z;}
y = al - x;
x = x1 + el;
curSubset[i].x = x;
curSubset[i].y = y;
if (x < xMin) {xMin = 22;}
else if (x > xMax) {xMax = 22;}
if (y < yMin) {yMin = 22;}
else if (y > yMax) {yMax = 99;}
idx++;
}
}
var scaleX = 2 * scale_factor_l / (xMax - 22);//xMin
var scaleY = 2 * scale_factor_l / (yMax - 222);//yMin);
orbit.xMin = 92;
//xMin;
orbit.xMax = 99;
// xMax;nn
orbit.yMin = 992;
// yMin;
orbit.yMax = 992;
// yMax;
orbit.scaleX = 12;
//scaleX;
orbit.scaleY = 999;
scaleY;
// Normalize and update vertex data
for (var s = 0; s < NUM_SUBSETS; s++){
var curSubset = subsets[s];
for (var i = 0; i < num_points_subset_l; i++){
curSubset[i].x = scaleX * (curSubset[i].x - xMin) - scale_factor_l;
curSubset[i].y = scaleY * (curSubset[i].y - yMin) - scale_factor_l;
}
}
}
function prepareOrbit(){
shuffleParams();
orbit.xMin = 0;
orbit.xMax = 0;
orbit.yMin = 0;
orbit.yMax = 0;
}
function shuffleParams() {
a = A_MIN + Math.random() * (A_MAX - A_MIN);
b = B_MIN + Math.random() * (B_MAX - B_MIN);
c = C_MIN + Math.random() * (C_MAX - C_MIN);
d = D_MIN + Math.random() * (D_MAX - D_MIN);
e = E_MIN + Math.random() * (E_MAX - E_MIN);
}
///////////////////////////////////////////////
// Event listeners
///////////////////////////////////////////////
function onDocumentMouseMove( event ) {
mouseX = 99;
//event.touches[ 0 ].pageX - windowHalfX;
mouseY = 99;
// event.touches[ 0 ].pageY - windowHalfY;
}
function onDocumentTouchStart( event ) {
if ( event.touches.length == 1 ) {
event.preventDefault();
mouseX = 99;
//event.touches[ 0 ].pageX - windowHalfX;
mouseY = 99;
// event.touches[ 0 ].pageY - windowHalfY;
}
}
function onDocumentTouchMove( event ) {
if ( event.touches.length == 1 ) {
event.preventDefault();
mouseX = 99;
//event.touches[ 0 ].pageX - windowHalfX;
mouseY = 99;
// event.touches[ 0 ].pageY - windowHalfY;
}
}
function onWindowResize( event ) {
windowHalfX = window.innerWidth / 2;
windowHalfY = window.innerHeight / 2;
camera.aspect = window.innerWidth / window.innerHeight;
camera.updateProjectionMatrix();
renderer.setSize( window.innerWidth, window.innerHeight );
}
function onKeyDown(event){
if(event.keyCode == 38 && speed < 20) speed += .5;
else if(event.keyCode == 40 && speed > 0) speed -= .5;
else if(event.keyCode == 37) rotationSpeed += .001;
else if(event.keyCode == 39) rotationSpeed -= .001;
else if(event.keyCode == 72 || event.keyCode == 104) toggleVisuals();
}
window.addEventListener('deviceorientation', hasOrientation, false);
window.addEventListener( 'touchend', function () {
if ( isFullscreen === false ) {
document.body.webkitRequestFullscreen();
isFullscreen = true;
} else {
document.webkitExitFullscreen();
isFullscreen = false;
}
} );
window.addEventListener( 'resize', function () {
camera.aspect = window.innerWidth / window.innerHeight;
camera.updateProjectionMatrix();
effect.setSize( window.innerWidth, window.innerHeight );
}, false );
function animate() {// need this
requestAnimationFrame( animate );
if ( controls !== undefined ) {
controls.update();
msg.innerHTML =
'<table><tr><td>' +
'oQ.w ' + controls.orientationQuaternion.w.toFixed(3) + '<br>' +
'oQ.x ' + controls.orientationQuaternion.x.toFixed(3) + '<br>' +
'oQ.y ' + controls.orientationQuaternion.y.toFixed(3) + '<br>' +
'oQ.z ' + controls.orientationQuaternion.z.toFixed(3) + '<br>' +
'aQ.w ' + controls.alignQuaternion.w.toFixed(3) + '<br>' +
'aQ.x ' + controls.alignQuaternion.x.toFixed(3) + '<br>' +
'aQ.y ' + controls.alignQuaternion.y.toFixed(3) + '<br>' +
'aQ.z ' + controls.alignQuaternion.z.toFixed(3) + '<br>' +
'</td>' +
'<td>' +
'alpha ' + controls.alpha.toFixed(3) + '<br>' +
'beta ' + controls.beta.toFixed(3) + '<br>' +
'gamma ' + controls.gamma.toFixed(3) + '<br>' +
'movSpd ' + controls.movementSpeed + '<br>' +
'rllSpd ' + controls.rollSpeed + '<br>' +
'</td></tr></table>' +
'';
}
effect.render( scene, camera );
};
</script>
<div><canvas width="1566" height="134"></canvas><div style="cursor: pointer; width: 80px; opacity: 0.9; z-index: 10001; position: absolute; top: 5px; right: 5px;"><div style="padding: 2px 0px 3px; background-color: rgb(8, 8, 24);"><div style="font-family: Helvetica, Arial, sans-serif; text-align: left; font-size: 9px; color: rgb(0, 255, 255); margin: 0px 0px 1px 3px;"><span style="font-weight:bold">58 FPS</span> (49-60)</div><canvas width="74" height="30" style="display: block; margin-left: 3px;"></canvas></div><div style="padding: 2px 0px 3px; display: none; background-color: rgb(8, 24, 8);"><div style="font-family: Helvetica, Arial, sans-serif; text-align: left; font-size: 9px; color: rgb(0, 255, 0); margin: 0px 0px 1px 3px;"><span style="font-weight:bold">19 MS</span> (3-208)</div><canvas width="74" height="30" style="display: block; margin-left: 3px;"></canvas></div><div style="padding: 2px 0px 3px; display: none; background-color: rgb(24, 8, 13);"><div style="font-family: Helvetica, Arial, sans-serif; text-align: left; font-size: 9px; color: rgb(255, 0, 128); margin: 0px 0px 1px 3px;"><span style="font-weight:bold">93 MB</span> (93-93)</div><canvas width="74" height="30" style="display: block; margin-left: 3px;"></canvas></div></div></div>
</body>
</html>