Ocean.js 15 KB

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