webgl_shaders_ocean2.html 7.2 KB

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