webgl_shadowmap_pointlight.html 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  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 { color: #f00; font-weight: bold; text-decoration: underline; cursor: pointer }
  24. </style>
  25. </head>
  26. <body>
  27. <div id="info">
  28. <a href="http://threejs.org" target="_blank" rel="noopener">three.js</a> - PointLight ShadowMap by <a href="https://github.com/mkkellogg">mkkellogg</a>
  29. </div>
  30. <script src="../build/three.js"></script>
  31. <script src="js/controls/OrbitControls.js"></script>
  32. <script src="js/Detector.js"></script>
  33. <script src="js/libs/stats.min.js"></script>
  34. <script>
  35. if ( ! Detector.webgl ) Detector.addGetWebGLMessage();
  36. var camera, scene, renderer, stats;
  37. var pointLight, pointLight2;
  38. var torusKnot;
  39. init();
  40. animate();
  41. function init() {
  42. camera = new THREE.PerspectiveCamera( 45, window.innerWidth / window.innerHeight, 1, 1000 );
  43. camera.position.set( 0, 10, 40 );
  44. scene = new THREE.Scene();
  45. scene.add( new THREE.AmbientLight( 0x222233 ) );
  46. // lights
  47. function createLight( color ) {
  48. var pointLight = new THREE.PointLight( color, 1, 30 );
  49. pointLight.castShadow = true;
  50. pointLight.shadow.camera.near = 1;
  51. pointLight.shadow.camera.far = 60;
  52. pointLight.shadow.bias = - 0.005; // reduces self-shadowing on double-sided objects
  53. var geometry = new THREE.SphereGeometry( 0.3, 12, 6 );
  54. var material = new THREE.MeshBasicMaterial( { color: color } );
  55. var sphere = new THREE.Mesh( geometry, material );
  56. pointLight.add( sphere );
  57. return pointLight;
  58. }
  59. pointLight = createLight( 0xffffff );
  60. scene.add( pointLight );
  61. pointLight2 = createLight( 0xff0000 );
  62. scene.add( pointLight2 );
  63. //
  64. var geometry = new THREE.TorusKnotGeometry( 14, 1, 150, 20 );
  65. var texture = new THREE.CanvasTexture( generateTexture() );
  66. texture.wrapS = texture.wrapT = THREE.RepeatWrapping;
  67. texture.repeat.set( 32, 1 );
  68. var material = new THREE.MeshPhongMaterial( {
  69. color: 0xff0000,
  70. shininess: 100,
  71. specular: 0x222222,
  72. alphaMap: texture, // alphaMap uses the g channel
  73. alphaTest: 0.5,
  74. side: THREE.DoubleSide
  75. } );
  76. torusKnot = new THREE.Mesh( geometry, material );
  77. torusKnot.position.set( 0, 5, 0 );
  78. torusKnot.castShadow = true;
  79. torusKnot.receiveShadow = true;
  80. scene.add( torusKnot );
  81. // custom distance material
  82. var distanceMaterial = new THREE.MeshDistanceMaterial( {
  83. alphaMap: material.alphaMap,
  84. alphaTest: material.alphaTest
  85. } );
  86. torusKnot.customDistanceMaterial = distanceMaterial;
  87. //
  88. var geometry = new THREE.BoxGeometry( 30, 30, 30 );
  89. var material = new THREE.MeshPhongMaterial( {
  90. color: 0xa0adaf,
  91. shininess: 10,
  92. specular: 0x111111,
  93. side: THREE.BackSide
  94. } );
  95. var mesh = new THREE.Mesh( geometry, material );
  96. mesh.position.y = 10;
  97. mesh.receiveShadow = true;
  98. scene.add( mesh );
  99. //
  100. renderer = new THREE.WebGLRenderer( { antialias: true } );
  101. renderer.setPixelRatio( window.devicePixelRatio );
  102. renderer.setSize( window.innerWidth, window.innerHeight );
  103. renderer.shadowMap.enabled = true;
  104. renderer.shadowMap.type = THREE.BasicShadowMap;
  105. renderer.shadowMap.renderSingleSided = false; // must be set to false to honor double-sided materials
  106. document.body.appendChild( renderer.domElement );
  107. var controls = new THREE.OrbitControls( camera, renderer.domElement );
  108. controls.target.set( 0, 10, 0 );
  109. controls.update();
  110. stats = new Stats();
  111. document.body.appendChild( stats.dom );
  112. //
  113. window.addEventListener( 'resize', onWindowResize, false );
  114. }
  115. function onWindowResize() {
  116. camera.aspect = window.innerWidth / window.innerHeight;
  117. camera.updateProjectionMatrix();
  118. renderer.setSize( window.innerWidth, window.innerHeight );
  119. }
  120. function generateTexture() {
  121. var canvas = document.createElement( 'canvas' );
  122. canvas.width = 256;
  123. canvas.height = 256;
  124. var context = canvas.getContext( '2d' );
  125. var image = context.getImageData( 0, 0, 256, 256 );
  126. var x = 0, y = 0, cvalue;
  127. for ( var i = 0, j = 0, l = image.data.length; i < l; i += 4, j ++ ) {
  128. x = j % 256; // pixel col
  129. y = ( x == 0 ) ? y + 1 : y; // pixel row
  130. // diagonal stripes
  131. cvalue = Math.floor( ( x + y ) / 32 ) % 2;
  132. image.data[ i + 0 ] = 255 * cvalue;
  133. image.data[ i + 1 ] = 255 * cvalue;
  134. image.data[ i + 2 ] = 255 * cvalue;
  135. image.data[ i + 3 ] = 255;
  136. }
  137. context.putImageData( image, 0, 0 );
  138. return canvas;
  139. }
  140. function animate() {
  141. requestAnimationFrame( animate );
  142. render();
  143. }
  144. function render() {
  145. var time = performance.now() * 0.001;
  146. pointLight.position.x = Math.sin( time ) * 9;
  147. pointLight.position.y = Math.sin( time * 1.1 ) * 9 + 5;
  148. pointLight.position.z = Math.sin( time * 1.2 ) * 9;
  149. time += 10000;
  150. pointLight2.position.x = Math.sin( time ) * 9;
  151. pointLight2.position.y = Math.sin( time * 1.1 ) * 9 + 5;
  152. pointLight2.position.z = Math.sin( time * 1.2 ) * 9;
  153. torusKnot.rotation.y = time * 0.1;
  154. renderer.render( scene, camera );
  155. stats.update();
  156. }
  157. </script>
  158. </body>
  159. </html>