| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192 |
- <!DOCTYPE html>
- <html lang="en">
- <head>
- <title>three.js webgl - lights - circle area light</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> - THREE.CircleAreaLight with Shadows<br/>
- CircleAreaLight approximates circular light sources using LTC and PCSS
- </div>
- <script type="importmap">
- {
- "imports": {
- "three": "../build/three.module.js",
- "three/addons/": "./jsm/"
- }
- }
- </script>
- <script type="module">
- import * as THREE from 'three';
- import Stats from 'three/addons/libs/stats.module.js';
- import { GUI } from 'three/addons/libs/lil-gui.module.min.js';
- import { OrbitControls } from 'three/addons/controls/OrbitControls.js';
- import { CircleAreaLightHelper } from 'three/addons/helpers/CircleAreaLightHelper.js';
- import { RectAreaLightUniformsLib } from 'three/addons/lights/RectAreaLightUniformsLib.js';
- let renderer, scene, camera;
- let stats, meshKnot, meshFloor;
- let circleLight1, circleLight2, circleLight3;
- let animateLights = true;
- init();
- function init() {
- renderer = new THREE.WebGLRenderer( { antialias: true } );
- renderer.setPixelRatio( window.devicePixelRatio );
- renderer.setSize( window.innerWidth, window.innerHeight );
- renderer.setAnimationLoop( animation );
- renderer.shadowMap.enabled = true;
- renderer.shadowMap.type = THREE.PCFSoftShadowMap;
- document.body.appendChild( renderer.domElement );
- camera = new THREE.PerspectiveCamera( 45, window.innerWidth / window.innerHeight, 1, 1000 );
- camera.position.set( 0, 5, - 15 );
- scene = new THREE.Scene();
- // CircleAreaLight uses the same LTC textures as RectAreaLight
- RectAreaLightUniformsLib.init();
- circleLight1 = new THREE.CircleAreaLight( 0xff0000, 5, 4 );
- circleLight1.position.set( - 5, 5, 5 );
- circleLight1.lookAt( 0, 0, 0 );
- circleLight1.castShadow = true;
- circleLight1.shadow.mapSize.width = 2048;
- circleLight1.shadow.mapSize.height = 2048;
- circleLight1.shadow.bias = - 0.001;
- scene.add( circleLight1 );
- circleLight2 = new THREE.CircleAreaLight( 0x00ff00, 5, 4 );
- circleLight2.position.set( 0, 5, 5 );
- circleLight2.lookAt( 0, 0, 0 );
- circleLight2.castShadow = true;
- circleLight2.shadow.mapSize.width = 2048;
- circleLight2.shadow.mapSize.height = 2048;
- circleLight2.shadow.bias = - 0.001;
- scene.add( circleLight2 );
- circleLight3 = new THREE.CircleAreaLight( 0x0000ff, 5, 4 );
- circleLight3.position.set( 5, 5, 5 );
- circleLight3.lookAt( 0, 0, 0 );
- circleLight3.castShadow = true;
- circleLight3.shadow.mapSize.width = 2048;
- circleLight3.shadow.mapSize.height = 2048;
- circleLight3.shadow.bias = - 0.001;
- scene.add( circleLight3 );
- scene.add( new CircleAreaLightHelper( circleLight1 ) );
- scene.add( new CircleAreaLightHelper( circleLight2 ) );
- scene.add( new CircleAreaLightHelper( circleLight3 ) );
- const geoFloor = new THREE.BoxGeometry( 2000, 0.1, 2000 );
- const matStdFloor = new THREE.MeshStandardMaterial( { color: 0xbcbcbc, roughness: 0.1, metalness: 0 } );
- meshFloor = new THREE.Mesh( geoFloor, matStdFloor );
- meshFloor.receiveShadow = true;
- scene.add( meshFloor );
- const geoKnot = new THREE.TorusKnotGeometry( 1.5, 0.5, 200, 16 );
- const matKnot = new THREE.MeshStandardMaterial( { color: 0xffffff, roughness: 0, metalness: 0 } );
- meshKnot = new THREE.Mesh( geoKnot, matKnot );
- meshKnot.position.set( 0, 5, 0 );
- meshKnot.castShadow = true;
- meshKnot.receiveShadow = true;
- scene.add( meshKnot );
- const controls = new OrbitControls( camera, renderer.domElement );
- controls.target.copy( meshKnot.position );
- controls.update();
- //
- window.addEventListener( 'resize', onWindowResize );
- stats = new Stats();
- document.body.appendChild( stats.dom );
- // GUI
- const gui = new GUI();
- const params = {
- animateLights: animateLights
- };
- gui.add( params, 'animateLights' ).name( 'Animate Lights' ).onChange( ( value ) => {
- animateLights = value;
- } );
- const knotFolder = gui.addFolder( 'Torus Knot Material' );
- knotFolder.add( meshKnot.material, 'roughness', 0, 1, 0.01 ).name( 'Roughness' );
- knotFolder.add( meshKnot.material, 'metalness', 0, 1, 0.01 ).name( 'Metalness' );
- knotFolder.open();
- const floorFolder = gui.addFolder( 'Floor Material' );
- floorFolder.add( meshFloor.material, 'roughness', 0, 1, 0.01 ).name( 'Roughness' );
- floorFolder.add( meshFloor.material, 'metalness', 0, 1, 0.01 ).name( 'Metalness' );
- floorFolder.open();
- }
- function onWindowResize() {
- renderer.setSize( window.innerWidth, window.innerHeight );
- camera.aspect = ( window.innerWidth / window.innerHeight );
- camera.updateProjectionMatrix();
- }
- function animation( time ) {
- meshKnot.rotation.y = time / 1000;
- if ( animateLights ) {
- // Animate the lights in a circular pattern
- const t = time / 1000;
- const radius = 6;
- const height = 5;
- // Light 1 - Red
- circleLight1.position.x = Math.cos( t ) * radius;
- circleLight1.position.z = Math.sin( t ) * radius;
- circleLight1.position.y = height + Math.sin( t * 2 ) * 2;
- circleLight1.radius = 3 + Math.sin( t * 1.5 ) * 1.5;
- circleLight1.lookAt( 0, 0, 0 );
- // Light 2 - Green (120 degrees offset)
- circleLight2.position.x = Math.cos( t + Math.PI * 2 / 3 ) * radius;
- circleLight2.position.z = Math.sin( t + Math.PI * 2 / 3 ) * radius;
- circleLight2.position.y = height + Math.sin( ( t + Math.PI * 2 / 3 ) * 2 ) * 2;
- circleLight2.radius = 3 + Math.sin( ( t + Math.PI * 2 / 3 ) * 1.5 ) * 1.5;
- circleLight2.lookAt( 0, 0, 0 );
- // Light 3 - Blue (240 degrees offset)
- circleLight3.position.x = Math.cos( t + Math.PI * 4 / 3 ) * radius;
- circleLight3.position.z = Math.sin( t + Math.PI * 4 / 3 ) * radius;
- circleLight3.position.y = height + Math.sin( ( t + Math.PI * 4 / 3 ) * 2 ) * 2;
- circleLight3.radius = 3 + Math.sin( ( t + Math.PI * 4 / 3 ) * 1.5 ) * 1.5;
- circleLight3.lookAt( 0, 0, 0 );
- }
- renderer.render( scene, camera );
- stats.update();
- }
- </script>
- </body>
- </html>
|