123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132 |
- <!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';
- var scene, camera, clock, renderer, refractor;
- init();
- function init() {
- // scene
- scene = new THREE.Scene();
- // camera
- camera = new THREE.PerspectiveCamera( 45, window.innerWidth / window.innerHeight, 1, 1000 );
- camera.position.set( - 10, 0, 15 );
- camera.lookAt( scene.position );
- // clock
- clock = new THREE.Clock();
- // mesh
- var geometry = new THREE.TorusKnotBufferGeometry( 3, 1, 256, 32 );
- var material = new THREE.MeshStandardMaterial( { color: 0x6083c2 } );
- var mesh = new THREE.Mesh( geometry, material );
- scene.add( mesh );
- // refractor
- var refractorGeometry = new THREE.PlaneBufferGeometry( 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
- var dudvMap = new THREE.TextureLoader().load( 'textures/waterdudv.jpg', function () {
- animate();
- } );
- dudvMap.wrapS = dudvMap.wrapT = THREE.RepeatWrapping;
- refractor.material.uniforms[ "tDudv" ].value = dudvMap;
- // light
- var ambientLight = new THREE.AmbientLight( 0xcccccc, 0.4 );
- scene.add( ambientLight );
- var 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 );
- //
- var controls = new OrbitControls( camera, renderer.domElement );
- //
- window.addEventListener( 'resize', onResize, false );
- }
- function onResize() {
- 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>
|