webgl_water.html 4.9 KB

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