webgl_lights_spotlight.html 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  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. <!-- Import maps polyfill -->
  14. <!-- Remove this when import maps will be widely supported -->
  15. <script async src="https://unpkg.com/[email protected]/dist/es-module-shims.js"></script>
  16. <script type="importmap">
  17. {
  18. "imports": {
  19. "three": "../build/three.module.js"
  20. }
  21. }
  22. </script>
  23. <script type="module">
  24. import * as THREE from 'three';
  25. import { GUI } from './jsm/libs/lil-gui.module.min.js';
  26. import { OrbitControls } from './jsm/controls/OrbitControls.js';
  27. let renderer, scene, camera;
  28. let spotLight, lightHelper, shadowCameraHelper;
  29. let gui;
  30. function init() {
  31. renderer = new THREE.WebGLRenderer();
  32. renderer.setPixelRatio( window.devicePixelRatio );
  33. renderer.setSize( window.innerWidth, window.innerHeight );
  34. document.body.appendChild( renderer.domElement );
  35. renderer.shadowMap.enabled = true;
  36. renderer.shadowMap.type = THREE.PCFSoftShadowMap;
  37. renderer.outputEncoding = THREE.sRGBEncoding;
  38. scene = new THREE.Scene();
  39. camera = new THREE.PerspectiveCamera( 35, window.innerWidth / window.innerHeight, 1, 1000 );
  40. camera.position.set( 160, 40, 10 );
  41. const controls = new OrbitControls( camera, renderer.domElement );
  42. controls.addEventListener( 'change', render );
  43. controls.minDistance = 20;
  44. controls.maxDistance = 500;
  45. controls.enablePan = false;
  46. const ambient = new THREE.AmbientLight( 0xffffff, 0.1 );
  47. scene.add( ambient );
  48. spotLight = new THREE.SpotLight( 0xffffff, 1 );
  49. spotLight.position.set( 15, 40, 35 );
  50. spotLight.angle = Math.PI / 4;
  51. spotLight.penumbra = 0.1;
  52. spotLight.decay = 2;
  53. spotLight.distance = 200;
  54. spotLight.castShadow = true;
  55. spotLight.shadow.mapSize.width = 512;
  56. spotLight.shadow.mapSize.height = 512;
  57. spotLight.shadow.camera.near = 10;
  58. spotLight.shadow.camera.far = 200;
  59. spotLight.shadow.focus = 1;
  60. scene.add( spotLight );
  61. lightHelper = new THREE.SpotLightHelper( spotLight );
  62. scene.add( lightHelper );
  63. shadowCameraHelper = new THREE.CameraHelper( spotLight.shadow.camera );
  64. scene.add( shadowCameraHelper );
  65. //
  66. let material = new THREE.MeshPhongMaterial( { color: 0x808080, dithering: true } );
  67. let geometry = new THREE.PlaneGeometry( 2000, 2000 );
  68. let mesh = new THREE.Mesh( geometry, material );
  69. mesh.position.set( 0, - 1, 0 );
  70. mesh.rotation.x = - Math.PI * 0.5;
  71. mesh.receiveShadow = true;
  72. scene.add( mesh );
  73. //
  74. material = new THREE.MeshPhongMaterial( { color: 0x4080ff, dithering: true } );
  75. geometry = new THREE.CylinderGeometry( 5, 5, 2, 32, 1, false );
  76. mesh = new THREE.Mesh( geometry, material );
  77. mesh.position.set( 0, 5, 0 );
  78. mesh.castShadow = true;
  79. scene.add( mesh );
  80. render();
  81. window.addEventListener( 'resize', onWindowResize );
  82. }
  83. function onWindowResize() {
  84. camera.aspect = window.innerWidth / window.innerHeight;
  85. camera.updateProjectionMatrix();
  86. renderer.setSize( window.innerWidth, window.innerHeight );
  87. }
  88. function render() {
  89. lightHelper.update();
  90. shadowCameraHelper.update();
  91. renderer.render( scene, camera );
  92. }
  93. function buildGui() {
  94. gui = new GUI();
  95. const params = {
  96. 'light color': spotLight.color.getHex(),
  97. intensity: spotLight.intensity,
  98. distance: spotLight.distance,
  99. angle: spotLight.angle,
  100. penumbra: spotLight.penumbra,
  101. decay: spotLight.decay,
  102. focus: spotLight.shadow.focus
  103. };
  104. gui.addColor( params, 'light color' ).onChange( function ( val ) {
  105. spotLight.color.setHex( val );
  106. render();
  107. } );
  108. gui.add( params, 'intensity', 0, 2 ).onChange( function ( val ) {
  109. spotLight.intensity = val;
  110. render();
  111. } );
  112. gui.add( params, 'distance', 50, 200 ).onChange( function ( val ) {
  113. spotLight.distance = val;
  114. render();
  115. } );
  116. gui.add( params, 'angle', 0, Math.PI / 3 ).onChange( function ( val ) {
  117. spotLight.angle = val;
  118. render();
  119. } );
  120. gui.add( params, 'penumbra', 0, 1 ).onChange( function ( val ) {
  121. spotLight.penumbra = val;
  122. render();
  123. } );
  124. gui.add( params, 'decay', 1, 2 ).onChange( function ( val ) {
  125. spotLight.decay = val;
  126. render();
  127. } );
  128. gui.add( params, 'focus', 0, 1 ).onChange( function ( val ) {
  129. spotLight.shadow.focus = val;
  130. render();
  131. } );
  132. gui.open();
  133. }
  134. init();
  135. buildGui();
  136. render();
  137. </script>
  138. </body>
  139. </html>