| 1 | //------------------------------------------------------------------------------ |
| 2 | // Shader code for texcube-sapp sample. |
| 3 | // |
| 4 | // NOTE: This source file also uses the '#pragma sokol' form of the |
| 5 | // custom tags. |
| 6 | //------------------------------------------------------------------------------ |
| 7 | //#pragma sokol @ctype mat4 my_mat4 |
| 8 | |
| 9 | #pragma sokol @vs vs |
| 10 | uniform vs_params { |
| 11 | mat4 mvp; |
| 12 | }; |
| 13 | |
| 14 | in vec4 pos; |
| 15 | in vec4 color0; |
| 16 | in vec2 texcoord0; |
| 17 | |
| 18 | out vec4 color; |
| 19 | out vec2 uv; |
| 20 | |
| 21 | void main() { |
| 22 | gl_Position = mvp * pos; |
| 23 | color = color0; |
| 24 | uv = texcoord0; |
| 25 | } |
| 26 | #pragma sokol @end |
| 27 | |
| 28 | #pragma sokol @fs fs |
| 29 | |
| 30 | uniform texture2D tex; |
| 31 | uniform sampler smp; |
| 32 | |
| 33 | uniform fs_params { |
| 34 | vec2 text_res; |
| 35 | float iTime; |
| 36 | }; |
| 37 | |
| 38 | in vec4 color; |
| 39 | in vec2 uv; |
| 40 | out vec4 frag_color; |
| 41 | |
| 42 | //********************************************************* |
| 43 | // RAY TRACE |
| 44 | // original code from: https://www.shadertoy.com/view/ldS3DW |
| 45 | //********************************************************* |
| 46 | float sphere(vec3 ray, vec3 dir, vec3 center, float radius) |
| 47 | { |
| 48 | vec3 rc = ray-center; |
| 49 | float c = dot(rc, rc) - (radius*radius); |
| 50 | float b = dot(dir, rc); |
| 51 | float d = b*b - c; |
| 52 | float t = -b - sqrt(abs(d)); |
| 53 | float st = step(0.0, min(t,d)); |
| 54 | return mix(-1.0, t, st); |
| 55 | } |
| 56 | |
| 57 | vec3 background(float t, vec3 rd) |
| 58 | { |
| 59 | vec3 light = normalize(vec3(sin(t), 0.6, cos(t))); |
| 60 | float sun = max(0.0, dot(rd, light)); |
| 61 | float sky = max(0.0, dot(rd, vec3(0.0, 1.0, 0.0))); |
| 62 | float ground = max(0.0, -dot(rd, vec3(0.0, 1.0, 0.0))); |
| 63 | return (pow(sun, 256.0)+0.2*pow(sun, 2.0))*vec3(2.0, 1.6, 1.0) + |
| 64 | pow(ground, 0.5)*vec3(0.4, 0.3, 0.2) + |
| 65 | pow(sky, 1.0)*vec3(0.5, 0.6, 0.7); |
| 66 | } |
| 67 | |
| 68 | vec4 mainImage(vec2 fragCoord) |
| 69 | { |
| 70 | vec2 uv = (fragCoord-vec2(0.4,0.4))*2.0; |
| 71 | |
| 72 | //vec2 uv = (-1.0 + 2.0*fc.xy / text_res.xy) * vec2(text_res.x/text_res.y, 1.0); |
| 73 | vec3 ro = vec3(0.0, 0.0, -3.0); |
| 74 | vec3 rd = normalize(vec3(uv, 1.0)); |
| 75 | vec3 p = vec3(0.0, 0.0, 0.0); |
| 76 | float t = sphere(ro, rd, p, 1.0); |
| 77 | vec3 nml = normalize(p - (ro+rd*t)); |
| 78 | vec3 bgCol = background(iTime, rd); |
| 79 | rd = reflect(rd, nml); |
| 80 | vec3 col = background(iTime, rd) * vec3(0.9, 0.8, 1.0); |
| 81 | vec4 fragColor = vec4( mix(bgCol, col, step(0.0, t)), 1.0 ); |
| 82 | return fragColor; |
| 83 | } |
| 84 | //********************************************************* |
| 85 | //********************************************************* |
| 86 | |
| 87 | void main() { |
| 88 | vec4 c = color; |
| 89 | vec4 txt = texture(sampler2D(tex, smp), uv/4.0); |
| 90 | c = txt * c; |
| 91 | vec4 col_ray = mainImage(uv); |
| 92 | float txt_mix = mod(iTime,5); |
| 93 | frag_color = c*txt_mix*0.1 + col_ray ; |
| 94 | } |
| 95 | |
| 96 | #pragma sokol @end |
| 97 | |
| 98 | #pragma sokol @program cube vs fs |
| 99 | |