| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272 |
- <!DOCTYPE html>
- <html lang="en">
- <head>
- <title>three.js webgpu - 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="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>Spot Light</span>
- </div>
- <small>Spot light projecting texture map.</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 { Inspector } from 'three/addons/inspector/Inspector.js';
- import { PLYLoader } from 'three/addons/loaders/PLYLoader.js';
- import { OrbitControls } from 'three/addons/controls/OrbitControls.js';
- let renderer, scene, camera;
- let spotLight;
- init();
- function init() {
- // Renderer
- renderer = new THREE.WebGPURenderer( { antialias: true } );
- renderer.setPixelRatio( window.devicePixelRatio );
- renderer.setSize( window.innerWidth, window.innerHeight );
- renderer.setAnimationLoop( animate );
- document.body.appendChild( renderer.domElement );
- renderer.inspector = new Inspector();
- renderer.toneMapping = THREE.NeutralToneMapping;
- renderer.toneMappingExposure = 1;
- renderer.shadowMap.enabled = true;
- renderer.shadowMap.type = THREE.PCFSoftShadowMap;
- scene = new THREE.Scene();
- camera = new THREE.PerspectiveCamera( 40, window.innerWidth / window.innerHeight, 0.1, 100 );
- camera.position.set( 7, 4, 1 );
- // Controls
- const controls = new OrbitControls( camera, renderer.domElement );
- controls.minDistance = 2;
- controls.maxDistance = 10;
- controls.maxPolarAngle = Math.PI / 2;
- controls.target.set( 0, 1, 0 );
- controls.update();
- // Textures
- const loader = new THREE.TextureLoader().setPath( 'textures/' );
- const filenames = [ 'disturb.jpg', 'colors.png', 'uv_grid_opengl.jpg' ];
- const textures = { none: null };
- for ( let i = 0; i < filenames.length; i ++ ) {
- const filename = filenames[ i ];
- const texture = loader.load( filename );
- texture.minFilter = THREE.LinearFilter;
- texture.magFilter = THREE.LinearFilter;
- texture.generateMipmaps = false;
- texture.colorSpace = THREE.SRGBColorSpace;
- textures[ filename ] = texture;
- }
- // Lights
- const ambient = new THREE.HemisphereLight( 0xffffff, 0x8d8d8d, 0.25 );
- scene.add( ambient );
- spotLight = new THREE.SpotLight( 0xffffff, 100 );
- spotLight.name = 'spotLight';
- spotLight.map = textures[ 'disturb.jpg' ];
- spotLight.position.set( 2.5, 5, 2.5 );
- spotLight.angle = Math.PI / 6;
- spotLight.penumbra = 1;
- spotLight.decay = 2;
- spotLight.distance = 0;
- spotLight.castShadow = true;
- spotLight.shadow.mapSize.width = 1024;
- spotLight.shadow.mapSize.height = 1024;
- spotLight.shadow.camera.near = 2;
- spotLight.shadow.camera.far = 10;
- spotLight.shadow.focus = 1;
- spotLight.shadow.bias = - .003;
- spotLight.shadow.intensity = 1;
- scene.add( spotLight );
- spotLight.lightHelper = new THREE.SpotLightHelper( spotLight );
- spotLight.lightHelper.visible = false;
- scene.add( spotLight.lightHelper );
- spotLight.shadowCameraHelper = new THREE.CameraHelper( spotLight.shadow.camera ); // colored lines
- spotLight.shadowCameraHelper.visible = false;
- scene.add( spotLight.shadowCameraHelper );
- //
- const geometry = new THREE.PlaneGeometry( 10, 10 );
- const material = new THREE.MeshLambertMaterial( { color: 0xbcbcbc } );
- const mesh = new THREE.Mesh( geometry, material );
- mesh.position.set( 0, - 1, 0 );
- mesh.rotation.x = - Math.PI / 2;
- mesh.receiveShadow = true;
- scene.add( mesh );
- // Model
- new PLYLoader().load( 'models/ply/binary/Lucy100k.ply', function ( geometry ) {
- geometry.scale( 0.0024, 0.0024, 0.0024 );
- geometry.computeVertexNormals();
- const material = new THREE.MeshLambertMaterial();
- const mesh = new THREE.Mesh( geometry, material );
- mesh.rotation.y = - Math.PI / 2;
- mesh.position.y = 0.8;
- mesh.castShadow = true;
- mesh.receiveShadow = true;
- scene.add( mesh );
- } );
- //
- window.addEventListener( 'resize', onWindowResize );
- // GUI
- const gui = renderer.inspector.createParameters( 'Settings' );
- const params = {
- map: textures[ 'disturb.jpg' ],
- color: spotLight.color.getHex(),
- intensity: spotLight.intensity,
- distance: spotLight.distance,
- angle: spotLight.angle,
- penumbra: spotLight.penumbra,
- decay: spotLight.decay,
- focus: spotLight.shadow.focus,
- shadowIntensity: spotLight.shadow.intensity,
- helpers: false
- };
- gui.add( params, 'map', textures ).onChange( function ( val ) {
- spotLight.map = val;
- } );
- gui.addColor( params, 'color' ).onChange( function ( val ) {
- spotLight.color.setHex( val );
- } );
- gui.add( params, 'intensity', 0, 500 ).onChange( function ( val ) {
- spotLight.intensity = val;
- } );
- gui.add( params, 'distance', 0, 20 ).onChange( function ( val ) {
- spotLight.distance = val;
- } );
- gui.add( params, 'angle', 0, Math.PI / 3 ).onChange( function ( val ) {
- spotLight.angle = val;
- } );
- gui.add( params, 'penumbra', 0, 1 ).onChange( function ( val ) {
- spotLight.penumbra = val;
- } );
- gui.add( params, 'decay', 1, 2 ).onChange( function ( val ) {
- spotLight.decay = val;
- } );
- gui.add( params, 'focus', 0, 1 ).onChange( function ( val ) {
- spotLight.shadow.focus = val;
- } );
- gui.add( params, 'shadowIntensity', 0, 1 ).onChange( function ( val ) {
- spotLight.shadow.intensity = val;
- } );
- gui.add( params, 'helpers' ).onChange( function ( val ) {
- spotLight.lightHelper.visible = val;
- spotLight.shadowCameraHelper.visible = val;
- } );
- }
- function onWindowResize() {
- camera.aspect = window.innerWidth / window.innerHeight;
- camera.updateProjectionMatrix();
- renderer.setSize( window.innerWidth, window.innerHeight );
- }
- function animate() {
- const time = performance.now() / 3000;
- spotLight.position.x = Math.cos( time ) * 2.5;
- spotLight.position.z = Math.sin( time ) * 2.5;
- spotLight.lightHelper.update();
- renderer.render( scene, camera );
- }
- </script>
- </body>
- </html>
|