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