webgpu_lights_spotlight.html 6.6 KB

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