webgl_shadowmap_pointlight.html 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <title>three.js webgl - THREE.PointLight ShadowMap</title>
  5. <meta charset="utf-8">
  6. <meta name="viewport" content="width=device-width, user-scalable=no, minimum-scale=1.0, maximum-scale=1.0">
  7. <link type="text/css" rel="stylesheet" href="main.css">
  8. </head>
  9. <body>
  10. <div id="info">
  11. <a href="http://threejs.org" target="_blank" rel="noopener">three.js</a> - THREE.PointLight ShadowMap by <a href="https://github.com/mkkellogg">mkkellogg</a>
  12. </div>
  13. <script type="module">
  14. import * as THREE from '../build/three.module.js';
  15. import Stats from './jsm/libs/stats.module.js';
  16. import { OrbitControls } from './jsm/controls/OrbitControls.js';
  17. var camera, scene, renderer, stats;
  18. var pointLight, pointLight2;
  19. var extraPointLights;
  20. init();
  21. animate();
  22. function init() {
  23. camera = new THREE.PerspectiveCamera( 45, window.innerWidth / window.innerHeight, 1, 1000 );
  24. camera.position.set( 0, 10, 40 );
  25. scene = new THREE.Scene();
  26. scene.add( new THREE.AmbientLight( 0x111122 ) );
  27. // lights
  28. function createLight( color ) {
  29. var intensity = 1.5;
  30. var pointLight = new THREE.PointLight( color, intensity, 20 );
  31. pointLight.castShadow = true;
  32. pointLight.shadow.camera.near = 1;
  33. pointLight.shadow.camera.far = 60;
  34. pointLight.shadow.bias = - 0.005; // reduces self-shadowing on double-sided objects
  35. var geometry = new THREE.SphereBufferGeometry( 0.3, 12, 6 );
  36. var material = new THREE.MeshBasicMaterial( { color: color } );
  37. material.color.multiplyScalar( intensity );
  38. var sphere = new THREE.Mesh( geometry, material );
  39. pointLight.add( sphere );
  40. var texture = new THREE.CanvasTexture( generateTexture() );
  41. texture.magFilter = THREE.NearestFilter;
  42. texture.wrapT = THREE.RepeatWrapping;
  43. texture.wrapS = THREE.RepeatWrapping;
  44. texture.repeat.set( 1, 3.5 );
  45. var geometry = new THREE.SphereBufferGeometry( 2, 32, 8 );
  46. var material = new THREE.MeshPhongMaterial( {
  47. side: THREE.DoubleSide,
  48. alphaMap: texture,
  49. alphaTest: 0.5
  50. } );
  51. var sphere = new THREE.Mesh( geometry, material );
  52. sphere.castShadow = true;
  53. sphere.receiveShadow = true;
  54. pointLight.add( sphere );
  55. // custom distance material
  56. var distanceMaterial = new THREE.MeshDistanceMaterial( {
  57. alphaMap: material.alphaMap,
  58. alphaTest: material.alphaTest
  59. } );
  60. sphere.customDistanceMaterial = distanceMaterial;
  61. return pointLight;
  62. }
  63. pointLight = createLight( 0x0088ff );
  64. scene.add( pointLight );
  65. pointLight2 = createLight( 0xff8888 );
  66. scene.add( pointLight2 );
  67. // The extra point lights demonstrate that it's possible to have lots of non-shadow-casting lights in
  68. // the same scene as the shadow casting lights.
  69. var createExtraLight = function () {
  70. var pointLight = new THREE.PointLight( 0xffffff, 0.1, 20 );
  71. pointLight.position.x = ( Math.random() - 0.5 ) * 29;
  72. pointLight.position.y = ( Math.random() - 0.5 ) * 29;
  73. pointLight.position.z = ( Math.random() - 0.5 ) * 29;
  74. var geometry = new THREE.SphereBufferGeometry( 0.1, 10, 6 );
  75. var material = new THREE.MeshBasicMaterial( { color: 0xffffff } );
  76. var sphere = new THREE.Mesh( geometry, material );
  77. pointLight.add( sphere );
  78. return pointLight;
  79. };
  80. var extraPointLights = new THREE.Object3D();
  81. scene.add( extraPointLights );
  82. extraPointLights.position.set( 0, 10, 0 );
  83. for ( var i = 0; i < 20; i ++ ) {
  84. extraPointLights.add( createExtraLight() );
  85. }
  86. //
  87. var geometry = new THREE.BoxBufferGeometry( 30, 30, 30 );
  88. var material = new THREE.MeshPhongMaterial( {
  89. color: 0xa0adaf,
  90. shininess: 10,
  91. specular: 0x111111,
  92. side: THREE.BackSide
  93. } );
  94. var mesh = new THREE.Mesh( geometry, material );
  95. mesh.position.y = 10;
  96. mesh.receiveShadow = true;
  97. scene.add( mesh );
  98. //
  99. renderer = new THREE.WebGLRenderer( { antialias: true } );
  100. renderer.setPixelRatio( window.devicePixelRatio );
  101. renderer.setSize( window.innerWidth, window.innerHeight );
  102. renderer.shadowMap.enabled = true;
  103. renderer.shadowMap.type = THREE.BasicShadowMap;
  104. document.body.appendChild( renderer.domElement );
  105. var controls = new OrbitControls( camera, renderer.domElement );
  106. controls.target.set( 0, 10, 0 );
  107. controls.update();
  108. stats = new Stats();
  109. document.body.appendChild( stats.dom );
  110. //
  111. window.addEventListener( 'resize', onWindowResize, false );
  112. }
  113. function onWindowResize() {
  114. camera.aspect = window.innerWidth / window.innerHeight;
  115. camera.updateProjectionMatrix();
  116. renderer.setSize( window.innerWidth, window.innerHeight );
  117. }
  118. function generateTexture() {
  119. var canvas = document.createElement( 'canvas' );
  120. canvas.width = 2;
  121. canvas.height = 2;
  122. var context = canvas.getContext( '2d' );
  123. context.fillStyle = 'white';
  124. context.fillRect( 0, 1, 2, 1 );
  125. return canvas;
  126. }
  127. function animate() {
  128. requestAnimationFrame( animate );
  129. render();
  130. }
  131. function render() {
  132. var time = performance.now() * 0.001;
  133. pointLight.position.x = Math.sin( time * 0.6 ) * 9;
  134. pointLight.position.y = Math.sin( time * 0.7 ) * 9 + 5;
  135. pointLight.position.z = Math.sin( time * 0.8 ) * 9;
  136. pointLight.rotation.x = time;
  137. pointLight.rotation.z = time;
  138. time += 10000;
  139. pointLight2.position.x = Math.sin( time * 0.6 ) * 9;
  140. pointLight2.position.y = Math.sin( time * 0.7 ) * 9 + 5;
  141. pointLight2.position.z = Math.sin( time * 0.8 ) * 9;
  142. pointLight2.rotation.x = time;
  143. pointLight2.rotation.z = time;
  144. renderer.render( scene, camera );
  145. stats.update();
  146. }
  147. </script>
  148. </body>
  149. </html>