1
0

webgpu_lights_spotlight.html 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  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 = new THREE.WebGPURenderer( { antialias: true } );
  33. renderer.setPixelRatio( window.devicePixelRatio );
  34. renderer.setSize( window.innerWidth, window.innerHeight );
  35. renderer.setAnimationLoop( animate );
  36. document.body.appendChild( renderer.domElement );
  37. renderer.shadowMap.enabled = true;
  38. renderer.shadowMap.type = THREE.PCFSoftShadowMap;
  39. renderer.toneMapping = THREE.ACESFilmicToneMapping;
  40. renderer.toneMappingExposure = 1;
  41. scene = new THREE.Scene();
  42. camera = new THREE.PerspectiveCamera( 40, window.innerWidth / window.innerHeight, 0.1, 100 );
  43. camera.position.set( 7, 4, 1 );
  44. const controls = new OrbitControls( camera, renderer.domElement );
  45. controls.minDistance = 2;
  46. controls.maxDistance = 10;
  47. controls.maxPolarAngle = Math.PI / 2;
  48. controls.target.set( 0, 1, 0 );
  49. controls.update();
  50. const ambient = new THREE.HemisphereLight( 0xffffff, 0x8d8d8d, 0.15 );
  51. scene.add( ambient );
  52. const loader = new THREE.TextureLoader().setPath( 'textures/' );
  53. const filenames = [ 'disturb.jpg', 'colors.png', 'uv_grid_opengl.jpg' ];
  54. const textures = { none: null };
  55. for ( let i = 0; i < filenames.length; i ++ ) {
  56. const filename = filenames[ i ];
  57. const texture = loader.load( filename );
  58. texture.minFilter = THREE.LinearFilter;
  59. texture.magFilter = THREE.LinearFilter;
  60. texture.generateMipmaps = false;
  61. texture.colorSpace = THREE.SRGBColorSpace;
  62. textures[ filename ] = texture;
  63. }
  64. spotLight = new THREE.SpotLight( 0xffffff, 100 );
  65. spotLight.position.set( 2.5, 5, 2.5 );
  66. spotLight.angle = Math.PI / 6;
  67. spotLight.penumbra = 1;
  68. spotLight.decay = 2;
  69. spotLight.distance = 0;
  70. spotLight.map = textures[ 'disturb.jpg' ];
  71. spotLight.castShadow = true;
  72. spotLight.shadow.mapSize.width = 1024;
  73. spotLight.shadow.mapSize.height = 1024;
  74. spotLight.shadow.camera.near = 1;
  75. spotLight.shadow.camera.far = 10;
  76. spotLight.shadow.focus = 1;
  77. spotLight.shadow.bias = - .003;
  78. scene.add( spotLight );
  79. lightHelper = new THREE.SpotLightHelper( spotLight );
  80. scene.add( lightHelper );
  81. //
  82. const geometry = new THREE.PlaneGeometry( 200, 200 );
  83. const material = new THREE.MeshLambertMaterial( { color: 0xbcbcbc } );
  84. const mesh = new THREE.Mesh( geometry, material );
  85. mesh.position.set( 0, - 1, 0 );
  86. mesh.rotation.x = - Math.PI / 2;
  87. mesh.receiveShadow = true;
  88. scene.add( mesh );
  89. //
  90. new PLYLoader().load( 'models/ply/binary/Lucy100k.ply', function ( geometry ) {
  91. geometry.scale( 0.0024, 0.0024, 0.0024 );
  92. geometry.computeVertexNormals();
  93. const material = new THREE.MeshLambertMaterial();
  94. const mesh = new THREE.Mesh( geometry, material );
  95. mesh.rotation.y = - Math.PI / 2;
  96. mesh.position.y = 0.8;
  97. mesh.castShadow = true;
  98. mesh.receiveShadow = true;
  99. scene.add( mesh );
  100. } );
  101. window.addEventListener( 'resize', onWindowResize );
  102. // GUI
  103. const gui = new GUI();
  104. const params = {
  105. map: textures[ 'disturb.jpg' ],
  106. color: spotLight.color.getHex(),
  107. intensity: spotLight.intensity,
  108. distance: spotLight.distance,
  109. angle: spotLight.angle,
  110. penumbra: spotLight.penumbra,
  111. decay: spotLight.decay,
  112. focus: spotLight.shadow.focus,
  113. shadows: true
  114. };
  115. gui.add( params, 'map', textures ).onChange( function ( val ) {
  116. spotLight.map = val;
  117. } );
  118. gui.addColor( params, 'color' ).onChange( function ( val ) {
  119. spotLight.color.setHex( val );
  120. } );
  121. gui.add( params, 'intensity', 0, 500 ).onChange( function ( val ) {
  122. spotLight.intensity = val;
  123. } );
  124. gui.add( params, 'distance', 0, 20 ).onChange( function ( val ) {
  125. spotLight.distance = val;
  126. } );
  127. gui.add( params, 'angle', 0, Math.PI / 3 ).onChange( function ( val ) {
  128. spotLight.angle = val;
  129. } );
  130. gui.add( params, 'penumbra', 0, 1 ).onChange( function ( val ) {
  131. spotLight.penumbra = val;
  132. } );
  133. gui.add( params, 'decay', 1, 2 ).onChange( function ( val ) {
  134. spotLight.decay = val;
  135. } );
  136. gui.add( params, 'focus', 0, 1 ).onChange( function ( val ) {
  137. spotLight.shadow.focus = val;
  138. } );
  139. gui.add( params, 'shadows' ).onChange( function ( val ) {
  140. renderer.shadowMap.enabled = val;
  141. scene.traverse( function ( child ) {
  142. if ( child.material ) {
  143. child.material.needsUpdate = true;
  144. }
  145. } );
  146. } );
  147. gui.open();
  148. }
  149. function onWindowResize() {
  150. camera.aspect = window.innerWidth / window.innerHeight;
  151. camera.updateProjectionMatrix();
  152. renderer.setSize( window.innerWidth, window.innerHeight );
  153. }
  154. function animate() {
  155. const time = performance.now() / 3000;
  156. spotLight.position.x = Math.cos( time ) * 2.5;
  157. spotLight.position.z = Math.sin( time ) * 2.5;
  158. lightHelper.update();
  159. renderer.render( scene, camera );
  160. }
  161. </script>
  162. </body>
  163. </html>
粤ICP备19079148号