webgpu_lights_spotlight.html 6.4 KB

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