webgl_water.html 4.9 KB

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