webgpu_lights_spotlight.html 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  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';
  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, lightHelper;
  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.shadowMap.enabled = true;
  39. renderer.shadowMap.type = THREE.PCFSoftShadowMap;
  40. renderer.toneMapping = THREE.ACESFilmicToneMapping;
  41. renderer.toneMappingExposure = 1;
  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.15 );
  67. scene.add( ambient );
  68. spotLight = new THREE.SpotLight( 0xffffff, 100 );
  69. spotLight.map = textures[ 'disturb.jpg' ];
  70. spotLight.position.set( 2.5, 5, 2.5 );
  71. spotLight.angle = Math.PI / 6;
  72. spotLight.penumbra = 1;
  73. spotLight.decay = 2;
  74. spotLight.distance = 0;
  75. spotLight.castShadow = true;
  76. spotLight.shadow.mapSize.width = 1024;
  77. spotLight.shadow.mapSize.height = 1024;
  78. spotLight.shadow.camera.near = 1;
  79. spotLight.shadow.camera.far = 10;
  80. spotLight.shadow.focus = 1;
  81. spotLight.shadow.bias = - .003;
  82. scene.add( spotLight );
  83. lightHelper = new THREE.SpotLightHelper( spotLight );
  84. scene.add( lightHelper );
  85. //
  86. const geometry = new THREE.PlaneGeometry( 200, 200 );
  87. const material = new THREE.MeshLambertMaterial( { color: 0xbcbcbc } );
  88. const mesh = new THREE.Mesh( geometry, material );
  89. mesh.position.set( 0, - 1, 0 );
  90. mesh.rotation.x = - Math.PI / 2;
  91. mesh.receiveShadow = true;
  92. scene.add( mesh );
  93. // Models
  94. new PLYLoader().load( 'models/ply/binary/Lucy100k.ply', function ( geometry ) {
  95. geometry.scale( 0.0024, 0.0024, 0.0024 );
  96. geometry.computeVertexNormals();
  97. const material = new THREE.MeshLambertMaterial();
  98. const mesh = new THREE.Mesh( geometry, material );
  99. mesh.rotation.y = - Math.PI / 2;
  100. mesh.position.y = 0.8;
  101. mesh.castShadow = true;
  102. mesh.receiveShadow = true;
  103. scene.add( mesh );
  104. } );
  105. window.addEventListener( 'resize', onWindowResize );
  106. // GUI
  107. const gui = new GUI();
  108. const params = {
  109. map: textures[ 'disturb.jpg' ],
  110. color: spotLight.color.getHex(),
  111. intensity: spotLight.intensity,
  112. distance: spotLight.distance,
  113. angle: spotLight.angle,
  114. penumbra: spotLight.penumbra,
  115. decay: spotLight.decay,
  116. focus: spotLight.shadow.focus,
  117. shadows: true,
  118. customAttenuation: false
  119. };
  120. gui.add( params, 'map', textures ).onChange( function ( val ) {
  121. spotLight.map = val;
  122. } );
  123. gui.addColor( params, 'color' ).onChange( function ( val ) {
  124. spotLight.color.setHex( val );
  125. } );
  126. gui.add( params, 'intensity', 0, 500 ).onChange( function ( val ) {
  127. spotLight.intensity = val;
  128. } );
  129. gui.add( params, 'distance', 0, 20 ).onChange( function ( val ) {
  130. spotLight.distance = val;
  131. } );
  132. gui.add( params, 'angle', 0, Math.PI / 3 ).onChange( function ( val ) {
  133. spotLight.angle = val;
  134. } );
  135. gui.add( params, 'penumbra', 0, 1 ).onChange( function ( val ) {
  136. spotLight.penumbra = val;
  137. } );
  138. gui.add( params, 'decay', 1, 2 ).onChange( function ( val ) {
  139. spotLight.decay = val;
  140. } );
  141. gui.add( params, 'focus', 0, 1 ).onChange( function ( val ) {
  142. spotLight.shadow.focus = val;
  143. } );
  144. gui.add( params, 'shadows' ).onChange( function ( val ) {
  145. renderer.shadowMap.enabled = val;
  146. scene.traverse( function ( child ) {
  147. if ( child.material ) {
  148. child.material.needsUpdate = true;
  149. }
  150. } );
  151. } );
  152. gui.open();
  153. }
  154. function onWindowResize() {
  155. camera.aspect = window.innerWidth / window.innerHeight;
  156. camera.updateProjectionMatrix();
  157. renderer.setSize( window.innerWidth, window.innerHeight );
  158. }
  159. function animate() {
  160. const time = performance.now() / 3000;
  161. spotLight.position.x = Math.cos( time ) * 2.5;
  162. spotLight.position.z = Math.sin( time ) * 2.5;
  163. lightHelper.update();
  164. renderer.render( scene, camera );
  165. }
  166. </script>
  167. </body>
  168. </html>
粤ICP备19079148号