|
| 1 | +#ifdef GL_ES |
| 2 | +// define default precision for float, vec, mat. |
| 3 | +precision highp float; |
| 4 | +#endif |
| 5 | + |
| 6 | +attribute vec4 position; |
| 7 | + |
| 8 | +uniform mat4 modelViewProjectionMatrix; |
| 9 | + |
| 10 | +uniform float timeValX; |
| 11 | +uniform float timeValY; |
| 12 | +uniform vec2 mouse; |
| 13 | + |
| 14 | +//generate a random value from four points |
| 15 | +vec4 rand(vec2 A,vec2 B,vec2 C,vec2 D){ |
| 16 | + |
| 17 | + vec2 s=vec2(12.9898,78.233); |
| 18 | + vec4 tmp=vec4(dot(A,s),dot(B,s),dot(C,s),dot(D,s)); |
| 19 | + |
| 20 | + return fract(sin(tmp) * 43758.5453)* 2.0 - 1.0; |
| 21 | +} |
| 22 | + |
| 23 | +//this is similar to a perlin noise function |
| 24 | +float noise(vec2 coord,float d){ |
| 25 | + |
| 26 | + vec2 C[4]; |
| 27 | + |
| 28 | + float d1 = 1.0/d; |
| 29 | + |
| 30 | + C[0]=floor(coord*d)*d1; |
| 31 | + |
| 32 | + C[1]=C[0]+vec2(d1,0.0); |
| 33 | + |
| 34 | + C[2]=C[0]+vec2(d1,d1); |
| 35 | + |
| 36 | + C[3]=C[0]+vec2(0.0,d1); |
| 37 | + |
| 38 | + |
| 39 | + vec2 p=fract(coord*d); |
| 40 | + |
| 41 | + vec2 q=1.0-p; |
| 42 | + |
| 43 | + vec4 w=vec4(q.x*q.y,p.x*q.y,p.x*p.y,q.x*p.y); |
| 44 | + |
| 45 | + return dot(vec4(rand(C[0],C[1],C[2],C[3])),w); |
| 46 | +} |
| 47 | + |
| 48 | + |
| 49 | +void main(){ |
| 50 | + |
| 51 | + //get our current vertex position so we can modify it |
| 52 | + vec4 pos = modelViewProjectionMatrix * position; |
| 53 | + |
| 54 | + //generate some noise values based on vertex position and the time value which comes in from our OF app |
| 55 | + float noiseAmntX = noise( vec2(-timeValX + pos.x / 1000.0, 100.0), 20.0 ); |
| 56 | + float noiseAmntY = noise( vec2(timeValY + pos.y / 1000.0, pos.x / 2000.0), 20.0 ); |
| 57 | + |
| 58 | + //generate noise for our blue pixel value |
| 59 | + float noiseB = noise( vec2(timeValY * 0.25, pos.y / 2000.0), 20.0 ); |
| 60 | + |
| 61 | + //lets also figure out the distance between the mouse and the vertex and apply a repelling force away from the mouse |
| 62 | + vec2 d = vec2(pos.x, pos.y) - mouse; |
| 63 | + float len = sqrt(d.x*d.x + d.y*d.y); |
| 64 | + if( len < 300.0 && len > 0.0 ){ |
| 65 | + |
| 66 | + //lets get the distance into 0-1 ranges |
| 67 | + float pct = len / 300.0; |
| 68 | + |
| 69 | + //this turns our linear 0-1 value into a curved 0-1 value |
| 70 | + pct *= pct; |
| 71 | + |
| 72 | + //flip it so the closer we are the greater the repulsion |
| 73 | + pct = 1.0 - pct; |
| 74 | + |
| 75 | + //normalize our repulsion vector |
| 76 | + d /= len; |
| 77 | + |
| 78 | + //apply the repulsion to our position |
| 79 | + pos.x += d.x * pct * 90.0; |
| 80 | + pos.y += d.y * pct * 90.0; |
| 81 | + } |
| 82 | + |
| 83 | + //modify our position with the smooth noise |
| 84 | + pos.x += noiseAmntX * 20.0; |
| 85 | + pos.y += noiseAmntY * 10.0; |
| 86 | + |
| 87 | + //finally set the pos to be that actual position rendered |
| 88 | + gl_Position = pos; |
| 89 | +} |
0 commit comments