webgl_water_flowmap.html 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <title>three.js - water flow map</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 flow map
  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. let scene, camera, renderer, water;
  20. init();
  21. animate();
  22. function init() {
  23. // scene
  24. scene = new THREE.Scene();
  25. // camera
  26. camera = new THREE.PerspectiveCamera( 45, window.innerWidth / window.innerHeight, 0.1, 200 );
  27. camera.position.set( 0, 25, 0 );
  28. camera.lookAt( scene.position );
  29. // ground
  30. const groundGeometry = new THREE.PlaneGeometry( 20, 20, 10, 10 );
  31. const groundMaterial = new THREE.MeshBasicMaterial( { color: 0xcccccc } );
  32. const ground = new THREE.Mesh( groundGeometry, groundMaterial );
  33. ground.rotation.x = Math.PI * - 0.5;
  34. scene.add( ground );
  35. const textureLoader = new THREE.TextureLoader();
  36. textureLoader.load( 'textures/floors/FloorsCheckerboard_S_Diffuse.jpg', function ( map ) {
  37. map.wrapS = THREE.RepeatWrapping;
  38. map.wrapT = THREE.RepeatWrapping;
  39. map.anisotropy = 16;
  40. map.repeat.set( 4, 4 );
  41. groundMaterial.map = map;
  42. groundMaterial.needsUpdate = true;
  43. } );
  44. // water
  45. const waterGeometry = new THREE.PlaneGeometry( 20, 20 );
  46. const flowMap = textureLoader.load( 'textures/water/Water_1_M_Flow.jpg' );
  47. water = new Water( waterGeometry, {
  48. scale: 2,
  49. textureWidth: 1024,
  50. textureHeight: 1024,
  51. flowMap: flowMap
  52. } );
  53. water.position.y = 1;
  54. water.rotation.x = Math.PI * - 0.5;
  55. scene.add( water );
  56. // flow map helper
  57. const helperGeometry = new THREE.PlaneGeometry( 20, 20 );
  58. const helperMaterial = new THREE.MeshBasicMaterial( { map: flowMap } );
  59. const helper = new THREE.Mesh( helperGeometry, helperMaterial );
  60. helper.position.y = 1.01;
  61. helper.rotation.x = Math.PI * - 0.5;
  62. helper.visible = false;
  63. scene.add( helper );
  64. // renderer
  65. renderer = new THREE.WebGLRenderer( { antialias: true } );
  66. renderer.setSize( window.innerWidth, window.innerHeight );
  67. renderer.setPixelRatio( window.devicePixelRatio );
  68. document.body.appendChild( renderer.domElement );
  69. //
  70. const gui = new GUI();
  71. gui.add( helper, 'visible' ).name( 'Show Flow Map' );
  72. gui.open();
  73. //
  74. const controls = new OrbitControls( camera, renderer.domElement );
  75. controls.minDistance = 5;
  76. controls.maxDistance = 50;
  77. //
  78. window.addEventListener( 'resize', onWindowResize );
  79. }
  80. function onWindowResize() {
  81. camera.aspect = window.innerWidth / window.innerHeight;
  82. camera.updateProjectionMatrix();
  83. renderer.setSize( window.innerWidth, window.innerHeight );
  84. }
  85. function animate() {
  86. requestAnimationFrame( animate );
  87. render();
  88. }
  89. function render() {
  90. renderer.render( scene, camera );
  91. }
  92. </script>
  93. </body>
  94. </html>