webgl_shadowmap_pointlight.html 4.8 KB

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