2
0

webgl_shadowmap_pointlight.html 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  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">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 = 30;
  52. // pointLight.shadowCameraVisible = true;
  53. pointLight.shadow.bias = 0.01;
  54. var geometry = new THREE.SphereGeometry( 0.3, 12, 6 );
  55. var material = new THREE.MeshBasicMaterial( { color: color } );
  56. var sphere = new THREE.Mesh( geometry, material );
  57. pointLight.add( sphere );
  58. return pointLight
  59. }
  60. pointLight = createLight( 0xffffff );
  61. scene.add( pointLight );
  62. pointLight2 = createLight( 0xff0000 );
  63. scene.add( pointLight2 );
  64. var geometry = new THREE.TorusKnotGeometry( 14, 1, 150, 20 );
  65. var material = new THREE.MeshPhongMaterial( {
  66. color: 0xff0000,
  67. shininess: 100,
  68. specular: 0x222222
  69. } );
  70. torusKnot = new THREE.Mesh( geometry, material );
  71. torusKnot.position.set( 0, 5, 0 );
  72. torusKnot.castShadow = true;
  73. torusKnot.receiveShadow = true;
  74. scene.add( torusKnot );
  75. var geometry = new THREE.BoxGeometry( 30, 30, 30 );
  76. var material = new THREE.MeshPhongMaterial( {
  77. color: 0xa0adaf,
  78. shininess: 10,
  79. specular: 0x111111,
  80. side: THREE.BackSide
  81. } );
  82. var mesh = new THREE.Mesh( geometry, material );
  83. mesh.position.y = 10;
  84. mesh.receiveShadow = true;
  85. scene.add( mesh );
  86. //
  87. renderer = new THREE.WebGLRenderer( { antialias: true } );
  88. renderer.setPixelRatio( window.devicePixelRatio );
  89. renderer.setSize( window.innerWidth, window.innerHeight );
  90. renderer.shadowMap.enabled = true;
  91. renderer.shadowMap.type = THREE.BasicShadowMap;
  92. document.body.appendChild( renderer.domElement );
  93. controls = new THREE.OrbitControls( camera, renderer.domElement );
  94. controls.target.set( 0, 10, 0 );
  95. controls.update();
  96. stats = new Stats();
  97. document.body.appendChild( stats.dom );
  98. //
  99. window.addEventListener( 'resize', onWindowResize, false );
  100. }
  101. function onWindowResize() {
  102. camera.aspect = window.innerWidth / window.innerHeight;
  103. camera.updateProjectionMatrix();
  104. renderer.setSize( window.innerWidth, window.innerHeight );
  105. }
  106. function animate() {
  107. requestAnimationFrame( animate );
  108. render();
  109. }
  110. function render() {
  111. var time = performance.now() * 0.001;
  112. pointLight.position.x = Math.sin( time ) * 9;
  113. pointLight.position.y = Math.sin( time * 1.1 ) * 9 + 5;
  114. pointLight.position.z = Math.sin( time * 1.2 ) * 9;
  115. time += 10000;
  116. pointLight2.position.x = Math.sin( time ) * 9;
  117. pointLight2.position.y = Math.sin( time * 1.1 ) * 9 + 5;
  118. pointLight2.position.z = Math.sin( time * 1.2 ) * 9;
  119. torusKnot.rotation.y = time * 0.1;
  120. renderer.render( scene, camera );
  121. stats.update();
  122. }
  123. </script>
  124. </body>
  125. </html>