webgl_shadowmap_pointlight.html 5.2 KB

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