webgl_shaders_ocean2.html 6.2 KB

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