webgl_lights_spotlights.html 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <title>three.js webgl - lights - spot light</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. <link type="text/css" rel="stylesheet" href="main.css">
  8. </head>
  9. <body>
  10. <div id="info">
  11. <a href="http://threejs.org" target="_blank" rel="noopener">three.js</a> - SpotLights<br/>
  12. by <a href="http://master-domain.com" target="_blank" rel="noopener">Master James</a>
  13. </div>
  14. <script src="../build/three.js"></script>
  15. <script src="js/libs/dat.gui.min.js"></script>
  16. <script src="js/libs/tween.min.js"></script>
  17. <script src="js/controls/OrbitControls.js"></script>
  18. <script src="js/WebGL.js"></script>
  19. <script>
  20. if ( WEBGL.isWebGLAvailable() === false ) {
  21. document.body.appendChild( WEBGL.getWebGLErrorMessage() );
  22. }
  23. var renderer = new THREE.WebGLRenderer();
  24. renderer.setPixelRatio( window.devicePixelRatio );
  25. var camera = new THREE.PerspectiveCamera( 35, window.innerWidth / window.innerHeight, 1, 2000 );
  26. var controls = new THREE.OrbitControls( camera, renderer.domElement );
  27. var scene = new THREE.Scene();
  28. var matFloor = new THREE.MeshPhongMaterial();
  29. var matBox = new THREE.MeshPhongMaterial( { color: 0xaaaaaa } );
  30. var geoFloor = new THREE.PlaneBufferGeometry( 2000, 2000 );
  31. var geoBox = new THREE.BoxBufferGeometry( 3, 1, 2 );
  32. var mshFloor = new THREE.Mesh( geoFloor, matFloor );
  33. mshFloor.rotation.x = - Math.PI * 0.5;
  34. var mshBox = new THREE.Mesh( geoBox, matBox );
  35. var ambient = new THREE.AmbientLight( 0x111111 );
  36. var spotLight1 = createSpotlight( 0xFF7F00 );
  37. var spotLight2 = createSpotlight( 0x00FF7F );
  38. var spotLight3 = createSpotlight( 0x7F00FF );
  39. var lightHelper1, lightHelper2, lightHelper3;
  40. function init() {
  41. renderer.shadowMap.enabled = true;
  42. renderer.shadowMap.type = THREE.PCFSoftShadowMap;
  43. renderer.gammaInput = true;
  44. renderer.gammaOutput = true;
  45. camera.position.set( 46, 22, - 21 );
  46. spotLight1.position.set( 15, 40, 45 );
  47. spotLight2.position.set( 0, 40, 35 );
  48. spotLight3.position.set( - 15, 40, 45 );
  49. lightHelper1 = new THREE.SpotLightHelper( spotLight1 );
  50. lightHelper2 = new THREE.SpotLightHelper( spotLight2 );
  51. lightHelper3 = new THREE.SpotLightHelper( spotLight3 );
  52. matFloor.color.set( 0x808080 );
  53. mshFloor.receiveShadow = true;
  54. mshFloor.position.set( 0, - 0.05, 0 );
  55. mshBox.castShadow = true;
  56. mshBox.receiveShadow = true;
  57. mshBox.position.set( 0, 5, 0 );
  58. scene.add( mshFloor );
  59. scene.add( mshBox );
  60. scene.add( ambient );
  61. scene.add( spotLight1, spotLight2, spotLight3 );
  62. scene.add( lightHelper1, lightHelper2, lightHelper3 );
  63. document.body.appendChild( renderer.domElement );
  64. onResize();
  65. window.addEventListener( 'resize', onResize, false );
  66. controls.target.set( 0, 7, 0 );
  67. controls.maxPolarAngle = Math.PI / 2;
  68. controls.update();
  69. }
  70. function createSpotlight( color ) {
  71. var newObj = new THREE.SpotLight( color, 2 );
  72. newObj.castShadow = true;
  73. newObj.angle = 0.3;
  74. newObj.penumbra = 0.2;
  75. newObj.decay = 2;
  76. newObj.distance = 50;
  77. newObj.shadow.mapSize.width = 1024;
  78. newObj.shadow.mapSize.height = 1024;
  79. return newObj;
  80. }
  81. function onResize() {
  82. camera.aspect = window.innerWidth / window.innerHeight;
  83. camera.updateProjectionMatrix();
  84. renderer.setSize( window.innerWidth, window.innerHeight );
  85. }
  86. function tween( light ) {
  87. new TWEEN.Tween( light ).to( {
  88. angle: ( Math.random() * 0.7 ) + 0.1,
  89. penumbra: Math.random() + 1
  90. }, Math.random() * 3000 + 2000 )
  91. .easing( TWEEN.Easing.Quadratic.Out ).start();
  92. new TWEEN.Tween( light.position ).to( {
  93. x: ( Math.random() * 30 ) - 15,
  94. y: ( Math.random() * 10 ) + 15,
  95. z: ( Math.random() * 30 ) - 15
  96. }, Math.random() * 3000 + 2000 )
  97. .easing( TWEEN.Easing.Quadratic.Out ).start();
  98. }
  99. function animate() {
  100. tween( spotLight1 );
  101. tween( spotLight2 );
  102. tween( spotLight3 );
  103. setTimeout( animate, 5000 );
  104. }
  105. function render() {
  106. TWEEN.update();
  107. if ( lightHelper1 ) lightHelper1.update();
  108. if ( lightHelper2 ) lightHelper2.update();
  109. if ( lightHelper3 ) lightHelper3.update();
  110. renderer.render( scene, camera );
  111. requestAnimationFrame( render );
  112. }
  113. init();
  114. render();
  115. animate();
  116. </script>
  117. </body>
  118. </html>