You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

151 lines
3.7 KiB

2 years ago
<!DOCTYPE html>
<html lang="en">
<head>
<title>three.js - water flow map</title>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, user-scalable=no, minimum-scale=1.0, maximum-scale=1.0">
<link type="text/css" rel="stylesheet" href="main.css">
</head>
<body>
<div id="container"></div>
<div id="info">
<a href="https://threejs.org" target="_blank" rel="noopener noreferrer">three.js</a> - water flow map
</div>
<!-- Import maps polyfill -->
<!-- Remove this when import maps will be widely supported -->
<script async src="https://unpkg.com/es-module-shims@1.3.6/dist/es-module-shims.js"></script>
<script type="importmap">
{
"imports": {
"three": "../build/three.module.js",
"three/addons/": "./jsm/"
}
}
</script>
<script type="module">
import * as THREE from 'three';
import { GUI } from 'three/addons/libs/lil-gui.module.min.js';
import { OrbitControls } from 'three/addons/controls/OrbitControls.js';
import { Water } from 'three/addons/objects/Water2.js';
let scene, camera, renderer, water;
init();
animate();
function init() {
// scene
scene = new THREE.Scene();
// camera
camera = new THREE.PerspectiveCamera( 45, window.innerWidth / window.innerHeight, 0.1, 200 );
camera.position.set( 0, 25, 0 );
camera.lookAt( scene.position );
// ground
const groundGeometry = new THREE.PlaneGeometry( 20, 20, 10, 10 );
const groundMaterial = new THREE.MeshBasicMaterial( { color: 0xcccccc } );
const ground = new THREE.Mesh( groundGeometry, groundMaterial );
ground.rotation.x = Math.PI * - 0.5;
scene.add( ground );
const textureLoader = new THREE.TextureLoader();
textureLoader.load( 'textures/floors/FloorsCheckerboard_S_Diffuse.jpg', function ( map ) {
map.wrapS = THREE.RepeatWrapping;
map.wrapT = THREE.RepeatWrapping;
map.anisotropy = 16;
map.repeat.set( 4, 4 );
groundMaterial.map = map;
groundMaterial.needsUpdate = true;
} );
// water
const waterGeometry = new THREE.PlaneGeometry( 20, 20 );
const flowMap = textureLoader.load( 'textures/water/Water_1_M_Flow.jpg' );
water = new Water( waterGeometry, {
scale: 2,
textureWidth: 1024,
textureHeight: 1024,
flowMap: flowMap
} );
water.position.y = 1;
water.rotation.x = Math.PI * - 0.5;
scene.add( water );
// flow map helper
const helperGeometry = new THREE.PlaneGeometry( 20, 20 );
const helperMaterial = new THREE.MeshBasicMaterial( { map: flowMap } );
const helper = new THREE.Mesh( helperGeometry, helperMaterial );
helper.position.y = 1.01;
helper.rotation.x = Math.PI * - 0.5;
helper.visible = false;
scene.add( helper );
// renderer
renderer = new THREE.WebGLRenderer( { antialias: true } );
renderer.setSize( window.innerWidth, window.innerHeight );
renderer.setPixelRatio( window.devicePixelRatio );
document.body.appendChild( renderer.domElement );
//
const gui = new GUI();
gui.add( helper, 'visible' ).name( 'Show Flow Map' );
gui.open();
//
const controls = new OrbitControls( camera, renderer.domElement );
controls.minDistance = 5;
controls.maxDistance = 50;
//
window.addEventListener( 'resize', onWindowResize );
}
function onWindowResize() {
camera.aspect = window.innerWidth / window.innerHeight;
camera.updateProjectionMatrix();
renderer.setSize( window.innerWidth, window.innerHeight );
}
function animate() {
requestAnimationFrame( animate );
render();
}
function render() {
renderer.render( scene, camera );
}
</script>
</body>
</html>