webgl_lights_spotlight.html 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  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<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. "three/addons/": "./jsm/"
  21. }
  22. }
  23. </script>
  24. <script type="module">
  25. import * as THREE from 'three';
  26. import { GUI } from 'three/addons/libs/lil-gui.module.min.js';
  27. import { PLYLoader } from 'three/addons/loaders/PLYLoader.js';
  28. import { OrbitControls } from 'three/addons/controls/OrbitControls.js';
  29. let renderer, scene, camera;
  30. let spotLight, lightHelper;
  31. init();
  32. function init() {
  33. renderer = new THREE.WebGLRenderer( { antialias: true } );
  34. renderer.setPixelRatio( window.devicePixelRatio );
  35. renderer.setSize( window.innerWidth, window.innerHeight );
  36. document.body.appendChild( renderer.domElement );
  37. renderer.shadowMap.enabled = true;
  38. renderer.shadowMap.type = THREE.PCFSoftShadowMap;
  39. renderer.outputEncoding = THREE.sRGBEncoding;
  40. renderer.toneMapping = THREE.ACESFilmicToneMapping;
  41. renderer.toneMappingExposure = 1;
  42. renderer.setAnimationLoop( render );
  43. scene = new THREE.Scene();
  44. camera = new THREE.PerspectiveCamera( 40, window.innerWidth / window.innerHeight, 1, 1000 );
  45. camera.position.set( 70, 50, 10 );
  46. const controls = new OrbitControls( camera, renderer.domElement );
  47. controls.minDistance = 20;
  48. controls.maxDistance = 100;
  49. controls.maxPolarAngle = Math.PI / 2;
  50. controls.target.set( 0, 18, 0 );
  51. controls.update();
  52. const ambient = new THREE.HemisphereLight( 0xffffff, 0x444444, 0.05 );
  53. scene.add( ambient );
  54. const loader = new THREE.TextureLoader().setPath( 'textures/' );
  55. const filenames = [ 'disturb.jpg', 'colors.png', 'uv_grid_opengl.jpg' ];
  56. const textures = { none: null };
  57. for ( let i = 0; i < filenames.length; i ++ ) {
  58. const filename = filenames[ i ];
  59. const texture = loader.load( filename );
  60. texture.minFilter = THREE.LinearFilter;
  61. texture.magFilter = THREE.LinearFilter;
  62. texture.encoding = THREE.sRGBEncoding;
  63. textures[ filename ] = texture;
  64. }
  65. spotLight = new THREE.SpotLight( 0xffffff, 5 );
  66. spotLight.position.set( 25, 50, 25 );
  67. spotLight.angle = Math.PI / 6;
  68. spotLight.penumbra = 1;
  69. spotLight.decay = 2;
  70. spotLight.distance = 100;
  71. spotLight.map = textures[ 'disturb.jpg' ];
  72. spotLight.castShadow = true;
  73. spotLight.shadow.mapSize.width = 1024;
  74. spotLight.shadow.mapSize.height = 1024;
  75. spotLight.shadow.camera.near = 10;
  76. spotLight.shadow.camera.far = 200;
  77. spotLight.shadow.focus = 1;
  78. scene.add( spotLight );
  79. lightHelper = new THREE.SpotLightHelper( spotLight );
  80. scene.add( lightHelper );
  81. //
  82. const geometry = new THREE.PlaneGeometry( 1000, 1000 );
  83. const material = new THREE.MeshLambertMaterial( { color: 0x808080 } );
  84. const mesh = new THREE.Mesh( geometry, material );
  85. mesh.position.set( 0, - 1, 0 );
  86. mesh.rotation.x = - Math.PI / 2;
  87. mesh.receiveShadow = true;
  88. scene.add( mesh );
  89. //
  90. new PLYLoader().load( 'models/ply/binary/Lucy100k.ply', function ( geometry ) {
  91. geometry.scale( 0.024, 0.024, 0.024 );
  92. geometry.computeVertexNormals();
  93. const material = new THREE.MeshLambertMaterial();
  94. const mesh = new THREE.Mesh( geometry, material );
  95. mesh.rotation.y = - Math.PI / 2;
  96. mesh.position.y = 18;
  97. mesh.castShadow = true;
  98. mesh.receiveShadow = true;
  99. scene.add( mesh );
  100. } );
  101. window.addEventListener( 'resize', onWindowResize );
  102. // GUI
  103. const gui = new GUI();
  104. const params = {
  105. map: textures[ 'disturb.jpg' ],
  106. color: spotLight.color.getHex(),
  107. intensity: spotLight.intensity,
  108. distance: spotLight.distance,
  109. angle: spotLight.angle,
  110. penumbra: spotLight.penumbra,
  111. decay: spotLight.decay,
  112. focus: spotLight.shadow.focus,
  113. shadows: true
  114. };
  115. gui.add( params, 'map', textures ).onChange( function ( val ) {
  116. spotLight.map = val;
  117. } );
  118. gui.addColor( params, 'color' ).onChange( function ( val ) {
  119. spotLight.color.setHex( val );
  120. } );
  121. gui.add( params, 'intensity', 0, 10 ).onChange( function ( val ) {
  122. spotLight.intensity = val;
  123. } );
  124. gui.add( params, 'distance', 50, 200 ).onChange( function ( val ) {
  125. spotLight.distance = val;
  126. } );
  127. gui.add( params, 'angle', 0, Math.PI / 3 ).onChange( function ( val ) {
  128. spotLight.angle = val;
  129. } );
  130. gui.add( params, 'penumbra', 0, 1 ).onChange( function ( val ) {
  131. spotLight.penumbra = val;
  132. } );
  133. gui.add( params, 'decay', 1, 2 ).onChange( function ( val ) {
  134. spotLight.decay = val;
  135. } );
  136. gui.add( params, 'focus', 0, 1 ).onChange( function ( val ) {
  137. spotLight.shadow.focus = val;
  138. } );
  139. gui.add( params, 'shadows' ).onChange( function ( val ) {
  140. renderer.shadowMap.enabled = val;
  141. scene.traverse( function ( child ) {
  142. if ( child.material ) {
  143. child.material.needsUpdate = true;
  144. }
  145. } );
  146. } );
  147. gui.open();
  148. }
  149. function onWindowResize() {
  150. camera.aspect = window.innerWidth / window.innerHeight;
  151. camera.updateProjectionMatrix();
  152. renderer.setSize( window.innerWidth, window.innerHeight );
  153. }
  154. function render() {
  155. const time = performance.now() / 3000;
  156. spotLight.position.x = Math.cos( time ) * 25;
  157. spotLight.position.z = Math.sin( time ) * 25;
  158. lightHelper.update();
  159. renderer.render( scene, camera );
  160. }
  161. </script>
  162. </body>
  163. </html>