1
0

webgl_lights_spotlight.html 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  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/es-module-shims@1.3.6/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 textureUrls, textures;
  30. let gui;
  31. function init() {
  32. renderer = new THREE.WebGLRenderer();
  33. renderer.setPixelRatio( window.devicePixelRatio );
  34. renderer.setSize( window.innerWidth, window.innerHeight );
  35. document.body.appendChild( renderer.domElement );
  36. renderer.shadowMap.enabled = true;
  37. renderer.shadowMap.type = THREE.PCFSoftShadowMap;
  38. renderer.outputEncoding = THREE.sRGBEncoding;
  39. scene = new THREE.Scene();
  40. camera = new THREE.PerspectiveCamera( 35, window.innerWidth / window.innerHeight, 1, 1000 );
  41. camera.position.set( 160, 40, 10 );
  42. const controls = new OrbitControls( camera, renderer.domElement );
  43. controls.addEventListener( 'change', render );
  44. controls.minDistance = 20;
  45. controls.maxDistance = 500;
  46. controls.enablePan = false;
  47. const ambient = new THREE.AmbientLight( 0xffffff, 0.1 );
  48. scene.add( ambient );
  49. const textureLoader = new THREE.TextureLoader();
  50. textureUrls = [ 'none', 'uv_grid_opengl.jpg', 'sprite2.png', 'colors.png' ];
  51. textures = { none: null }
  52. for ( let i = 1; i < textureUrls.length; i ++ ) {
  53. textures[ textureUrls[ i ] ] = textureLoader.load( 'textures/' + textureUrls[ i ] );
  54. textures[ textureUrls[ i ] ].minFilter = THREE.LinearFilter;
  55. textures[ textureUrls[ i ] ].magFilter = THREE.LinearFilter;
  56. }
  57. spotLight = new THREE.SpotLight( 0xffffff, 1 );
  58. spotLight.position.set( 15, 40, 35 );
  59. spotLight.angle = Math.PI / 4;
  60. spotLight.penumbra = 0.1;
  61. spotLight.decay = 2;
  62. spotLight.distance = 200;
  63. spotLight.castShadow = true;
  64. spotLight.shadow.mapSize.width = 512;
  65. spotLight.shadow.mapSize.height = 512;
  66. spotLight.shadow.camera.near = 10;
  67. spotLight.shadow.camera.far = 200;
  68. spotLight.shadow.focus = 1;
  69. scene.add( spotLight );
  70. lightHelper = new THREE.SpotLightHelper( spotLight );
  71. scene.add( lightHelper );
  72. shadowCameraHelper = new THREE.CameraHelper( spotLight.shadow.camera );
  73. scene.add( shadowCameraHelper );
  74. //
  75. let material = new THREE.MeshPhongMaterial( { color: 0x808080, dithering: true } );
  76. let geometry = new THREE.PlaneGeometry( 2000, 2000 );
  77. let mesh = new THREE.Mesh( geometry, material );
  78. mesh.position.set( 0, - 1, 0 );
  79. mesh.rotation.x = - Math.PI * 0.5;
  80. mesh.receiveShadow = true;
  81. scene.add( mesh );
  82. //
  83. material = new THREE.MeshPhongMaterial( { color: 0x4080ff, dithering: true } );
  84. geometry = new THREE.CylinderGeometry( 5, 5, 2, 32, 1, false );
  85. mesh = new THREE.Mesh( geometry, material );
  86. mesh.position.set( 0, 5, 0 );
  87. mesh.castShadow = true;
  88. scene.add( mesh );
  89. render();
  90. window.addEventListener( 'resize', onWindowResize );
  91. }
  92. function onWindowResize() {
  93. camera.aspect = window.innerWidth / window.innerHeight;
  94. camera.updateProjectionMatrix();
  95. renderer.setSize( window.innerWidth, window.innerHeight );
  96. }
  97. function render() {
  98. lightHelper.update();
  99. shadowCameraHelper.update();
  100. renderer.render( scene, camera );
  101. }
  102. function buildGui() {
  103. gui = new GUI();
  104. const params = {
  105. 'light color': spotLight.color.getHex(),
  106. intensity: spotLight.intensity,
  107. distance: spotLight.distance,
  108. angle: spotLight.angle,
  109. penumbra: spotLight.penumbra,
  110. decay: spotLight.decay,
  111. focus: spotLight.shadow.focus,
  112. map: 'none'
  113. };
  114. gui.addColor( params, 'light color' ).onChange( function ( val ) {
  115. spotLight.color.setHex( val );
  116. render();
  117. } );
  118. gui.add( params, 'intensity', 0, 2 ).onChange( function ( val ) {
  119. spotLight.intensity = val;
  120. render();
  121. } );
  122. gui.add( params, 'distance', 50, 200 ).onChange( function ( val ) {
  123. spotLight.distance = val;
  124. render();
  125. } );
  126. gui.add( params, 'angle', 0, Math.PI / 3 ).onChange( function ( val ) {
  127. spotLight.angle = val;
  128. render();
  129. } );
  130. gui.add( params, 'penumbra', 0, 1 ).onChange( function ( val ) {
  131. spotLight.penumbra = val;
  132. render();
  133. } );
  134. gui.add( params, 'decay', 1, 2 ).onChange( function ( val ) {
  135. spotLight.decay = val;
  136. render();
  137. } );
  138. gui.add( params, 'focus', 0, 1 ).onChange( function ( val ) {
  139. spotLight.shadow.focus = val;
  140. render();
  141. } );
  142. gui.add( params, 'map', textureUrls ).onChange( function ( val ) {
  143. spotLight.map = textures[ val ];
  144. render();
  145. } );
  146. gui.open();
  147. }
  148. init();
  149. buildGui();
  150. render();
  151. </script>
  152. </body>
  153. </html>
粤ICP备19079148号