123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184 |
- <!DOCTYPE html>
- <html lang="en">
- <head>
- <title>three.js webgl - THREE.PointLight ShadowMap</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.PointLight ShadowMap by <a href="https://github.com/mkkellogg">mkkellogg</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';
- var camera, scene, renderer, stats;
- var pointLight, pointLight2;
- init();
- animate();
- function init() {
- camera = new THREE.PerspectiveCamera( 45, window.innerWidth / window.innerHeight, 1, 1000 );
- camera.position.set( 0, 10, 40 );
- scene = new THREE.Scene();
- scene.add( new THREE.AmbientLight( 0x111122 ) );
- // lights
- function createLight( color ) {
- var intensity = 1.5;
- var pointLight = new THREE.PointLight( color, intensity, 20 );
- pointLight.castShadow = true;
- pointLight.shadow.camera.near = 1;
- pointLight.shadow.camera.far = 60;
- pointLight.shadow.bias = - 0.005; // reduces self-shadowing on double-sided objects
- var geometry = new THREE.SphereBufferGeometry( 0.3, 12, 6 );
- var material = new THREE.MeshBasicMaterial( { color: color } );
- material.color.multiplyScalar( intensity );
- var sphere = new THREE.Mesh( geometry, material );
- pointLight.add( sphere );
- var texture = new THREE.CanvasTexture( generateTexture() );
- texture.magFilter = THREE.NearestFilter;
- texture.wrapT = THREE.RepeatWrapping;
- texture.wrapS = THREE.RepeatWrapping;
- texture.repeat.set( 1, 4.5 );
- var geometry = new THREE.SphereBufferGeometry( 2, 32, 8 );
- var material = new THREE.MeshPhongMaterial( {
- side: THREE.DoubleSide,
- alphaMap: texture,
- alphaTest: 0.5
- } );
- var sphere = new THREE.Mesh( geometry, material );
- sphere.castShadow = true;
- sphere.receiveShadow = true;
- pointLight.add( sphere );
- // custom distance material
- var distanceMaterial = new THREE.MeshDistanceMaterial( {
- alphaMap: material.alphaMap,
- alphaTest: material.alphaTest
- } );
- sphere.customDistanceMaterial = distanceMaterial;
- return pointLight;
- }
- pointLight = createLight( 0x0088ff );
- scene.add( pointLight );
- pointLight2 = createLight( 0xff8888 );
- scene.add( pointLight2 );
- //
- var geometry = new THREE.BoxBufferGeometry( 30, 30, 30 );
- var material = new THREE.MeshPhongMaterial( {
- color: 0xa0adaf,
- shininess: 10,
- specular: 0x111111,
- side: THREE.BackSide
- } );
- var mesh = new THREE.Mesh( geometry, material );
- mesh.position.y = 10;
- mesh.receiveShadow = true;
- scene.add( mesh );
- //
- 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;
- document.body.appendChild( renderer.domElement );
- var controls = new OrbitControls( camera, renderer.domElement );
- controls.target.set( 0, 10, 0 );
- controls.update();
- stats = new Stats();
- document.body.appendChild( stats.dom );
- //
- window.addEventListener( 'resize', onWindowResize, false );
- }
- function onWindowResize() {
- camera.aspect = window.innerWidth / window.innerHeight;
- camera.updateProjectionMatrix();
- renderer.setSize( window.innerWidth, window.innerHeight );
- }
- function generateTexture() {
- var canvas = document.createElement( 'canvas' );
- canvas.width = 2;
- canvas.height = 2;
- var context = canvas.getContext( '2d' );
- context.fillStyle = 'white';
- context.fillRect( 0, 1, 2, 1 );
- return canvas;
- }
- function animate() {
- requestAnimationFrame( animate );
- render();
- }
- function render() {
- var time = performance.now() * 0.001;
- pointLight.position.x = Math.sin( time * 0.6 ) * 9;
- pointLight.position.y = Math.sin( time * 0.7 ) * 9 + 6;
- pointLight.position.z = Math.sin( time * 0.8 ) * 9;
- pointLight.rotation.x = time;
- pointLight.rotation.z = time;
- time += 10000;
- pointLight2.position.x = Math.sin( time * 0.6 ) * 9;
- pointLight2.position.y = Math.sin( time * 0.7 ) * 9 + 6;
- pointLight2.position.z = Math.sin( time * 0.8 ) * 9;
- pointLight2.rotation.x = time;
- pointLight2.rotation.z = time;
- renderer.render( scene, camera );
- stats.update();
- }
- </script>
- </body>
- </html>
|