webgl_shaders_ocean2.html 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="utf-8" />
  5. <meta name="viewport" content="width=device-width, user-scalable=no, minimum-scale=1.0, maximum-scale=1.0">
  6. <style type="text/css">
  7. body {
  8. margin: 0px;
  9. overflow: hidden;
  10. font-family: Monospace;
  11. text-align: center;
  12. }
  13. #info {
  14. color: #fff;
  15. position: absolute;
  16. top: 10px;
  17. width: 100%;
  18. }
  19. a {
  20. color: #09f;
  21. }
  22. #type-status {
  23. font-weight: bold;
  24. }
  25. </style>
  26. </head>
  27. <body>
  28. <div id="info">
  29. <a href="http://threejs.org" target="_blank" rel="noopener">three.js</a> - webgl ocean simulation<br/>
  30. current simulation framebuffers type is <span id="type-status"></span><br/>
  31. change type to <span id="change-type"></span>
  32. </div>
  33. <script src="../build/three.js"></script>
  34. <script src="js/libs/stats.min.js"></script>
  35. <script src="js/libs/dat.gui.min.js"></script>
  36. <script src="js/controls/OrbitControls.js"></script>
  37. <script src="js/shaders/OceanShaders.js"></script>
  38. <script src="js/Ocean.js"></script>
  39. <script>
  40. var stats = new Stats();
  41. document.body.appendChild( stats.dom );
  42. var lastTime = ( new Date() ).getTime();
  43. var types = { 'float': 'half-float', 'half-float': 'float' };
  44. var hash = document.location.hash.substr( 1 );
  45. if ( ! ( hash in types ) ) hash = 'half-float';
  46. document.getElementById( 'type-status' ).innerHTML = hash;
  47. document.getElementById( 'change-type' ).innerHTML = '<a href="#" onclick="return change(\'' + types[ hash ] + '\')">' + types[ hash ] + '</a>';
  48. var lastTime = ( new Date() ).getTime();
  49. function change( n ) {
  50. location.hash = n;
  51. location.reload();
  52. return false;
  53. }
  54. var DEMO = {
  55. ms_Renderer: null,
  56. ms_Camera: null,
  57. ms_Scene: null,
  58. ms_Controls: null,
  59. ms_Ocean: null,
  60. Initialize: function () {
  61. this.ms_Renderer = new THREE.WebGLRenderer();
  62. this.ms_Renderer.setPixelRatio( window.devicePixelRatio );
  63. this.ms_Renderer.context.getExtension( 'OES_texture_float' );
  64. this.ms_Renderer.context.getExtension( 'OES_texture_float_linear' );
  65. document.body.appendChild( this.ms_Renderer.domElement );
  66. this.ms_Scene = new THREE.Scene();
  67. this.ms_Camera = new THREE.PerspectiveCamera( 55.0, window.innerWidth / window.innerHeight, 0.5, 300000 );
  68. this.ms_Camera.position.set( 450, 350, 450 );
  69. this.ms_Camera.lookAt( 0, 0, 0 );
  70. // Initialize Orbit control
  71. this.ms_Controls = new THREE.OrbitControls( this.ms_Camera, this.ms_Renderer.domElement );
  72. this.ms_Controls.userPan = false;
  73. this.ms_Controls.userPanSpeed = 0.0;
  74. this.ms_Controls.minDistance = 0;
  75. this.ms_Controls.maxDistance = 2000.0;
  76. this.ms_Controls.minPolarAngle = 0;
  77. this.ms_Controls.maxPolarAngle = Math.PI * 0.495;
  78. var gsize = 512;
  79. var res = 1024;
  80. var gres = res / 2;
  81. var origx = - gsize / 2;
  82. var origz = - gsize / 2;
  83. this.ms_Ocean = new THREE.Ocean( this.ms_Renderer, this.ms_Camera, this.ms_Scene,
  84. {
  85. USE_HALF_FLOAT: hash === 'half-float',
  86. INITIAL_SIZE: 256.0,
  87. INITIAL_WIND: [ 10.0, 10.0 ],
  88. INITIAL_CHOPPINESS: 1.5,
  89. CLEAR_COLOR: [ 1.0, 1.0, 1.0, 0.0 ],
  90. GEOMETRY_ORIGIN: [ origx, origz ],
  91. SUN_DIRECTION: [ - 1.0, 1.0, 1.0 ],
  92. OCEAN_COLOR: new THREE.Vector3( 0.004, 0.016, 0.047 ),
  93. SKY_COLOR: new THREE.Vector3( 3.2, 9.6, 12.8 ),
  94. EXPOSURE: 0.35,
  95. GEOMETRY_RESOLUTION: gres,
  96. GEOMETRY_SIZE: gsize,
  97. RESOLUTION: res
  98. } );
  99. this.ms_Ocean.materialOcean.uniforms.u_projectionMatrix = { value: this.ms_Camera.projectionMatrix };
  100. this.ms_Ocean.materialOcean.uniforms.u_viewMatrix = { value: this.ms_Camera.matrixWorldInverse };
  101. this.ms_Ocean.materialOcean.uniforms.u_cameraPosition = { value: this.ms_Camera.position };
  102. this.ms_Scene.add( this.ms_Ocean.oceanMesh );
  103. var gui = new dat.GUI();
  104. gui.add( this.ms_Ocean, "size", 100, 5000 ).onChange( function ( v ) {
  105. this.object.size = v;
  106. this.object.changed = true;
  107. } );
  108. gui.add( this.ms_Ocean, "choppiness", 0.1, 4 ).onChange( function ( v ) {
  109. this.object.choppiness = v;
  110. this.object.changed = true;
  111. } );
  112. gui.add( this.ms_Ocean, "windX", - 15, 15 ).onChange( function ( v ) {
  113. this.object.windX = v;
  114. this.object.changed = true;
  115. } );
  116. gui.add( this.ms_Ocean, "windY", - 15, 15 ).onChange( function ( v ) {
  117. this.object.windY = v;
  118. this.object.changed = true;
  119. } );
  120. gui.add( this.ms_Ocean, "sunDirectionX", - 1.0, 1.0 ).onChange( function ( v ) {
  121. this.object.sunDirectionX = v;
  122. this.object.changed = true;
  123. } );
  124. gui.add( this.ms_Ocean, "sunDirectionY", - 1.0, 1.0 ).onChange( function ( v ) {
  125. this.object.sunDirectionY = v;
  126. this.object.changed = true;
  127. } );
  128. gui.add( this.ms_Ocean, "sunDirectionZ", - 1.0, 1.0 ).onChange( function ( v ) {
  129. this.object.sunDirectionZ = v;
  130. this.object.changed = true;
  131. } );
  132. gui.add( this.ms_Ocean, "exposure", 0.0, 0.5 ).onChange( function ( v ) {
  133. this.object.exposure = v;
  134. this.object.changed = true;
  135. } );
  136. },
  137. Display: function () {
  138. this.ms_Renderer.render( this.ms_Scene, this.ms_Camera );
  139. },
  140. Update: function () {
  141. var currentTime = new Date().getTime();
  142. this.ms_Ocean.deltaTime = ( currentTime - lastTime ) / 1000 || 0.0;
  143. lastTime = currentTime;
  144. this.ms_Ocean.render( this.ms_Ocean.deltaTime );
  145. this.ms_Ocean.overrideMaterial = this.ms_Ocean.materialOcean;
  146. if ( this.ms_Ocean.changed ) {
  147. this.ms_Ocean.materialOcean.uniforms.u_size.value = this.ms_Ocean.size;
  148. this.ms_Ocean.materialOcean.uniforms.u_sunDirection.value.set( this.ms_Ocean.sunDirectionX, this.ms_Ocean.sunDirectionY, this.ms_Ocean.sunDirectionZ );
  149. this.ms_Ocean.materialOcean.uniforms.u_exposure.value = this.ms_Ocean.exposure;
  150. this.ms_Ocean.changed = false;
  151. }
  152. this.ms_Ocean.materialOcean.uniforms.u_normalMap.value = this.ms_Ocean.normalMapFramebuffer.texture;
  153. this.ms_Ocean.materialOcean.uniforms.u_displacementMap.value = this.ms_Ocean.displacementMapFramebuffer.texture;
  154. this.ms_Ocean.materialOcean.uniforms.u_projectionMatrix.value = this.ms_Camera.projectionMatrix;
  155. this.ms_Ocean.materialOcean.uniforms.u_viewMatrix.value = this.ms_Camera.matrixWorldInverse;
  156. this.ms_Ocean.materialOcean.uniforms.u_cameraPosition.value = this.ms_Camera.position;
  157. this.ms_Ocean.materialOcean.depthTest = true;
  158. //this.ms_Scene.__lights[1].position.x = this.ms_Scene.__lights[1].position.x + 0.01;
  159. this.Display();
  160. },
  161. Resize: function ( inWidth, inHeight ) {
  162. this.ms_Camera.aspect = inWidth / inHeight;
  163. this.ms_Camera.updateProjectionMatrix();
  164. this.ms_Renderer.setSize( inWidth, inHeight );
  165. this.Display();
  166. }
  167. };
  168. DEMO.Initialize();
  169. window.addEventListener( 'resize', function () {
  170. DEMO.Resize( window.innerWidth, window.innerHeight );
  171. } );
  172. DEMO.Resize( window.innerWidth, window.innerHeight );
  173. var render = function () {
  174. requestAnimationFrame( render );
  175. DEMO.Update();
  176. stats.update();
  177. };
  178. render();
  179. </script>
  180. </body>
  181. </html>