webgl_shadowmap_pointlight.html 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  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="https://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. let camera, scene, renderer, stats;
  18. let pointLight, pointLight2;
  19. init();
  20. animate();
  21. function init() {
  22. camera = new THREE.PerspectiveCamera( 45, window.innerWidth / window.innerHeight, 1, 1000 );
  23. camera.position.set( 0, 10, 40 );
  24. scene = new THREE.Scene();
  25. scene.add( new THREE.AmbientLight( 0x111122 ) );
  26. // lights
  27. function createLight( color ) {
  28. const intensity = 1.5;
  29. const light = new THREE.PointLight( color, intensity, 20 );
  30. light.castShadow = true;
  31. light.shadow.bias = - 0.005; // reduces self-shadowing on double-sided objects
  32. let geometry = new THREE.SphereGeometry( 0.3, 12, 6 );
  33. let material = new THREE.MeshBasicMaterial( { color: color } );
  34. material.color.multiplyScalar( intensity );
  35. let sphere = new THREE.Mesh( geometry, material );
  36. light.add( sphere );
  37. const texture = new THREE.CanvasTexture( generateTexture() );
  38. texture.magFilter = THREE.NearestFilter;
  39. texture.wrapT = THREE.RepeatWrapping;
  40. texture.wrapS = THREE.RepeatWrapping;
  41. texture.repeat.set( 1, 4.5 );
  42. geometry = new THREE.SphereGeometry( 2, 32, 8 );
  43. material = new THREE.MeshPhongMaterial( {
  44. side: THREE.DoubleSide,
  45. alphaMap: texture,
  46. alphaTest: 0.5
  47. } );
  48. sphere = new THREE.Mesh( geometry, material );
  49. sphere.castShadow = true;
  50. sphere.receiveShadow = true;
  51. light.add( sphere );
  52. // custom distance material
  53. const distanceMaterial = new THREE.MeshDistanceMaterial( {
  54. alphaMap: material.alphaMap,
  55. alphaTest: material.alphaTest
  56. } );
  57. sphere.customDistanceMaterial = distanceMaterial;
  58. return light;
  59. }
  60. pointLight = createLight( 0x0088ff );
  61. scene.add( pointLight );
  62. pointLight2 = createLight( 0xff8888 );
  63. scene.add( pointLight2 );
  64. //
  65. const geometry = new THREE.BoxGeometry( 30, 30, 30 );
  66. const material = new THREE.MeshPhongMaterial( {
  67. color: 0xa0adaf,
  68. shininess: 10,
  69. specular: 0x111111,
  70. side: THREE.BackSide
  71. } );
  72. const mesh = new THREE.Mesh( geometry, material );
  73. mesh.position.y = 10;
  74. mesh.receiveShadow = true;
  75. scene.add( mesh );
  76. //
  77. renderer = new THREE.WebGLRenderer( { antialias: true } );
  78. renderer.setPixelRatio( window.devicePixelRatio );
  79. renderer.setSize( window.innerWidth, window.innerHeight );
  80. renderer.shadowMap.enabled = true;
  81. renderer.shadowMap.type = THREE.BasicShadowMap;
  82. document.body.appendChild( renderer.domElement );
  83. const controls = new OrbitControls( camera, renderer.domElement );
  84. controls.target.set( 0, 10, 0 );
  85. controls.update();
  86. stats = new Stats();
  87. document.body.appendChild( stats.dom );
  88. //
  89. window.addEventListener( 'resize', onWindowResize );
  90. }
  91. function onWindowResize() {
  92. camera.aspect = window.innerWidth / window.innerHeight;
  93. camera.updateProjectionMatrix();
  94. renderer.setSize( window.innerWidth, window.innerHeight );
  95. }
  96. function generateTexture() {
  97. const canvas = document.createElement( 'canvas' );
  98. canvas.width = 2;
  99. canvas.height = 2;
  100. const context = canvas.getContext( '2d' );
  101. context.fillStyle = 'white';
  102. context.fillRect( 0, 1, 2, 1 );
  103. return canvas;
  104. }
  105. function animate() {
  106. requestAnimationFrame( animate );
  107. render();
  108. }
  109. function render() {
  110. let time = performance.now() * 0.001;
  111. pointLight.position.x = Math.sin( time * 0.6 ) * 9;
  112. pointLight.position.y = Math.sin( time * 0.7 ) * 9 + 6;
  113. pointLight.position.z = Math.sin( time * 0.8 ) * 9;
  114. pointLight.rotation.x = time;
  115. pointLight.rotation.z = time;
  116. time += 10000;
  117. pointLight2.position.x = Math.sin( time * 0.6 ) * 9;
  118. pointLight2.position.y = Math.sin( time * 0.7 ) * 9 + 6;
  119. pointLight2.position.z = Math.sin( time * 0.8 ) * 9;
  120. pointLight2.rotation.x = time;
  121. pointLight2.rotation.z = time;
  122. renderer.render( scene, camera );
  123. stats.update();
  124. }
  125. </script>
  126. </body>
  127. </html>