webgl_water.html 4.7 KB

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