URL: http://glslsandbox.com/e#20310.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 196 197 198 199 200 201 202 203 |
// Simple Ray Marcher // Morphing Ray Marched Cube // By: Brandon Fogerty // bfogerty at gmail dot com // xdpixel.com #ifdef GL_ES precision mediump float; #endif uniform float time; uniform vec2 mouse; uniform vec2 resolution; #ifdef GL_ES precision mediump float; #endif const float EPS = 0.01; const int MAXI = 100; mat4 transpose( mat4 m ) { mat4 t = mat4( vec4( m[0][0], m[1][0], m[2][0], m[3][0] ), vec4( m[0][1], m[1][1], m[2][1], m[3][1] ), vec4( m[0][2], m[1][2], m[2][2], m[3][2] ), vec4( m[0][3], m[1][3], m[2][3], m[3][3] ) ); return t; } mat4 invert( mat4 mat ) { vec3 right = vec3( mat[0][0], mat[0][1], mat[0][2] ); vec3 up = vec3( mat[1][0], mat[1][1], mat[1][2] ); vec3 fwd = vec3( mat[2][0], mat[2][1], mat[2][2] ); vec3 pos = vec3( mat[3][0], mat[3][1], mat[3][2] ); mat4 t = transpose( mat ); t[0][3] = -dot(right, pos); t[1][3] = -dot(right, pos); t[2][3] = -dot(right, pos); return t; } float smin( float a, float b, float k ) { float res = exp( -k*a ) + exp( -k*b ); return -log( res )/k; } float cube( vec3 ray, float r, mat4 transform ) { vec3 rayPrime = vec3(transpose( transform ) * vec4(ray,1)); float d = length(max(abs(rayPrime)-vec3(0.5,0.5,0.5),0.0))-r; return d; } float sphere( vec3 ray, float r, mat4 transform ) { vec3 rayPrime = vec3(invert( transform ) * vec4(ray,1)); float d = length(rayPrime)-r; return d; } float torus( vec3 ray, vec2 t, mat4 transform ) { vec3 rayPrime = vec3(invert( transform ) * vec4(ray,1)); vec2 q = vec2(length(rayPrime.xz)-t.x,rayPrime.y); return length(q)-t.y; } float SceneDist( vec3 ray ) { float t = time; float c = cos( t ); float s = sin( t ); mat4 rotX = mat4( vec4(1,0,0,0), vec4(0,c,-s,0), vec4(0,s,c,0), vec4(0,0,0,1) ); mat4 rotY = mat4( vec4(c,0,-s,0), vec4(0,1,0,0), vec4(s,0,c,0), vec4(0,0,0,1) ); mat4 rotZ = mat4( vec4(c,s,0,0), vec4(-s,c,0,0), vec4(0,0,1,0), vec4(0,0,0,1) ); mat4 pos = mat4( vec4(1,0,0,s*1.0), vec4(0,1,0,0), vec4(0,0,1,c*1.0), vec4(0,0,0,1) ); mat4 transform = pos * rotZ * rotY * rotY; float d1 = cube( ray, 0.15, transform ); //float d2 = sphere( ray, 0.5, transform ); float d2 = torus( ray, vec2(0.7, 0.1), transform); float tVal = sin(time)*0.5+0.5; float final = mix(d1,d2, tVal); return final; } vec3 getNormal(vec3 pos){ // this gets the normal // if you understand, good. if not, not much I can do. vec2 eps = vec2(0.0, EPS); return normalize(vec3( // always normalise directions SceneDist(pos + eps.yxx) - SceneDist(pos - eps.yxx), SceneDist(pos + eps.xyx) - SceneDist(pos - eps.xyx), SceneDist(pos + eps.xxy) - SceneDist(pos - eps.xxy))); } vec3 hsv(float h,float s,float v) { // this is just a hue/saturation/luminance to RGB conversion return mix(vec3(1.),clamp((abs(fract(h+vec3(3.,2.,1.)/3.)*6.-3.)-1.),0.,1.),s)*v; } vec3 Lighting( vec3 camPos, vec3 pos, vec3 normal, vec3 diffuseColor ) { vec3 lightPos = vec3(0,0,10); vec3 lightDir = normalize( lightPos - pos ); vec3 viewDir = normalize( camPos - pos ); vec3 lightAmbientColor = vec3(0.1,0.1,0.1); vec3 lightSpecularColor = vec3(1,1,1); vec3 halfDir = normalize(viewDir + lightDir); //float lightSpecularIntensity = pow( clamp(dot( normal, halfDir ), 0.0, 1.0), 2.0 ); float lightSpecularIntensity = pow( clamp(dot( normal, reflect(lightDir, normal )), 0.0, 1.0), 80.0 ); float lightDiffuseIntensity = clamp( dot( -normalize(pos), lightDir ), 0.00, 1.0); return lightAmbientColor + (lightDiffuseIntensity * diffuseColor) + (lightSpecularIntensity * lightSpecularColor); } vec3 RenderScene( vec2 uv ) { vec3 color = vec3(1,0,0); //vec3 camPos = vec3( sin(time) * 3.0, 0.0, cos(time) * 3.0); vec3 camPos = vec3(0,0,-3); vec3 camTarget = vec3(0.0, 0.0, 0.0); vec3 camUp = vec3(0,1.0,0); vec3 camFwd = normalize( camTarget - camPos ); vec3 camRight = normalize( cross( camUp, camFwd ) ); camUp = normalize( cross( camRight, camFwd ) ); float dist = SceneDist( camPos ); float total = dist; vec3 rayDir = vec3( normalize( camFwd + camRight * uv.x + camUp * uv.y ) ); for(int i=0; i < MAXI; ++i) { dist = SceneDist( camPos + rayDir * total ); total += dist; if( dist <= EPS ) { break; } } vec3 dest = camPos + rayDir * total; if( dist <= EPS ) { float t = sin(time)*0.5+0.5; vec3 diffuse = mix( vec3(1.0,0.0,0.0), vec3(1.0,1.0,0.0), t); color = Lighting( camPos, dest, getNormal( dest ), diffuse ); } else { vec2 p = floor( time + gl_FragCoord.xy/resolution.x*10.0 ); float s = mod( p.x + p.y, 2.0 ); vec3 bg0 = vec3(s,s,s); vec3 bg1 = hsv(.55,smoothstep(-1.5,1.,uv.y),1.); float t = sin(time) * 0.5 + 0.5; color = mix( bg0, bg1, t); } return color; } void main(void) { vec2 uv = ((gl_FragCoord.xy / resolution.xy) - 0.5) * vec2(2.0, 2.0 * resolution.y / resolution.x); vec3 color = RenderScene( uv ); // Scan line color -= mod(gl_FragCoord.y, 2.0) < 1.0 ? 0.5 : 0.0; gl_FragColor = vec4(color, 1.0); } |