webgl_water.html 5.3 KB

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