webgl_water.html 5.1 KB

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