webgl_shaders_ocean2.html 6.1 KB

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