webgl_water_flowmap.html 3.6 KB

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