webgl_lights_spotlight.html 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <title>three.js webgl - lights - spotlight</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="https://threejs.org" target="_blank" rel="noopener">three.js</a> webgl - spotlight by <a href="http://master-domain.com" target="_blank" rel="noopener">Master James</a><br />
  12. </div>
  13. <script type="module">
  14. import * as THREE from '../build/three.module.js';
  15. import { GUI } from './jsm/libs/dat.gui.module.js';
  16. import { OrbitControls } from './jsm/controls/OrbitControls.js';
  17. var renderer, scene, camera;
  18. var spotLight, lightHelper, shadowCameraHelper;
  19. var gui;
  20. function init() {
  21. renderer = new THREE.WebGLRenderer();
  22. renderer.setPixelRatio( window.devicePixelRatio );
  23. renderer.setSize( window.innerWidth, window.innerHeight );
  24. document.body.appendChild( renderer.domElement );
  25. renderer.shadowMap.enabled = true;
  26. renderer.shadowMap.type = THREE.PCFSoftShadowMap;
  27. renderer.outputEncoding = THREE.sRGBEncoding;
  28. scene = new THREE.Scene();
  29. camera = new THREE.PerspectiveCamera( 35, window.innerWidth / window.innerHeight, 1, 1000 );
  30. camera.position.set( 65, 8, - 10 );
  31. var controls = new OrbitControls( camera, renderer.domElement );
  32. controls.addEventListener( 'change', render );
  33. controls.minDistance = 20;
  34. controls.maxDistance = 500;
  35. controls.enablePan = false;
  36. var ambient = new THREE.AmbientLight( 0xffffff, 0.1 );
  37. scene.add( ambient );
  38. spotLight = new THREE.SpotLight( 0xffffff, 1 );
  39. spotLight.position.set( 15, 40, 35 );
  40. spotLight.angle = Math.PI / 4;
  41. spotLight.penumbra = 0.05;
  42. spotLight.decay = 2;
  43. spotLight.distance = 200;
  44. spotLight.castShadow = true;
  45. spotLight.shadow.mapSize.width = 1024;
  46. spotLight.shadow.mapSize.height = 1024;
  47. spotLight.shadow.camera.near = 10;
  48. spotLight.shadow.camera.far = 200;
  49. scene.add( spotLight );
  50. lightHelper = new THREE.SpotLightHelper( spotLight );
  51. scene.add( lightHelper );
  52. shadowCameraHelper = new THREE.CameraHelper( spotLight.shadow.camera );
  53. scene.add( shadowCameraHelper );
  54. scene.add( new THREE.AxesHelper( 10 ) );
  55. var material = new THREE.MeshPhongMaterial( { color: 0x808080, dithering: true } );
  56. var geometry = new THREE.PlaneBufferGeometry( 2000, 2000 );
  57. var mesh = new THREE.Mesh( geometry, material );
  58. mesh.position.set( 0, - 1, 0 );
  59. mesh.rotation.x = - Math.PI * 0.5;
  60. mesh.receiveShadow = true;
  61. scene.add( mesh );
  62. var material = new THREE.MeshPhongMaterial( { color: 0x4080ff, dithering: true } );
  63. var geometry = new THREE.BoxBufferGeometry( 3, 1, 2 );
  64. var mesh = new THREE.Mesh( geometry, material );
  65. mesh.position.set( 40, 2, 0 );
  66. mesh.castShadow = true;
  67. scene.add( mesh );
  68. controls.target.copy( mesh.position );
  69. controls.update();
  70. window.addEventListener( 'resize', onResize, false );
  71. }
  72. function onResize() {
  73. camera.aspect = window.innerWidth / window.innerHeight;
  74. camera.updateProjectionMatrix();
  75. renderer.setSize( window.innerWidth, window.innerHeight );
  76. }
  77. function render() {
  78. lightHelper.update();
  79. shadowCameraHelper.update();
  80. renderer.render( scene, camera );
  81. }
  82. function buildGui() {
  83. gui = new GUI();
  84. var params = {
  85. 'light color': spotLight.color.getHex(),
  86. intensity: spotLight.intensity,
  87. distance: spotLight.distance,
  88. angle: spotLight.angle,
  89. penumbra: spotLight.penumbra,
  90. decay: spotLight.decay
  91. };
  92. gui.addColor( params, 'light color' ).onChange( function ( val ) {
  93. spotLight.color.setHex( val );
  94. render();
  95. } );
  96. gui.add( params, 'intensity', 0, 2 ).onChange( function ( val ) {
  97. spotLight.intensity = val;
  98. render();
  99. } );
  100. gui.add( params, 'distance', 50, 200 ).onChange( function ( val ) {
  101. spotLight.distance = val;
  102. render();
  103. } );
  104. gui.add( params, 'angle', 0, Math.PI / 3 ).onChange( function ( val ) {
  105. spotLight.angle = val;
  106. render();
  107. } );
  108. gui.add( params, 'penumbra', 0, 1 ).onChange( function ( val ) {
  109. spotLight.penumbra = val;
  110. render();
  111. } );
  112. gui.add( params, 'decay', 1, 2 ).onChange( function ( val ) {
  113. spotLight.decay = val;
  114. render();
  115. } );
  116. gui.open();
  117. }
  118. init();
  119. buildGui();
  120. render();
  121. </script>
  122. </body>
  123. </html>