Advertisement
Guest User

Untitled

a guest
Aug 25th, 2016
171
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. varying vec2 vUv;
  2. uniform float time;
  3. uniform vec2 resolution;
  4.  
  5. const int NUM_STEPS = 8;
  6. const float PI = 3.1415;
  7. const float EPSILON = 1e-3;
  8.  
  9. // sea
  10. const int ITER_GEOMETRY = 3;
  11. const int ITER_FRAGMENT = 5;
  12. const float SEA_HEIGHT = 0.6;
  13. const float SEA_CHOPPY = 4.0;
  14. const float SEA_SPEED = 0.8;
  15. const float SEA_FREQ = 0.16;
  16. const vec3 SEA_BASE = vec3(0.1,0.19,0.22);
  17. const vec3 SEA_WATER_COLOR = vec3(0.8,0.9,0.6);
  18.  
  19. uniform float speedRotation;
  20.  
  21. mat2 octave_m = mat2(1.6,1.2,-1.2,1.6);
  22.  
  23. // math
  24. mat3 fromEuler(vec3 ang) {
  25.     vec2 a1 = vec2(sin(ang.x),cos(ang.x));
  26.     vec2 a2 = vec2(sin(ang.y),cos(ang.y));
  27.     vec2 a3 = vec2(sin(ang.z),cos(ang.z));
  28.     mat3 m;
  29.     m[0] = vec3(a1.y*a3.y+a1.x*a2.x*a3.x,a1.y*a2.x*a3.x+a3.y*a1.x,-a2.y*a3.x);
  30.     m[1] = vec3(-a2.y*a1.x,a1.y*a2.y,a2.x);
  31.     m[2] = vec3(a3.y*a1.x*a2.x+a1.y*a3.x,a1.x*a3.x-a1.y*a3.y*a2.x,a2.y*a3.y);
  32.     return m;
  33. }
  34.  
  35. float hash( vec2 p ) {
  36.     float h = dot(p,vec2(127.1,311.7));    
  37.     return fract(sin(h)*43758.5453123);
  38. }
  39.  
  40. float noise( in vec2 p ) {
  41.     vec2 i = floor( p );
  42.     vec2 f = fract( p );    
  43.     vec2 u = f*f*(3.0-2.0*f);
  44.     return -1.0+2.0*mix( mix( hash( i + vec2(0.0,0.0) ),
  45.                      hash( i + vec2(1.0,0.0) ), u.x),
  46.                 mix( hash( i + vec2(0.0,1.0) ),
  47.                      hash( i + vec2(1.0,1.0) ), u.x), u.y);
  48. }
  49.  
  50. // lighting
  51. float diffuse(vec3 n,vec3 l,float p) {
  52.     return pow(abs(dot(n,l) * 0.4 + 0.6),p);
  53. }
  54.  
  55. float specular(vec3 n,vec3 l,vec3 e,float s) {    
  56.     float nrm = (s + 8.0) / (PI * 8.0);
  57.     return pow(abs(max(dot(reflect(e,n),l),0.0)),s) * nrm;
  58. }
  59.  
  60. // sky
  61. vec3 getSkyColor(vec3 e) {
  62.     e.y = max(e.y,0.0);
  63.     vec3 ret;
  64.     ret.x = pow(abs(1.0-e.y),2.0);
  65.     ret.y = 1.0-e.y;
  66.     ret.z = 0.6+(1.0-e.y)*0.4;
  67.     return ret;
  68. }
  69.  
  70. // sea
  71. float sea_octave(vec2 uv, float choppy) {
  72.     uv += noise(uv);        
  73.     vec2 wv = 1.0-abs(sin(uv));
  74.     vec2 swv = abs(cos(uv));    
  75.     wv = mix(wv,swv,wv);
  76.     return pow(abs(1.0-pow(abs(wv.x * wv.y),0.65)),choppy);
  77. }
  78.  
  79. float maps(vec3 p) {
  80.     float freq = SEA_FREQ;
  81.     float amp = SEA_HEIGHT;
  82.     float choppy = SEA_CHOPPY;
  83.     vec2 uv = p.xz; uv.x *= 0.75;
  84.     float d, h = 0.0;    
  85.     for(int i = 0; i < ITER_GEOMETRY; i++) {        
  86.         d = sea_octave((uv+(time * SEA_SPEED))*freq,choppy);
  87.         d += sea_octave((uv-(time * SEA_SPEED))*freq,choppy);
  88.         h += d * amp;        
  89.         uv *= octave_m; freq *= 1.9; amp *= 0.22;
  90.         choppy = mix(choppy,1.0,0.2);
  91.     }
  92.     return p.y - h;
  93. }
  94.  
  95. float map_detailed(vec3 p) {
  96.     float freq = SEA_FREQ;
  97.     float amp = SEA_HEIGHT;
  98.     float choppy = SEA_CHOPPY;
  99.     vec2 uv = p.xz; uv.x *= 0.75;
  100.     float d, h = 0.0;    
  101.     for(int i = 0; i < ITER_FRAGMENT; i++) {        
  102.         d = sea_octave((uv+(time * SEA_SPEED))*freq,choppy);
  103.         d += sea_octave((uv-(time * SEA_SPEED))*freq,choppy);
  104.         h += d * amp;        
  105.         uv *= octave_m; freq *= 1.9; amp *= 0.22;
  106.         choppy = mix(choppy,1.0,0.2);
  107.     }
  108.     return p.y - h;
  109. }
  110.  
  111. vec3 getSeaColor(vec3 p, vec3 n, vec3 l, vec3 eye, vec3 dist) {  
  112.     float fresnel = 1.0 - max(dot(n,-eye),0.0);
  113.     fresnel = pow(abs(fresnel),3.0) * 0.65;
  114.     vec3 reflected = getSkyColor(reflect(eye,n));    
  115.     vec3 refracted = SEA_BASE + diffuse(n,l,80.0) * SEA_WATER_COLOR * 0.12;
  116.     vec3 color = mix(refracted,reflected,fresnel);
  117.     float atten = max(1.0 - dot(dist,dist) * 0.001, 0.0);
  118.     color += SEA_WATER_COLOR * (p.y - SEA_HEIGHT) * 0.18 * atten;
  119.     color += vec3(specular(n,l,eye,60.0));
  120.     return color;
  121. }
  122.  
  123. // tracing
  124. vec3 getNormal(vec3 p, float eps) {
  125.     vec3 n;
  126.     n.y = map_detailed(p);    
  127.     n.x = map_detailed(vec3(p.x+eps,p.y,p.z)) - n.y;
  128.     n.z = map_detailed(vec3(p.x,p.y,p.z+eps)) - n.y;
  129.     n.y = eps;
  130.     return normalize(n);
  131. }
  132.  
  133. float heightMapTracing(vec3 ori, vec3 dir, out vec3 p) {  
  134.     float tm = 0.0;
  135.     float tx = 1000.0;    
  136.     float hx = maps(ori + dir * tx);
  137.     if(hx > 0.0) return tx;  
  138.     float hm = maps(ori + dir * tm);    
  139.     float tmid = 0.0;
  140.     for(int i = 0; i < NUM_STEPS; i++) {
  141.         tmid = mix(tm,tx, hm/(hm-hx));                  
  142.         p = ori + dir * tmid;                  
  143.         float hmid = maps(p);
  144.         if(hmid < 0.0) {
  145.             tx = tmid;
  146.             hx = hmid;
  147.         } else {
  148.             tm = tmid;
  149.             hm = hmid;
  150.         }
  151.     }
  152.     return tmid;
  153. }
  154.  
  155. void main() {
  156.     vec2 uv = gl_FragCoord.xy / resolution.xy;
  157.     uv = uv * 2.0 - 1.5;
  158.     uv.x *= resolution.x / resolution.y;
  159.    
  160.     float EPSILON_NRM = 0.1 / resolution.x;
  161.    
  162.     float ttime = time * speedRotation;
  163.     vec3 ang = vec3(sin(ttime*2.0)*0.1,sin(ttime)*0.2+0.3,ttime);    
  164.     vec3 ori = vec3(0.85,3.5,ttime*5.0);
  165.     vec3 dir = normalize(vec3(uv.xy,-2.0));
  166.     dir.z += length(uv) * 0.15;
  167.     dir = normalize(dir) * fromEuler(ang);
  168.    
  169.     // tracing
  170.     vec3 p;
  171.     heightMapTracing(ori,dir,p);
  172.     vec3 dist = p - ori;
  173.     vec3 n = getNormal(p, dot(dist,dist) * EPSILON_NRM);
  174.     vec3 light = normalize(vec3(0.0,1.0,0.8));
  175.    
  176.     // color
  177.     vec3 color = mix(
  178.         getSkyColor(dir),
  179.         getSeaColor(p,n,light,dir,dist),
  180.         pow(abs(smoothstep(0.0,-0.05,dir.y)),0.3));
  181.    
  182.     gl_FragColor = vec4(pow(abs(color),vec3(0.75)), 1.0);
  183. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement