123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210 |
- <!DOCTYPE html>
- <html lang="en">
- <head>
- <title>three.js webgl - ShadowMapViewer example </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> - ShadowMapViewer example by <a href="https://github.com/arya-s">arya-s</a>
- </div>
- <script type="module">
- import * as THREE from '../build/three.module.js';
- import Stats from './jsm/libs/stats.module.js';
- import { OrbitControls } from './jsm/controls/OrbitControls.js';
- import { ShadowMapViewer } from './jsm/utils/ShadowMapViewer.js';
- var camera, scene, renderer, clock, stats;
- var dirLight, spotLight;
- var torusKnot, cube;
- var dirLightShadowMapViewer, spotLightShadowMapViewer;
- init();
- animate();
- function init() {
- initScene();
- initShadowMapViewers();
- initMisc();
- document.body.appendChild( renderer.domElement );
- window.addEventListener( 'resize', onWindowResize, false );
- }
- function initScene() {
- camera = new THREE.PerspectiveCamera( 45, window.innerWidth / window.innerHeight, 1, 1000 );
- camera.position.set( 0, 15, 35 );
- scene = new THREE.Scene();
- // Lights
- scene.add( new THREE.AmbientLight( 0x404040 ) );
- spotLight = new THREE.SpotLight( 0xffffff );
- spotLight.name = 'Spot Light';
- spotLight.angle = Math.PI / 5;
- spotLight.penumbra = 0.3;
- spotLight.position.set( 10, 10, 5 );
- spotLight.castShadow = true;
- spotLight.shadow.camera.near = 8;
- spotLight.shadow.camera.far = 30;
- spotLight.shadow.mapSize.width = 1024;
- spotLight.shadow.mapSize.height = 1024;
- scene.add( spotLight );
- scene.add( new THREE.CameraHelper( spotLight.shadow.camera ) );
- dirLight = new THREE.DirectionalLight( 0xffffff, 1 );
- dirLight.name = 'Dir. Light';
- dirLight.position.set( 0, 10, 0 );
- dirLight.castShadow = true;
- dirLight.shadow.camera.near = 1;
- dirLight.shadow.camera.far = 10;
- dirLight.shadow.camera.right = 15;
- dirLight.shadow.camera.left = - 15;
- dirLight.shadow.camera.top = 15;
- dirLight.shadow.camera.bottom = - 15;
- dirLight.shadow.mapSize.width = 1024;
- dirLight.shadow.mapSize.height = 1024;
- scene.add( dirLight );
- scene.add( new THREE.CameraHelper( dirLight.shadow.camera ) );
- // Geometry
- var geometry = new THREE.TorusKnotBufferGeometry( 25, 8, 75, 20 );
- var material = new THREE.MeshPhongMaterial( {
- color: 0xff0000,
- shininess: 150,
- specular: 0x222222
- } );
- torusKnot = new THREE.Mesh( geometry, material );
- torusKnot.scale.multiplyScalar( 1 / 18 );
- torusKnot.position.y = 3;
- torusKnot.castShadow = true;
- torusKnot.receiveShadow = true;
- scene.add( torusKnot );
- var geometry = new THREE.BoxBufferGeometry( 3, 3, 3 );
- cube = new THREE.Mesh( geometry, material );
- cube.position.set( 8, 3, 8 );
- cube.castShadow = true;
- cube.receiveShadow = true;
- scene.add( cube );
- var geometry = new THREE.BoxBufferGeometry( 10, 0.15, 10 );
- var material = new THREE.MeshPhongMaterial( {
- color: 0xa0adaf,
- shininess: 150,
- specular: 0x111111
- } );
- var ground = new THREE.Mesh( geometry, material );
- ground.scale.multiplyScalar( 3 );
- ground.castShadow = false;
- ground.receiveShadow = true;
- scene.add( ground );
- }
- function initShadowMapViewers() {
- dirLightShadowMapViewer = new ShadowMapViewer( dirLight );
- dirLightShadowMapViewer.position.x = 10;
- dirLightShadowMapViewer.position.y = 10;
- dirLightShadowMapViewer.size.width = 256;
- dirLightShadowMapViewer.size.height = 256;
- dirLightShadowMapViewer.update(); //Required when setting position or size directly
- spotLightShadowMapViewer = new ShadowMapViewer( spotLight );
- spotLightShadowMapViewer.size.set( 256, 256 );
- spotLightShadowMapViewer.position.set( 276, 10 );
- // spotLightShadowMapViewer.update(); //NOT required because .set updates automatically
- }
- function initMisc() {
- renderer = new THREE.WebGLRenderer( { antialias: true } );
- renderer.setPixelRatio( window.devicePixelRatio );
- renderer.setSize( window.innerWidth, window.innerHeight );
- renderer.shadowMap.enabled = true;
- renderer.shadowMap.type = THREE.BasicShadowMap;
- // Mouse control
- var controls = new OrbitControls( camera, renderer.domElement );
- controls.target.set( 0, 2, 0 );
- controls.update();
- clock = new THREE.Clock();
- stats = new Stats();
- document.body.appendChild( stats.dom );
- }
- function onWindowResize() {
- camera.aspect = window.innerWidth / window.innerHeight;
- camera.updateProjectionMatrix();
- renderer.setSize( window.innerWidth, window.innerHeight );
- dirLightShadowMapViewer.updateForWindowResize();
- spotLightShadowMapViewer.updateForWindowResize();
- }
- function animate() {
- requestAnimationFrame( animate );
- render();
- stats.update();
- }
- function renderScene() {
- renderer.render( scene, camera );
- }
- function renderShadowMapViewers() {
- dirLightShadowMapViewer.render( renderer );
- spotLightShadowMapViewer.render( renderer );
- }
- function render() {
- var delta = clock.getDelta();
- renderScene();
- renderShadowMapViewers();
- torusKnot.rotation.x += 0.25 * delta;
- torusKnot.rotation.y += 2 * delta;
- torusKnot.rotation.z += 1 * delta;
- cube.rotation.x += 0.25 * delta;
- cube.rotation.y += 2 * delta;
- cube.rotation.z += 1 * delta;
- }
- </script>
- </body>
- </html>
|