| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372 |
- <!DOCTYPE html>
- <html lang="en">
- <head>
- <title>three.js webgpu - materials - retroreflective</title>
- <meta charset="utf-8">
- <meta name="viewport" content="width=device-width, user-scalable=no, minimum-scale=1.0, maximum-scale=1.0">
- <meta property="og:title" content="three.js webgpu - materials - retroreflective">
- <meta property="og:type" content="website">
- <meta property="og:url" content="https://threejs.org/examples/webgpu_materials_retroreflective.html">
- <link type="text/css" rel="stylesheet" href="example.css">
- </head>
- <body>
- <div id="info">
- <a href="https://threejs.org/" target="_blank" rel="noopener" class="logo-link"></a>
- <div class="title-wrapper">
- <a href="https://threejs.org/" target="_blank" rel="noopener">three.js</a><span>Retroreflective Materials</span>
- </div>
- <small>
- Traffic cones with retroreflective bands and a camera-mounted light demonstrating the <a href="https://jcgt.org/published/0015/01/04/" target="_blank" rel="noopener">Minimal Retroreflective Microfacet Model</a>. By <a href="https://ben3d.ca" target="_blank" rel="noopener">Ben Houston</a>.
- </small>
- </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/webgpu';
- import { color, mix, mx_noise_float, pass, positionLocal, positionWorld, materialColor, materialRoughness, reflector, transformNormalToView, vec2, vec3 } from 'three/tsl';
- import { bloom } from 'three/addons/tsl/display/BloomNode.js';
- import { hashBlur } from 'three/addons/tsl/display/hashBlur.js';
- import { Inspector } from 'three/addons/inspector/Inspector.js';
- import { OrbitControls } from 'three/addons/controls/OrbitControls.js';
- import WebGPU from 'three/addons/capabilities/WebGPU.js';
- let camera, scene, renderer, renderPipeline, controls;
- let frontLight, reflectedFlashLight, bandMaterial;
- const params = {
- enabled: true,
- amount: 1.0,
- flashLight: 5.0
- };
- init();
- async function init() {
- if ( WebGPU.isAvailable() === false ) {
- document.body.appendChild( WebGPU.getErrorMessage() );
- throw new Error( 'No WebGPU support' );
- }
- const container = document.createElement( 'div' );
- document.body.appendChild( container );
- camera = new THREE.PerspectiveCamera( 45, window.innerWidth / window.innerHeight, 0.1, 100 );
- camera.position.set( 2.3, 1.32, 3.25 );
- scene = new THREE.Scene();
- scene.background = new THREE.Color( 0x0f131a );
- scene.fog = new THREE.Fog( 0x0f131a, 4, 18 );
- scene.add( camera );
- // camera-mounted LED flashlight (~6500K = sRGB white)
- frontLight = new THREE.PointLight( 0xffffff, params.flashLight, 12 );
- camera.add( frontLight );
- // the flashlight's mirror image below the ground: it stands in for the
- // puddle-bounced beam, so the retro bands also light up in the reflection
- reflectedFlashLight = new THREE.PointLight( 0xffffff, params.flashLight * 0.35, 12 );
- scene.add( reflectedFlashLight );
- createFloor();
- createCones();
- renderer = new THREE.WebGPURenderer( { antialias: true } );
- renderer.toneMapping = THREE.ACESFilmicToneMapping;
- renderer.setPixelRatio( window.devicePixelRatio );
- renderer.setSize( window.innerWidth, window.innerHeight );
- renderer.setAnimationLoop( render );
- renderer.inspector = new Inspector();
- container.appendChild( renderer.domElement );
- await renderer.init();
- const pmremGenerator = new THREE.PMREMGenerator( renderer );
- scene.environment = pmremGenerator.fromScene( createNightEnvironment(), 0.02 ).texture;
- renderPipeline = new THREE.RenderPipeline( renderer );
- const scenePass = pass( scene, camera );
- const scenePassColor = scenePass.getTextureNode();
- renderPipeline.outputNode = scenePassColor.add( bloom( scenePassColor, 0.15, 0, 0 ) );
- controls = new OrbitControls( camera, renderer.domElement );
- controls.target.set( 0, 0.33, 0 );
- controls.maxPolarAngle = Math.PI / 2 - 0.05; // keep the camera above the ground
- controls.enableDamping = true;
- controls.update();
- const gui = renderer.inspector.createParameters( 'Settings' );
- gui.add( params, 'enabled' ).onChange( updateMaterial );
- gui.add( params, 'amount', 0, 1, 0.01 ).onChange( updateMaterial );
- gui.add( params, 'flashLight', 0, 30, 0.1 ).name( 'flash light' ).onChange( ( value ) => frontLight.intensity = value );
- window.addEventListener( 'resize', onWindowResize );
- updateMaterial();
- }
- function createNightEnvironment() {
- const environment = new THREE.Scene();
- // sky: faint city glow at the horizon fading to a dark zenith
- const skyMaterial = new THREE.MeshBasicNodeMaterial( { side: THREE.BackSide } );
- skyMaterial.colorNode = mix( color( 0x05070c ), color( 0x2a3148 ), positionLocal.normalize().y.abs().oneMinus().pow( 4 ) );
- environment.add( new THREE.Mesh( new THREE.SphereGeometry( 10 ), skyMaterial ) );
- // distant buildings with sparse lit windows
- const buildingMaterial = new THREE.MeshBasicNodeMaterial();
- const cell = vec2( positionWorld.x.add( positionWorld.z.mul( 1.7 ) ).mul( 3 ), positionWorld.y.mul( 2.5 ) );
- const cellUV = cell.fract();
- const window_ = cellUV.x.smoothstep( 0.15, 0.3 ).mul( cellUV.x.smoothstep( 0.7, 0.85 ).oneMinus() ).mul( cellUV.y.smoothstep( 0.2, 0.35 ) ).mul( cellUV.y.smoothstep( 0.65, 0.8 ).oneMinus() );
- const lit = mx_noise_float( cell.floor().mul( 0.731 ).add( 0.37 ) ).smoothstep( 0.0, 0.1 );
- const windowColor = mix( color( 0x88a8ff ), color( 0xffc16b ), mx_noise_float( cell.floor().mul( 0.317 ) ).add( 1 ).mul( 0.5 ) );
- buildingMaterial.colorNode = mix( color( 0x05060a ), windowColor.mul( 12 ), window_.mul( lit ) );
- for ( let i = 0; i < 8; i ++ ) {
- const angle = ( i / 8 ) * Math.PI * 2 + 0.35;
- const height = 2.5 + ( ( i * 2.7 ) % 3.5 );
- const building = new THREE.Mesh( new THREE.BoxGeometry( 2, height, 1 ), buildingMaterial );
- building.position.set( Math.cos( angle ) * 9, height / 2 - 0.5, Math.sin( angle ) * 9 );
- building.lookAt( 0, height / 2 - 0.5, 0 );
- environment.add( building );
- }
- // street lamps: sodium and LED
- const warmLampMaterial = new THREE.MeshBasicMaterial();
- warmLampMaterial.color.setRGB( 18, 11, 4 );
- const coolLampMaterial = new THREE.MeshBasicMaterial();
- coolLampMaterial.color.setRGB( 7, 9, 13 );
- for ( let i = 0; i < 6; i ++ ) {
- const angle = ( i / 6 ) * Math.PI * 2 + 0.7;
- const lamp = new THREE.Mesh( new THREE.SphereGeometry( 0.35 ), i % 3 === 2 ? coolLampMaterial : warmLampMaterial );
- lamp.position.set( Math.cos( angle ) * 8, 2.5 + ( i % 3 ), Math.sin( angle ) * 8 );
- environment.add( lamp );
- }
- // moon
- const moonMaterial = new THREE.MeshBasicMaterial();
- moonMaterial.color.setRGB( 2.5, 3, 4 );
- const moon = new THREE.Mesh( new THREE.SphereGeometry( 0.6 ), moonMaterial );
- moon.position.set( 4, 7, - 5 );
- environment.add( moon );
- return environment;
- }
- function createFloor() {
- const material = new THREE.MeshStandardNodeMaterial( {
- color: 0x1b1e23,
- roughness: 0.6
- } );
- const reflection = reflector( { resolutionScale: 0.5 } );
- reflection.target.rotateX( - Math.PI / 2 );
- scene.add( reflection.target );
- const position = positionWorld.xz;
- // fade procedural detail as its period approaches the pixel footprint,
- // which grows anisotropically at grazing angles and with distance
- const bandlimit = ( p ) => p.fwidth().length().smoothstep( 0.5, 1.5 ).oneMinus();
- // asphalt: tonal grain and broad worn patches
- const grain = mx_noise_float( position.mul( 60 ) ).mul( bandlimit( position.mul( 60 ) ) ).mul( 0.2 );
- const patches = mx_noise_float( position.mul( 1.5 ) ).mul( 0.08 );
- const asphalt = materialColor.mul( grain.add( patches ).add( 1 ) );
- // puddles: flat, dark and mirror-like
- const puddle = mx_noise_float( position.mul( 0.45 ).add( 6.27 ) ).smoothstep( 0.25, 0.45 );
- // bump: heightfield normal from finite differences of a fine noise, flattened in puddles
- const bumpNoise = ( p ) => mx_noise_float( p.mul( 180 ) );
- const e = 0.002;
- const strength = puddle.oneMinus().mul( bandlimit( position.mul( 180 ) ) ).mul( 0.0015 );
- const h = bumpNoise( position );
- const hx = bumpNoise( position.add( vec2( e, 0 ) ) );
- const hz = bumpNoise( position.add( vec2( 0, e ) ) );
- const bump = vec3( h.sub( hx ).mul( strength ), hz.sub( h ).mul( strength ), e ).normalize();
- material.normalNode = transformNormalToView( bump );
- // the surface relief wobbles the reflection; puddles stay calm
- reflection.uvNode = reflection.uvNode.add( bump.xy.mul( 0.03 ) );
- material.colorNode = mix( asphalt, asphalt.mul( 0.3 ), puddle );
- material.roughnessNode = mix( mx_noise_float( position.mul( 50 ) ).mul( bandlimit( position.mul( 50 ) ) ).mul( 0.15 ).add( materialRoughness ), 0.05, puddle );
- // rain-soaked: the whole surface reflects, streaky on wet asphalt and sharpest in the puddles
- const wetness = mix( 0.15, 0.7, puddle );
- const reflectionBlur = mix( 0.08, 0.005, puddle );
- material.emissiveNode = hashBlur( reflection, reflectionBlur, { repeats: 32 } ).rgb.mul( wetness );
- const floor = new THREE.Mesh( new THREE.PlaneGeometry( 40, 40 ), material );
- floor.rotation.x = - Math.PI / 2;
- scene.add( floor );
- }
- function createCones() {
- const orangeMaterial = new THREE.MeshPhysicalNodeMaterial( {
- color: 0xff6a00,
- roughness: 0.45
- } );
- // worn plastic: uncorrelated noise on roughness and tone
- orangeMaterial.roughnessNode = mx_noise_float( positionLocal.mul( 30 ) ).mul( 0.2 ).add( materialRoughness );
- orangeMaterial.colorNode = materialColor.mul( mx_noise_float( positionLocal.add( 7.3 ).mul( 20 ) ).mul( 0.1 ).add( 1 ) );
- bandMaterial = new THREE.MeshPhysicalNodeMaterial( {
- color: 0xf7f0d6,
- roughness: 0.22,
- retroreflective: params.amount
- } );
- const count = 5;
- const radius = 1.1;
- for ( let i = 0; i < count; i ++ ) {
- const cone = createCone( orangeMaterial, bandMaterial );
- const angle = ( i / count ) * Math.PI * 2;
- cone.position.set( Math.cos( angle ) * radius, 0, Math.sin( angle ) * radius );
- scene.add( cone );
- }
- }
- function createCone( orangeMaterial, bandMaterial ) {
- const cone = new THREE.Group();
- const base = new THREE.Mesh(
- createBaseGeometry( 0.42, 0.04, 0.08 ),
- orangeMaterial
- );
- cone.add( base );
- const bodyHeight = 0.71;
- const bodyBottom = 0.04;
- const bodyRadiusBottom = 0.17;
- const bodyRadiusTop = 0.035;
- const body = new THREE.Mesh(
- new THREE.CylinderGeometry( bodyRadiusTop, bodyRadiusBottom, bodyHeight, 96, 1, false ),
- orangeMaterial
- );
- body.position.y = bodyBottom + bodyHeight * 0.5;
- cone.add( body );
- const bands = [
- { center: bodyBottom + bodyHeight * 0.6, height: 0.10 },
- { center: bodyBottom + bodyHeight * 0.4, height: 0.06 }
- ];
- for ( const { center, height } of bands ) {
- const bandRadiusBottom = radiusAtHeight( center - height * 0.5, bodyBottom, bodyHeight, bodyRadiusBottom, bodyRadiusTop ) * 1.01;
- const bandRadiusTop = radiusAtHeight( center + height * 0.5, bodyBottom, bodyHeight, bodyRadiusBottom, bodyRadiusTop ) * 1.01;
- const band = new THREE.Mesh(
- new THREE.CylinderGeometry( bandRadiusTop, bandRadiusBottom, height, 96, 1, true ),
- bandMaterial
- );
- band.position.y = center;
- cone.add( band );
- }
- return cone;
- }
- function radiusAtHeight( y, bodyBottom, bodyHeight, radiusBottom, radiusTop ) {
- const t = THREE.MathUtils.clamp( ( y - bodyBottom ) / bodyHeight, 0, 1 );
- return THREE.MathUtils.lerp( radiusBottom, radiusTop, t );
- }
- function createBaseGeometry( size, thickness, radius ) {
- const half = size / 2;
- const shape = new THREE.Shape();
- shape.moveTo( - half + radius, - half );
- shape.lineTo( half - radius, - half );
- shape.absarc( half - radius, - half + radius, radius, - Math.PI / 2, 0 );
- shape.lineTo( half, half - radius );
- shape.absarc( half - radius, half - radius, radius, 0, Math.PI / 2 );
- shape.lineTo( - half + radius, half );
- shape.absarc( - half + radius, half - radius, radius, Math.PI / 2, Math.PI );
- shape.lineTo( - half, - half + radius );
- shape.absarc( - half + radius, - half + radius, radius, Math.PI, Math.PI * 1.5 );
- const geometry = new THREE.ExtrudeGeometry( shape, { depth: thickness, bevelEnabled: false } );
- geometry.rotateX( - Math.PI / 2 );
- return geometry;
- }
- function updateMaterial() {
- bandMaterial.retroreflective = params.enabled ? params.amount : 0;
- }
- function onWindowResize() {
- camera.aspect = window.innerWidth / window.innerHeight;
- camera.updateProjectionMatrix();
- renderer.setSize( window.innerWidth, window.innerHeight );
- }
- function render() {
- controls.update();
- reflectedFlashLight.position.set( camera.position.x, - camera.position.y, camera.position.z );
- reflectedFlashLight.intensity = frontLight.intensity * 0.35;
- renderPipeline.render();
- }
- </script>
- </body>
- </html>
|