webgl_shaders_ocean2.html 6.9 KB

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