webgl_shadowmap_pointlight.html 5.0 KB

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