webgl_water_flowmap.html 3.8 KB

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