| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247 |
- <!DOCTYPE html>
- <html lang="en">
- <head>
- <title>three.js webgpu - caustics</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="info">
- <a href="https://threejs.org" target="_blank" rel="noopener">three.js webgpu</a> - realtime caustics
- </div>
- <script type="importmap">
- {
- "imports": {
- "three": "../build/three.webgpu.js",
- "three/webgpu": "../build/three.webgpu.js",
- "three/tsl": "../build/three.tsl.js",
- "three/addons/": "./jsm/"
- }
- }
- </script>
- <script type="module">
- import * as THREE from 'three';
- import { uniform, refract, div, positionViewDirection, positionLocal, normalView, texture, Fn, vec2, vec3, vec4 } from 'three/tsl';
- import { OrbitControls } from 'three/addons/controls/OrbitControls.js';
- import { GLTFLoader } from 'three/addons/loaders/GLTFLoader.js';
- import { DRACOLoader } from 'three/addons/loaders/DRACOLoader.js';
- import { GUI } from 'three/addons/libs/lil-gui.module.min.js';
- import Stats from 'three/addons/libs/stats.module.js';
- let camera, scene, renderer, controls;
- let stats;
- let gltf;
- init();
- async function init() {
- camera = new THREE.PerspectiveCamera( 25, window.innerWidth / window.innerHeight, 0.025, 5 );
- camera.position.set( - 0.5, 0.35, 0.2 );
- scene = new THREE.Scene();
- // light
- const spotLight = new THREE.SpotLight( 0xffffff, 1 );
- spotLight.position.set( .2, .3, .2 );
- spotLight.castShadow = true;
- spotLight.angle = Math.PI / 6;
- spotLight.penumbra = 1;
- spotLight.decay = 2;
- spotLight.distance = 0;
- spotLight.shadow.mapType = THREE.HalfFloatType; // For HDR Caustics
- spotLight.shadow.mapSize.width = 1024;
- spotLight.shadow.mapSize.height = 1024;
- spotLight.shadow.camera.near = .1;
- spotLight.shadow.camera.far = 1;
- spotLight.shadow.bias = - .003;
- spotLight.shadow.intensity = .95;
- scene.add( spotLight );
- // model / textures
- const dracoLoader = new DRACOLoader();
- dracoLoader.setDecoderPath( 'jsm/libs/draco/' );
- dracoLoader.setDecoderConfig( { type: 'js' } );
- gltf = ( await new GLTFLoader().setDRACOLoader( dracoLoader ).loadAsync( './models/gltf/duck.glb' ) ).scene;
- gltf.scale.setScalar( .5 );
- scene.add( gltf );
- const causticMap = new THREE.TextureLoader().load( './textures/opengameart/Caustic_Free.jpg' );
- causticMap.wrapS = causticMap.wrapT = THREE.RepeatWrapping;
- causticMap.colorSpace = THREE.SRGBColorSpace;
- // objects / material
- const duck = gltf.children[ 0 ];
- duck.material = new THREE.MeshPhysicalNodeMaterial();
- duck.material.side = THREE.DoubleSide;
- duck.material.transparent = true;
- duck.material.color = new THREE.Color( 0xFFD700 );
- duck.material.transmission = 1;
- duck.material.thickness = .25;
- duck.material.ior = 1.5;
- duck.material.metalness = 0;
- duck.material.roughness = .1;
- duck.castShadow = true;
- // tsl shader
- const causticOcclusion = uniform( 20 );
- duck.material.castShadowPositionNode = Fn( () => {
- // optional: add some distortion to the geometry shadow position if needed
- return positionLocal;
- } )();
- duck.material.castShadowNode = Fn( () => {
- const refractionVector = refract( positionViewDirection.negate(), normalView, div( 1.0, duck.material.ior ) ).normalize();
- const viewZ = normalView.z.pow( causticOcclusion );
- const textureUV = refractionVector.xy.mul( .6 );
- const causticColor = uniform( duck.material.color );
- const chromaticAberrationOffset = normalView.z.pow( - .9 ).mul( .004 );
- const causticProjection = vec3(
- texture( causticMap, textureUV.add( vec2( chromaticAberrationOffset.x.negate(), 0 ) ) ).r,
- texture( causticMap, textureUV.add( vec2( 0, chromaticAberrationOffset.y.negate() ) ) ).g,
- texture( causticMap, textureUV.add( vec2( chromaticAberrationOffset.x, chromaticAberrationOffset.y ) ) ).b
- );
- return causticProjection.mul( viewZ.mul( 25 ) ).add( viewZ ).mul( causticColor );
- } )();
- //
- const textureLoader = new THREE.TextureLoader();
- // glass
- const colorMap = textureLoader.load( 'textures/colors.png' );
- colorMap.wrapS = colorMap.wrapT = THREE.RepeatWrapping;
- colorMap.colorSpace = THREE.SRGBColorSpace;
- const glassMaterial = new THREE.MeshPhysicalNodeMaterial();
- glassMaterial.map = colorMap;
- glassMaterial.side = THREE.DoubleSide;
- glassMaterial.transparent = true;
- glassMaterial.color = new THREE.Color( 0xffffff );
- glassMaterial.transmission = 1;
- glassMaterial.ior = 1.5;
- glassMaterial.metalness = 0;
- glassMaterial.roughness = .1;
- glassMaterial.castShadowNode = vec4( texture( colorMap ).rgb, .8 );
- const glass = new THREE.Mesh( new THREE.PlaneGeometry( .2, .2 ), glassMaterial );
- glass.position.y = .1;
- glass.castShadow = true;
- glass.visible = false;
- scene.add( glass );
- // gui
- const gui = new GUI();
- gui.add( causticOcclusion, 'value', 0, 20 ).name( 'caustic occlusion' );
- gui.addColor( duck.material, 'color' ).name( 'material color' );
- gui.add( { model: 'duck' }, 'model', [
- 'duck',
- 'glass'
- ] ).onChange( model => {
- duck.visible = glass.visible = false;
- if ( model === 'duck' ) {
- duck.visible = true;
- } else if ( model === 'glass' ) {
- glass.visible = true;
- }
- } );
- // ground
- const map = textureLoader.load( 'textures/hardwood2_diffuse.jpg' );
- map.wrapS = map.wrapT = THREE.RepeatWrapping;
- map.repeat.set( 10, 10 );
- const geometry = new THREE.PlaneGeometry( 2, 2 );
- const material = new THREE.MeshStandardMaterial( { color: 0x999999, map } );
- const ground = new THREE.Mesh( geometry, material );
- ground.rotation.x = - Math.PI / 2;
- ground.receiveShadow = true;
- scene.add( ground );
- // renderer
- renderer = new THREE.WebGPURenderer( { antialias: true } );
- renderer.shadowMap.enabled = true;
- renderer.setPixelRatio( window.devicePixelRatio );
- renderer.setSize( window.innerWidth, window.innerHeight );
- renderer.setAnimationLoop( animate );
- document.body.appendChild( renderer.domElement );
- // stats
- stats = new Stats();
- document.body.appendChild( stats.dom );
- // controls
- controls = new OrbitControls( camera, renderer.domElement );
- controls.maxDistance = 3;
- controls.maxPolarAngle = Math.PI / 2;
- window.addEventListener( 'resize', onWindowResize );
- }
- function onWindowResize() {
- camera.aspect = window.innerWidth / window.innerHeight;
- camera.updateProjectionMatrix();
- renderer.setSize( window.innerWidth, window.innerHeight );
- }
- function animate() {
- stats.update();
- for ( const mesh of gltf.children ) {
- mesh.rotation.y -= .01;
- }
- controls.update();
- renderer.render( scene, camera );
- }
- </script>
- </body>
- </html>
|