URL: http://glslsandbox.com/e#37553.0
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 |
// Majestic Wireframe Cube // By: Brandon Fogerty // bfogerty at gmail dot com // xdpixel.com #ifdef GL_ES precision mediump float; #endif #extension GL_OES_standard_derivatives : enable uniform float time; uniform vec2 mouse; uniform vec2 resolution; float line( vec2 a, vec2 b, vec2 p ) { vec2 aTob = b - a; vec2 aTop = p - a; float t = dot( aTop, aTob ) / dot( aTob, aTob); t = clamp( t, 0.0, 1.0); float d = length( p - (a + aTob * t) ); d = 0.05 / d; d = pow(d, 7.0); return clamp( d, 0.0, 1.0 ); } mat4 perspectiveMatrix(float fovYInRad, float aspectRatio) { float yScale = 1.0/tan(fovYInRad / 2.0); float xScale = yScale / aspectRatio; float zf = 100.0; float zn = 0.3; float z1 = zf/(zf-zn); float z2 = -zn*zf/(zf-zn); mat4 result = mat4(xScale, 0.0, 0.0, 0.0, 0.0, yScale, 0.0, 0.0, 0.0, 0.0, z1, z2, 0.0, 0.0, -1.0, 0.0); return result; } mat4 translationMatrix(vec3 pos) { mat4 result = mat4(1.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0, pos.x, pos.y, pos.z, 1.0 ); return result; } mat4 rotXMatrix(float theta) { float cs = cos(theta); float ss = sin(theta); mat4 result = mat4(1.0, 0.0, 0.0, 0.0, 0.0, cs, -ss, 0.0, 0.0, ss, cs, 0.0, 0.0, 0.0, 0.0, 1.0 ); return result; } mat4 rotYMatrix(float theta) { float cs = cos(theta); float ss = sin(theta); mat4 result = mat4(cs, 0.0, -ss, 0.0, 0.0, 1.0, 0.0, 0.0, ss, 0.0, cs, 0.0, 0.0, 0.0, 0.0, 1.0 ); return result; } float particle(vec2 uv, vec4 vert, mat4 mvp, vec2 sizeRange, float sizeOffset, vec2 moveSpeed) { vec4 v = mvp * vert; vec2 vp = (v.xy / v.w); float size = mix(sizeRange.x, sizeRange.y, sin(time + sizeOffset) * 0.5 + 0.5); float t = 1.0 / abs(length(uv.xy - (vp.xy + vec2(cos(time * moveSpeed.x), sin(time * moveSpeed.y)))) * size); t = pow( t, 1.5); return t; } void main( void ) { vec2 uv = ( gl_FragCoord.xy / resolution.xy ) * 2.0 - 1.0; uv *= 10.0; const float fovYInRad = (45.0/180.0) * 3.14159; float aspectRatio = resolution.x / resolution.y; const float vs = 10.0; #define maxVerts 16 vec4 verts [maxVerts]; verts[0] = vec4( -vs, -vs, vs, 1.0 ); verts[1] = vec4( -vs, vs, vs, 1.0 ); verts[2] = vec4( vs, vs, vs, 1.0 ); verts[3] = vec4( vs, -vs, vs, 1.0 ); verts[4] = vec4( -vs, -vs, vs, 1.0 ); verts[5] = vec4( -vs, vs, vs, 1.0 ); verts[6] = vec4( -vs, vs, -vs, 1.0 ); verts[7] = vec4( -vs, -vs, -vs, 1.0 ); verts[8] = vec4( -vs, -vs, -vs, 1.0 ); verts[9] = vec4( -vs, vs, -vs, 1.0 ); verts[10] = vec4( vs, vs, -vs, 1.0 ); verts[11] = vec4( vs, -vs, -vs, 1.0 ); verts[12] = vec4( vs, -vs, vs, 1.0 ); verts[13] = vec4( vs, vs, vs, 1.0 ); verts[14] = vec4( vs, vs, -vs, 1.0 ); verts[15] = vec4( vs, -vs, -vs, 1.0 ); float moveX = mix(10.0, -10.0, sin(time * 0.4) * 0.5 + 0.5); float moveY = mix(10.0, -10.0, sin(time * 0.2) * 0.5 + 0.5); float moveZ = mix(40.0, 60.0, sin(time * 0.2) * 0.5 + 0.5); vec3 pos = vec3( moveX, moveY, moveZ); mat4 rotY = rotYMatrix(time) * rotXMatrix(time * 0.5); mat4 worldMat = translationMatrix(pos) * rotY; mat4 perspective = perspectiveMatrix(fovYInRad, aspectRatio); mat4 mvp = perspective * worldMat; float t = 0.0; for(int i = 0; i < maxVerts; ++i) { vec4 startWorldVert = mvp * verts[i]; vec4 endWorldVert; if( i+1 < maxVerts) { endWorldVert = mvp * verts[i + 1]; } else { endWorldVert = mvp * verts[i - 3]; } if(i != 0 && mod(float(i+1), 4.0) == 0.0) { endWorldVert = mvp * verts[i - 3]; } vec2 sp = startWorldVert.xy / startWorldVert.w; vec2 ep = endWorldVert.xy / endWorldVert.w; t += line( sp, ep, uv); } vec3 fc = vec3( 0.00 ); fc += vec3(pow(abs(uv.y / 10.0), 2.5)); fc += vec3( 2.0, 4.0, 8.0 ) * pow(t, 0.2); uv.x *= resolution.x / resolution.y; t = particle(uv, verts[0], mvp, vec2(20.0, 40.0), 1.0, vec2( 3.0, 1.0)); fc += vec3(8.0, 4.0, 2.0) * t; t = particle(uv, verts[1], mvp, vec2(20.0, 40.0), 14.0, vec2( 1.0, 3.0)); fc += vec3(4.0, 8.0, 2.0) * t; t = particle(uv, verts[10], mvp, vec2(10.0, 40.0), 25.0, vec2( 2.0, 1.0)); fc += vec3(3.0, 2.0, 7.0) * t; t = particle(uv, verts[12], mvp, vec2(10.0, 40.0), 78.0, vec2( 5.0, 3.0)); fc += vec3(4.0, 3.0, 8.0) * t; t = particle(uv, verts[6], mvp, vec2(10.0, 40.0), 100.0, vec2( 5.0, 3.0)); fc += vec3(8.0, 1.0, 1.0) * t; gl_FragColor = vec4( fc, 1.0 ); } |