123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260 |
- <!DOCTYPE html>
- <html lang="en">
- <head>
- <title>three.js webgl - lights - rect 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.RectAreaLight<br/>
- by <a href="http://github.com/abelnation" target="_blank" rel="noopener">abelnation</a>
- </div>
- <script type="module">
- import * as THREE from '../build/three.module.js';
- import Stats from './jsm/libs/stats.module.js';
- import { GUI } from './jsm/libs/dat.gui.module.js';
- import { OrbitControls } from './jsm/controls/OrbitControls.js';
- import { RectAreaLightUniformsLib } from './jsm/lights/RectAreaLightUniformsLib.js';
- var renderer, scene, camera;
- var origin = new THREE.Vector3();
- var rectLight;
- var param = {};
- var stats;
- init();
- animate();
- function init() {
- renderer = new THREE.WebGLRenderer( { antialias: true } );
- renderer.setPixelRatio( window.devicePixelRatio );
- renderer.setSize( window.innerWidth, window.innerHeight );
- renderer.shadowMap.enabled = true;
- renderer.shadowMap.type = THREE.PCFSoftShadowMap;
- renderer.outputEncoding = THREE.sRGBEncoding;
- document.body.appendChild( renderer.domElement );
- // Check for float-RT support
- // TODO (abelnation): figure out fall-back for float textures
- if ( ! renderer.extensions.get( 'OES_texture_float' ) ) {
- alert( 'OES_texture_float not supported' );
- throw 'missing webgl extension';
- }
- if ( ! renderer.extensions.get( 'OES_texture_float_linear' ) ) {
- alert( 'OES_texture_float_linear not supported' );
- throw 'missing webgl extension';
- }
- camera = new THREE.PerspectiveCamera( 45, window.innerWidth / window.innerHeight, 1, 1000 );
- camera.position.set( 0, 20, 35 );
- scene = new THREE.Scene();
- var ambient = new THREE.AmbientLight( 0xffffff, 0.1 );
- scene.add( ambient );
- RectAreaLightUniformsLib.init();
- rectLight = new THREE.RectAreaLight( 0xffffff, 1, 10, 10 );
- rectLight.position.set( 5, 5, 0 );
- scene.add( rectLight );
- var rectLightMesh = new THREE.Mesh( new THREE.PlaneBufferGeometry(), new THREE.MeshBasicMaterial( { side: THREE.BackSide } ) );
- rectLightMesh.scale.x = rectLight.width;
- rectLightMesh.scale.y = rectLight.height;
- rectLight.add( rectLightMesh );
- var rectLightMeshBack = new THREE.Mesh( new THREE.PlaneBufferGeometry(), new THREE.MeshBasicMaterial( { color: 0x080808 } ) );
- rectLightMesh.add( rectLightMeshBack );
- var geoFloor = new THREE.BoxBufferGeometry( 2000, 0.1, 2000 );
- var matStdFloor = new THREE.MeshStandardMaterial( { color: 0x808080, roughness: 0, metalness: 0 } );
- var mshStdFloor = new THREE.Mesh( geoFloor, matStdFloor );
- scene.add( mshStdFloor );
- var matStdObjects = new THREE.MeshStandardMaterial( { color: 0xA00000, roughness: 0, metalness: 0 } );
- var geoBox = new THREE.BoxBufferGeometry( Math.PI, Math.sqrt( 2 ), Math.E );
- var mshStdBox = new THREE.Mesh( geoBox, matStdObjects );
- mshStdBox.position.set( 0, 5, 0 );
- mshStdBox.rotation.set( 0, Math.PI / 2.0, 0 );
- mshStdBox.castShadow = true;
- mshStdBox.receiveShadow = true;
- scene.add( mshStdBox );
- var geoSphere = new THREE.SphereBufferGeometry( 1.5, 32, 32 );
- var mshStdSphere = new THREE.Mesh( geoSphere, matStdObjects );
- mshStdSphere.position.set( - 5, 5, 0 );
- mshStdSphere.castShadow = true;
- mshStdSphere.receiveShadow = true;
- scene.add( mshStdSphere );
- var geoKnot = new THREE.TorusKnotBufferGeometry( 1.5, 0.5, 100, 16 );
- var mshStdKnot = new THREE.Mesh( geoKnot, matStdObjects );
- mshStdKnot.position.set( 5, 5, 0 );
- mshStdKnot.castShadow = true;
- mshStdKnot.receiveShadow = true;
- scene.add( mshStdKnot );
- var controls = new OrbitControls( camera, renderer.domElement );
- controls.target.copy( mshStdBox.position );
- controls.update();
- // GUI
- var gui = new GUI( { width: 300 } );
- gui.open();
- param = {
- motion: true,
- width: rectLight.width,
- height: rectLight.height,
- color: rectLight.color.getHex(),
- intensity: rectLight.intensity,
- 'ambient': ambient.intensity,
- 'floor color': matStdFloor.color.getHex(),
- 'object color': matStdObjects.color.getHex(),
- 'roughness': matStdFloor.roughness,
- 'metalness': matStdFloor.metalness
- };
- gui.add( param, 'motion' );
- var lightFolder = gui.addFolder( 'Light' );
- lightFolder.add( param, 'width', 1, 20 ).step( 0.1 ).onChange( function ( val ) {
- rectLight.width = val;
- rectLightMesh.scale.x = val;
- } );
- lightFolder.add( param, 'height', 1, 20 ).step( 0.1 ).onChange( function ( val ) {
- rectLight.height = val;
- rectLightMesh.scale.y = val;
- } );
- lightFolder.addColor( param, 'color' ).onChange( function ( val ) {
- rectLight.color.setHex( val );
- rectLightMesh.material.color.copy( rectLight.color ).multiplyScalar( rectLight.intensity );
- } );
- lightFolder.add( param, 'intensity', 0.0, 4.0 ).step( 0.01 ).onChange( function ( val ) {
- rectLight.intensity = val;
- rectLightMesh.material.color.copy( rectLight.color ).multiplyScalar( rectLight.intensity );
- } );
- lightFolder.add( param, 'ambient', 0.0, 0.2 ).step( 0.01 ).onChange( function ( val ) {
- ambient.intensity = val;
- } );
- lightFolder.open();
- var standardFolder = gui.addFolder( 'Standard Material' );
- standardFolder.addColor( param, 'floor color' ).onChange( function ( val ) {
- matStdFloor.color.setHex( val );
- } );
- standardFolder.addColor( param, 'object color' ).onChange( function ( val ) {
- matStdObjects.color.setHex( val );
- } );
- standardFolder.add( param, 'roughness', 0.0, 1.0 ).step( 0.01 ).onChange( function ( val ) {
- matStdObjects.roughness = val;
- matStdFloor.roughness = val;
- } );
- // TODO (abelnation): use env map to reflect metal property
- standardFolder.add( param, 'metalness', 0.0, 1.0 ).step( 0.01 ).onChange( function ( val ) {
- matStdObjects.metalness = val;
- matStdFloor.metalness = val;
- } );
- standardFolder.open();
- // TODO: rect area light distance
- // TODO: rect area light decay
- //
- window.addEventListener( 'resize', onResize, false );
- stats = new Stats();
- document.body.appendChild( stats.dom );
- }
- function onResize() {
- renderer.setSize( window.innerWidth, window.innerHeight );
- camera.aspect = ( window.innerWidth / window.innerHeight );
- camera.updateProjectionMatrix();
- }
- function animate() {
- requestAnimationFrame( animate );
- if ( param.motion ) {
- var t = ( Date.now() / 2000 );
- // move light in circle around center
- // change light height with sine curve
- var r = 15.0;
- var lx = r * Math.cos( t );
- var lz = r * Math.sin( t );
- var ly = 5.0 + 5.0 * Math.sin( t / 3.0 );
- rectLight.position.set( lx, ly, lz );
- rectLight.lookAt( origin );
- }
- renderer.render( scene, camera );
- stats.update();
- }
- </script>
- </body>
- </html>
|