OceanShaders.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388
  1. // Author: Aleksandr Albert
  2. // Website: www.routter.co.tt
  3. // Description: A deep water ocean shader set
  4. // based on an implementation of a Tessendorf Waves
  5. // originally presented by David Li ( www.david.li/waves )
  6. // The general method is to apply shaders to simulation Framebuffers
  7. // and then sample these framebuffers when rendering the ocean mesh
  8. // The set uses 7 shaders:
  9. // -- Simulation shaders
  10. // [1] ocean_sim_vertex -> Vertex shader used to set up a 2x2 simulation plane centered at (0,0)
  11. // [2] ocean_subtransform -> Fragment shader used to subtransform the mesh (generates the displacement map)
  12. // [3] ocean_initial_spectrum -> Fragment shader used to set intitial wave frequency at a texel coordinate
  13. // [4] ocean_phase -> Fragment shader used to set wave phase at a texel coordinate
  14. // [5] ocean_spectrum -> Fragment shader used to set current wave frequency at a texel coordinate
  15. // [6] ocean_normal -> Fragment shader used to set face normals at a texel coordinate
  16. // -- Rendering Shader
  17. // [7] ocean_main -> Vertex and Fragment shader used to create the final render
  18. THREE.ShaderLib['ocean_sim_vertex'] = {
  19. varying: {
  20. "vUV": { type: "v2" }
  21. },
  22. vertexShader: [
  23. 'varying vec2 vUV;',
  24. 'void main (void) {',
  25. 'vUV = position.xy * 0.5 + 0.5;',
  26. 'gl_Position = vec4(position, 1.0 );',
  27. '}'
  28. ].join('\n')
  29. };
  30. THREE.ShaderLib['ocean_subtransform'] = {
  31. uniforms: {
  32. "u_input": { type: "t", value: null },
  33. "u_transformSize": { type: "f", value: 512.0 },
  34. "u_subtransformSize": { type: "f", value: 250.0 }
  35. },
  36. varying: {
  37. "vUV": { type: "v2" }
  38. },
  39. fragmentShader: [
  40. //GPU FFT using a Stockham formulation
  41. 'precision highp float;',
  42. 'const float PI = 3.14159265359;',
  43. 'uniform sampler2D u_input;',
  44. 'uniform float u_transformSize;',
  45. 'uniform float u_subtransformSize;',
  46. 'varying vec2 vUV;',
  47. 'vec2 multiplyComplex (vec2 a, vec2 b) {',
  48. 'return vec2(a[0] * b[0] - a[1] * b[1], a[1] * b[0] + a[0] * b[1]);',
  49. '}',
  50. 'void main (void) {',
  51. '#ifdef HORIZONTAL',
  52. 'float index = vUV.x * u_transformSize - 0.5;',
  53. '#else',
  54. 'float index = vUV.y * u_transformSize - 0.5;',
  55. '#endif',
  56. 'float evenIndex = floor(index / u_subtransformSize) * (u_subtransformSize * 0.5) + mod(index, u_subtransformSize * 0.5);',
  57. //transform two complex sequences simultaneously
  58. '#ifdef HORIZONTAL',
  59. 'vec4 even = texture2D(u_input, vec2(evenIndex + 0.5, gl_FragCoord.y) / u_transformSize).rgba;',
  60. 'vec4 odd = texture2D(u_input, vec2(evenIndex + u_transformSize * 0.5 + 0.5, gl_FragCoord.y) / u_transformSize).rgba;',
  61. '#else',
  62. 'vec4 even = texture2D(u_input, vec2(gl_FragCoord.x, evenIndex + 0.5) / u_transformSize).rgba;',
  63. 'vec4 odd = texture2D(u_input, vec2(gl_FragCoord.x, evenIndex + u_transformSize * 0.5 + 0.5) / u_transformSize).rgba;',
  64. '#endif',
  65. 'float twiddleArgument = -2.0 * PI * (index / u_subtransformSize);',
  66. 'vec2 twiddle = vec2(cos(twiddleArgument), sin(twiddleArgument));',
  67. 'vec2 outputA = even.xy + multiplyComplex(twiddle, odd.xy);',
  68. 'vec2 outputB = even.zw + multiplyComplex(twiddle, odd.zw);',
  69. 'gl_FragColor = vec4(outputA, outputB);',
  70. '}'
  71. ].join('\n')
  72. };
  73. THREE.ShaderLib['ocean_initial_spectrum'] = {
  74. uniforms: {
  75. "u_wind": { type: "v2", value: new THREE.Vector2(10.0, 10.0) },
  76. "u_resolution": { type: "f", value: 512.0 },
  77. "u_size": { type: "f", value: 250.0 },
  78. },
  79. fragmentShader: [
  80. 'precision highp float;',
  81. 'const float PI = 3.14159265359;',
  82. 'const float G = 9.81;',
  83. 'const float KM = 370.0;',
  84. 'const float CM = 0.23;',
  85. 'uniform vec2 u_wind;',
  86. 'uniform float u_resolution;',
  87. 'uniform float u_size;',
  88. 'float square (float x) {',
  89. 'return x * x;',
  90. '}',
  91. 'float omega (float k) {',
  92. 'return sqrt(G * k * (1.0 + square(k / KM)));',
  93. '}',
  94. 'float tanh (float x) {',
  95. 'return (1.0 - exp(-2.0 * x)) / (1.0 + exp(-2.0 * x));',
  96. '}',
  97. 'void main (void) {',
  98. 'vec2 coordinates = gl_FragCoord.xy - 0.5;',
  99. 'float n = (coordinates.x < u_resolution * 0.5) ? coordinates.x : coordinates.x - u_resolution;',
  100. 'float m = (coordinates.y < u_resolution * 0.5) ? coordinates.y : coordinates.y - u_resolution;',
  101. 'vec2 K = (2.0 * PI * vec2(n, m)) / u_size;',
  102. 'float k = length(K);',
  103. 'float l_wind = length(u_wind);',
  104. 'float Omega = 0.84;',
  105. 'float kp = G * square(Omega / l_wind);',
  106. 'float c = omega(k) / k;',
  107. 'float cp = omega(kp) / kp;',
  108. 'float Lpm = exp(-1.25 * square(kp / k));',
  109. 'float gamma = 1.7;',
  110. 'float sigma = 0.08 * (1.0 + 4.0 * pow(Omega, -3.0));',
  111. 'float Gamma = exp(-square(sqrt(k / kp) - 1.0) / 2.0 * square(sigma));',
  112. 'float Jp = pow(gamma, Gamma);',
  113. 'float Fp = Lpm * Jp * exp(-Omega / sqrt(10.0) * (sqrt(k / kp) - 1.0));',
  114. 'float alphap = 0.006 * sqrt(Omega);',
  115. 'float Bl = 0.5 * alphap * cp / c * Fp;',
  116. 'float z0 = 0.000037 * square(l_wind) / G * pow(l_wind / cp, 0.9);',
  117. 'float uStar = 0.41 * l_wind / log(10.0 / z0);',
  118. 'float alpham = 0.01 * ((uStar < CM) ? (1.0 + log(uStar / CM)) : (1.0 + 3.0 * log(uStar / CM)));',
  119. 'float Fm = exp(-0.25 * square(k / KM - 1.0));',
  120. 'float Bh = 0.5 * alpham * CM / c * Fm * Lpm;',
  121. 'float a0 = log(2.0) / 4.0;',
  122. 'float am = 0.13 * uStar / CM;',
  123. 'float Delta = tanh(a0 + 4.0 * pow(c / cp, 2.5) + am * pow(CM / c, 2.5));',
  124. 'float cosPhi = dot(normalize(u_wind), normalize(K));',
  125. 'float S = (1.0 / (2.0 * PI)) * pow(k, -4.0) * (Bl + Bh) * (1.0 + Delta * (2.0 * cosPhi * cosPhi - 1.0));',
  126. 'float dk = 2.0 * PI / u_size;',
  127. 'float h = sqrt(S / 2.0) * dk;',
  128. 'if (K.x == 0.0 && K.y == 0.0) {',
  129. 'h = 0.0;', //no DC term
  130. '}',
  131. 'gl_FragColor = vec4(h, 0.0, 0.0, 0.0);',
  132. '}'
  133. ].join('\n')
  134. };
  135. THREE.ShaderLib['ocean_phase'] = {
  136. uniforms: {
  137. "u_phases": { type: "t", value: null },
  138. "u_deltaTime": { type: "f", value: null },
  139. "u_resolution": { type: "f", value: null },
  140. "u_size": { type: "f", value: null },
  141. },
  142. varying: {
  143. "vUV": { type: "v2" }
  144. },
  145. fragmentShader: [
  146. 'precision highp float;',
  147. 'const float PI = 3.14159265359;',
  148. 'const float G = 9.81;',
  149. 'const float KM = 370.0;',
  150. 'varying vec2 vUV;',
  151. 'uniform sampler2D u_phases;',
  152. 'uniform float u_deltaTime;',
  153. 'uniform float u_resolution;',
  154. 'uniform float u_size;',
  155. 'float omega (float k) {',
  156. 'return sqrt(G * k * (1.0 + k * k / KM * KM));',
  157. '}',
  158. 'void main (void) {',
  159. 'float deltaTime = 1.0 / 60.0;',
  160. 'vec2 coordinates = gl_FragCoord.xy - 0.5;',
  161. 'float n = (coordinates.x < u_resolution * 0.5) ? coordinates.x : coordinates.x - u_resolution;',
  162. 'float m = (coordinates.y < u_resolution * 0.5) ? coordinates.y : coordinates.y - u_resolution;',
  163. 'vec2 waveVector = (2.0 * PI * vec2(n, m)) / u_size;',
  164. 'float phase = texture2D(u_phases, vUV).r;',
  165. 'float deltaPhase = omega(length(waveVector)) * u_deltaTime;',
  166. 'phase = mod(phase + deltaPhase, 2.0 * PI);',
  167. 'gl_FragColor = vec4(phase, 0.0, 0.0, 0.0);',
  168. '}'
  169. ].join('\n')
  170. };
  171. THREE.ShaderLib['ocean_spectrum'] = {
  172. uniforms: {
  173. "u_size": { type: "f", value: null },
  174. "u_resolution": { type: "f", value: null },
  175. "u_choppiness": { type: "f", value: null },
  176. "u_phases": { type: "t", value: null },
  177. "u_initialSpectrum": { type: "t", value: null },
  178. },
  179. varying: {
  180. "vUV": { type: "v2" }
  181. },
  182. fragmentShader: [
  183. 'precision highp float;',
  184. 'const float PI = 3.14159265359;',
  185. 'const float G = 9.81;',
  186. 'const float KM = 370.0;',
  187. 'varying vec2 vUV;',
  188. 'uniform float u_size;',
  189. 'uniform float u_resolution;',
  190. 'uniform float u_choppiness;',
  191. 'uniform sampler2D u_phases;',
  192. 'uniform sampler2D u_initialSpectrum;',
  193. 'vec2 multiplyComplex (vec2 a, vec2 b) {',
  194. 'return vec2(a[0] * b[0] - a[1] * b[1], a[1] * b[0] + a[0] * b[1]);',
  195. '}',
  196. 'vec2 multiplyByI (vec2 z) {',
  197. 'return vec2(-z[1], z[0]);',
  198. '}',
  199. 'float omega (float k) {',
  200. 'return sqrt(G * k * (1.0 + k * k / KM * KM));',
  201. '}',
  202. 'void main (void) {',
  203. 'vec2 coordinates = gl_FragCoord.xy - 0.5;',
  204. 'float n = (coordinates.x < u_resolution * 0.5) ? coordinates.x : coordinates.x - u_resolution;',
  205. 'float m = (coordinates.y < u_resolution * 0.5) ? coordinates.y : coordinates.y - u_resolution;',
  206. 'vec2 waveVector = (2.0 * PI * vec2(n, m)) / u_size;',
  207. 'float phase = texture2D(u_phases, vUV).r;',
  208. 'vec2 phaseVector = vec2(cos(phase), sin(phase));',
  209. 'vec2 h0 = texture2D(u_initialSpectrum, vUV).rg;',
  210. 'vec2 h0Star = texture2D(u_initialSpectrum, vec2(1.0 - vUV + 1.0 / u_resolution)).rg;',
  211. 'h0Star.y *= -1.0;',
  212. 'vec2 h = multiplyComplex(h0, phaseVector) + multiplyComplex(h0Star, vec2(phaseVector.x, -phaseVector.y));',
  213. 'vec2 hX = -multiplyByI(h * (waveVector.x / length(waveVector))) * u_choppiness;',
  214. 'vec2 hZ = -multiplyByI(h * (waveVector.y / length(waveVector))) * u_choppiness;',
  215. //no DC term
  216. 'if (waveVector.x == 0.0 && waveVector.y == 0.0) {',
  217. 'h = vec2(0.0);',
  218. 'hX = vec2(0.0);',
  219. 'hZ = vec2(0.0);',
  220. '}',
  221. 'gl_FragColor = vec4(hX + multiplyByI(h), hZ);',
  222. '}'
  223. ].join('\n')
  224. };
  225. THREE.ShaderLib['ocean_normals'] = {
  226. uniforms: {
  227. "u_displacementMap": { type: "t", value: null },
  228. "u_resolution": { type: "f", value: null },
  229. "u_size": { type: "f", value: null },
  230. },
  231. varying: {
  232. "vUV": { type: "v2" }
  233. },
  234. fragmentShader: [
  235. 'precision highp float;',
  236. 'varying vec2 vUV;',
  237. 'uniform sampler2D u_displacementMap;',
  238. 'uniform float u_resolution;',
  239. 'uniform float u_size;',
  240. 'void main (void) {',
  241. 'float texel = 1.0 / u_resolution;',
  242. 'float texelSize = u_size / u_resolution;',
  243. 'vec3 center = texture2D(u_displacementMap, vUV).rgb;',
  244. 'vec3 right = vec3(texelSize, 0.0, 0.0) + texture2D(u_displacementMap, vUV + vec2(texel, 0.0)).rgb - center;',
  245. 'vec3 left = vec3(-texelSize, 0.0, 0.0) + texture2D(u_displacementMap, vUV + vec2(-texel, 0.0)).rgb - center;',
  246. 'vec3 top = vec3(0.0, 0.0, -texelSize) + texture2D(u_displacementMap, vUV + vec2(0.0, -texel)).rgb - center;',
  247. 'vec3 bottom = vec3(0.0, 0.0, texelSize) + texture2D(u_displacementMap, vUV + vec2(0.0, texel)).rgb - center;',
  248. 'vec3 topRight = cross(right, top);',
  249. 'vec3 topLeft = cross(top, left);',
  250. 'vec3 bottomLeft = cross(left, bottom);',
  251. 'vec3 bottomRight = cross(bottom, right);',
  252. 'gl_FragColor = vec4(normalize(topRight + topLeft + bottomLeft + bottomRight), 1.0);',
  253. '}'
  254. ].join('\n')
  255. };
  256. THREE.ShaderLib['ocean_main'] = {
  257. uniforms: {
  258. "u_displacementMap": { type: "t", value: null },
  259. "u_normalMap": { type: "t", value: null },
  260. "u_geometrySize": { type: "f", value: null },
  261. "u_size": { type: "f", value: null },
  262. "u_projectionMatrix": { type: "m4", value: null },
  263. "u_viewMatrix": { type: "m4", value: null },
  264. "u_cameraPosition": { type: "v3", value: null },
  265. "u_skyColor": { type: "v3", value: null },
  266. "u_oceanColor": { type: "v3", value: null },
  267. "u_sunDirection": { type: "v3", value: null },
  268. "u_exposure": { type: "f", value: null },
  269. },
  270. varying: {
  271. "vPos": { type: "v3" },
  272. "vUV": { type: "v2" }
  273. },
  274. vertexShader: [
  275. 'precision highp float;',
  276. 'varying vec3 vPos;',
  277. 'varying vec2 vUV;',
  278. 'uniform mat4 u_projectionMatrix;',
  279. 'uniform mat4 u_viewMatrix;',
  280. 'uniform float u_size;',
  281. 'uniform float u_geometrySize;',
  282. 'uniform sampler2D u_displacementMap;',
  283. 'void main (void) {',
  284. 'vec3 newPos = position + texture2D(u_displacementMap, uv).rgb * (u_geometrySize / u_size);',
  285. 'vPos = newPos;',
  286. 'vUV = uv;',
  287. 'gl_Position = u_projectionMatrix * u_viewMatrix * vec4(newPos, 1.0);',
  288. '}'
  289. ].join('\n'),
  290. fragmentShader: [
  291. 'precision highp float;',
  292. 'varying vec3 vPos;',
  293. 'varying vec2 vUV;',
  294. 'uniform sampler2D u_displacementMap;',
  295. 'uniform sampler2D u_normalMap;',
  296. 'uniform vec3 u_cameraPosition;',
  297. 'uniform vec3 u_oceanColor;',
  298. 'uniform vec3 u_skyColor;',
  299. 'uniform vec3 u_sunDirection;',
  300. 'uniform float u_exposure;',
  301. 'vec3 hdr (vec3 color, float exposure) {',
  302. 'return 1.0 - exp(-color * exposure);',
  303. '}',
  304. 'void main (void) {',
  305. 'vec3 normal = texture2D(u_normalMap, vUV).rgb;',
  306. 'vec3 view = normalize(u_cameraPosition - vPos);',
  307. 'float fresnel = 0.02 + 0.98 * pow(1.0 - dot(normal, view), 5.0);',
  308. 'vec3 sky = fresnel * u_skyColor;',
  309. 'float diffuse = clamp(dot(normal, normalize(u_sunDirection)), 0.0, 1.0);',
  310. 'vec3 water = (1.0 - fresnel) * u_oceanColor * u_skyColor * diffuse;',
  311. 'vec3 color = sky + water;',
  312. 'gl_FragColor = vec4(hdr(color, u_exposure), 1.0);',
  313. '}'
  314. ].join('\n')
  315. };