-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.html
367 lines (247 loc) · 8.89 KB
/
index.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
<html>
<head>
<title>Dancing Pixel Mirror</title>
<script type="text/javascript" src="js/three.min.js"></script>
<script type="text/javascript" src="js/tinycolor.js"></script>
<script type="text/javascript" src="js/dancer.js"></script>
<script src="js/vr.js"></script>
<script src="js/bokeh.js"></script>
<script src="js/controls/OculusRiftControls.js"></script>
<script src="js/effects/OculusRiftEffect.js"></script>
<style>
body{ margin:0; padding:0; background-color: black;}
</style>
</head>
<body>
<audio src="/music/deep.mp3" id="theSong"></audio>
<script>
var container;
var camera, scene, renderer, particles, geometry, material, i, h, color, sprite, size;
var mouseX = 0, mouseY = 0;
var positions = [];
var img
var loudness = 0
var vrstate = new vr.State();
var postprocessing = { enabled : true };
var shaderSettings = {
rings: 3,
samples: 4
};
var material_depth;
var imageDataStorage = {}
var windowHalfX = window.innerWidth / 2;
var windowHalfY = window.innerHeight / 2;
var dancer = new Dancer();
var maxW = 208
var maxH = 208
vr.load()
init();
animate();
setTimeout(afterLoad)
function applyImage(imgSrc){
var theImg = new Image()
theImg.src = imgSrc;
theImg.onload = function(){
if( !imageDataStorage[theImg.src] ){
var canvas = document.createElement('canvas')
canvas.width = theImg.width
canvas.height = theImg.height
canvas.getContext('2d').drawImage(theImg, 0, 0, theImg.width, theImg.height);
imageDataStorage[theImg.src] = canvas.getContext('2d').getImageData(0, 0, theImg.width, theImg.height).data
}
imageData = imageDataStorage[theImg.src]
window.getPixel = function(x,y){
var index = (y*theImg.width+x)*4
return {r: imageData[index], g:imageData[index+1], b:imageData[index+2]};
}
// vertex colors
var colors = [];
for( var i = 0; i < positions.length; i++ ) {
colors[i] = new THREE.Color();
var p = positions[i]
var x = Math.round(p.x+maxH/2),
y = Math.round(p.y+maxH/2)
var c = window.getPixel(x,maxH-y)
colors[i].setRGB(c.r/255, c.g/255, c.b/255 )
}
particles.geometry.colors = colors;
}
}
function init() {
container = document.createElement( 'div' );
document.body.appendChild( container );
camera = new THREE.PerspectiveCamera( 55, window.innerWidth / window.innerHeight, 2, 2000 );
camera.position.z = 2000;
material_depth = new THREE.MeshDepthMaterial();
scene = new THREE.Scene();
scene.fog = new THREE.FogExp2( 0x000000, 0.0005 );
geometry = new THREE.Geometry();
sprite = THREE.ImageUtils.loadTexture( "disc.png" );
for ( i = 0; i < 10000; i ++ ) {
var vertex = new THREE.Vector3();
vertex.x = maxW * (Math.random()-.5);
vertex.y = maxH * (Math.random()-.5);
vertex.z = 500 * (Math.random()-1)
positions.push( vertex )
geometry.vertices.push( vertex );
}
material = new THREE.ParticleBasicMaterial( {
size: 35,
//sizeAttenuation: false,
map: sprite,
transparent: true,
//blending: THREE.AdditiveBlending,
//blending: THREE.SubtractiveBlending,
vertexColors: true
});
material.color.setHSL( 1.0, 0.3, 0.7 );
particles = new THREE.ParticleSystem( geometry, material );
particles.sortParticles = true;
scene.add( particles );
//
renderer = new THREE.WebGLRenderer( { clearAlpha: 1 } );
renderer.setSize( window.innerWidth, window.innerHeight );
container.appendChild( renderer.domElement );
effect = new THREE.OculusRiftEffect( renderer );
scene.add( camera );
//
//initPostprocessing()
document.addEventListener( 'mousemove', onDocumentMouseMove, false );
window.addEventListener( 'resize', onWindowResize, false );
for( var i=1; i<=8; i++){
applyImage('horse/'+ i +'@2x.png');
}
}
function afterLoad(){
var a = document.querySelector('#theSong')
dancer.load( a );
dancer.play()
}
function thingy(){
frequency = [ 0, 10 ]
var
max = 0,
fft = this.dancer.getSpectrum();
// Sloppy array check
if ( !frequency.length ) {
return frequency < fft.length ?
fft[ ~~frequency ] :
null;
}
for ( var i = frequency[ 0 ], l = frequency[ 1 ]; i <= l; i++ ) {
if ( fft[ i ] > max ) { max = fft[ i ]; }
}
material.size = 3 + Math.min(50*max,30)//*max*30
loudness = max
//console.log( max ) .04
}
function onWindowResize() {
windowHalfX = window.innerWidth / 2;
windowHalfY = window.innerHeight / 2;
camera.aspect = window.innerWidth / window.innerHeight;
camera.updateProjectionMatrix();
renderer.setSize( window.innerWidth, window.innerHeight );
}
function onDocumentMouseMove( event ) {
mouseX = event.clientX - windowHalfX;
mouseY = event.clientY - windowHalfY;
}
//
function kick(){}
function animate() {
requestAnimationFrame( animate );
render();
}
function initPostprocessing() {
postprocessing.scene = new THREE.Scene();
postprocessing.camera = new THREE.OrthographicCamera( window.innerWidth / - 2, window.innerWidth / 2, window.innerHeight / 2, window.innerHeight / - 2, -10000, 10000 );
postprocessing.camera.position.z = 100;
postprocessing.scene.add( postprocessing.camera );
var pars = { minFilter: THREE.LinearFilter, magFilter: THREE.LinearFilter, format: THREE.RGBFormat };
postprocessing.rtTextureDepth = new THREE.WebGLRenderTarget( window.innerWidth, window.innerHeight, pars );
postprocessing.rtTextureColor = new THREE.WebGLRenderTarget( window.innerWidth, window.innerHeight, pars );
var bokeh_shader = THREE.BokehShader;
postprocessing.bokeh_uniforms = THREE.UniformsUtils.clone( bokeh_shader.uniforms );
postprocessing.bokeh_uniforms[ "tColor" ].value = postprocessing.rtTextureColor;
postprocessing.bokeh_uniforms[ "tDepth" ].value = postprocessing.rtTextureDepth;
postprocessing.bokeh_uniforms[ "textureWidth" ].value = window.innerWidth;
postprocessing.bokeh_uniforms[ "textureHeight" ].value = window.innerHeight;
postprocessing.materialBokeh = new THREE.ShaderMaterial( {
uniforms: postprocessing.bokeh_uniforms,
vertexShader: bokeh_shader.vertexShader,
fragmentShader: bokeh_shader.fragmentShader,
defines: {
RINGS: shaderSettings.rings,
SAMPLES: shaderSettings.samples
}
} );
postprocessing.quad = new THREE.Mesh( new THREE.PlaneGeometry( window.innerWidth, window.innerHeight ), postprocessing.materialBokeh );
postprocessing.quad.position.z = - 500;
postprocessing.scene.add( postprocessing.quad );
}
function render() {
var time = Date.now() * 0.00005;
if( loudness > .06 ){
//particles.scale.x = particles.scale.y = 1+max
material.color.setHSL(loudness, .5,0.5);
} else {
material.color.setHSL(0, 0,0.5);
}
var rotation = new THREE.Quaternion();
var angles = new THREE.Vector3();
if (vrstate) {
rotation.set(
vrstate.hmd.rotation[0],
vrstate.hmd.rotation[1],
vrstate.hmd.rotation[2],
vrstate.hmd.rotation[3]);
angles.setEulerFromQuaternion(rotation, 'XYZ');
//angles.z = 0;
rotation.setFromEuler(angles, 'XYZ');
// velocity.applyQuaternion(rotation);
}
//console.log( rotation )
//rotationSpeed = rotation.z/5
camera.rotation.x = rotation.x//time * 0.25;
camera.rotation.y = rotation.y//time * 0.5;
camera.rotation.z = rotation.z//time * 0.5;
camera.position.z = 300 - Math.abs(rotation.x*1500)
/*for( var i = 0; i < positions.length; i++ ) {
//particles.geometry.vertices[i].x += 3*Math.sin(time*10)+(i%10-5)/10
//particles.geometry.vertices[i].y += 3*Math.cos(time*10)+(i%10-5)/10
particles.geometry.vertices[i].z += 3*Math.sin(time*50+i)//(Math.random()-.1)
particles.geometry.verticesNeedUpdate = true
} */
//console.log( rotation.y )
//var which = Math.round(camera.position.y/10)%8+1
//var which = Math.round(Date.now()/100)%8+1
//console.log(Math.round(20*Math.abs(rotation.y)))
var which = Math.round(Math.abs(rotation.y)*20)%8+1
//img.src=
applyImage("horse/"+which+"@2x.png")
//console.log( img )
//camera.lookAt( scene.position );
//h = ( 360 * ( 1.0 + time ) % 360 ) / 360;
//material.color.setHSL( h, 0.5, 0.5 );
//var t = tinycolor("red");
//particles.materials[ 0 ].color.setHex( 0xff0000 );
//material.size = 35 + 20*Math.sin( time*10 )
vr.pollState(vrstate)
try{
thingy()
} catch(error){}
//renderer.render( scene, camera );
effect.render( scene, camera );
/*renderer.clear();
// Render scene into texture
scene.overrideMaterial = null;
renderer.render( scene, camera, postprocessing.rtTextureColor, true );
// Render depth into texture
scene.overrideMaterial = material_depth;
renderer.render( scene, camera, postprocessing.rtTextureDepth, true );
// Render bokeh composite
renderer.render( postprocessing.scene, postprocessing.camera ); */
}
</script>
</body>
</html>