OceanShaders.js 12 KB

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