webgl_shaders_ocean2.html 7.2 KB

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