webgl_shaders_ocean2.html 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277
  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="//ajax.googleapis.com/ajax/libs/jquery/1.8.2/jquery.min.js"></script>
  15. <script src="../build/three.min.js"></script>
  16. <script src="js/libs/dat.gui.min.js"></script>
  17. <script src="js/controls/OrbitControls.js"></script>
  18. <script src="js/shaders/OceanShaders.js"></script>
  19. <script src="js/Ocean.js"></script>
  20. <script>
  21. if (!window.requestAnimationFrame) {
  22. window.requestAnimationFrame = (function () {
  23. return window.webkitRequestAnimationFrame ||
  24. window.mozRequestAnimationFrame || // comment out if FF4 is slow (it caps framerate at ~30fps: https://bugzilla.mozilla.org/show_bug.cgi?id=630127)
  25. window.oRequestAnimationFrame ||
  26. window.msRequestAnimationFrame ||
  27. function ( /* function FrameRequestCallback */ callback, /* DOMElement Element */ element) {
  28. window.setTimeout(callback, 1000 / 60);
  29. };
  30. })();
  31. }
  32. var lastTime = (new Date()).getTime();
  33. var WINDOW =
  34. {
  35. ms_Width: 0,
  36. ms_Height: 0,
  37. ms_Callbacks: 70: "WINDOW.ToggleFullScreen()",
  38. Initialize: function () {
  39. this.UpdateSize();
  40. // Create callbacks from keyboard
  41. $(document).keydown(function (inEvent) { WINDOW.CallAction(inEvent.keyCode); });
  42. $(window).resize(function (inEvent) {
  43. WINDOW.UpdateSize();
  44. WINDOW.ResizeCallback(WINDOW.ms_Width, WINDOW.ms_Height);
  45. });
  46. },
  47. UpdateSize: function () {
  48. this.ms_Width = $(window).width();
  49. this.ms_Height = $(window).height() - 4;
  50. },
  51. CallAction: function (inId) {
  52. if (inId in this.ms_Callbacks) {
  53. eval(this.ms_Callbacks[inId]);
  54. return false;
  55. }
  56. },
  57. ToggleFullScreen: function () {
  58. if (!document.fullscreenElement && !document.mozFullScreenElement && !document.webkitFullscreenElement) {
  59. if (document.documentElement.requestFullscreen)
  60. document.documentElement.requestFullscreen();
  61. else if (document.documentElement.mozRequestFullScreen)
  62. document.documentElement.mozRequestFullScreen();
  63. else if (document.documentElement.webkitRequestFullscreen)
  64. document.documentElement.webkitRequestFullscreen(Element.ALLOW_KEYBOARD_INPUT);
  65. }
  66. else {
  67. if (document.cancelFullScreen)
  68. document.cancelFullScreen();
  69. else if (document.mozCancelFullScreen)
  70. document.mozCancelFullScreen();
  71. else if (document.webkitCancelFullScreen)
  72. document.webkitCancelFullScreen();
  73. }
  74. },
  75. ResizeCallback: function (inWidth, inHeight) {},
  76. };
  77. var DEMO =
  78. {
  79. ms_Canvas: null,
  80. ms_Renderer: null,
  81. ms_Camera: null,
  82. ms_Scene: null,
  83. ms_Controls: null,
  84. ms_Ocean: null,
  85. Enable: (function () {
  86. try {
  87. var aCanvas = document.createElement('canvas');
  88. return !!window.WebGLRenderingContext && (aCanvas.getContext('webgl') || aCanvas.getContext('experimental-webgl'));
  89. }
  90. catch (e) { return false; }
  91. })(),
  92. Initialize: function (inIdCanvas) {
  93. this.ms_Canvas = $('#' + inIdCanvas);
  94. // Initialize Renderer, Camera and Scene
  95. this.ms_Renderer =
  96. this.Enable ?
  97. new THREE.WebGLRenderer({ premultipliedAlpha: false, antialias: true })
  98. : new THREE.CanvasRenderer({ premultipliedAlpha: false, antialias: true });
  99. this.ms_Renderer.shadowMapEnabled = true;
  100. this.ms_Renderer.context.getExtension('OES_texture_float');
  101. this.ms_Renderer.context.getExtension('OES_texture_float_linear');
  102. this.ms_Renderer.autoClear = true;
  103. this.ms_Canvas.html(this.ms_Renderer.domElement);
  104. this.ms_Scene = new THREE.Scene();
  105. this.ms_Camera = new THREE.PerspectiveCamera(55.0, WINDOW.ms_Width / WINDOW.ms_Height, 0.5, 300000);
  106. this.ms_Camera.position.set(900, 700, 900);
  107. this.ms_Camera.lookAt(new THREE.Vector3(0,0,0));
  108. // Initialize Orbit control
  109. this.ms_Controls = new THREE.OrbitControls(this.ms_Camera, this.ms_Renderer.domElement);
  110. this.ms_Controls.userPan = false;
  111. this.ms_Controls.userPanSpeed = 0.0;
  112. this.ms_Controls.minDistance = 0;
  113. this.ms_Controls.maxDistance = 2000.0;
  114. this.ms_Controls.minPolarAngle = 0;
  115. this.ms_Controls.maxPolarAngle = Math.PI * 0.495;
  116. this.debugaxis = function (axisLength) {
  117. //Shorten the vertex function
  118. function v(a, b, c) {
  119. return new THREE.Vector3(a, b, c);
  120. }
  121. //Create axis (point1, point2, colour)
  122. function createAxis(p1, p2, color,scene) {
  123. var line, lineGeometry = new THREE.Geometry(),
  124. lineMat = new THREE.LineBasicMaterial({ color: color, lineWidth: 1 });
  125. lineGeometry.vertices.push(p1, p2);
  126. line = new THREE.Line(lineGeometry, lineMat);
  127. scene.add(line);
  128. }
  129. createAxis(v(-axisLength, 0, 0), v(axisLength, 0, 0), 0xFF0000, this.ms_Scene);
  130. createAxis(v(0, -axisLength, 0), v(0, axisLength, 0), 0x00FF00, this.ms_Scene);
  131. createAxis(v(0, 0, -axisLength), v(0, 0, axisLength), 0xFFFF00, this.ms_Scene);
  132. };
  133. //To use enter the axis length
  134. this.debugaxis(20);
  135. var gsize = 512;
  136. var res = 1024;
  137. var gres = res / 2;
  138. var origx = -gsize / 2;
  139. var origz = -gsize / 2;
  140. this.ms_Ocean = new THREE.Ocean(this.ms_Renderer, this.ms_Camera, this.ms_Scene,
  141. {
  142. INITIAL_SIZE : 1000.0,
  143. INITIAL_WIND : [10.0, 10.0],
  144. INITIAL_CHOPPINESS : 1.5,
  145. CLEAR_COLOR : [1.0, 1.0, 1.0, 0.0],
  146. GEOMETRY_ORIGIN : [origx, origz],
  147. SUN_DIRECTION : [-1.0, 1.0, 1.0],
  148. OCEAN_COLOR: new THREE.Vector3(0.004, 0.016, 0.047),
  149. SKY_COLOR: new THREE.Vector3(3.2, 9.6, 12.8),
  150. EXPOSURE : 0.35,
  151. GEOMETRY_RESOLUTION: gres,
  152. GEOMETRY_SIZE : gsize,
  153. RESOLUTION : res
  154. });
  155. this.ms_Ocean.materialOcean.uniforms.u_projectionMatrix = { type: "m4", value: this.ms_Camera.projectionMatrix };
  156. this.ms_Ocean.materialOcean.uniforms.u_viewMatrix = { type: "m4", value: this.ms_Camera.matrixWorldInverse };
  157. this.ms_Ocean.materialOcean.uniforms.u_cameraPosition = { type: "v3", value: this.ms_Camera.position };
  158. this.ms_Scene.add(this.ms_Ocean.oceanMesh);
  159. var gui = new dat.GUI();
  160. var c1 = gui.add(this.ms_Ocean, "size",100, 5000);
  161. c1.onChange(function(v) {
  162. this.object.size = v;
  163. this.object.changed = true;
  164. });
  165. var c2 = gui.add(this.ms_Ocean, "choppiness", 0.1, 4);
  166. c2.onChange(function (v) {
  167. this.object.choppiness = v;
  168. this.object.changed = true;
  169. });
  170. var c3 = gui.add(this.ms_Ocean, "windX",-15, 15);
  171. c3.onChange(function (v) {
  172. this.object.windX = v;
  173. this.object.changed = true;
  174. });
  175. var c4 = gui.add(this.ms_Ocean, "windY", -15, 15);
  176. c4.onChange(function (v) {
  177. this.object.windY = v;
  178. this.object.changed = true;
  179. });
  180. var c5 = gui.add(this.ms_Ocean, "sunDirectionX", -1.0, 1.0);
  181. c5.onChange(function (v) {
  182. this.object.sunDirectionX = v;
  183. this.object.changed = true;
  184. });
  185. var c6 = gui.add(this.ms_Ocean, "sunDirectionY", -1.0, 1.0);
  186. c6.onChange(function (v) {
  187. this.object.sunDirectionY = v;
  188. this.object.changed = true;
  189. });
  190. var c7 = gui.add(this.ms_Ocean, "sunDirectionZ", -1.0, 1.0);
  191. c7.onChange(function (v) {
  192. this.object.sunDirectionZ = v;
  193. this.object.changed = true;
  194. });
  195. var c8 = gui.add(this.ms_Ocean, "exposure", 0.0, 0.5);
  196. c8.onChange(function (v) {
  197. this.object.exposure = v;
  198. this.object.changed = true;
  199. });
  200. },
  201. Display: function () {
  202. this.ms_Renderer.render(this.ms_Scene, this.ms_Camera);
  203. },
  204. Update: function () {
  205. this.ms_Renderer.clear();
  206. var currentTime = new Date().getTime();
  207. this.ms_Ocean.deltaTime = (currentTime - lastTime) / 1000 || 0.0;
  208. lastTime = currentTime;
  209. this.ms_Ocean.render(this.ms_Ocean.deltaTime);
  210. this.ms_Ocean.overrideMaterial = this.ms_Ocean.materialOcean;
  211. if (this.ms_Ocean.changed) {
  212. this.ms_Ocean.materialOcean.uniforms.u_size.value = this.ms_Ocean.size;
  213. this.ms_Ocean.materialOcean.uniforms.u_sunDirection.value = new THREE.Vector3(this.ms_Ocean.sunDirectionX, this.ms_Ocean.sunDirectionY, this.ms_Ocean.sunDirectionZ);
  214. this.ms_Ocean.materialOcean.uniforms.u_exposure.value = this.ms_Ocean.exposure;
  215. this.ms_Ocean.changed = false;
  216. }
  217. this.ms_Ocean.materialOcean.uniforms.u_normalMap.value = this.ms_Ocean.normalMapFramebuffer ;
  218. this.ms_Ocean.materialOcean.uniforms.u_displacementMap.value = this.ms_Ocean.displacementMapFramebuffer ;
  219. this.ms_Ocean.materialOcean.uniforms.u_projectionMatrix.value = this.ms_Camera.projectionMatrix ;
  220. this.ms_Ocean.materialOcean.uniforms.u_viewMatrix.value = this.ms_Camera.matrixWorldInverse ;
  221. this.ms_Ocean.materialOcean.uniforms.u_cameraPosition.value = this.ms_Camera.position;
  222. this.ms_Ocean.materialOcean.depthTest = true;
  223. //this.ms_Scene.__lights[1].position.x = this.ms_Scene.__lights[1].position.x + 0.01;
  224. this.ms_Controls.update();
  225. this.Display();
  226. },
  227. Resize: function (inWidth, inHeight) {
  228. this.ms_Camera.aspect = inWidth / inHeight;
  229. this.ms_Camera.updateProjectionMatrix();
  230. this.ms_Renderer.setSize(inWidth, inHeight);
  231. this.ms_Canvas.html(this.ms_Renderer.domElement);
  232. this.Display();
  233. }
  234. };
  235. function MainLoop() {
  236. requestAnimationFrame(MainLoop);
  237. DEMO.Update();
  238. };
  239. $(function () {
  240. WINDOW.Initialize();
  241. DEMO.Initialize('main_canvas');
  242. WINDOW.ResizeCallback = function (inWidth, inHeight) { DEMO.Resize(inWidth, inHeight); };
  243. DEMO.Resize(WINDOW.ms_Width, WINDOW.ms_Height);
  244. MainLoop();
  245. });
  246. </script>
  247. </body>
  248. </html>