webgl_shadowmap_pointlight.html 4.9 KB

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