webgl_shadowmap_pointlight.html 4.8 KB

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