webgl_shadowmap_pointlight.html 4.9 KB

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