webgl_shaders_ocean.html 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <title>three.js webgl - geometry - terrain</title>
  5. <meta charset="utf-8">
  6. <meta name="viewport" content="width=device-width, user-scalable=no, minimum-scale=1.0, maximum-scale=1.0">
  7. <style>
  8. body {
  9. color: #000;
  10. font-family:Monospace;
  11. font-size:13px;
  12. text-align:center;
  13. margin: 0px;
  14. overflow: hidden;
  15. }
  16. #info {
  17. position: absolute;
  18. top: 0px; width: 100%;
  19. padding: 5px;
  20. }
  21. a {
  22. color: #a06851;
  23. }
  24. </style>
  25. </head>
  26. <body>
  27. <div id="info"><a href="http://threejs.org" target="_blank">three.js</a> - webgl ocean demo</div>
  28. <script src="../build/three.min.js"></script>
  29. <script src="js/controls/OrbitControls.js"></script>
  30. <script src="js/Mirror.js"></script>
  31. <script src="js/WaterShader.js"></script>
  32. <script src="js/Detector.js"></script>
  33. <script src="js/libs/stats.min.js"></script>
  34. <script>
  35. if ( ! Detector.webgl ) {
  36. Detector.addGetWebGLMessage();
  37. document.getElementById( 'container' ).innerHTML = "";
  38. }
  39. var container, stats;
  40. var camera, scene, renderer;
  41. var parameters = {
  42. width: 2000,
  43. height: 2000,
  44. widthSegments: 250,
  45. heightSegments: 250,
  46. depth: 1500,
  47. param: 4,
  48. filterparam: 1
  49. }
  50. var waterNormals;
  51. init();
  52. animate();
  53. function init() {
  54. container = document.createElement( 'div' );
  55. document.body.appendChild( container );
  56. renderer = new THREE.WebGLRenderer();
  57. renderer.setSize( window.innerWidth, window.innerHeight );
  58. container.appendChild( renderer.domElement );
  59. scene = new THREE.Scene();
  60. camera = new THREE.PerspectiveCamera( 55, window.innerWidth / window.innerHeight, 0.5, 3000000 );
  61. // resize(innerWidth, innerHeight);
  62. camera.position.set( 0, Math.max( parameters.width * 1.5, parameters.height ) / 8, -parameters.height );
  63. camera.lookAt( new THREE.Vector3( 0, 0, 0 ) );
  64. controls = new THREE.OrbitControls( camera, renderer.domElement );
  65. controls.userPan = false;
  66. controls.userPanSpeed = 0.0;
  67. controls.maxDistance = 5000.0;
  68. controls.maxPolarAngle = Math.PI * 0.495;
  69. directionalLight = new THREE.DirectionalLight( 0xffff55, 1 );
  70. directionalLight.position.set( -600, 300, 600 );
  71. scene.add( directionalLight );
  72. waterNormals = new THREE.ImageUtils.loadTexture( 'textures/waternormals.jpg' );
  73. waterNormals.wrapS = waterNormals.wrapT = THREE.RepeatWrapping;
  74. water = new THREE.Water( renderer, camera, scene, {
  75. textureWidth: 512,
  76. textureHeight: 512,
  77. waterNormals: waterNormals,
  78. alpha: 1.0,
  79. sunDirection: directionalLight.position.normalize(),
  80. sunColor: 0xffffff,
  81. waterColor: 0x001e0f,
  82. distortionScale: 50.0,
  83. } );
  84. mirrorMesh = new THREE.Mesh(
  85. new THREE.PlaneGeometry( parameters.width * 500, parameters.height * 500, 50, 50 ),
  86. water.material
  87. );
  88. mirrorMesh.add( water );
  89. mirrorMesh.rotation.x = - Math.PI * 0.5;
  90. scene.add( mirrorMesh );
  91. // load skybox
  92. var path = "textures/cube/skybox/";
  93. var cubeMap = THREE.ImageUtils.loadTextureCube( [
  94. path + 'px.jpg',
  95. path + 'nx.jpg',
  96. path + 'py.jpg',
  97. path + 'ny.jpg',
  98. path + 'pz.jpg',
  99. path + 'nz.jpg'
  100. ] );
  101. cubeMap.format = THREE.RGBFormat;
  102. var cubeShader = THREE.ShaderLib['cube'];
  103. cubeShader.uniforms['tCube'].value = cubeMap;
  104. var skyBoxMaterial = new THREE.ShaderMaterial( {
  105. fragmentShader: cubeShader.fragmentShader,
  106. vertexShader: cubeShader.vertexShader,
  107. uniforms: cubeShader.uniforms,
  108. depthWrite: false,
  109. side: THREE.BackSide
  110. });
  111. var skyBox = new THREE.Mesh(
  112. new THREE.CubeGeometry( 1000000, 1000000, 1000000 ),
  113. skyBoxMaterial
  114. );
  115. scene.add( skyBox );
  116. }
  117. //
  118. function animate() {
  119. requestAnimationFrame( animate );
  120. render();
  121. }
  122. function render() {
  123. water.material.uniforms.time.value += 1.0 / 60.0;
  124. controls.update();
  125. water.render();
  126. renderer.render( scene, camera );
  127. }
  128. function resize(width, height) {
  129. camera.aspect = width / height;
  130. camera.updateProjectionMatrix();
  131. renderer.setSize( width, height );
  132. }
  133. </script>
  134. </body>
  135. </html>