webgl_shaders_ocean2.html 8.7 KB

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