webgl_shadowmap_pointlight.html 5.1 KB

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