webgl_water_flowmap.html 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  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. var 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, 1000 );
  27. camera.position.set( 0, 25, 0 );
  28. camera.lookAt( scene.position );
  29. // ground
  30. var groundGeometry = new THREE.PlaneBufferGeometry( 20, 20, 10, 10 );
  31. var groundMaterial = new THREE.MeshBasicMaterial( { color: 0xcccccc } );
  32. var ground = new THREE.Mesh( groundGeometry, groundMaterial );
  33. ground.rotation.x = Math.PI * - 0.5;
  34. scene.add( ground );
  35. var 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. var waterGeometry = new THREE.PlaneBufferGeometry( 20, 20 );
  46. var 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. var helperGeometry = new THREE.PlaneBufferGeometry( 20, 20 );
  58. var helperMaterial = new THREE.MeshBasicMaterial( { map: flowMap } );
  59. var 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. var gui = new GUI();
  71. gui.add( helper, 'visible' ).name( 'Show Flow Map' );
  72. gui.open();
  73. //
  74. var controls = new OrbitControls( camera, renderer.domElement );
  75. //
  76. window.addEventListener( 'resize', onResize, false );
  77. }
  78. function onResize() {
  79. camera.aspect = window.innerWidth / window.innerHeight;
  80. camera.updateProjectionMatrix();
  81. renderer.setSize( window.innerWidth, window.innerHeight );
  82. }
  83. function animate() {
  84. requestAnimationFrame( animate );
  85. render();
  86. }
  87. function render() {
  88. renderer.render( scene, camera );
  89. }
  90. </script>
  91. </body>
  92. </html>