| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238 |
- <!DOCTYPE html>
- <html lang="en">
- <head>
- <title>three.js webgl - light probe volume</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</a> - light probe volume<br/>
- Position-dependent diffuse global illumination via L1 SH probe grid
- </div>
- <script type="importmap">
- {
- "imports": {
- "three": "../build/three.module.js",
- "three/addons/": "./jsm/"
- }
- }
- </script>
- <script type="module">
- import * as THREE from 'three';
- import { OrbitControls } from 'three/addons/controls/OrbitControls.js';
- import { GUI } from 'three/addons/libs/lil-gui.module.min.js';
- import { LightProbeGrid } from 'three/addons/lighting/LightProbeGrid.js';
- import { LightProbeGridHelper } from 'three/addons/helpers/LightProbeGridHelper.js';
- let camera, scene, renderer, controls;
- let probes, probesHelper;
- init();
- async function init() {
- // Camera
- camera = new THREE.PerspectiveCamera( 50, window.innerWidth / window.innerHeight, 0.1, 100 );
- camera.position.set( 0, 2.5, 8 );
- // Scene
- scene = new THREE.Scene();
- scene.background = new THREE.Color( 0x111111 );
- // Cornell box
- const wallMaterial = new THREE.MeshStandardMaterial( { color: 0xcccccc } );
- const redMaterial = new THREE.MeshStandardMaterial( { color: 0xff0000 } );
- const greenMaterial = new THREE.MeshStandardMaterial( { color: 0x00ff00 } );
- // Floor
- const floor = new THREE.Mesh( new THREE.PlaneGeometry( 6, 6 ), wallMaterial );
- floor.rotation.x = - Math.PI / 2;
- floor.receiveShadow = true;
- scene.add( floor );
- // Ceiling
- const ceiling = new THREE.Mesh( new THREE.PlaneGeometry( 6, 6 ), wallMaterial );
- ceiling.rotation.x = Math.PI / 2;
- ceiling.position.y = 5;
- ceiling.receiveShadow = true;
- scene.add( ceiling );
- // Back wall
- const backWall = new THREE.Mesh( new THREE.PlaneGeometry( 6, 5 ), wallMaterial );
- backWall.position.set( 0, 2.5, - 3 );
- backWall.receiveShadow = true;
- scene.add( backWall );
- // Front wall
- const frontWall = new THREE.Mesh( new THREE.PlaneGeometry( 6, 5 ), wallMaterial );
- frontWall.rotation.y = Math.PI;
- frontWall.position.set( 0, 2.5, 3 );
- frontWall.receiveShadow = true;
- scene.add( frontWall );
- // Left wall (red)
- const leftWall = new THREE.Mesh( new THREE.PlaneGeometry( 6, 5 ), redMaterial );
- leftWall.rotation.y = Math.PI / 2;
- leftWall.position.set( - 3, 2.5, 0 );
- leftWall.receiveShadow = true;
- scene.add( leftWall );
- // Right wall (green)
- const rightWall = new THREE.Mesh( new THREE.PlaneGeometry( 6, 5 ), greenMaterial );
- rightWall.rotation.y = - Math.PI / 2;
- rightWall.position.set( 3, 2.5, 0 );
- rightWall.receiveShadow = true;
- scene.add( rightWall );
- // Objects inside the box
- const objectMaterial = new THREE.MeshStandardMaterial( { color: 0xeeeeee } );
- // Tall box
- const tallBox = new THREE.Mesh( new THREE.BoxGeometry( 1.2, 2.5, 1.2 ), objectMaterial );
- tallBox.position.set( - 0.8, 1.25, - 0.8 );
- tallBox.rotation.y = Math.PI / 8;
- tallBox.castShadow = true;
- tallBox.receiveShadow = true;
- scene.add( tallBox );
- // Short box
- const shortBox = new THREE.Mesh( new THREE.BoxGeometry( 1.2, 1.2, 1.2 ), objectMaterial );
- shortBox.position.set( 1, 0.6, 0.5 );
- shortBox.rotation.y = - Math.PI / 6;
- shortBox.castShadow = true;
- shortBox.receiveShadow = true;
- scene.add( shortBox );
- // Sphere (to show smooth GI variation)
- const sphere = new THREE.Mesh( new THREE.SphereGeometry( 0.5, 32, 32 ), objectMaterial.clone() );
- sphere.position.set( 1, 1.9, 0.5 );
- sphere.castShadow = true;
- sphere.receiveShadow = true;
- scene.add( sphere );
- // Light
- const light = new THREE.PointLight( 0xffffff, 40 );
- light.position.set( 0, 4.5, 0 );
- light.castShadow = true;
- light.shadow.mapSize.setScalar( 256 );
- light.shadow.radius = 10;
- light.shadow.normalBias = - 0.02;
- scene.add( light );
- // Dim ambient to see GI effect better
- const ambient = new THREE.AmbientLight( 0xffffff, 0.05 );
- scene.add( ambient );
- // Renderer
- renderer = new THREE.WebGLRenderer( { antialias: true } );
- renderer.setPixelRatio( window.devicePixelRatio );
- renderer.setSize( window.innerWidth, window.innerHeight );
- renderer.setAnimationLoop( animate );
- renderer.shadowMap.enabled = true;
- renderer.toneMapping = THREE.ACESFilmicToneMapping;
- renderer.toneMappingExposure = 1.0;
- document.body.appendChild( renderer.domElement );
- // Controls
- controls = new OrbitControls( camera, renderer.domElement );
- controls.target.set( 0, 2.5, 0 );
- controls.update();
- // Bake light probe volume
- async function bakeWithResolution( resolution ) {
- if ( probes ) {
- scene.remove( probes );
- probes.dispose();
- }
- probes = new LightProbeGrid( 5.6, 4.7, 5.6, resolution, resolution, resolution );
- probes.position.set( 0, 2.45, 0 );
- probes.bake( renderer, scene, { cubemapSize: 32, near: 0.05, far: 20 } );
- probes.visible = params.enabled;
- scene.add( probes );
- // Update debug visualization
- if ( ! probesHelper ) {
- probesHelper = new LightProbeGridHelper( probes );
- probesHelper.visible = params.showProbes;
- scene.add( probesHelper );
- } else {
- probesHelper.probes = probes;
- probesHelper.update();
- }
- }
- const params = {
- enabled: true,
- showProbes: false,
- resolution: 6
- };
- await bakeWithResolution( params.resolution );
- // GUI
- const gui = new GUI();
- gui.add( params, 'enabled' ).name( 'GI' ).onChange( ( value ) => {
- probes.visible = value;
- } );
- gui.add( params, 'resolution', 2, 12, 1 ).name( 'Resolution' ).onFinishChange( ( value ) => {
- bakeWithResolution( value );
- } );
- gui.add( params, 'showProbes' ).name( 'Show Probes' ).onChange( ( value ) => {
- probesHelper.visible = value;
- } );
- //
- window.addEventListener( 'resize', onWindowResize );
- }
- function onWindowResize() {
- camera.aspect = window.innerWidth / window.innerHeight;
- camera.updateProjectionMatrix();
- renderer.setSize( window.innerWidth, window.innerHeight );
- }
- function animate() {
- renderer.render( scene, camera );
- }
- </script>
- </body>
- </html>
|