123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190 |
- <!DOCTYPE html>
- <html lang="en">
- <head>
- <title>three.js webgl - lights - spotlight</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> webgl - spotlight by <a href="http://master-domain.com" target="_blank" rel="noopener">Master James</a><br />
- </div>
- <script type="module">
- import * as THREE from '../build/three.module.js';
- import { GUI } from './jsm/libs/dat.gui.module.js';
- import { OrbitControls } from './jsm/controls/OrbitControls.js';
- var renderer, scene, camera;
- var spotLight, lightHelper, shadowCameraHelper;
- var gui;
- function init() {
- renderer = new THREE.WebGLRenderer();
- renderer.setPixelRatio( window.devicePixelRatio );
- renderer.setSize( window.innerWidth, window.innerHeight );
- document.body.appendChild( renderer.domElement );
- renderer.shadowMap.enabled = true;
- renderer.shadowMap.type = THREE.PCFSoftShadowMap;
- renderer.outputEncoding = THREE.sRGBEncoding;
- scene = new THREE.Scene();
- camera = new THREE.PerspectiveCamera( 35, window.innerWidth / window.innerHeight, 1, 1000 );
- camera.position.set( 65, 8, - 10 );
- var controls = new OrbitControls( camera, renderer.domElement );
- controls.addEventListener( 'change', render );
- controls.minDistance = 20;
- controls.maxDistance = 500;
- controls.enablePan = false;
- var ambient = new THREE.AmbientLight( 0xffffff, 0.1 );
- scene.add( ambient );
- spotLight = new THREE.SpotLight( 0xffffff, 1 );
- spotLight.position.set( 15, 40, 35 );
- spotLight.angle = Math.PI / 4;
- spotLight.penumbra = 0.05;
- spotLight.decay = 2;
- spotLight.distance = 200;
- spotLight.castShadow = true;
- spotLight.shadow.mapSize.width = 1024;
- spotLight.shadow.mapSize.height = 1024;
- spotLight.shadow.camera.near = 10;
- spotLight.shadow.camera.far = 200;
- scene.add( spotLight );
- lightHelper = new THREE.SpotLightHelper( spotLight );
- scene.add( lightHelper );
- shadowCameraHelper = new THREE.CameraHelper( spotLight.shadow.camera );
- scene.add( shadowCameraHelper );
- scene.add( new THREE.AxesHelper( 10 ) );
- var material = new THREE.MeshPhongMaterial( { color: 0x808080, dithering: true } );
- var geometry = new THREE.PlaneBufferGeometry( 2000, 2000 );
- var mesh = new THREE.Mesh( geometry, material );
- mesh.position.set( 0, - 1, 0 );
- mesh.rotation.x = - Math.PI * 0.5;
- mesh.receiveShadow = true;
- scene.add( mesh );
- var material = new THREE.MeshPhongMaterial( { color: 0x4080ff, dithering: true } );
- var geometry = new THREE.BoxBufferGeometry( 3, 1, 2 );
- var mesh = new THREE.Mesh( geometry, material );
- mesh.position.set( 40, 2, 0 );
- mesh.castShadow = true;
- scene.add( mesh );
- controls.target.copy( mesh.position );
- controls.update();
- window.addEventListener( 'resize', onResize, false );
- }
- function onResize() {
- camera.aspect = window.innerWidth / window.innerHeight;
- camera.updateProjectionMatrix();
- renderer.setSize( window.innerWidth, window.innerHeight );
- }
- function render() {
- lightHelper.update();
- shadowCameraHelper.update();
- renderer.render( scene, camera );
- }
- function buildGui() {
- gui = new GUI();
- var params = {
- 'light color': spotLight.color.getHex(),
- intensity: spotLight.intensity,
- distance: spotLight.distance,
- angle: spotLight.angle,
- penumbra: spotLight.penumbra,
- decay: spotLight.decay
- };
- gui.addColor( params, 'light color' ).onChange( function ( val ) {
- spotLight.color.setHex( val );
- render();
- } );
- gui.add( params, 'intensity', 0, 2 ).onChange( function ( val ) {
- spotLight.intensity = val;
- render();
- } );
- gui.add( params, 'distance', 50, 200 ).onChange( function ( val ) {
- spotLight.distance = val;
- render();
- } );
- gui.add( params, 'angle', 0, Math.PI / 3 ).onChange( function ( val ) {
- spotLight.angle = val;
- render();
- } );
- gui.add( params, 'penumbra', 0, 1 ).onChange( function ( val ) {
- spotLight.penumbra = val;
- render();
- } );
- gui.add( params, 'decay', 1, 2 ).onChange( function ( val ) {
- spotLight.decay = val;
- render();
- } );
- gui.open();
- }
- init();
- buildGui();
- render();
- </script>
- </body>
- </html>
|