Last active
August 29, 2015 14:26
-
-
Save 0b5vr/a717241e149b0cac5674 to your computer and use it in GitHub Desktop.
glsl glitch experiment ( will not works in better gpu )
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#ifdef GL_ES | |
precision mediump float; | |
#endif | |
uniform float time; | |
uniform vec2 resolution; | |
#define saturate(i) clamp(i,0.,1.) | |
mat2 rotate( float _t ){ | |
return mat2( cos( _t ), sin( _t ), -sin( _t ), cos( _t ) ); | |
} | |
float distFunc( vec3 _p ){ | |
vec3 p = _p; | |
return length( p ) - 0.5; | |
p.xy = rotate( time ) * p.xy; | |
p.yz = rotate( time ) * p.yz; | |
p.zx = rotate( time ) * p.zx; | |
vec3 d = abs( p ) - vec3( 0.4 ); | |
return min( max( max( d.x, d.y ), d.z ), 0.0 ) + length( max( d, 0.0 ) ); | |
} | |
vec3 normalFunc( vec3 _p, float _d ){ | |
vec2 d = vec2( 0.0, 1.0 ) * _d; | |
return normalize( vec3( | |
distFunc( _p + d.yxx ) - distFunc( _p - d.yxx ), | |
distFunc( _p + d.xyx ) - distFunc( _p - d.xyx ), | |
distFunc( _p + d.xxy ) - distFunc( _p - d.xxy ) | |
) ); | |
} | |
void main(){ | |
vec2 uv = gl_FragCoord.xy / resolution; | |
vec2 p = ( gl_FragCoord.xy * 2.0 - resolution ) / resolution.x; | |
vec3 rayBeg = vec3( 0.0, 0.0, 1.0 ); | |
vec3 rayDir = vec3( p.x * vec3( 1.0, 0.0, 0.0 ) + p.y * vec3( 0.0, 1.0, 0.0 ) + vec3( 0.0, 0.0, -1.0 ) ); | |
float rayLen = 0.0; | |
vec3 rayPos = rayBeg; | |
vec3 rayCol = vec3( 0.0, 0.0, 0.0 ); | |
float dist = 0.0; | |
for( int i=0; i<64; i++ ){ | |
dist = distFunc( rayPos ); | |
rayLen += dist; | |
rayPos = rayBeg + rayLen * rayDir; | |
} | |
if( dist < 0.01 ){ | |
vec3 ligPos = vec3( 0.6, 0.7, 1.0 ); | |
vec3 ligDir = normalize( rayPos - ligPos ); | |
vec3 nor = normalFunc( rayPos, 1E-3 ); | |
vec3 dif = ( saturate( dot( ligDir, -nor ) ) * 0.8 + 0.2 ) * vec3( 1.0, 0.0, 0.3 ); | |
vec3 spe = pow( saturate( dot( normalize( ligDir + rayDir ), -nor ) ), 40.0 ) * vec3( 0.6 ); | |
rayCol = dif + spe; | |
}else{ | |
rayCol = gl_FragColor.xyz; | |
} | |
gl_FragColor = vec4( rayCol, 1.0 ); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment