webgl_shadowmap_pointlight.html 4.5 KB

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