webgl_shaders_ocean2.html 6.2 KB

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