135 lines
3.1 KiB
HTML
135 lines
3.1 KiB
HTML
<!DOCTYPE html>
|
|
<html lang="en">
|
|
<head>
|
|
<title>three.js refraction</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> - refraction
|
|
</div>
|
|
|
|
<script type="module">
|
|
|
|
import * as THREE from '../build/three.module.js';
|
|
|
|
import { OrbitControls } from './jsm/controls/OrbitControls.js';
|
|
import { Refractor } from './jsm/objects/Refractor.js';
|
|
import { WaterRefractionShader } from './jsm/shaders/WaterRefractionShader.js';
|
|
|
|
let scene, camera, clock, renderer, refractor;
|
|
|
|
init();
|
|
|
|
function init() {
|
|
|
|
// scene
|
|
|
|
scene = new THREE.Scene();
|
|
|
|
// camera
|
|
|
|
camera = new THREE.PerspectiveCamera( 45, window.innerWidth / window.innerHeight, 0.1, 200 );
|
|
camera.position.set( - 10, 0, 15 );
|
|
camera.lookAt( scene.position );
|
|
|
|
// clock
|
|
|
|
clock = new THREE.Clock();
|
|
|
|
// mesh
|
|
|
|
const geometry = new THREE.TorusKnotGeometry( 3, 1, 256, 32 );
|
|
const material = new THREE.MeshStandardMaterial( { color: 0x6083c2 } );
|
|
|
|
const mesh = new THREE.Mesh( geometry, material );
|
|
scene.add( mesh );
|
|
|
|
// refractor
|
|
|
|
const refractorGeometry = new THREE.PlaneGeometry( 10, 10 );
|
|
|
|
refractor = new Refractor( refractorGeometry, {
|
|
color: 0x999999,
|
|
textureWidth: 1024,
|
|
textureHeight: 1024,
|
|
shader: WaterRefractionShader
|
|
} );
|
|
|
|
refractor.position.set( 0, 0, 5 );
|
|
|
|
scene.add( refractor );
|
|
|
|
// load dudv map for distortion effect
|
|
|
|
const dudvMap = new THREE.TextureLoader().load( 'textures/waterdudv.jpg', function () {
|
|
|
|
animate();
|
|
|
|
} );
|
|
|
|
dudvMap.wrapS = dudvMap.wrapT = THREE.RepeatWrapping;
|
|
refractor.material.uniforms[ "tDudv" ].value = dudvMap;
|
|
|
|
// light
|
|
|
|
const ambientLight = new THREE.AmbientLight( 0xcccccc, 0.4 );
|
|
scene.add( ambientLight );
|
|
|
|
const pointLight = new THREE.PointLight( 0xffffff, 0.8 );
|
|
camera.add( pointLight );
|
|
scene.add( camera );
|
|
|
|
// renderer
|
|
|
|
renderer = new THREE.WebGLRenderer( { antialias: true } );
|
|
renderer.setSize( window.innerWidth, window.innerHeight );
|
|
renderer.setClearColor( 0x20252f );
|
|
renderer.setPixelRatio( window.devicePixelRatio );
|
|
document.body.appendChild( renderer.domElement );
|
|
|
|
//
|
|
|
|
const controls = new OrbitControls( camera, renderer.domElement );
|
|
controls.minDistance = 10;
|
|
controls.maxDistance = 100;
|
|
|
|
//
|
|
|
|
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() {
|
|
|
|
refractor.material.uniforms[ "time" ].value += clock.getDelta();
|
|
|
|
renderer.render( scene, camera );
|
|
|
|
}
|
|
|
|
</script>
|
|
|
|
</body>
|
|
</html>
|