webgl_shadowmap_pointlight.html 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  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. return light;
  64. }
  65. pointLight = createLight( 0x0088ff );
  66. scene.add( pointLight );
  67. pointLight2 = createLight( 0xff8888 );
  68. scene.add( pointLight2 );
  69. //
  70. const geometry = new THREE.BoxGeometry( 30, 30, 30 );
  71. const material = new THREE.MeshPhongMaterial( {
  72. color: 0xa0adaf,
  73. shininess: 10,
  74. specular: 0x111111,
  75. side: THREE.BackSide
  76. } );
  77. const mesh = new THREE.Mesh( geometry, material );
  78. mesh.position.y = 10;
  79. mesh.receiveShadow = true;
  80. scene.add( mesh );
  81. //
  82. renderer = new THREE.WebGLRenderer( { antialias: true } );
  83. renderer.setPixelRatio( window.devicePixelRatio );
  84. renderer.setSize( window.innerWidth, window.innerHeight );
  85. renderer.shadowMap.enabled = true;
  86. renderer.shadowMap.type = THREE.BasicShadowMap;
  87. document.body.appendChild( renderer.domElement );
  88. const controls = new OrbitControls( camera, renderer.domElement );
  89. controls.target.set( 0, 10, 0 );
  90. controls.update();
  91. stats = new Stats();
  92. document.body.appendChild( stats.dom );
  93. //
  94. window.addEventListener( 'resize', onWindowResize );
  95. }
  96. function onWindowResize() {
  97. camera.aspect = window.innerWidth / window.innerHeight;
  98. camera.updateProjectionMatrix();
  99. renderer.setSize( window.innerWidth, window.innerHeight );
  100. }
  101. function generateTexture() {
  102. const canvas = document.createElement( 'canvas' );
  103. canvas.width = 2;
  104. canvas.height = 2;
  105. const context = canvas.getContext( '2d' );
  106. context.fillStyle = 'white';
  107. context.fillRect( 0, 1, 2, 1 );
  108. return canvas;
  109. }
  110. function animate() {
  111. requestAnimationFrame( animate );
  112. render();
  113. }
  114. function render() {
  115. let time = performance.now() * 0.001;
  116. pointLight.position.x = Math.sin( time * 0.6 ) * 9;
  117. pointLight.position.y = Math.sin( time * 0.7 ) * 9 + 6;
  118. pointLight.position.z = Math.sin( time * 0.8 ) * 9;
  119. pointLight.rotation.x = time;
  120. pointLight.rotation.z = time;
  121. time += 10000;
  122. pointLight2.position.x = Math.sin( time * 0.6 ) * 9;
  123. pointLight2.position.y = Math.sin( time * 0.7 ) * 9 + 6;
  124. pointLight2.position.z = Math.sin( time * 0.8 ) * 9;
  125. pointLight2.rotation.x = time;
  126. pointLight2.rotation.z = time;
  127. renderer.render( scene, camera );
  128. stats.update();
  129. }
  130. </script>
  131. </body>
  132. </html>