Ocean.js 15 KB

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