webgpu_lights_spotlight.html 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <title>three.js webgpu - 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. <meta property="og:title" content="three.js webgpu - spotlight">
  8. <meta property="og:type" content="website">
  9. <meta property="og:url" content="https://threejs.org/examples/webgpu_lights_spotlight.html">
  10. <meta property="og:image" content="https://threejs.org/examples/screenshots/webgpu_lights_spotlight.jpg">
  11. <link type="text/css" rel="stylesheet" href="example.css">
  12. </head>
  13. <body>
  14. <div id="info">
  15. <a href="https://threejs.org/" target="_blank" rel="noopener" class="logo-link"></a>
  16. <div class="title-wrapper">
  17. <a href="https://threejs.org/" target="_blank" rel="noopener">three.js</a><span>Spot Light</span>
  18. </div>
  19. <small>Spot light projecting texture map.</small>
  20. </div>
  21. <script type="importmap">
  22. {
  23. "imports": {
  24. "three": "../build/three.webgpu.js",
  25. "three/webgpu": "../build/three.webgpu.js",
  26. "three/tsl": "../build/three.tsl.js",
  27. "three/addons/": "./jsm/"
  28. }
  29. }
  30. </script>
  31. <script type="module">
  32. import * as THREE from 'three/webgpu';
  33. import { Inspector } from 'three/addons/inspector/Inspector.js';
  34. import { PLYLoader } from 'three/addons/loaders/PLYLoader.js';
  35. import { OrbitControls } from 'three/addons/controls/OrbitControls.js';
  36. let renderer, scene, camera;
  37. let spotLight;
  38. init();
  39. function init() {
  40. // Renderer
  41. renderer = new THREE.WebGPURenderer( { antialias: true } );
  42. renderer.setPixelRatio( window.devicePixelRatio );
  43. renderer.setSize( window.innerWidth, window.innerHeight );
  44. renderer.setAnimationLoop( animate );
  45. document.body.appendChild( renderer.domElement );
  46. renderer.inspector = new Inspector();
  47. renderer.toneMapping = THREE.NeutralToneMapping;
  48. renderer.toneMappingExposure = 1;
  49. renderer.shadowMap.enabled = true;
  50. renderer.shadowMap.type = THREE.PCFSoftShadowMap;
  51. scene = new THREE.Scene();
  52. camera = new THREE.PerspectiveCamera( 40, window.innerWidth / window.innerHeight, 0.1, 100 );
  53. camera.position.set( 7, 4, 1 );
  54. // Controls
  55. const controls = new OrbitControls( camera, renderer.domElement );
  56. controls.minDistance = 2;
  57. controls.maxDistance = 10;
  58. controls.maxPolarAngle = Math.PI / 2;
  59. controls.target.set( 0, 1, 0 );
  60. controls.update();
  61. // Textures
  62. const loader = new THREE.TextureLoader().setPath( 'textures/' );
  63. const filenames = [ 'disturb.jpg', 'colors.png', 'uv_grid_opengl.jpg' ];
  64. const textures = { none: null };
  65. for ( let i = 0; i < filenames.length; i ++ ) {
  66. const filename = filenames[ i ];
  67. const texture = loader.load( filename );
  68. texture.minFilter = THREE.LinearFilter;
  69. texture.magFilter = THREE.LinearFilter;
  70. texture.generateMipmaps = false;
  71. texture.colorSpace = THREE.SRGBColorSpace;
  72. textures[ filename ] = texture;
  73. }
  74. // Lights
  75. const ambient = new THREE.HemisphereLight( 0xffffff, 0x8d8d8d, 0.25 );
  76. scene.add( ambient );
  77. spotLight = new THREE.SpotLight( 0xffffff, 100 );
  78. spotLight.name = 'spotLight';
  79. spotLight.map = textures[ 'disturb.jpg' ];
  80. spotLight.position.set( 2.5, 5, 2.5 );
  81. spotLight.angle = Math.PI / 6;
  82. spotLight.penumbra = 1;
  83. spotLight.decay = 2;
  84. spotLight.distance = 0;
  85. spotLight.castShadow = true;
  86. spotLight.shadow.mapSize.width = 1024;
  87. spotLight.shadow.mapSize.height = 1024;
  88. spotLight.shadow.camera.near = 2;
  89. spotLight.shadow.camera.far = 10;
  90. spotLight.shadow.focus = 1;
  91. spotLight.shadow.intensity = 1;
  92. scene.add( spotLight );
  93. spotLight.lightHelper = new THREE.SpotLightHelper( spotLight );
  94. spotLight.lightHelper.visible = false;
  95. scene.add( spotLight.lightHelper );
  96. spotLight.shadowCameraHelper = new THREE.CameraHelper( spotLight.shadow.camera ); // colored lines
  97. spotLight.shadowCameraHelper.visible = false;
  98. scene.add( spotLight.shadowCameraHelper );
  99. //
  100. const geometry = new THREE.PlaneGeometry( 10, 10 );
  101. const material = new THREE.MeshLambertMaterial( { color: 0xbcbcbc } );
  102. const mesh = new THREE.Mesh( geometry, material );
  103. mesh.position.set( 0, - 1, 0 );
  104. mesh.rotation.x = - Math.PI / 2;
  105. mesh.receiveShadow = true;
  106. scene.add( mesh );
  107. // Model
  108. new PLYLoader().load( 'models/ply/binary/Lucy100k.ply', function ( geometry ) {
  109. geometry.scale( 0.0024, 0.0024, 0.0024 );
  110. geometry.computeVertexNormals();
  111. const material = new THREE.MeshLambertMaterial();
  112. const mesh = new THREE.Mesh( geometry, material );
  113. mesh.rotation.y = - Math.PI / 2;
  114. mesh.position.y = 0.8;
  115. mesh.castShadow = true;
  116. mesh.receiveShadow = true;
  117. scene.add( mesh );
  118. } );
  119. //
  120. window.addEventListener( 'resize', onWindowResize );
  121. // GUI
  122. const gui = renderer.inspector.createParameters( 'Settings' );
  123. const params = {
  124. map: textures[ 'disturb.jpg' ],
  125. color: spotLight.color.getHex(),
  126. intensity: spotLight.intensity,
  127. distance: spotLight.distance,
  128. angle: spotLight.angle,
  129. penumbra: spotLight.penumbra,
  130. decay: spotLight.decay,
  131. focus: spotLight.shadow.focus,
  132. shadowIntensity: spotLight.shadow.intensity,
  133. helpers: false
  134. };
  135. gui.add( params, 'map', textures ).onChange( function ( val ) {
  136. spotLight.map = val;
  137. } );
  138. gui.addColor( params, 'color' ).onChange( function ( val ) {
  139. spotLight.color.setHex( val );
  140. } );
  141. gui.add( params, 'intensity', 0, 500 ).onChange( function ( val ) {
  142. spotLight.intensity = val;
  143. } );
  144. gui.add( params, 'distance', 0, 20 ).onChange( function ( val ) {
  145. spotLight.distance = val;
  146. } );
  147. gui.add( params, 'angle', 0, Math.PI / 3 ).onChange( function ( val ) {
  148. spotLight.angle = val;
  149. } );
  150. gui.add( params, 'penumbra', 0, 1 ).onChange( function ( val ) {
  151. spotLight.penumbra = val;
  152. } );
  153. gui.add( params, 'decay', 1, 2 ).onChange( function ( val ) {
  154. spotLight.decay = val;
  155. } );
  156. gui.add( params, 'focus', 0, 1 ).onChange( function ( val ) {
  157. spotLight.shadow.focus = val;
  158. } );
  159. gui.add( params, 'shadowIntensity', 0, 1 ).onChange( function ( val ) {
  160. spotLight.shadow.intensity = val;
  161. } );
  162. gui.add( params, 'helpers' ).onChange( function ( val ) {
  163. spotLight.lightHelper.visible = val;
  164. spotLight.shadowCameraHelper.visible = val;
  165. } );
  166. }
  167. function onWindowResize() {
  168. camera.aspect = window.innerWidth / window.innerHeight;
  169. camera.updateProjectionMatrix();
  170. renderer.setSize( window.innerWidth, window.innerHeight );
  171. }
  172. function animate() {
  173. const time = performance.now() / 3000;
  174. spotLight.position.x = Math.cos( time ) * 2.5;
  175. spotLight.position.z = Math.sin( time ) * 2.5;
  176. spotLight.lightHelper.update();
  177. renderer.render( scene, camera );
  178. }
  179. </script>
  180. </body>
  181. </html>
粤ICP备19079148号