Ocean.js 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410
  1. /*
  2. three.js Ocean
  3. */
  4. import {
  5. ClampToEdgeWrapping,
  6. DataTexture,
  7. FloatType,
  8. HalfFloatType,
  9. LinearFilter,
  10. Mesh,
  11. NearestFilter,
  12. OrthographicCamera,
  13. PlaneBufferGeometry,
  14. RGBAFormat,
  15. RepeatWrapping,
  16. Scene,
  17. ShaderMaterial,
  18. UniformsUtils,
  19. Vector2,
  20. Vector3,
  21. WebGLRenderTarget
  22. } from "../../../build/three.module.js";
  23. import { OceanShaders } from "../shaders/OceanShaders.js";
  24. var Ocean = function ( renderer, camera, scene, options ) {
  25. // flag used to trigger parameter changes
  26. this.changed = true;
  27. this.initial = true;
  28. // Assign required parameters as object properties
  29. this.oceanCamera = new OrthographicCamera(); //camera.clone();
  30. this.oceanCamera.position.z = 1;
  31. this.renderer = renderer;
  32. this.renderer.clearColor( 0xffffff );
  33. this.scene = new Scene();
  34. // Assign optional parameters as variables and object properties
  35. function optionalParameter( value, defaultValue ) {
  36. return value !== undefined ? value : defaultValue;
  37. }
  38. options = options || {};
  39. this.clearColor = optionalParameter( options.CLEAR_COLOR, [ 1.0, 1.0, 1.0, 0.0 ] );
  40. this.geometryOrigin = optionalParameter( options.GEOMETRY_ORIGIN, [ - 1000.0, - 1000.0 ] );
  41. this.sunDirectionX = optionalParameter( options.SUN_DIRECTION[ 0 ], - 1.0 );
  42. this.sunDirectionY = optionalParameter( options.SUN_DIRECTION[ 1 ], 1.0 );
  43. this.sunDirectionZ = optionalParameter( options.SUN_DIRECTION[ 2 ], 1.0 );
  44. this.oceanColor = optionalParameter( options.OCEAN_COLOR, new Vector3( 0.004, 0.016, 0.047 ) );
  45. this.skyColor = optionalParameter( options.SKY_COLOR, new Vector3( 3.2, 9.6, 12.8 ) );
  46. this.exposure = optionalParameter( options.EXPOSURE, 0.35 );
  47. this.geometryResolution = optionalParameter( options.GEOMETRY_RESOLUTION, 32 );
  48. this.geometrySize = optionalParameter( options.GEOMETRY_SIZE, 2000 );
  49. this.resolution = optionalParameter( options.RESOLUTION, 64 );
  50. this.floatSize = optionalParameter( options.SIZE_OF_FLOAT, 4 );
  51. this.windX = optionalParameter( options.INITIAL_WIND[ 0 ], 10.0 );
  52. this.windY = optionalParameter( options.INITIAL_WIND[ 1 ], 10.0 );
  53. this.size = optionalParameter( options.INITIAL_SIZE, 250.0 );
  54. this.choppiness = optionalParameter( options.INITIAL_CHOPPINESS, 1.5 );
  55. //
  56. this.matrixNeedsUpdate = false;
  57. // Setup framebuffer pipeline
  58. var renderTargetType = optionalParameter( options.USE_HALF_FLOAT, false ) ? HalfFloatType : FloatType;
  59. var LinearClampParams = {
  60. minFilter: LinearFilter,
  61. magFilter: LinearFilter,
  62. wrapS: ClampToEdgeWrapping,
  63. wrapT: ClampToEdgeWrapping,
  64. format: RGBAFormat,
  65. stencilBuffer: false,
  66. depthBuffer: false,
  67. premultiplyAlpha: false,
  68. type: renderTargetType
  69. };
  70. var NearestClampParams = {
  71. minFilter: NearestFilter,
  72. magFilter: NearestFilter,
  73. wrapS: ClampToEdgeWrapping,
  74. wrapT: ClampToEdgeWrapping,
  75. format: RGBAFormat,
  76. stencilBuffer: false,
  77. depthBuffer: false,
  78. premultiplyAlpha: false,
  79. type: renderTargetType
  80. };
  81. var NearestRepeatParams = {
  82. minFilter: NearestFilter,
  83. magFilter: NearestFilter,
  84. wrapS: RepeatWrapping,
  85. wrapT: RepeatWrapping,
  86. format: RGBAFormat,
  87. stencilBuffer: false,
  88. depthBuffer: false,
  89. premultiplyAlpha: false,
  90. type: renderTargetType
  91. };
  92. this.initialSpectrumFramebuffer = new WebGLRenderTarget( this.resolution, this.resolution, NearestRepeatParams );
  93. this.spectrumFramebuffer = new WebGLRenderTarget( this.resolution, this.resolution, NearestClampParams );
  94. this.pingPhaseFramebuffer = new WebGLRenderTarget( this.resolution, this.resolution, NearestClampParams );
  95. this.pongPhaseFramebuffer = new WebGLRenderTarget( this.resolution, this.resolution, NearestClampParams );
  96. this.pingTransformFramebuffer = new WebGLRenderTarget( this.resolution, this.resolution, NearestClampParams );
  97. this.pongTransformFramebuffer = new WebGLRenderTarget( this.resolution, this.resolution, NearestClampParams );
  98. this.displacementMapFramebuffer = new WebGLRenderTarget( this.resolution, this.resolution, LinearClampParams );
  99. this.normalMapFramebuffer = new WebGLRenderTarget( this.resolution, this.resolution, LinearClampParams );
  100. // Define shaders and constant uniforms
  101. ////////////////////////////////////////
  102. // 0 - The vertex shader used in all of the simulation steps
  103. var fullscreeenVertexShader = OceanShaders[ "ocean_sim_vertex" ];
  104. // 1 - Horizontal wave vertices used for FFT
  105. var oceanHorizontalShader = OceanShaders[ "ocean_subtransform" ];
  106. var oceanHorizontalUniforms = UniformsUtils.clone( oceanHorizontalShader.uniforms );
  107. this.materialOceanHorizontal = new ShaderMaterial( {
  108. uniforms: oceanHorizontalUniforms,
  109. vertexShader: fullscreeenVertexShader.vertexShader,
  110. fragmentShader: "#define HORIZONTAL \n" + oceanHorizontalShader.fragmentShader
  111. } );
  112. this.materialOceanHorizontal.uniforms.u_transformSize = { value: this.resolution };
  113. this.materialOceanHorizontal.uniforms.u_subtransformSize = { value: null };
  114. this.materialOceanHorizontal.uniforms.u_input = { value: null };
  115. this.materialOceanHorizontal.depthTest = false;
  116. // 2 - Vertical wave vertices used for FFT
  117. var oceanVerticalShader = OceanShaders[ "ocean_subtransform" ];
  118. var oceanVerticalUniforms = UniformsUtils.clone( oceanVerticalShader.uniforms );
  119. this.materialOceanVertical = new ShaderMaterial( {
  120. uniforms: oceanVerticalUniforms,
  121. vertexShader: fullscreeenVertexShader.vertexShader,
  122. fragmentShader: oceanVerticalShader.fragmentShader
  123. } );
  124. this.materialOceanVertical.uniforms.u_transformSize = { value: this.resolution };
  125. this.materialOceanVertical.uniforms.u_subtransformSize = { value: null };
  126. this.materialOceanVertical.uniforms.u_input = { value: null };
  127. this.materialOceanVertical.depthTest = false;
  128. // 3 - Initial spectrum used to generate height map
  129. var initialSpectrumShader = OceanShaders[ "ocean_initial_spectrum" ];
  130. var initialSpectrumUniforms = UniformsUtils.clone( initialSpectrumShader.uniforms );
  131. this.materialInitialSpectrum = new ShaderMaterial( {
  132. uniforms: initialSpectrumUniforms,
  133. vertexShader: initialSpectrumShader.vertexShader,
  134. fragmentShader: initialSpectrumShader.fragmentShader
  135. } );
  136. this.materialInitialSpectrum.uniforms.u_wind = { value: new Vector2() };
  137. this.materialInitialSpectrum.uniforms.u_resolution = { value: this.resolution };
  138. this.materialInitialSpectrum.depthTest = false;
  139. // 4 - Phases used to animate heightmap
  140. var phaseShader = OceanShaders[ "ocean_phase" ];
  141. var phaseUniforms = UniformsUtils.clone( phaseShader.uniforms );
  142. this.materialPhase = new ShaderMaterial( {
  143. uniforms: phaseUniforms,
  144. vertexShader: fullscreeenVertexShader.vertexShader,
  145. fragmentShader: phaseShader.fragmentShader
  146. } );
  147. this.materialPhase.uniforms.u_resolution = { value: this.resolution };
  148. this.materialPhase.depthTest = false;
  149. // 5 - Shader used to update spectrum
  150. var spectrumShader = OceanShaders[ "ocean_spectrum" ];
  151. var spectrumUniforms = UniformsUtils.clone( spectrumShader.uniforms );
  152. this.materialSpectrum = new ShaderMaterial( {
  153. uniforms: spectrumUniforms,
  154. vertexShader: fullscreeenVertexShader.vertexShader,
  155. fragmentShader: spectrumShader.fragmentShader
  156. } );
  157. this.materialSpectrum.uniforms.u_initialSpectrum = { value: null };
  158. this.materialSpectrum.uniforms.u_resolution = { value: this.resolution };
  159. this.materialSpectrum.depthTest = false;
  160. // 6 - Shader used to update spectrum normals
  161. var normalShader = OceanShaders[ "ocean_normals" ];
  162. var normalUniforms = UniformsUtils.clone( normalShader.uniforms );
  163. this.materialNormal = new ShaderMaterial( {
  164. uniforms: normalUniforms,
  165. vertexShader: fullscreeenVertexShader.vertexShader,
  166. fragmentShader: normalShader.fragmentShader
  167. } );
  168. this.materialNormal.uniforms.u_displacementMap = { value: null };
  169. this.materialNormal.uniforms.u_resolution = { value: this.resolution };
  170. this.materialNormal.depthTest = false;
  171. // 7 - Shader used to update normals
  172. var oceanShader = OceanShaders[ "ocean_main" ];
  173. var oceanUniforms = UniformsUtils.clone( oceanShader.uniforms );
  174. this.materialOcean = new ShaderMaterial( {
  175. uniforms: oceanUniforms,
  176. vertexShader: oceanShader.vertexShader,
  177. fragmentShader: oceanShader.fragmentShader
  178. } );
  179. // this.materialOcean.wireframe = true;
  180. this.materialOcean.uniforms.u_geometrySize = { value: this.resolution };
  181. this.materialOcean.uniforms.u_displacementMap = { value: this.displacementMapFramebuffer.texture };
  182. this.materialOcean.uniforms.u_normalMap = { value: this.normalMapFramebuffer.texture };
  183. this.materialOcean.uniforms.u_oceanColor = { value: this.oceanColor };
  184. this.materialOcean.uniforms.u_skyColor = { value: this.skyColor };
  185. this.materialOcean.uniforms.u_sunDirection = { value: new Vector3( this.sunDirectionX, this.sunDirectionY, this.sunDirectionZ ) };
  186. this.materialOcean.uniforms.u_exposure = { value: this.exposure };
  187. // Disable blending to prevent default premultiplied alpha values
  188. this.materialOceanHorizontal.blending = 0;
  189. this.materialOceanVertical.blending = 0;
  190. this.materialInitialSpectrum.blending = 0;
  191. this.materialPhase.blending = 0;
  192. this.materialSpectrum.blending = 0;
  193. this.materialNormal.blending = 0;
  194. this.materialOcean.blending = 0;
  195. // Create the simulation plane
  196. this.screenQuad = new Mesh( new PlaneBufferGeometry( 2, 2 ) );
  197. this.scene.add( this.screenQuad );
  198. // Initialise spectrum data
  199. this.generateSeedPhaseTexture();
  200. // Generate the ocean mesh
  201. this.generateMesh();
  202. };
  203. Ocean.prototype.generateMesh = function () {
  204. var geometry = new PlaneBufferGeometry( this.geometrySize, this.geometrySize, this.geometryResolution, this.geometryResolution );
  205. geometry.rotateX( - Math.PI / 2 );
  206. this.oceanMesh = new Mesh( geometry, this.materialOcean );
  207. };
  208. Ocean.prototype.render = function () {
  209. var currentRenderTarget = this.renderer.getRenderTarget();
  210. this.scene.overrideMaterial = null;
  211. if ( this.changed )
  212. this.renderInitialSpectrum();
  213. this.renderWavePhase();
  214. this.renderSpectrum();
  215. this.renderSpectrumFFT();
  216. this.renderNormalMap();
  217. this.scene.overrideMaterial = null;
  218. this.renderer.setRenderTarget( currentRenderTarget );
  219. };
  220. Ocean.prototype.generateSeedPhaseTexture = function () {
  221. // Setup the seed texture
  222. this.pingPhase = true;
  223. var phaseArray = new window.Float32Array( this.resolution * this.resolution * 4 );
  224. for ( var i = 0; i < this.resolution; i ++ ) {
  225. for ( var j = 0; j < this.resolution; j ++ ) {
  226. phaseArray[ i * this.resolution * 4 + j * 4 ] = Math.random() * 2.0 * Math.PI;
  227. phaseArray[ i * this.resolution * 4 + j * 4 + 1 ] = 0.0;
  228. phaseArray[ i * this.resolution * 4 + j * 4 + 2 ] = 0.0;
  229. phaseArray[ i * this.resolution * 4 + j * 4 + 3 ] = 0.0;
  230. }
  231. }
  232. this.pingPhaseTexture = new DataTexture( phaseArray, this.resolution, this.resolution, RGBAFormat );
  233. this.pingPhaseTexture.wrapS = ClampToEdgeWrapping;
  234. this.pingPhaseTexture.wrapT = ClampToEdgeWrapping;
  235. this.pingPhaseTexture.type = FloatType;
  236. };
  237. Ocean.prototype.renderInitialSpectrum = function () {
  238. this.scene.overrideMaterial = this.materialInitialSpectrum;
  239. this.materialInitialSpectrum.uniforms.u_wind.value.set( this.windX, this.windY );
  240. this.materialInitialSpectrum.uniforms.u_size.value = this.size;
  241. this.renderer.setRenderTarget( this.initialSpectrumFramebuffer );
  242. this.renderer.clear();
  243. this.renderer.render( this.scene, this.oceanCamera );
  244. };
  245. Ocean.prototype.renderWavePhase = function () {
  246. this.scene.overrideMaterial = this.materialPhase;
  247. this.screenQuad.material = this.materialPhase;
  248. if ( this.initial ) {
  249. this.materialPhase.uniforms.u_phases.value = this.pingPhaseTexture;
  250. this.initial = false;
  251. } else {
  252. this.materialPhase.uniforms.u_phases.value = this.pingPhase ? this.pingPhaseFramebuffer.texture : this.pongPhaseFramebuffer.texture;
  253. }
  254. this.materialPhase.uniforms.u_deltaTime.value = this.deltaTime;
  255. this.materialPhase.uniforms.u_size.value = this.size;
  256. this.renderer.setRenderTarget( this.pingPhase ? this.pongPhaseFramebuffer : this.pingPhaseFramebuffer );
  257. this.renderer.render( this.scene, this.oceanCamera );
  258. this.pingPhase = ! this.pingPhase;
  259. };
  260. Ocean.prototype.renderSpectrum = function () {
  261. this.scene.overrideMaterial = this.materialSpectrum;
  262. this.materialSpectrum.uniforms.u_initialSpectrum.value = this.initialSpectrumFramebuffer.texture;
  263. this.materialSpectrum.uniforms.u_phases.value = this.pingPhase ? this.pingPhaseFramebuffer.texture : this.pongPhaseFramebuffer.texture;
  264. this.materialSpectrum.uniforms.u_choppiness.value = this.choppiness;
  265. this.materialSpectrum.uniforms.u_size.value = this.size;
  266. this.renderer.setRenderTarget( this.spectrumFramebuffer );
  267. this.renderer.render( this.scene, this.oceanCamera );
  268. };
  269. Ocean.prototype.renderSpectrumFFT = function () {
  270. // GPU FFT using Stockham formulation
  271. var iterations = Math.log( this.resolution ) / Math.log( 2 ); // log2
  272. this.scene.overrideMaterial = this.materialOceanHorizontal;
  273. for ( var i = 0; i < iterations; i ++ ) {
  274. if ( i === 0 ) {
  275. this.materialOceanHorizontal.uniforms.u_input.value = this.spectrumFramebuffer.texture;
  276. this.materialOceanHorizontal.uniforms.u_subtransformSize.value = Math.pow( 2, ( i % ( iterations ) ) + 1 );
  277. this.renderer.setRenderTarget( this.pingTransformFramebuffer );
  278. this.renderer.render( this.scene, this.oceanCamera );
  279. } else if ( i % 2 === 1 ) {
  280. this.materialOceanHorizontal.uniforms.u_input.value = this.pingTransformFramebuffer.texture;
  281. this.materialOceanHorizontal.uniforms.u_subtransformSize.value = Math.pow( 2, ( i % ( iterations ) ) + 1 );
  282. this.renderer.setRenderTarget( this.pongTransformFramebuffer );
  283. this.renderer.render( this.scene, this.oceanCamera );
  284. } else {
  285. this.materialOceanHorizontal.uniforms.u_input.value = this.pongTransformFramebuffer.texture;
  286. this.materialOceanHorizontal.uniforms.u_subtransformSize.value = Math.pow( 2, ( i % ( iterations ) ) + 1 );
  287. this.renderer.setRenderTarget( this.pingTransformFramebuffer );
  288. this.renderer.render( this.scene, this.oceanCamera );
  289. }
  290. }
  291. this.scene.overrideMaterial = this.materialOceanVertical;
  292. for ( var i = iterations; i < iterations * 2; i ++ ) {
  293. if ( i === iterations * 2 - 1 ) {
  294. this.materialOceanVertical.uniforms.u_input.value = ( iterations % 2 === 0 ) ? this.pingTransformFramebuffer.texture : this.pongTransformFramebuffer.texture;
  295. this.materialOceanVertical.uniforms.u_subtransformSize.value = Math.pow( 2, ( i % ( iterations ) ) + 1 );
  296. this.renderer.setRenderTarget( this.displacementMapFramebuffer );
  297. this.renderer.render( this.scene, this.oceanCamera );
  298. } else if ( i % 2 === 1 ) {
  299. this.materialOceanVertical.uniforms.u_input.value = this.pingTransformFramebuffer.texture;
  300. this.materialOceanVertical.uniforms.u_subtransformSize.value = Math.pow( 2, ( i % ( iterations ) ) + 1 );
  301. this.renderer.setRenderTarget( this.pongTransformFramebuffer );
  302. this.renderer.render( this.scene, this.oceanCamera );
  303. } else {
  304. this.materialOceanVertical.uniforms.u_input.value = this.pongTransformFramebuffer.texture;
  305. this.materialOceanVertical.uniforms.u_subtransformSize.value = Math.pow( 2, ( i % ( iterations ) ) + 1 );
  306. this.renderer.setRenderTarget( this.pingTransformFramebuffer );
  307. this.renderer.render( this.scene, this.oceanCamera );
  308. }
  309. }
  310. };
  311. Ocean.prototype.renderNormalMap = function () {
  312. this.scene.overrideMaterial = this.materialNormal;
  313. if ( this.changed ) this.materialNormal.uniforms.u_size.value = this.size;
  314. this.materialNormal.uniforms.u_displacementMap.value = this.displacementMapFramebuffer.texture;
  315. this.renderer.setRenderTarget( this.normalMapFramebuffer );
  316. this.renderer.clear();
  317. this.renderer.render( this.scene, this.oceanCamera );
  318. };
  319. export { Ocean };