webgl_shaders_ocean2.html 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  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. html,body,div,span,applet,object,iframe,h1,h2,h3,h4,h5,h6,p,blockquote,pre,a,abbr,acronym,address,big,cite,code,del,dfn,em,img,ins,kbd,q,s,samp,small,strike,strong,sub,sup,tt,var,b,u,i,center,dl,dt,dd,ol,ul,li,fieldset,form,label,legend,table,caption,tbody,tfoot,thead,tr,th,td,article,aside,canvas,details,embed,figure,figcaption,footer,header,hgroup,menu,nav,output,ruby,section,summary,time,mark,audio,video{margin:0;padding:0;border:0;font:inherit;font-size:100%;vertical-align:baseline}html{line-height:1}ol,ul{list-style:none}table{border-collapse:collapse;border-spacing:0}caption,th,td{text-align:left;font-weight:normal;vertical-align:middle}q,blockquote{quotes:none}q:before,q:after,blockquote:before,blockquote:after{content:"";content:none}a img{border:none}article,aside,details,figcaption,figure,footer,header,hgroup,menu,nav,section,summary{display:block}body{width:100%;height:100%}body #body_container{width:100%;height:100%;background-color:#000000}
  8. </style>
  9. </head>
  10. <body>
  11. <section id="body_container">
  12. <div id="main_canvas"></div>
  13. </section>
  14. <script src="../build/three.min.js"></script>
  15. <script src="js/libs/dat.gui.min.js"></script>
  16. <script src="js/controls/OrbitControls.js"></script>
  17. <script src="js/shaders/OceanShaders.js"></script>
  18. <script src="js/Ocean.js"></script>
  19. <script>
  20. var lastTime = (new Date()).getTime();
  21. var WINDOW = {
  22. ms_Width: 0,
  23. ms_Height: 0,
  24. ms_Callbacks: {
  25. 70: "WINDOW.ToggleFullScreen()", // Toggle fullscreen
  26. },
  27. Initialize: function () {
  28. this.UpdateSize();
  29. // Create callbacks from keyboard
  30. document.onkeydown = function (inEvent) { WINDOW.CallAction(inEvent.keyCode); };
  31. window.onresize = function (inEvent) {
  32. WINDOW.UpdateSize();
  33. WINDOW.ResizeCallback(WINDOW.ms_Width, WINDOW.ms_Height);
  34. };
  35. },
  36. UpdateSize: function () {
  37. this.ms_Width = window.outerWidth;
  38. this.ms_Height = window.outerHeight - 4;
  39. },
  40. CallAction: function (inId) {
  41. if (inId in this.ms_Callbacks) {
  42. eval(this.ms_Callbacks[inId]);
  43. return false;
  44. }
  45. },
  46. ToggleFullScreen: function () {
  47. if (!document.fullscreenElement && !document.mozFullScreenElement && !document.webkitFullscreenElement) {
  48. if (document.documentElement.requestFullscreen)
  49. document.documentElement.requestFullscreen();
  50. else if (document.documentElement.mozRequestFullScreen)
  51. document.documentElement.mozRequestFullScreen();
  52. else if (document.documentElement.webkitRequestFullscreen)
  53. document.documentElement.webkitRequestFullscreen(Element.ALLOW_KEYBOARD_INPUT);
  54. }
  55. else {
  56. if (document.cancelFullScreen)
  57. document.cancelFullScreen();
  58. else if (document.mozCancelFullScreen)
  59. document.mozCancelFullScreen();
  60. else if (document.webkitCancelFullScreen)
  61. document.webkitCancelFullScreen();
  62. }
  63. },
  64. ResizeCallback: function (inWidth, inHeight) { },
  65. };
  66. var lastTime = (new Date()).getTime();
  67. var DEMO =
  68. {
  69. ms_Canvas: null,
  70. ms_Renderer: null,
  71. ms_Camera: null,
  72. ms_Scene: null,
  73. ms_Controls: null,
  74. ms_Ocean: null,
  75. Initialize: function (inIdCanvas) {
  76. this.ms_Canvas = document.getElementById(inIdCanvas);
  77. this.ms_Renderer = new THREE.WebGLRenderer();
  78. this.ms_Renderer.shadowMapEnabled = true;
  79. this.ms_Renderer.context.getExtension('OES_texture_float');
  80. this.ms_Renderer.context.getExtension('OES_texture_float_linear');
  81. this.ms_Canvas.appendChild(this.ms_Renderer.domElement);
  82. this.ms_Scene = new THREE.Scene();
  83. this.ms_Camera = new THREE.PerspectiveCamera(55.0, WINDOW.ms_Width / WINDOW.ms_Height, 0.5, 300000);
  84. this.ms_Camera.position.set(450, 350, 450);
  85. this.ms_Camera.lookAt(new THREE.Vector3(0,0,0));
  86. // Initialize Orbit control
  87. this.ms_Controls = new THREE.OrbitControls(this.ms_Camera, this.ms_Renderer.domElement);
  88. this.ms_Controls.userPan = false;
  89. this.ms_Controls.userPanSpeed = 0.0;
  90. this.ms_Controls.minDistance = 0;
  91. this.ms_Controls.maxDistance = 2000.0;
  92. this.ms_Controls.minPolarAngle = 0;
  93. this.ms_Controls.maxPolarAngle = Math.PI * 0.495;
  94. this.debugaxis = function (axisLength) {
  95. //Shorten the vertex function
  96. function v(a, b, c) {
  97. return new THREE.Vector3(a, b, c);
  98. }
  99. //Create axis (point1, point2, colour)
  100. function createAxis(p1, p2, color,scene) {
  101. var line, lineGeometry = new THREE.Geometry(),
  102. lineMat = new THREE.LineBasicMaterial({ color: color, lineWidth: 1 });
  103. lineGeometry.vertices.push(p1, p2);
  104. line = new THREE.Line(lineGeometry, lineMat);
  105. scene.add(line);
  106. }
  107. createAxis(v(-axisLength, 0, 0), v(axisLength, 0, 0), 0xFF0000, this.ms_Scene);
  108. createAxis(v(0, -axisLength, 0), v(0, axisLength, 0), 0x00FF00, this.ms_Scene);
  109. createAxis(v(0, 0, -axisLength), v(0, 0, axisLength), 0xFFFF00, this.ms_Scene);
  110. };
  111. //To use enter the axis length
  112. this.debugaxis(50);
  113. var gsize = 512;
  114. var res = 1024;
  115. var gres = res / 2;
  116. var origx = -gsize / 2;
  117. var origz = -gsize / 2;
  118. this.ms_Ocean = new THREE.Ocean(this.ms_Renderer, this.ms_Camera, this.ms_Scene,
  119. {
  120. INITIAL_SIZE : 1000.0,
  121. INITIAL_WIND : [10.0, 10.0],
  122. INITIAL_CHOPPINESS : 1.5,
  123. CLEAR_COLOR : [1.0, 1.0, 1.0, 0.0],
  124. GEOMETRY_ORIGIN : [origx, origz],
  125. SUN_DIRECTION : [-1.0, 1.0, 1.0],
  126. OCEAN_COLOR: new THREE.Vector3(0.004, 0.016, 0.047),
  127. SKY_COLOR: new THREE.Vector3(3.2, 9.6, 12.8),
  128. EXPOSURE : 0.35,
  129. GEOMETRY_RESOLUTION: gres,
  130. GEOMETRY_SIZE : gsize,
  131. RESOLUTION : res
  132. });
  133. this.ms_Ocean.materialOcean.uniforms.u_projectionMatrix = { type: "m4", value: this.ms_Camera.projectionMatrix };
  134. this.ms_Ocean.materialOcean.uniforms.u_viewMatrix = { type: "m4", value: this.ms_Camera.matrixWorldInverse };
  135. this.ms_Ocean.materialOcean.uniforms.u_cameraPosition = { type: "v3", value: this.ms_Camera.position };
  136. this.ms_Scene.add(this.ms_Ocean.oceanMesh);
  137. var gui = new dat.GUI();
  138. var c1 = gui.add(this.ms_Ocean, "size",100, 5000);
  139. c1.onChange(function(v) {
  140. this.object.size = v;
  141. this.object.changed = true;
  142. });
  143. var c2 = gui.add(this.ms_Ocean, "choppiness", 0.1, 4);
  144. c2.onChange(function (v) {
  145. this.object.choppiness = v;
  146. this.object.changed = true;
  147. });
  148. var c3 = gui.add(this.ms_Ocean, "windX",-15, 15);
  149. c3.onChange(function (v) {
  150. this.object.windX = v;
  151. this.object.changed = true;
  152. });
  153. var c4 = gui.add(this.ms_Ocean, "windY", -15, 15);
  154. c4.onChange(function (v) {
  155. this.object.windY = v;
  156. this.object.changed = true;
  157. });
  158. var c5 = gui.add(this.ms_Ocean, "sunDirectionX", -1.0, 1.0);
  159. c5.onChange(function (v) {
  160. this.object.sunDirectionX = v;
  161. this.object.changed = true;
  162. });
  163. var c6 = gui.add(this.ms_Ocean, "sunDirectionY", -1.0, 1.0);
  164. c6.onChange(function (v) {
  165. this.object.sunDirectionY = v;
  166. this.object.changed = true;
  167. });
  168. var c7 = gui.add(this.ms_Ocean, "sunDirectionZ", -1.0, 1.0);
  169. c7.onChange(function (v) {
  170. this.object.sunDirectionZ = v;
  171. this.object.changed = true;
  172. });
  173. var c8 = gui.add(this.ms_Ocean, "exposure", 0.0, 0.5);
  174. c8.onChange(function (v) {
  175. this.object.exposure = v;
  176. this.object.changed = true;
  177. });
  178. },
  179. Display: function () {
  180. this.ms_Renderer.render(this.ms_Scene, this.ms_Camera);
  181. },
  182. Update: function () {
  183. this.ms_Renderer.clear();
  184. var currentTime = new Date().getTime();
  185. this.ms_Ocean.deltaTime = (currentTime - lastTime) / 1000 || 0.0;
  186. lastTime = currentTime;
  187. this.ms_Ocean.render(this.ms_Ocean.deltaTime);
  188. this.ms_Ocean.overrideMaterial = this.ms_Ocean.materialOcean;
  189. if (this.ms_Ocean.changed) {
  190. this.ms_Ocean.materialOcean.uniforms.u_size.value = this.ms_Ocean.size;
  191. this.ms_Ocean.materialOcean.uniforms.u_sunDirection.value = new THREE.Vector3(this.ms_Ocean.sunDirectionX, this.ms_Ocean.sunDirectionY, this.ms_Ocean.sunDirectionZ);
  192. this.ms_Ocean.materialOcean.uniforms.u_exposure.value = this.ms_Ocean.exposure;
  193. this.ms_Ocean.changed = false;
  194. }
  195. this.ms_Ocean.materialOcean.uniforms.u_normalMap.value = this.ms_Ocean.normalMapFramebuffer ;
  196. this.ms_Ocean.materialOcean.uniforms.u_displacementMap.value = this.ms_Ocean.displacementMapFramebuffer ;
  197. this.ms_Ocean.materialOcean.uniforms.u_projectionMatrix.value = this.ms_Camera.projectionMatrix ;
  198. this.ms_Ocean.materialOcean.uniforms.u_viewMatrix.value = this.ms_Camera.matrixWorldInverse ;
  199. this.ms_Ocean.materialOcean.uniforms.u_cameraPosition.value = this.ms_Camera.position;
  200. this.ms_Ocean.materialOcean.depthTest = true;
  201. //this.ms_Scene.__lights[1].position.x = this.ms_Scene.__lights[1].position.x + 0.01;
  202. this.ms_Controls.update();
  203. this.Display();
  204. },
  205. Resize: function (inWidth, inHeight) {
  206. this.ms_Camera.aspect = inWidth / inHeight;
  207. this.ms_Camera.updateProjectionMatrix();
  208. this.ms_Renderer.setSize(inWidth, inHeight);
  209. this.ms_Canvas.appendChild(this.ms_Renderer.domElement);
  210. this.Display();
  211. }
  212. };
  213. function MainLoop() {
  214. requestAnimationFrame(MainLoop);
  215. DEMO.Update();
  216. };
  217. WINDOW.Initialize();
  218. DEMO.Initialize('main_canvas');
  219. WINDOW.ResizeCallback = function (inWidth, inHeight) { DEMO.Resize(inWidth, inHeight); };
  220. DEMO.Resize(WINDOW.ms_Width, WINDOW.ms_Height);
  221. MainLoop();
  222. </script>
  223. </body>
  224. </html>