webgl_shaders_ocean2.html 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  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. <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_Renderer: null,
  70. ms_Camera: null,
  71. ms_Scene: null,
  72. ms_Controls: null,
  73. ms_Ocean: null,
  74. Initialize: function () {
  75. this.ms_Renderer = new THREE.WebGLRenderer();
  76. this.ms_Renderer.context.getExtension('OES_texture_float');
  77. this.ms_Renderer.context.getExtension('OES_texture_float_linear');
  78. document.body.appendChild(this.ms_Renderer.domElement);
  79. this.ms_Scene = new THREE.Scene();
  80. this.ms_Camera = new THREE.PerspectiveCamera(55.0, WINDOW.ms_Width / WINDOW.ms_Height, 0.5, 300000);
  81. this.ms_Camera.position.set(450, 350, 450);
  82. this.ms_Camera.lookAt(new THREE.Vector3());
  83. // Initialize Orbit control
  84. this.ms_Controls = new THREE.OrbitControls(this.ms_Camera, this.ms_Renderer.domElement);
  85. this.ms_Controls.userPan = false;
  86. this.ms_Controls.userPanSpeed = 0.0;
  87. this.ms_Controls.minDistance = 0;
  88. this.ms_Controls.maxDistance = 2000.0;
  89. this.ms_Controls.minPolarAngle = 0;
  90. this.ms_Controls.maxPolarAngle = Math.PI * 0.495;
  91. var gsize = 512;
  92. var res = 1024;
  93. var gres = res / 2;
  94. var origx = -gsize / 2;
  95. var origz = -gsize / 2;
  96. this.ms_Ocean = new THREE.Ocean(this.ms_Renderer, this.ms_Camera, this.ms_Scene,
  97. {
  98. INITIAL_SIZE : 256.0,
  99. INITIAL_WIND : [10.0, 10.0],
  100. INITIAL_CHOPPINESS : 1.5,
  101. CLEAR_COLOR : [1.0, 1.0, 1.0, 0.0],
  102. GEOMETRY_ORIGIN : [origx, origz],
  103. SUN_DIRECTION : [-1.0, 1.0, 1.0],
  104. OCEAN_COLOR: new THREE.Vector3(0.004, 0.016, 0.047),
  105. SKY_COLOR: new THREE.Vector3(3.2, 9.6, 12.8),
  106. EXPOSURE : 0.35,
  107. GEOMETRY_RESOLUTION: gres,
  108. GEOMETRY_SIZE : gsize,
  109. RESOLUTION : res
  110. });
  111. this.ms_Ocean.materialOcean.uniforms.u_projectionMatrix = { type: "m4", value: this.ms_Camera.projectionMatrix };
  112. this.ms_Ocean.materialOcean.uniforms.u_viewMatrix = { type: "m4", value: this.ms_Camera.matrixWorldInverse };
  113. this.ms_Ocean.materialOcean.uniforms.u_cameraPosition = { type: "v3", value: this.ms_Camera.position };
  114. this.ms_Scene.add(this.ms_Ocean.oceanMesh);
  115. var gui = new dat.GUI();
  116. var c1 = gui.add(this.ms_Ocean, "size",100, 5000);
  117. c1.onChange(function(v) {
  118. this.object.size = v;
  119. this.object.changed = true;
  120. });
  121. var c2 = gui.add(this.ms_Ocean, "choppiness", 0.1, 4);
  122. c2.onChange(function (v) {
  123. this.object.choppiness = v;
  124. this.object.changed = true;
  125. });
  126. var c3 = gui.add(this.ms_Ocean, "windX",-15, 15);
  127. c3.onChange(function (v) {
  128. this.object.windX = v;
  129. this.object.changed = true;
  130. });
  131. var c4 = gui.add(this.ms_Ocean, "windY", -15, 15);
  132. c4.onChange(function (v) {
  133. this.object.windY = v;
  134. this.object.changed = true;
  135. });
  136. var c5 = gui.add(this.ms_Ocean, "sunDirectionX", -1.0, 1.0);
  137. c5.onChange(function (v) {
  138. this.object.sunDirectionX = v;
  139. this.object.changed = true;
  140. });
  141. var c6 = gui.add(this.ms_Ocean, "sunDirectionY", -1.0, 1.0);
  142. c6.onChange(function (v) {
  143. this.object.sunDirectionY = v;
  144. this.object.changed = true;
  145. });
  146. var c7 = gui.add(this.ms_Ocean, "sunDirectionZ", -1.0, 1.0);
  147. c7.onChange(function (v) {
  148. this.object.sunDirectionZ = v;
  149. this.object.changed = true;
  150. });
  151. var c8 = gui.add(this.ms_Ocean, "exposure", 0.0, 0.5);
  152. c8.onChange(function (v) {
  153. this.object.exposure = v;
  154. this.object.changed = true;
  155. });
  156. },
  157. Display: function () {
  158. this.ms_Renderer.render(this.ms_Scene, this.ms_Camera);
  159. },
  160. Update: function () {
  161. var currentTime = new Date().getTime();
  162. this.ms_Ocean.deltaTime = (currentTime - lastTime) / 1000 || 0.0;
  163. lastTime = currentTime;
  164. this.ms_Ocean.render(this.ms_Ocean.deltaTime);
  165. this.ms_Ocean.overrideMaterial = this.ms_Ocean.materialOcean;
  166. if (this.ms_Ocean.changed) {
  167. this.ms_Ocean.materialOcean.uniforms.u_size.value = this.ms_Ocean.size;
  168. this.ms_Ocean.materialOcean.uniforms.u_sunDirection.value.set( this.ms_Ocean.sunDirectionX, this.ms_Ocean.sunDirectionY, this.ms_Ocean.sunDirectionZ );
  169. this.ms_Ocean.materialOcean.uniforms.u_exposure.value = this.ms_Ocean.exposure;
  170. this.ms_Ocean.changed = false;
  171. }
  172. this.ms_Ocean.materialOcean.uniforms.u_normalMap.value = this.ms_Ocean.normalMapFramebuffer ;
  173. this.ms_Ocean.materialOcean.uniforms.u_displacementMap.value = this.ms_Ocean.displacementMapFramebuffer ;
  174. this.ms_Ocean.materialOcean.uniforms.u_projectionMatrix.value = this.ms_Camera.projectionMatrix ;
  175. this.ms_Ocean.materialOcean.uniforms.u_viewMatrix.value = this.ms_Camera.matrixWorldInverse ;
  176. this.ms_Ocean.materialOcean.uniforms.u_cameraPosition.value = this.ms_Camera.position;
  177. this.ms_Ocean.materialOcean.depthTest = true;
  178. //this.ms_Scene.__lights[1].position.x = this.ms_Scene.__lights[1].position.x + 0.01;
  179. this.ms_Controls.update();
  180. this.Display();
  181. },
  182. Resize: function (inWidth, inHeight) {
  183. this.ms_Camera.aspect = inWidth / inHeight;
  184. this.ms_Camera.updateProjectionMatrix();
  185. this.ms_Renderer.setSize(inWidth, inHeight);
  186. this.Display();
  187. }
  188. };
  189. WINDOW.Initialize();
  190. DEMO.Initialize();
  191. WINDOW.ResizeCallback = function (inWidth, inHeight) { DEMO.Resize(inWidth, inHeight); };
  192. DEMO.Resize(WINDOW.ms_Width, WINDOW.ms_Height);
  193. var render = function () {
  194. requestAnimationFrame( render );
  195. DEMO.Update();
  196. };
  197. render();
  198. </script>
  199. </body>
  200. </html>