Shuvit game master repo. http://shuvit.org
Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  1. #version 120
  2. varying vec4 fragPos; //fragment coordinates
  3. varying vec3 T, B, N; //tangent binormal normal
  4. varying vec3 viewPos;
  5. varying vec3 worldPos;
  6. uniform float timer;
  7. uniform sampler2D reflectionSampler;
  8. uniform sampler2D refractionSampler;
  9. uniform sampler2D depthSampler;
  10. uniform sampler2D normalSampler;
  11. uniform vec3 cameraPos;
  12. //----------------
  13. //tweakables
  14. uniform float windSpeed = 0.2; //wind speed
  15. uniform float scale = 1.0; //overall wave scale
  16. vec2 windDir = vec2(0.5, -0.8); //wind direction XY
  17. float visibility = 1.0; //28
  18. vec2 bigWaves = vec2(0.3, 0.3); //strength of big waves
  19. vec2 midWaves = vec2(0.3, 0.15); //strength of middle sized waves
  20. vec2 smallWaves = vec2(0.15, 0.1); //strength of small waves
  21. vec3 waterColor = vec3(0.2,0.4,0.5); //color of the water
  22. float choppy = 0.15; //wave choppyness
  23. float aberration = 0.002; //chromatic aberration amount
  24. float bump = 2.6; //overall water surface bumpyness
  25. float reflBump = 0.04; //reflection distortion amount
  26. float refrBump = 0.03; //refraction distortion amount
  27. float sunSpec = 1000.0; //Sun specular hardness
  28. float scatterAmount = 3.5; //amount of sunlight scattering of waves
  29. vec3 scatterColor = vec3(0.0,1.0,0.95);// color of the sunlight scattering
  30. //----------------
  31. vec3 sunPos = vec3(gl_ModelViewMatrixInverse*gl_LightSource[0].position);
  32. vec3 tangentSpace(vec3 v)
  33. {
  34. vec3 vec;
  35. vec.xy=v.xy;
  36. vec.z=sqrt(1.0-dot(vec.xy,vec.xy));
  37. vec.xyz= normalize(vec.x*T+vec.y*B+vec.z*N);
  38. return vec;
  39. }
  40. float fresnel_dielectric(vec3 Incoming, vec3 Normal, float eta)
  41. {
  42. /* compute fresnel reflectance without explicitly computing
  43. the refracted direction */
  44. float c = abs(dot(Incoming, Normal));
  45. float g = eta * eta - 1.0 + c * c;
  46. float result;
  47. if(g > 0.0) {
  48. g = sqrt(g);
  49. float A =(g - c)/(g + c);
  50. float B =(c *(g + c)- 1.0)/(c *(g - c)+ 1.0);
  51. result = 0.5 * A * A *(1.0 + B * B);
  52. }
  53. else
  54. result = 1.0; /* TIR (no refracted component) */
  55. return result;
  56. }
  57. void main() {
  58. vec2 fragCoord = (fragPos.xy/fragPos.w)*0.5+0.5;
  59. fragCoord = clamp(fragCoord,0.002,0.998);
  60. //normal map
  61. vec2 nCoord = vec2(0.0); //normal coords
  62. vec2 coo = ((gl_TexCoord[0].st)/1106.208)-0.5;
  63. float depth = texture2D(depthSampler,coo).r;
  64. float coast = smoothstep(0.3,0.7,depth);
  65. float coast1 = smoothstep(0.49,0.5,depth);
  66. choppy = choppy * (coast)+0.05;
  67. bump = -bump*clamp(1.0-coast+0.0,0.0,1.0);
  68. bump = bump*clamp(1.0-coast1+0.0,0.0,1.0);
  69. //scale = scale - (coast*0.1);
  70. //scale = scale * (coast*0.1+0.9);
  71. float time = timer - (coast)*80.0; //hmmm
  72. //timer = timer * (coast*0.5+0.5)*1.5;
  73. //smallWaves.x = smallWaves.x + (0.1*coast);
  74. //smallWaves.y = smallWaves.y + (0.1*coast);
  75. //bigWaves.x = bigWaves.x - (0.1*(1.0-coast));
  76. //bigWaves.y = bigWaves.y + (0.1*(1.0-coast));
  77. vec3 mudext = vec3(1.0, 0.7, 0.5);//mud extinction
  78. float atmosphere = length(cameraPos-worldPos)/200.0;
  79. vec3 atmopherefog = clamp(1.0-exp(-atmosphere/mudext),0.0,1.0);
  80. nCoord = worldPos.xy * (scale * 0.04) + windDir * time * (windSpeed*0.04);
  81. vec3 normal0 = 2.0 * texture2D(normalSampler, nCoord + vec2(-time*0.015,-time*0.005)).rgb - 1.0;
  82. nCoord = worldPos.xy * (scale * 0.1) + windDir * time * (windSpeed*0.08)-(normal0.xy/normal0.zz)*choppy;
  83. vec3 normal1 = 2.0 * texture2D(normalSampler, nCoord + vec2(+time*0.020,+time*0.015)).rgb - 1.0;
  84. nCoord = worldPos.xy * (scale * 0.25) + windDir * time * (windSpeed*0.07)-(normal1.xy/normal1.zz)*choppy;
  85. vec3 normal2 = 2.0 * texture2D(normalSampler, nCoord + vec2(-time*0.04,-time*0.03)).rgb - 1.0;
  86. nCoord = worldPos.xy * (scale * 0.5) + windDir * time * (windSpeed*0.09)-(normal2.xy/normal2.z)*choppy;
  87. vec3 normal3 = 2.0 * texture2D(normalSampler, nCoord + vec2(+time*0.03,+time*0.04)).rgb - 1.0;
  88. nCoord = worldPos.xy * (scale* 1.0) + windDir * time * (windSpeed*0.4)-(normal3.xy/normal3.zz)*choppy;
  89. vec3 normal4 = 2.0 * texture2D(normalSampler, nCoord + vec2(-time*0.02,+time*0.1)).rgb - 1.0;
  90. nCoord = worldPos.xy * (scale * 2.0) + windDir * time * (windSpeed*0.7)-(normal4.xy/normal4.zz)*choppy;
  91. vec3 normal5 = 2.0 * texture2D(normalSampler, nCoord + vec2(+time*0.1,-time*0.06)).rgb - 1.0;
  92. vec3 normal = normalize(normal0 * bigWaves.x + normal1 * bigWaves.y +
  93. normal2 * midWaves.x + normal3 * midWaves.y +
  94. normal4 * smallWaves.x + normal5 * smallWaves.y);
  95. //normal.x = -normal.x; //in case you need to invert Red channel
  96. //normal.y = -normal.y; //in case you need to invert Green channel
  97. vec3 nVec = tangentSpace(normal*bump); //converting normals to tangent space
  98. vec3 vVec = normalize(viewPos);
  99. vec3 lVec = normalize(sunPos);
  100. //normal for light scattering
  101. vec3 lNormal = normalize(normal0 * bigWaves.x*0.5 + normal1 * bigWaves.y*0.5 +
  102. normal2 * midWaves.x*0.1 + normal3 * midWaves.y*0.1 +
  103. normal4 * smallWaves.x*0.1 + normal5 * smallWaves.y*0.1);
  104. lNormal = tangentSpace(lNormal*bump);
  105. vec3 pNormal = tangentSpace(vec3(0.0));
  106. vec3 lR = reflect(lVec, lNormal);
  107. vec3 llR = reflect(lVec, pNormal);
  108. float sunFade = clamp((sunPos.z+10.0)/20.0,0.0,1.0);
  109. float scatterFade = clamp((sunPos.z+50.0)/200.0,0.0,1.0);
  110. vec3 sunext = vec3(0.45, 0.55, 0.68);//sunlight extinction
  111. float s = clamp((dot(lR, vVec)*2.0-1.2), 0.0,1.0);
  112. float lightScatter = clamp((clamp(dot(-lVec,lNormal)*0.7+0.3,0.0,1.0)*s)*scatterAmount,0.0,1.0)*sunFade *clamp(1.0-exp(-(sunPos.z/500.0)),0.0,1.0);
  113. scatterColor = mix(vec3(scatterColor)*vec3(1.0,0.4,0.0), scatterColor, clamp(1.0-exp(-(sunPos.z/500.0)*sunext),0.0,1.0));
  114. //fresnel term
  115. float ior = 1.33;
  116. ior = (cameraPos.z>0.0)?(1.333/1.0):(1.0/1.333); //air to water; water to air
  117. float eta = max(ior, 0.00001);
  118. float fresnel = fresnel_dielectric(-vVec,nVec,eta);
  119. //texture edge bleed removal
  120. float fade = 12.0;
  121. vec2 distortFade = vec2(0.0);
  122. distortFade.s = clamp(fragCoord.s*fade,0.0,1.0);
  123. distortFade.s -= clamp(1.0-(1.0-fragCoord.s)*fade,0.0,1.0);
  124. distortFade.t = clamp(fragCoord.t*fade,0.0,1.0);
  125. distortFade.t -= clamp(1.0-(1.0-fragCoord.t)*fade,0.0,1.0);
  126. vec3 reflection = texture2D(reflectionSampler, fragCoord+(nVec.st*vec2(reflBump,reflBump*(6.0))*distortFade)).rgb;
  127. vec3 luminosity = vec3(0.30, 0.59, 0.11);
  128. float reflectivity = pow(dot(luminosity, reflection.rgb*2.0),3.0);
  129. float reflectivity1 = pow(dot(luminosity, reflection.rgb),3.0);
  130. vec3 R = reflect(vVec, nVec);
  131. float specular = clamp(pow(atan(max(dot(R, lVec),0.0)*1.55),1000.0)*reflectivity*8.0,0.0,1.0);
  132. vec3 specColor = mix(vec3(1.0,0.5,0.2), vec3(1.0,1.0,1.0), clamp(1.0-exp(-(sunPos.z/500.0)*sunext),0.0,1.0));
  133. vec2 rcoord = reflect(vVec,nVec).st;
  134. vec3 refraction = vec3(0.0);
  135. refraction.r = texture2D(refractionSampler, (fragCoord-(nVec.st*refrBump*distortFade))*1.0).r;
  136. refraction.g = texture2D(refractionSampler, (fragCoord-(nVec.st*refrBump*distortFade))*1.0-(rcoord*aberration)).g;
  137. refraction.b = texture2D(refractionSampler, (fragCoord-(nVec.st*refrBump*distortFade))*1.0-(rcoord*aberration*2.0)).b;
  138. float waterSunGradient = dot(normalize(cameraPos-worldPos), -normalize(sunPos));
  139. waterSunGradient = clamp(pow(waterSunGradient*0.7+0.3,2.0),0.0,1.0);
  140. vec3 waterSunColor = vec3(0.0,1.0,0.85)*waterSunGradient;
  141. waterSunColor = (cameraPos.z<0.0)?waterSunColor*0.5:waterSunColor*0.25;//below or above water?
  142. float waterGradient = dot(normalize(cameraPos-worldPos), vec3(0.0,0.0,-1.0));
  143. waterGradient = clamp((waterGradient*0.5+0.5),0.2,1.0);
  144. vec3 watercolor = (vec3(0.0078, 0.5176, 0.700)+waterSunColor)*waterGradient*1.5;
  145. vec3 waterext = vec3(0.6, 0.8, 1.0);//water extinction
  146. watercolor = mix(watercolor*0.3*sunFade, watercolor, clamp(1.0-exp(-(sunPos.z/500.0)*sunext),0.0,1.0));
  147. float fog = length(cameraPos-worldPos)/visibility;
  148. fog = (cameraPos.z<0.0)?fog:1.0;
  149. fog = clamp(pow(fog,1.0),0.0,1.0);
  150. float darkness = visibility*2.0;
  151. darkness = clamp((cameraPos.z+darkness)/darkness,0.0,1.0);
  152. fresnel = clamp(fresnel,0.0,1.0);
  153. vec3 color = mix(mix(refraction,scatterColor,lightScatter),reflection,fresnel*0.6);
  154. color = (cameraPos.z<0.0)?mix(clamp(refraction*1.2,0.0,1.0),reflection,fresnel):color;
  155. color = (cameraPos.z<0.0)?mix(color, watercolor*darkness*scatterFade, clamp(fog/ waterext,0.0,1.0)):color;
  156. //color = mix(color,atmopherefog*0.8,atmopherefog.b);
  157. //gl_FragColor = vec4(vec3(color*0.001+(specColor*specular)),1.0);
  158. gl_FragColor = vec4(vec3(color+(specColor*specular)),1.0);
  159. }