webgl_shaders_ocean2.html 7.1 KB

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