webgl_lights_spotlight.html 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264
  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;
  28. init();
  29. function init() {
  30. // Renderer
  31. renderer = new THREE.WebGLRenderer( { antialias: true } );
  32. renderer.setPixelRatio( window.devicePixelRatio );
  33. renderer.setSize( window.innerWidth, window.innerHeight );
  34. renderer.setAnimationLoop( animate );
  35. document.body.appendChild( renderer.domElement );
  36. renderer.toneMapping = THREE.NeutralToneMapping;
  37. renderer.toneMappingExposure = 1;
  38. renderer.shadowMap.enabled = true;
  39. renderer.shadowMap.type = THREE.PCFShadowMap;
  40. scene = new THREE.Scene();
  41. camera = new THREE.PerspectiveCamera( 40, window.innerWidth / window.innerHeight, 0.1, 100 );
  42. camera.position.set( 7, 4, 1 );
  43. // Controls
  44. const controls = new OrbitControls( camera, renderer.domElement );
  45. controls.minDistance = 2;
  46. controls.maxDistance = 10;
  47. controls.maxPolarAngle = Math.PI / 2;
  48. controls.target.set( 0, 1, 0 );
  49. controls.update();
  50. // Textures
  51. const loader = new THREE.TextureLoader().setPath( 'textures/' );
  52. const filenames = [ 'disturb.jpg', 'colors.png', 'uv_grid_opengl.jpg' ];
  53. const textures = { none: null };
  54. for ( let i = 0; i < filenames.length; i ++ ) {
  55. const filename = filenames[ i ];
  56. const texture = loader.load( filename );
  57. texture.minFilter = THREE.LinearFilter;
  58. texture.magFilter = THREE.LinearFilter;
  59. texture.generateMipmaps = false;
  60. texture.colorSpace = THREE.SRGBColorSpace;
  61. textures[ filename ] = texture;
  62. }
  63. // Lights
  64. const ambient = new THREE.HemisphereLight( 0xffffff, 0x8d8d8d, 0.25 );
  65. scene.add( ambient );
  66. spotLight = new THREE.SpotLight( 0xffffff, 100 );
  67. spotLight.name = 'spotLight';
  68. spotLight.map = textures[ 'disturb.jpg' ];
  69. spotLight.position.set( 2.5, 5, 2.5 );
  70. spotLight.angle = Math.PI / 6;
  71. spotLight.penumbra = 1;
  72. spotLight.decay = 2;
  73. spotLight.distance = 0;
  74. spotLight.castShadow = true;
  75. spotLight.shadow.mapSize.width = 1024;
  76. spotLight.shadow.mapSize.height = 1024;
  77. spotLight.shadow.camera.near = 2;
  78. spotLight.shadow.camera.far = 10;
  79. spotLight.shadow.focus = 1;
  80. spotLight.shadow.bias = - .003;
  81. spotLight.shadow.intensity = 1;
  82. scene.add( spotLight );
  83. spotLight.lightHelper = new THREE.SpotLightHelper( spotLight );
  84. spotLight.lightHelper.visible = false;
  85. scene.add( spotLight.lightHelper );
  86. spotLight.shadowCameraHelper = new THREE.CameraHelper( spotLight.shadow.camera ); // colored lines
  87. spotLight.shadowCameraHelper.visible = false;
  88. scene.add( spotLight.shadowCameraHelper );
  89. //
  90. const geometry = new THREE.PlaneGeometry( 10, 10 );
  91. const material = new THREE.MeshLambertMaterial( { color: 0xbcbcbc } );
  92. const mesh = new THREE.Mesh( geometry, material );
  93. mesh.position.set( 0, - 1, 0 );
  94. mesh.rotation.x = - Math.PI / 2;
  95. mesh.receiveShadow = true;
  96. scene.add( mesh );
  97. // Model
  98. new PLYLoader().load( 'models/ply/binary/Lucy100k.ply', function ( geometry ) {
  99. geometry.scale( 0.0024, 0.0024, 0.0024 );
  100. geometry.computeVertexNormals();
  101. const material = new THREE.MeshLambertMaterial();
  102. const mesh = new THREE.Mesh( geometry, material );
  103. mesh.rotation.y = - Math.PI / 2;
  104. mesh.position.y = 0.8;
  105. mesh.castShadow = true;
  106. mesh.receiveShadow = true;
  107. scene.add( mesh );
  108. } );
  109. //
  110. window.addEventListener( 'resize', onWindowResize );
  111. // GUI
  112. const gui = new GUI();
  113. const params = {
  114. map: textures[ 'disturb.jpg' ],
  115. color: spotLight.color.getHex(),
  116. intensity: spotLight.intensity,
  117. distance: spotLight.distance,
  118. angle: spotLight.angle,
  119. penumbra: spotLight.penumbra,
  120. decay: spotLight.decay,
  121. focus: spotLight.shadow.focus,
  122. shadowIntensity: spotLight.shadow.intensity,
  123. helpers: false
  124. };
  125. gui.add( params, 'map', textures ).onChange( function ( val ) {
  126. spotLight.map = val;
  127. } );
  128. gui.addColor( params, 'color' ).onChange( function ( val ) {
  129. spotLight.color.setHex( val );
  130. } );
  131. gui.add( params, 'intensity', 0, 500 ).onChange( function ( val ) {
  132. spotLight.intensity = val;
  133. } );
  134. gui.add( params, 'distance', 0, 20 ).onChange( function ( val ) {
  135. spotLight.distance = val;
  136. } );
  137. gui.add( params, 'angle', 0, Math.PI / 3 ).onChange( function ( val ) {
  138. spotLight.angle = val;
  139. } );
  140. gui.add( params, 'penumbra', 0, 1 ).onChange( function ( val ) {
  141. spotLight.penumbra = val;
  142. } );
  143. gui.add( params, 'decay', 1, 2 ).onChange( function ( val ) {
  144. spotLight.decay = val;
  145. } );
  146. gui.add( params, 'focus', 0, 1 ).onChange( function ( val ) {
  147. spotLight.shadow.focus = val;
  148. } );
  149. gui.add( params, 'shadowIntensity', 0, 1 ).onChange( function ( val ) {
  150. spotLight.shadow.intensity = val;
  151. } );
  152. gui.add( params, 'helpers' ).onChange( function ( val ) {
  153. spotLight.lightHelper.visible = val;
  154. spotLight.shadowCameraHelper.visible = val;
  155. } );
  156. gui.open();
  157. }
  158. function onWindowResize() {
  159. camera.aspect = window.innerWidth / window.innerHeight;
  160. camera.updateProjectionMatrix();
  161. renderer.setSize( window.innerWidth, window.innerHeight );
  162. }
  163. function animate() {
  164. const time = performance.now() / 3000;
  165. spotLight.position.x = Math.cos( time ) * 2.5;
  166. spotLight.position.z = Math.sin( time ) * 2.5;
  167. spotLight.lightHelper.update();
  168. renderer.render( scene, camera );
  169. }
  170. </script>
  171. </body>
  172. </html>
粤ICP备19079148号