demo.txt 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559
  1. // A documented, altered, recolored version of "Seascape".
  2. // The famous original at:
  3. // https://www.shadertoy.com/view/Ms2SD1
  4. // "Seascape" by Alexander Alekseev aka TDM - 2014
  5. // Commenting added by bteitler
  6. // HSV/color adjustments and additional commenting by CaliCoastReplay - 2016
  7. // License Creative Commons Attribution-NonCommercial-ShareAlike 3.0 Unported License.
  8. // PI is a mathematical constant relating the ratio of a circle's circumference (distance around
  9. // the edge) to its diameter (distance between two points opposite on the edge).
  10. // Change pi at your own peril, with your own apologies to God.
  11. const float PI = 3.14159265358;
  12. // Can you explain these epsilons to a wide graphics audience? YOUR comment could go here.
  13. const float EPSILON = 1e-3;
  14. #define EPSILON_NRM (0.5 / iResolution.x)
  15. // Constant indicaing the number of steps taken while marching the light ray.
  16. const int NUM_STEPS = 6;
  17. //Constants relating to the iteration of the heightmap for the wave, another part of the rendering
  18. //process.
  19. const int ITER_GEOMETRY = 2;
  20. const int ITER_FRAGMENT =5;
  21. // Constants that represent physical characteristics of the sea, can and should be changed and
  22. // played with
  23. const float SEA_HEIGHT = 0.5;
  24. const float SEA_CHOPPY = 3.0;
  25. const float SEA_SPEED = 1.9;
  26. const float SEA_FREQ = 0.24;
  27. const vec3 SEA_BASE = vec3(0.11,0.19,0.22);
  28. const vec3 SEA_WATER_COLOR = vec3(0.55,0.9,0.7);
  29. #define SEA_TIME (iTime * SEA_SPEED)
  30. //Matrix to permute the water surface into a complex, realistic form
  31. mat2 octave_m = mat2(1.7,1.2,-1.2,1.4);
  32. //Space bar key constant
  33. const float KEY_SP = 32.5/256.0;
  34. //CaliCoastReplay : These HSV/RGB translation functions are
  35. //from http://gamedev.stackexchange.com/questions/59797/glsl-shader-change-hue-saturation-brightness
  36. //This one converts red-green-blue color to hue-saturation-value color
  37. vec3 rgb2hsv(vec3 c)
  38. {
  39. vec4 K = vec4(0.0, -1.0 / 3.0, 2.0 / 3.0, -1.0);
  40. vec4 p = mix(vec4(c.bg, K.wz), vec4(c.gb, K.xy), step(c.b, c.g));
  41. vec4 q = mix(vec4(p.xyw, c.r), vec4(c.r, p.yzx), step(p.x, c.r));
  42. float d = q.x - min(q.w, q.y);
  43. float e = 1.0e-10;
  44. return vec3(abs(q.z + (q.w - q.y) / (6.0 * d + e)), d / (q.x + e), q.x);
  45. }
  46. //CaliCoastReplay : These HSV/RGB translation functions are
  47. //from http://gamedev.stackexchange.com/questions/59797/glsl-shader-change-hue-saturation-brightness
  48. //This one converts hue-saturation-value color to red-green-blue color
  49. vec3 hsv2rgb(vec3 c)
  50. {
  51. vec4 K = vec4(1.0, 2.0 / 3.0, 1.0 / 3.0, 3.0);
  52. vec3 p = abs(fract(c.xxx + K.xyz) * 6.0 - K.www);
  53. return c.z * mix(K.xxx, clamp(p - K.xxx, 0.0, 1.0), c.y);
  54. }
  55. // math
  56. // bteitler: Turn a vector of Euler angles into a rotation matrix
  57. mat3 fromEuler(vec3 ang) {
  58. vec2 a1 = vec2(sin(ang.x),cos(ang.x));
  59. vec2 a2 = vec2(sin(ang.y),cos(ang.y));
  60. vec2 a3 = vec2(sin(ang.z),cos(ang.z));
  61. mat3 m;
  62. 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);
  63. m[1] = vec3(-a2.y*a1.x,a1.y*a2.y,a2.x);
  64. 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);
  65. return m;
  66. }
  67. // bteitler: A 2D hash function for use in noise generation that returns range [0 .. 1]. You could
  68. // use any hash function of choice, just needs to deterministic and return
  69. // between 0 and 1, and also behave randomly. Googling "GLSL hash function" returns almost exactly
  70. // this function: http://stackoverflow.com/questions/4200224/random-noise-functions-for-glsl
  71. // Performance is a real consideration of hash functions since ray-marching is already so heavy.
  72. float hash( vec2 p ) {
  73. float h = dot(p,vec2(127.1,311.7));
  74. return fract(sin(h)*83758.5453123);
  75. }
  76. // bteitler: A 2D psuedo-random wave / terrain function. This is actually a poor name in my opinion,
  77. // since its the "hash" function that is really the noise, and this function is smoothly interpolating
  78. // between noisy points to create a continuous surface.
  79. float noise( in vec2 p ) {
  80. vec2 i = floor( p );
  81. vec2 f = fract( p );
  82. // bteitler: This is equivalent to the "smoothstep" interpolation function.
  83. // This is a smooth wave function with input between 0 and 1
  84. // (since it is taking the fractional part of <p>) and gives an output
  85. // between 0 and 1 that behaves and looks like a wave. This is far from obvious, but we can graph it to see
  86. // Wolfram link: http://www.wolframalpha.com/input/?i=plot+x*x*%283.0-2.0*x%29+from+x%3D0+to+1
  87. // This is used to interpolate between random points. Any smooth wave function that ramps up from 0 and
  88. // and hit 1.0 over the domain 0 to 1 would work. For instance, sin(f * PI / 2.0) gives similar visuals.
  89. // This function is nice however because it does not require an expensive sine calculation.
  90. vec2 u = f*f*(3.0-2.0*f);
  91. // bteitler: This very confusing looking mish-mash is simply pulling deterministic random values (between 0 and 1)
  92. // for 4 corners of the grid square that <p> is inside, and doing 2D interpolation using the <u> function
  93. // (remember it looks like a nice wave!)
  94. // The grid square has points defined at integer boundaries. For example, if <p> is (4.3, 2.1), we will
  95. // evaluate at points (4, 2), (5, 2), (4, 3), (5, 3), and then interpolate x using u(.3) and y using u(.1).
  96. return -1.0+2.0*mix(
  97. mix( hash( i + vec2(0.0,0.0) ),
  98. hash( i + vec2(1.0,0.0) ),
  99. u.x),
  100. mix( hash( i + vec2(0.0,1.0) ),
  101. hash( i + vec2(1.0,1.0) ),
  102. u.x),
  103. u.y);
  104. }
  105. // bteitler: diffuse lighting calculation - could be tweaked to taste
  106. // lighting
  107. float diffuse(vec3 n,vec3 l,float p) {
  108. return pow(dot(n,l) * 0.4 + 0.6,p);
  109. }
  110. // bteitler: specular lighting calculation - could be tweaked taste
  111. float specular(vec3 n,vec3 l,vec3 e,float s) {
  112. float nrm = (s + 8.0) / (3.1415 * 8.0);
  113. return pow(max(dot(reflect(e,n),l),0.0),s) * nrm;
  114. }
  115. // bteitler: Generate a smooth sky gradient color based on ray direction's Y value
  116. // sky
  117. vec3 getSkyColor(vec3 e) {
  118. e.y = max(e.y,0.0);
  119. vec3 ret;
  120. ret.x = pow(1.0-e.y,2.0);
  121. ret.y = 1.0-e.y;
  122. ret.z = 0.6+(1.0-e.y)*0.4;
  123. return ret;
  124. }
  125. // sea
  126. // bteitler: TLDR is that this passes a low frequency random terrain through a 2D symmetric wave function that looks like this:
  127. // http://www.wolframalpha.com/input/?i=%7B1-%7B%7B%7BAbs%5BCos%5B0.16x%5D%5D+%2B+Abs%5BCos%5B0.16x%5D%5D+%28%281.+-+Abs%5BSin%5B0.16x%5D%5D%29+-+Abs%5BCos%5B0.16x%5D%5D%29%7D+*+%7BAbs%5BCos%5B0.16y%5D%5D+%2B+Abs%5BCos%5B0.16y%5D%5D+%28%281.+-+Abs%5BSin%5B0.16y%5D%5D%29+-+Abs%5BCos%5B0.16y%5D%5D%29%7D%7D%5E0.65%7D%7D%5E4+from+-20+to+20
  128. // The <choppy> parameter affects the wave shape.
  129. float sea_octave(vec2 uv, float choppy) {
  130. // bteitler: Add the smoothed 2D terrain / wave function to the input coordinates
  131. // which are going to be our X and Z world coordinates. It may be unclear why we are doing this.
  132. // This value is about to be passed through a wave function. So we have a smoothed psuedo random height
  133. // field being added to our (X, Z) coordinates, and then fed through yet another wav function below.
  134. uv += noise(uv);
  135. // Note that you could simply return noise(uv) here and it would take on the characteristics of our
  136. // noise interpolation function u and would be a reasonable heightmap for terrain.
  137. // However, that isn't the shape we want in the end for an ocean with waves, so it will be fed through
  138. // a more wave like function. Note that although both x and y channels of <uv> have the same value added, there is a
  139. // symmetry break because <uv>.x and <uv>.y will typically be different values.
  140. // bteitler: This is a wave function with pointy peaks and curved troughs:
  141. // http://www.wolframalpha.com/input/?i=1-abs%28cos%28x%29%29%3B
  142. vec2 wv = 1.0-abs(sin(uv));
  143. // bteitler: This is a wave function with curved peaks and pointy troughs:
  144. // http://www.wolframalpha.com/input/?i=abs%28cos%28x%29%29%3B
  145. vec2 swv = abs(cos(uv));
  146. // bteitler: Blending both wave functions gets us a new, cooler wave function (output between 0 and 1):
  147. // http://www.wolframalpha.com/input/?i=abs%28cos%28x%29%29+%2B+abs%28cos%28x%29%29+*+%28%281.0-abs%28sin%28x%29%29%29+-+abs%28cos%28x%29%29%29
  148. wv = mix(wv,swv,wv);
  149. // bteitler: Finally, compose both of the wave functions for X and Y channels into a final
  150. // 1D height value, shaping it a bit along the way. First, there is the composition (multiplication) of
  151. // the wave functions: wv.x * wv.y. Wolfram will give us a cute 2D height graph for this!:
  152. // http://www.wolframalpha.com/input/?i=%7BAbs%5BCos%5Bx%5D%5D+%2B+Abs%5BCos%5Bx%5D%5D+%28%281.+-+Abs%5BSin%5Bx%5D%5D%29+-+Abs%5BCos%5Bx%5D%5D%29%7D+*+%7BAbs%5BCos%5By%5D%5D+%2B+Abs%5BCos%5By%5D%5D+%28%281.+-+Abs%5BSin%5By%5D%5D%29+-+Abs%5BCos%5By%5D%5D%29%7D
  153. // Next, we reshape the 2D wave function by exponentiation: (wv.x * wv.y)^0.65. This slightly rounds the base of the wave:
  154. // http://www.wolframalpha.com/input/?i=%7B%7BAbs%5BCos%5Bx%5D%5D+%2B+Abs%5BCos%5Bx%5D%5D+%28%281.+-+Abs%5BSin%5Bx%5D%5D%29+-+Abs%5BCos%5Bx%5D%5D%29%7D+*+%7BAbs%5BCos%5By%5D%5D+%2B+Abs%5BCos%5By%5D%5D+%28%281.+-+Abs%5BSin%5By%5D%5D%29+-+Abs%5BCos%5By%5D%5D%29%7D%7D%5E0.65
  155. // one last final transform (with choppy = 4) results in this which resembles a recognizable ocean wave shape in 2D:
  156. // http://www.wolframalpha.com/input/?i=%7B1-%7B%7B%7BAbs%5BCos%5Bx%5D%5D+%2B+Abs%5BCos%5Bx%5D%5D+%28%281.+-+Abs%5BSin%5Bx%5D%5D%29+-+Abs%5BCos%5Bx%5D%5D%29%7D+*+%7BAbs%5BCos%5By%5D%5D+%2B+Abs%5BCos%5By%5D%5D+%28%281.+-+Abs%5BSin%5By%5D%5D%29+-+Abs%5BCos%5By%5D%5D%29%7D%7D%5E0.65%7D%7D%5E4
  157. // Note that this function is called with a specific frequency multiplier which will stretch out the wave. Here is the graph
  158. // with the base frequency used by map and map_detailed (0.16):
  159. // http://www.wolframalpha.com/input/?i=%7B1-%7B%7B%7BAbs%5BCos%5B0.16x%5D%5D+%2B+Abs%5BCos%5B0.16x%5D%5D+%28%281.+-+Abs%5BSin%5B0.16x%5D%5D%29+-+Abs%5BCos%5B0.16x%5D%5D%29%7D+*+%7BAbs%5BCos%5B0.16y%5D%5D+%2B+Abs%5BCos%5B0.16y%5D%5D+%28%281.+-+Abs%5BSin%5B0.16y%5D%5D%29+-+Abs%5BCos%5B0.16y%5D%5D%29%7D%7D%5E0.65%7D%7D%5E4+from+-20+to+20
  160. return pow(1.0-pow(wv.x * wv.y,0.65),choppy);
  161. }
  162. // bteitler: Compute the distance along Y axis of a point to the surface of the ocean
  163. // using a low(er) resolution ocean height composition function (less iterations).
  164. float map(vec3 p) {
  165. float freq = SEA_FREQ;
  166. float amp = SEA_HEIGHT;
  167. float choppy = SEA_CHOPPY;
  168. vec2 uv = p.xz; uv.x *= 0.75;
  169. // bteitler: Compose our wave noise generation ("sea_octave") with different frequencies
  170. // and offsets to achieve a final height map that looks like an ocean. Likely lots
  171. // of black magic / trial and error here to get it to look right. Each sea_octave has this shape:
  172. // http://www.wolframalpha.com/input/?i=%7B1-%7B%7B%7BAbs%5BCos%5B0.16x%5D%5D+%2B+Abs%5BCos%5B0.16x%5D%5D+%28%281.+-+Abs%5BSin%5B0.16x%5D%5D%29+-+Abs%5BCos%5B0.16x%5D%5D%29%7D+*+%7BAbs%5BCos%5B0.16y%5D%5D+%2B+Abs%5BCos%5B0.16y%5D%5D+%28%281.+-+Abs%5BSin%5B0.16y%5D%5D%29+-+Abs%5BCos%5B0.16y%5D%5D%29%7D%7D%5E0.65%7D%7D%5E4+from+-20+to+20
  173. // which should give you an idea of what is going. You don't need to graph this function because it
  174. // appears to your left :)
  175. float d, h = 0.0;
  176. for(int i = 0; i < ITER_GEOMETRY; i++) {
  177. // bteitler: start out with our 2D symmetric wave at the current frequency
  178. d = sea_octave((uv+SEA_TIME)*freq,choppy);
  179. // bteitler: stack wave ontop of itself at an offset that varies over time for more height and wave pattern variance
  180. //d += sea_octave((uv-SEA_TIME)*freq,choppy);
  181. h += d * amp; // bteitler: Bump our height by the current wave function
  182. // bteitler: "Twist" our domain input into a different space based on a permutation matrix
  183. // The scales of the matrix values affect the frequency of the wave at this iteration, but more importantly
  184. // it is responsible for the realistic assymetry since the domain is shiftly differently.
  185. // This is likely the most important parameter for wave topology.
  186. uv *= octave_m;
  187. freq *= 1.9; // bteitler: Exponentially increase frequency every iteration (on top of our permutation)
  188. amp *= 0.22; // bteitler: Lower the amplitude every frequency, since we are adding finer and finer detail
  189. // bteitler: finally, adjust the choppy parameter which will effect our base 2D sea_octave shape a bit. This makes
  190. // the "waves within waves" have different looking shapes, not just frequency and offset
  191. choppy = mix(choppy,1.0,0.2);
  192. }
  193. return p.y - h;
  194. }
  195. // bteitler: Compute the distance along Y axis of a point to the surface of the ocean
  196. // using a high(er) resolution ocean height composition function (more iterations).
  197. float map_detailed(vec3 p) {
  198. float freq = SEA_FREQ;
  199. float amp = SEA_HEIGHT;
  200. float choppy = SEA_CHOPPY;
  201. vec2 uv = p.xz; uv.x *= 0.75;
  202. // bteitler: Compose our wave noise generation ("sea_octave") with different frequencies
  203. // and offsets to achieve a final height map that looks like an ocean. Likely lots
  204. // of black magic / trial and error here to get it to look right. Each sea_octave has this shape:
  205. // http://www.wolframalpha.com/input/?i=%7B1-%7B%7B%7BAbs%5BCos%5B0.16x%5D%5D+%2B+Abs%5BCos%5B0.16x%5D%5D+%28%281.+-+Abs%5BSin%5B0.16x%5D%5D%29+-+Abs%5BCos%5B0.16x%5D%5D%29%7D+*+%7BAbs%5BCos%5B0.16y%5D%5D+%2B+Abs%5BCos%5B0.16y%5D%5D+%28%281.+-+Abs%5BSin%5B0.16y%5D%5D%29+-+Abs%5BCos%5B0.16y%5D%5D%29%7D%7D%5E0.65%7D%7D%5E4+from+-20+to+20
  206. // which should give you an idea of what is going. You don't need to graph this function because it
  207. // appears to your left :)
  208. float d, h = 0.0;
  209. for(int i = 0; i < ITER_FRAGMENT; i++) {
  210. // bteitler: start out with our 2D symmetric wave at the current frequency
  211. d = sea_octave((uv+SEA_TIME)*freq,choppy);
  212. // bteitler: stack wave ontop of itself at an offset that varies over time for more height and wave pattern variance
  213. d += sea_octave((uv-SEA_TIME)*freq,choppy);
  214. h += d * amp; // bteitler: Bump our height by the current wave function
  215. // bteitler: "Twist" our domain input into a different space based on a permutation matrix
  216. // The scales of the matrix values affect the frequency of the wave at this iteration, but more importantly
  217. // it is responsible for the realistic assymetry since the domain is shiftly differently.
  218. // This is likely the most important parameter for wave topology.
  219. uv *= octave_m/1.2;
  220. freq *= 1.9; // bteitler: Exponentially increase frequency every iteration (on top of our permutation)
  221. amp *= 0.22; // bteitler: Lower the amplitude every frequency, since we are adding finer and finer detail
  222. // bteitler: finally, adjust the choppy parameter which will effect our base 2D sea_octave shape a bit. This makes
  223. // the "waves within waves" have different looking shapes, not just frequency and offset
  224. choppy = mix(choppy,1.0,0.2);
  225. }
  226. return p.y - h;
  227. }
  228. // bteitler:
  229. // p: point on ocean surface to get color for
  230. // n: normal on ocean surface at <p>
  231. // l: light (sun) direction
  232. // eye: ray direction from camera position for this pixel
  233. // dist: distance from camera to point <p> on ocean surface
  234. vec3 getSeaColor(vec3 p, vec3 n, vec3 l, vec3 eye, vec3 dist) {
  235. // bteitler: Fresnel is an exponential that gets bigger when the angle between ocean
  236. // surface normal and eye ray is smaller
  237. float fresnel = 1.0 - max(dot(n,-eye),0.0);
  238. fresnel = pow(fresnel,3.0) * 0.45;
  239. // bteitler: Bounce eye ray off ocean towards sky, and get the color of the sky
  240. vec3 reflected = getSkyColor(reflect(eye,n))*0.99;
  241. // bteitler: refraction effect based on angle between light surface normal
  242. vec3 refracted = SEA_BASE + diffuse(n,l,80.0) * SEA_WATER_COLOR * 0.27;
  243. // bteitler: blend the refracted color with the reflected color based on our fresnel term
  244. vec3 color = mix(refracted,reflected,fresnel);
  245. // bteitler: Apply a distance based attenuation factor which is stronger
  246. // at peaks
  247. float atten = max(1.0 - dot(dist,dist) * 0.001, 0.0);
  248. color += SEA_WATER_COLOR * (p.y - SEA_HEIGHT) * 0.15 * atten;
  249. // bteitler: Apply specular highlight
  250. color += vec3(specular(n,l,eye,90.0))*0.5;
  251. return color;
  252. }
  253. // bteitler: Estimate the normal at a point <p> on the ocean surface using a slight more detailed
  254. // ocean mapping function (using more noise octaves).
  255. // Takes an argument <eps> (stands for epsilon) which is the resolution to use
  256. // for the gradient. See here for more info on gradients: https://en.wikipedia.org/wiki/Gradient
  257. // tracing
  258. vec3 getNormal(vec3 p, float eps) {
  259. // bteitler: Approximate gradient. An exact gradient would need the "map" / "map_detailed" functions
  260. // to return x, y, and z, but it only computes height relative to surface along Y axis. I'm assuming
  261. // for simplicity and / or optimization reasons we approximate the gradient by the change in ocean
  262. // height for all axis.
  263. vec3 n;
  264. n.y = map_detailed(p); // bteitler: Detailed height relative to surface, temporarily here to save a variable?
  265. n.x = map_detailed(vec3(p.x+eps,p.y,p.z)) - n.y; // bteitler approximate X gradient as change in height along X axis delta
  266. n.z = map_detailed(vec3(p.x,p.y,p.z+eps)) - n.y; // bteitler approximate Z gradient as change in height along Z axis delta
  267. // bteitler: Taking advantage of the fact that we know we won't have really steep waves, we expect
  268. // the Y normal component to be fairly large always. Sacrifices yet more accurately to avoid some calculation.
  269. n.y = eps;
  270. return normalize(n);
  271. // bteitler: A more naive and easy to understand version could look like this and
  272. // produces almost the same visuals and is a little more expensive.
  273. // vec3 n;
  274. // float h = map_detailed(p);
  275. // n.y = map_detailed(vec3(p.x,p.y+eps,p.z)) - h;
  276. // n.x = map_detailed(vec3(p.x+eps,p.y,p.z)) - h;
  277. // n.z = map_detailed(vec3(p.x,p.y,p.z+eps)) - h;
  278. // return normalize(n);
  279. }
  280. //CaliCoastReplay : Keyboard checking function from the iChannel representing keyboard input
  281. float isKeyPressed(float key)
  282. {
  283. return texture( iChannel1, vec2(key, 1.0) ).x;
  284. }
  285. // bteitler: Find out where a ray intersects the current ocean
  286. float heightMapTracing(vec3 ori, vec3 dir, out vec3 p) {
  287. float tm = 0.0;
  288. float tx = 500.0; // bteitler: a really far distance, this could likely be tweaked a bit as desired
  289. // bteitler: At a really far away distance along the ray, what is it's height relative
  290. // to the ocean in ONLY the Y direction?
  291. float hx = map(ori + dir * tx);
  292. // bteitler: A positive height relative to the ocean surface (in Y direction) at a really far distance means
  293. // this pixel is pure sky. Quit early and return the far distance constant.
  294. if(hx > 0.0) return tx;
  295. // bteitler: hm starts out as the height of the camera position relative to ocean.
  296. float hm = map(ori + dir * tm);
  297. // bteitler: This is the main ray marching logic. This is probably the single most confusing part of the shader
  298. // since height mapping is not an exact distance field (tells you distance to surface if you drop a line down to ocean
  299. // surface in the Y direction, but there could have been a peak at a very close point along the x and z
  300. // directions that is closer). Therefore, it would be possible/easy to overshoot the surface using the raw height field
  301. // as the march distance. The author uses a trick to compensate for this.
  302. float tmid = 0.0;
  303. for(int i = 0; i < NUM_STEPS; i++) { // bteitler: Constant number of ray marches per ray that hits the water
  304. // bteitler: Move forward along ray in such a way that has the following properties:
  305. // 1. If our current height relative to ocean is higher, move forward more
  306. // 2. If the height relative to ocean floor very far along the ray is much lower
  307. // below the ocean surface, move forward less
  308. // Idea behind 1. is that if we are far above the ocean floor we can risk jumping
  309. // forward more without shooting under ocean, because the ocean is mostly level.
  310. // The idea behind 2. is that if extruding the ray goes farther under the ocean, then
  311. // you are looking more orthgonal to ocean surface (as opposed to looking towards horizon), and therefore
  312. // movement along the ray gets closer to ocean faster, so we need to move forward less to reduce risk
  313. // of overshooting.
  314. tmid = mix(tm,tx, hm/(hm-hx));
  315. p = ori + dir * tmid;
  316. float hmid = map(p); // bteitler: Re-evaluate height relative to ocean surface in Y axis
  317. if(hmid < 0.0) { // bteitler: We went through the ocean surface if we are negative relative to surface now
  318. // bteitler: So instead of actually marching forward to cross the surface, we instead
  319. // assign our really far distance and height to be where we just evaluated that crossed the surface.
  320. // Next iteration will attempt to go forward more and is less likely to cross the boundary.
  321. // A naive implementation might have returned <tmid> immediately here, which
  322. // results in a much poorer / somewhat indeterministic quality rendering.
  323. tx = tmid;
  324. hx = hmid;
  325. } else {
  326. // Haven't hit surface yet, easy case, just march forward
  327. tm = tmid;
  328. hm = hmid;
  329. }
  330. }
  331. // bteitler: Return the distance, which should be really close to the height map without going under the ocean
  332. return tmid;
  333. }
  334. // main
  335. void mainImage( out vec4 fragColor, in vec2 fragCoord ) {
  336. // bteitler: 2D Pixel location passed in as raw pixel, let's divide by resolution
  337. // to convert to coordinates between 0 and 1
  338. vec2 uv = fragCoord.xy / iResolution.xy;
  339. uv = uv * 2.0 - 1.0; // bteitler: Shift pixel coordinates from 0 to 1 to between -1 and 1
  340. uv.x *= iResolution.x / iResolution.y; // bteitler: Aspect ratio correction - if you don't do this your rays will be distorted
  341. float time = iTime * 2.7; // bteitler: Animation is based on time, but allows you to scrub the animation based on mouse movement
  342. // ray
  343. // bteitler: Calculated a vector that smoothly changes over time in a sinusoidal (wave) pattern.
  344. // This will be used to drive where the user is looking in world space.
  345. // vec3 ang = vec3(sin(time*3.0)*0.1,sin(time)*0.2+0.3,time);
  346. float roll = PI + sin(iTime)/14.0 + cos(iTime/2.0)/14.0 ;
  347. float pitch = PI*1.021 + (sin(iTime/2.0)+ cos(iTime))/40.0
  348. + (iMouse.y/iResolution.y - .8)*PI/3.0 ;
  349. float yaw = iMouse.x/iResolution.x * PI * 4.0;
  350. vec3 ang = vec3(roll,pitch,yaw);
  351. // vec3 ang = vec3(roll,pitch,0);
  352. // bteitler: Calculate the "origin" of the camera in world space based on time. Camera is located
  353. // at height 3.5 atx 0 (zero), and flies over the ocean in the z axis over time.
  354. vec3 ori = vec3(0.0,3.5,time*3.0);
  355. // bteitler: This is the ray direction we are shooting from the camera location ("ori") that we need to light
  356. // for this pixel. The -2.0 indicates we are using a focal length of 2.0 - this is just an artistic choice and
  357. // results in about a 90 degree field of view.
  358. // CaliCoastReplay : Adjusted slightly to a lower focal length. Seems to dramatize the scene.
  359. vec3 dir = normalize(vec3(uv.xy,-1.6));
  360. // bteitler: Distort the ray a bit for a fish eye effect (if you remove this line, it will remove
  361. // the fish eye effect and look like a realistic perspective).
  362. // dir.z += length(uv) * 0.15;
  363. // bteitler: Renormalize the ray direction, and then rotate it based on the previously calculated
  364. // animation angle "ang". "fromEuler" just calculates a rotation matrix from a vector of angles.
  365. // if you remove the " * fromEuler(ang)" part, you will disable the camera rotation animation.
  366. dir = normalize(dir) * fromEuler(ang);
  367. // tracing
  368. // bteitler: ray-march to the ocean surface (which can be thought of as a randomly generated height map)
  369. // and store in p
  370. vec3 p;
  371. heightMapTracing(ori,dir,p);
  372. vec3 dist = p - ori; // bteitler: distance vector to ocean surface for this pixel's ray
  373. // bteitler: Calculate the normal on the ocean surface where we intersected (p), using
  374. // different "resolution" (in a sense) based on how far away the ray traveled. Normals close to
  375. // the camera should be calculated with high resolution, and normals far from the camera should be calculated with low resolution
  376. // The reason to do this is that specular effects (or non linear normal based lighting effects) become fairly random at
  377. // far distances and low resolutions and can cause unpleasant shimmering during motion.
  378. vec3 n = getNormal(p,
  379. dot(dist,dist) // bteitler: Think of this as inverse resolution, so far distances get bigger at an expnential rate
  380. * EPSILON_NRM // bteitler: Just a resolution constant.. could easily be tweaked to artistic content
  381. );
  382. // bteitler: direction of the infinitely far away directional light. Changing this will change
  383. // the sunlight direction.
  384. vec3 light = normalize(vec3(0.0,1.0,0.8));
  385. // CaliCoastReplay: Get the sky and sea colors
  386. vec3 skyColor = getSkyColor(dir);
  387. vec3 seaColor = getSeaColor(p,n,light,dir,dist);
  388. //Sea/sky preprocessing
  389. //CaliCoastReplay: A distance falloff for the sea color. Drastically darkens the sea,
  390. //this will be reversed later based on day/night.
  391. seaColor /= sqrt(sqrt(length(dist))) ;
  392. //CaliCoastReplay: Day/night mode
  393. bool night;
  394. if( isKeyPressed(KEY_SP) > 0.0 ) //night mode!
  395. {
  396. //Brighten the sea up again, but not too bright at night
  397. seaColor *= seaColor * 8.5;
  398. //Turn down the sky
  399. skyColor /= 1.69;
  400. //Store that it's night mode for later HSV calcc
  401. night = true;
  402. }
  403. else //day mode!
  404. {
  405. //Brighten the sea up again - bright and beautiful blue at day
  406. seaColor *= sqrt(sqrt(seaColor)) * 4.0;
  407. skyColor *= 1.05;
  408. skyColor -= 0.03;
  409. night = false;
  410. }
  411. //CaliCoastReplay: A slight "constrasting" for the sky to match the more contrasted ocean
  412. skyColor *= skyColor;
  413. //CaliCoastReplay: A rather hacky manipulation of the high-value regions in the image that seems
  414. //to add a subtle charm and "sheen" and foamy effect to high value regions through subtle darkening,
  415. //but it is hacky, and not physically modeled at all.
  416. vec3 seaHsv = rgb2hsv(seaColor);
  417. if (seaHsv.z > .75 && length(dist) < 50.0)
  418. seaHsv.z -= (0.9 - seaHsv.z) * 1.3;
  419. seaColor = hsv2rgb(seaHsv);
  420. // bteitler: Mix (linear interpolate) a color calculated for the sky (based solely on ray direction) and a sea color
  421. // which contains a realistic lighting model. This is basically doing a fog calculation: weighing more the sky color
  422. // in the distance in an exponential manner.
  423. vec3 color = mix(
  424. skyColor,
  425. seaColor,
  426. pow(smoothstep(0.0,-0.05,dir.y), 0.3) // bteitler: Can be thought of as "fog" that gets thicker in the distance
  427. );
  428. // Postprocessing
  429. // bteitler: Apply an overall image brightness factor as the final color for this pixel. Can be
  430. // tweaked artistically.
  431. fragColor = vec4(pow(color,vec3(0.75)), 1.0);
  432. // CaliCoastReplay: Adjust hue, saturation, and value adjustment for an even more processed look
  433. // hsv.x is hue, hsv.y is saturation, and hsv.z is value
  434. vec3 hsv = rgb2hsv(fragColor.xyz);
  435. //CaliCoastReplay: Increase saturation slightly
  436. hsv.y += 0.131;
  437. //CaliCoastReplay:
  438. //A pseudo-multiplicative adjustment of value, increasing intensity near 1 and decreasing it near
  439. //0 to achieve a more contrasted, real-world look
  440. hsv.z *= sqrt(hsv.z) * 1.1;
  441. if (night)
  442. {
  443. ///CaliCoastReplay:
  444. //Slight value adjustment at night to turn down global intensity
  445. hsv.z -= 0.045;
  446. hsv*=0.8;
  447. hsv.x += 0.12 + hsv.z/100.0;
  448. //Highly increased saturation at night op, oddly. Nights appear to be very colorful
  449. //within their ranges.
  450. hsv.y *= 2.87;
  451. }
  452. else
  453. {
  454. //CaliCoastReplay:
  455. //Add green tinge to the high range
  456. //Turn down intensity in day in a different way
  457. hsv.z *= 0.9;
  458. //CaliCoastReplay: Hue alteration
  459. hsv.x -= hsv.z/10.0;
  460. hsv.x += 0.02 + hsv.z/50.0;
  461. //Final brightening
  462. hsv.z *= 1.01;
  463. //This really "cinemafies" it for the day -
  464. //puts the saturation on a squared, highly magnified footing.
  465. //Worth looking into more as to exactly why.
  466. // hsv.y *= 5.10 * hsv.y * sqrt(hsv.y);
  467. hsv.y += 0.07;
  468. }
  469. //CaliCoastReplay:
  470. //Replace the final color with the adjusted, translated HSV values
  471. fragColor.xyz = hsv2rgb(hsv);
  472. }