2
0

webgl_shaders_ocean2.html 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="utf-8" />
  5. <meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=no"/>
  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;
  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 = 'float';
  46. document.getElementById('type-status').innerHTML = hash;
  47. document.getElementById('change-type').innerHTML =
  48. '<a href="#" onclick="return change(\'' + types[hash] + '\')">' + types[hash] + '</a>';
  49. var WINDOW = {
  50. ms_Width: 0,
  51. ms_Height: 0,
  52. ms_Callbacks: {
  53. 70: "WINDOW.ToggleFullScreen()", // Toggle fullscreen
  54. },
  55. Initialize: function () {
  56. this.UpdateSize();
  57. stats = new Stats();
  58. document.body.appendChild( stats.domElement );
  59. // Create callbacks from keyboard
  60. document.onkeydown = function (inEvent) { WINDOW.CallAction(inEvent.keyCode); };
  61. window.onresize = function (inEvent) {
  62. WINDOW.UpdateSize();
  63. WINDOW.ResizeCallback(WINDOW.ms_Width, WINDOW.ms_Height);
  64. };
  65. },
  66. UpdateSize: function () {
  67. this.ms_Width = window.outerWidth;
  68. this.ms_Height = window.outerHeight - 4;
  69. },
  70. CallAction: function (inId) {
  71. if (inId in this.ms_Callbacks) {
  72. eval(this.ms_Callbacks[inId]);
  73. return false;
  74. }
  75. },
  76. ToggleFullScreen: function () {
  77. if (!document.fullscreenElement && !document.mozFullScreenElement && !document.webkitFullscreenElement) {
  78. if (document.documentElement.requestFullscreen)
  79. document.documentElement.requestFullscreen();
  80. else if (document.documentElement.mozRequestFullScreen)
  81. document.documentElement.mozRequestFullScreen();
  82. else if (document.documentElement.webkitRequestFullscreen)
  83. document.documentElement.webkitRequestFullscreen(Element.ALLOW_KEYBOARD_INPUT);
  84. }
  85. else {
  86. if (document.cancelFullScreen)
  87. document.cancelFullScreen();
  88. else if (document.mozCancelFullScreen)
  89. document.mozCancelFullScreen();
  90. else if (document.webkitCancelFullScreen)
  91. document.webkitCancelFullScreen();
  92. }
  93. },
  94. ResizeCallback: function (inWidth, inHeight) { },
  95. };
  96. var lastTime = (new Date()).getTime();
  97. function change(n) {
  98. location.hash = n;
  99. location.reload();
  100. return false;
  101. }
  102. var DEMO =
  103. {
  104. ms_Renderer: null,
  105. ms_Camera: null,
  106. ms_Scene: null,
  107. ms_Controls: null,
  108. ms_Ocean: null,
  109. Initialize: function () {
  110. this.ms_Renderer = new THREE.WebGLRenderer();
  111. this.ms_Renderer.setPixelRatio( window.devicePixelRatio );
  112. this.ms_Renderer.context.getExtension('OES_texture_float');
  113. this.ms_Renderer.context.getExtension('OES_texture_float_linear');
  114. document.body.appendChild(this.ms_Renderer.domElement);
  115. this.ms_Scene = new THREE.Scene();
  116. this.ms_Camera = new THREE.PerspectiveCamera(55.0, WINDOW.ms_Width / WINDOW.ms_Height, 0.5, 300000);
  117. this.ms_Camera.position.set(450, 350, 450);
  118. this.ms_Camera.lookAt(new THREE.Vector3());
  119. // Initialize Orbit control
  120. this.ms_Controls = new THREE.OrbitControls(this.ms_Camera, this.ms_Renderer.domElement);
  121. this.ms_Controls.userPan = false;
  122. this.ms_Controls.userPanSpeed = 0.0;
  123. this.ms_Controls.minDistance = 0;
  124. this.ms_Controls.maxDistance = 2000.0;
  125. this.ms_Controls.minPolarAngle = 0;
  126. this.ms_Controls.maxPolarAngle = Math.PI * 0.495;
  127. var gsize = 512;
  128. var res = 1024;
  129. var gres = res / 2;
  130. var origx = -gsize / 2;
  131. var origz = -gsize / 2;
  132. this.ms_Ocean = new THREE.Ocean(this.ms_Renderer, this.ms_Camera, this.ms_Scene,
  133. {
  134. USE_HALF_FLOAT : hash === 'half-float',
  135. INITIAL_SIZE : 256.0,
  136. INITIAL_WIND : [10.0, 10.0],
  137. INITIAL_CHOPPINESS : 1.5,
  138. CLEAR_COLOR : [1.0, 1.0, 1.0, 0.0],
  139. GEOMETRY_ORIGIN : [origx, origz],
  140. SUN_DIRECTION : [-1.0, 1.0, 1.0],
  141. OCEAN_COLOR: new THREE.Vector3(0.004, 0.016, 0.047),
  142. SKY_COLOR: new THREE.Vector3(3.2, 9.6, 12.8),
  143. EXPOSURE : 0.35,
  144. GEOMETRY_RESOLUTION: gres,
  145. GEOMETRY_SIZE : gsize,
  146. RESOLUTION : res
  147. });
  148. this.ms_Ocean.materialOcean.uniforms.u_projectionMatrix = { type: "m4", value: this.ms_Camera.projectionMatrix };
  149. this.ms_Ocean.materialOcean.uniforms.u_viewMatrix = { type: "m4", value: this.ms_Camera.matrixWorldInverse };
  150. this.ms_Ocean.materialOcean.uniforms.u_cameraPosition = { type: "v3", value: this.ms_Camera.position };
  151. this.ms_Scene.add(this.ms_Ocean.oceanMesh);
  152. var gui = new dat.GUI();
  153. var c1 = gui.add(this.ms_Ocean, "size",100, 5000);
  154. c1.onChange(function(v) {
  155. this.object.size = v;
  156. this.object.changed = true;
  157. });
  158. var c2 = gui.add(this.ms_Ocean, "choppiness", 0.1, 4);
  159. c2.onChange(function (v) {
  160. this.object.choppiness = v;
  161. this.object.changed = true;
  162. });
  163. var c3 = gui.add(this.ms_Ocean, "windX",-15, 15);
  164. c3.onChange(function (v) {
  165. this.object.windX = v;
  166. this.object.changed = true;
  167. });
  168. var c4 = gui.add(this.ms_Ocean, "windY", -15, 15);
  169. c4.onChange(function (v) {
  170. this.object.windY = v;
  171. this.object.changed = true;
  172. });
  173. var c5 = gui.add(this.ms_Ocean, "sunDirectionX", -1.0, 1.0);
  174. c5.onChange(function (v) {
  175. this.object.sunDirectionX = v;
  176. this.object.changed = true;
  177. });
  178. var c6 = gui.add(this.ms_Ocean, "sunDirectionY", -1.0, 1.0);
  179. c6.onChange(function (v) {
  180. this.object.sunDirectionY = v;
  181. this.object.changed = true;
  182. });
  183. var c7 = gui.add(this.ms_Ocean, "sunDirectionZ", -1.0, 1.0);
  184. c7.onChange(function (v) {
  185. this.object.sunDirectionZ = v;
  186. this.object.changed = true;
  187. });
  188. var c8 = gui.add(this.ms_Ocean, "exposure", 0.0, 0.5);
  189. c8.onChange(function (v) {
  190. this.object.exposure = v;
  191. this.object.changed = true;
  192. });
  193. },
  194. Display: function () {
  195. this.ms_Renderer.render(this.ms_Scene, this.ms_Camera);
  196. },
  197. Update: function () {
  198. var currentTime = new Date().getTime();
  199. this.ms_Ocean.deltaTime = (currentTime - lastTime) / 1000 || 0.0;
  200. lastTime = currentTime;
  201. this.ms_Ocean.render(this.ms_Ocean.deltaTime);
  202. this.ms_Ocean.overrideMaterial = this.ms_Ocean.materialOcean;
  203. if (this.ms_Ocean.changed) {
  204. this.ms_Ocean.materialOcean.uniforms.u_size.value = this.ms_Ocean.size;
  205. this.ms_Ocean.materialOcean.uniforms.u_sunDirection.value.set( this.ms_Ocean.sunDirectionX, this.ms_Ocean.sunDirectionY, this.ms_Ocean.sunDirectionZ );
  206. this.ms_Ocean.materialOcean.uniforms.u_exposure.value = this.ms_Ocean.exposure;
  207. this.ms_Ocean.changed = false;
  208. }
  209. this.ms_Ocean.materialOcean.uniforms.u_normalMap.value = this.ms_Ocean.normalMapFramebuffer ;
  210. this.ms_Ocean.materialOcean.uniforms.u_displacementMap.value = this.ms_Ocean.displacementMapFramebuffer ;
  211. this.ms_Ocean.materialOcean.uniforms.u_projectionMatrix.value = this.ms_Camera.projectionMatrix ;
  212. this.ms_Ocean.materialOcean.uniforms.u_viewMatrix.value = this.ms_Camera.matrixWorldInverse ;
  213. this.ms_Ocean.materialOcean.uniforms.u_cameraPosition.value = this.ms_Camera.position;
  214. this.ms_Ocean.materialOcean.depthTest = true;
  215. //this.ms_Scene.__lights[1].position.x = this.ms_Scene.__lights[1].position.x + 0.01;
  216. this.ms_Controls.update();
  217. this.Display();
  218. },
  219. Resize: function (inWidth, inHeight) {
  220. this.ms_Camera.aspect = inWidth / inHeight;
  221. this.ms_Camera.updateProjectionMatrix();
  222. this.ms_Renderer.setSize(inWidth, inHeight);
  223. this.Display();
  224. }
  225. };
  226. WINDOW.Initialize();
  227. DEMO.Initialize();
  228. WINDOW.ResizeCallback = function (inWidth, inHeight) { DEMO.Resize(inWidth, inHeight); };
  229. DEMO.Resize(WINDOW.ms_Width, WINDOW.ms_Height);
  230. var render = function () {
  231. requestAnimationFrame( render );
  232. DEMO.Update();
  233. stats.update();
  234. };
  235. render();
  236. </script>
  237. </body>
  238. </html>