webgl_water.html 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <title>three.js - water</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. <link type="text/css" rel="stylesheet" href="main.css">
  8. </head>
  9. <body>
  10. <div id="container"></div>
  11. <div id="info">
  12. <a href="https://threejs.org" target="_blank" rel="noopener noreferrer">three.js</a> - water
  13. </div>
  14. <script type="module">
  15. import {
  16. AmbientLight,
  17. BackSide,
  18. BoxBufferGeometry,
  19. Clock,
  20. CubeTextureLoader,
  21. DirectionalLight,
  22. Mesh,
  23. MeshNormalMaterial,
  24. MeshStandardMaterial,
  25. PerspectiveCamera,
  26. PlaneBufferGeometry,
  27. RepeatWrapping,
  28. Scene,
  29. ShaderLib,
  30. ShaderMaterial,
  31. TextureLoader,
  32. TorusKnotBufferGeometry,
  33. Vector2,
  34. WebGLRenderer
  35. } from "../build/three.module.js";
  36. import { GUI } from './jsm/libs/dat.gui.module.js';
  37. import { OrbitControls } from './jsm/controls/OrbitControls.js';
  38. import { Water } from './jsm/objects/Water2.js';
  39. var scene, camera, clock, renderer, water;
  40. var torusKnot;
  41. var params = {
  42. color: '#ffffff',
  43. scale: 4,
  44. flowX: 1,
  45. flowY: 1
  46. };
  47. init();
  48. animate();
  49. function init() {
  50. // scene
  51. scene = new Scene();
  52. // camera
  53. camera = new PerspectiveCamera( 45, window.innerWidth / window.innerHeight, 0.1, 1000 );
  54. camera.position.set( - 15, 7, 15 );
  55. camera.lookAt( scene.position );
  56. // clock
  57. clock = new Clock();
  58. // mesh
  59. var torusKnotGeometry = new TorusKnotBufferGeometry( 3, 1, 256, 32 );
  60. var torusKnotMaterial = new MeshNormalMaterial();
  61. torusKnot = new Mesh( torusKnotGeometry, torusKnotMaterial );
  62. torusKnot.position.y = 4;
  63. torusKnot.scale.set( 0.5, 0.5, 0.5 );
  64. scene.add( torusKnot );
  65. // ground
  66. var groundGeometry = new PlaneBufferGeometry( 20, 20 );
  67. var groundMaterial = new MeshStandardMaterial( { roughness: 0.8, metalness: 0.4 } );
  68. var ground = new Mesh( groundGeometry, groundMaterial );
  69. ground.rotation.x = Math.PI * - 0.5;
  70. scene.add( ground );
  71. var textureLoader = new TextureLoader();
  72. textureLoader.load( 'textures/hardwood2_diffuse.jpg', function ( map ) {
  73. map.wrapS = RepeatWrapping;
  74. map.wrapT = RepeatWrapping;
  75. map.anisotropy = 16;
  76. map.repeat.set( 4, 4 );
  77. groundMaterial.map = map;
  78. groundMaterial.needsUpdate = true;
  79. } );
  80. // water
  81. var waterGeometry = new PlaneBufferGeometry( 20, 20 );
  82. water = new Water( waterGeometry, {
  83. color: params.color,
  84. scale: params.scale,
  85. flowDirection: new Vector2( params.flowX, params.flowY ),
  86. textureWidth: 1024,
  87. textureHeight: 1024
  88. } );
  89. water.position.y = 1;
  90. water.rotation.x = Math.PI * - 0.5;
  91. scene.add( water );
  92. // skybox
  93. var cubeTextureLoader = new CubeTextureLoader();
  94. cubeTextureLoader.setPath( 'textures/cube/skybox/' );
  95. var cubeTexture = cubeTextureLoader.load( [
  96. 'px.jpg', 'nx.jpg',
  97. 'py.jpg', 'ny.jpg',
  98. 'pz.jpg', 'nz.jpg',
  99. ] );
  100. var cubeShader = ShaderLib[ 'cube' ];
  101. cubeShader.uniforms[ 'tCube' ].value = cubeTexture;
  102. var skyBoxMaterial = new ShaderMaterial( {
  103. fragmentShader: cubeShader.fragmentShader,
  104. vertexShader: cubeShader.vertexShader,
  105. uniforms: cubeShader.uniforms,
  106. side: BackSide
  107. } );
  108. var skyBox = new Mesh( new BoxBufferGeometry( 1000, 1000, 1000 ), skyBoxMaterial );
  109. scene.add( skyBox );
  110. // light
  111. var ambientLight = new AmbientLight( 0xcccccc, 0.4 );
  112. scene.add( ambientLight );
  113. var directionalLight = new DirectionalLight( 0xffffff, 0.6 );
  114. directionalLight.position.set( - 1, 1, 1 );
  115. scene.add( directionalLight );
  116. // renderer
  117. renderer = new WebGLRenderer( { antialias: true } );
  118. renderer.setSize( window.innerWidth, window.innerHeight );
  119. renderer.setPixelRatio( window.devicePixelRatio );
  120. document.body.appendChild( renderer.domElement );
  121. // dat.gui
  122. var gui = new GUI();
  123. gui.addColor( params, 'color' ).onChange( function ( value ) {
  124. water.material.uniforms[ "color" ].value.set( value );
  125. } );
  126. gui.add( params, 'scale', 1, 10 ).onChange( function ( value ) {
  127. water.material.uniforms[ "config" ].value.w = value;
  128. } );
  129. gui.add( params, 'flowX', - 1, 1 ).step( 0.01 ).onChange( function ( value ) {
  130. water.material.uniforms[ "flowDirection" ].value.x = value;
  131. water.material.uniforms[ "flowDirection" ].value.normalize();
  132. } );
  133. gui.add( params, 'flowY', - 1, 1 ).step( 0.01 ).onChange( function ( value ) {
  134. water.material.uniforms[ "flowDirection" ].value.y = value;
  135. water.material.uniforms[ "flowDirection" ].value.normalize();
  136. } );
  137. gui.open();
  138. //
  139. var controls = new OrbitControls( camera, renderer.domElement );
  140. //
  141. window.addEventListener( 'resize', onResize, false );
  142. }
  143. function onResize() {
  144. camera.aspect = window.innerWidth / window.innerHeight;
  145. camera.updateProjectionMatrix();
  146. renderer.setSize( window.innerWidth, window.innerHeight );
  147. }
  148. function animate() {
  149. requestAnimationFrame( animate );
  150. render();
  151. }
  152. function render() {
  153. var delta = clock.getDelta();
  154. torusKnot.rotation.x += delta;
  155. torusKnot.rotation.y += delta * 0.5;
  156. renderer.render( scene, camera );
  157. }
  158. </script>
  159. </body>
  160. </html>