webgl_lights_spotlight.html 5.7 KB

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