OceanShaders.js 12 KB

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