webgl_shadowmap_pointlight.html 4.5 KB

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