webgpu_lights_spotlight.html 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295
  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 { Fn, vec2, length, uniform, abs, max, min, sub, div, saturate, acos } from 'three/tsl';
  26. import { GUI } from 'three/addons/libs/lil-gui.module.min.js';
  27. import { PLYLoader } from 'three/addons/loaders/PLYLoader.js';
  28. import { OrbitControls } from 'three/addons/controls/OrbitControls.js';
  29. let renderer, scene, camera;
  30. let spotLight, lightHelper;
  31. init();
  32. function init() {
  33. // Renderer
  34. renderer = new THREE.WebGPURenderer( { antialias: true } );
  35. renderer.setPixelRatio( window.devicePixelRatio );
  36. renderer.setSize( window.innerWidth, window.innerHeight );
  37. renderer.setAnimationLoop( animate );
  38. document.body.appendChild( renderer.domElement );
  39. renderer.shadowMap.enabled = true;
  40. renderer.shadowMap.type = THREE.PCFSoftShadowMap;
  41. renderer.toneMapping = THREE.ACESFilmicToneMapping;
  42. renderer.toneMappingExposure = 1;
  43. scene = new THREE.Scene();
  44. camera = new THREE.PerspectiveCamera( 40, window.innerWidth / window.innerHeight, 0.1, 100 );
  45. camera.position.set( 7, 4, 1 );
  46. // Controls
  47. const controls = new OrbitControls( camera, renderer.domElement );
  48. controls.minDistance = 2;
  49. controls.maxDistance = 10;
  50. controls.maxPolarAngle = Math.PI / 2;
  51. controls.target.set( 0, 1, 0 );
  52. controls.update();
  53. // Textures
  54. const loader = new THREE.TextureLoader().setPath( 'textures/' );
  55. const filenames = [ 'disturb.jpg', 'colors.png', 'uv_grid_opengl.jpg' ];
  56. const textures = { none: null };
  57. for ( let i = 0; i < filenames.length; i ++ ) {
  58. const filename = filenames[ i ];
  59. const texture = loader.load( filename );
  60. texture.minFilter = THREE.LinearFilter;
  61. texture.magFilter = THREE.LinearFilter;
  62. texture.generateMipmaps = false;
  63. texture.colorSpace = THREE.SRGBColorSpace;
  64. textures[ filename ] = texture;
  65. }
  66. // Lights
  67. const ambient = new THREE.HemisphereLight( 0xffffff, 0x8d8d8d, 0.15 );
  68. scene.add( ambient );
  69. const boxAttenuationFn = Fn( ( [ lightNode ], builder ) => {
  70. const light = lightNode.light;
  71. const sdBox = Fn( ( [ p, b ] ) => {
  72. const d = vec2( abs( p ).sub( b ) ).toVar();
  73. return length( max( d, 0.0 ) ).add( min( max( d.x, d.y ), 0.0 ) );
  74. } );
  75. const penumbraCos = uniform( 'float' ).onRenderUpdate( () => Math.min( Math.cos( light.angle * ( 1 - light.penumbra ) ), .99999 ) );
  76. const spotLightCoord = lightNode.getSpotLightCoord( builder );
  77. const coord = spotLightCoord.xyz.div( spotLightCoord.w );
  78. const boxDist = sdBox( coord.xy.sub( vec2( 0.5 ) ), vec2( 0.5 ) );
  79. const angleFactor = div( -1.0, sub( 1.0, acos( penumbraCos ) ).sub( 1.0 ) );
  80. const attenuation = saturate( boxDist.mul( - 2.0 ).mul( angleFactor ) );
  81. return attenuation;
  82. } );
  83. spotLight = new THREE.SpotLight( 0xffffff, 100 );
  84. spotLight.map = textures[ 'disturb.jpg' ];
  85. spotLight.position.set( 2.5, 5, 2.5 );
  86. spotLight.angle = Math.PI / 6;
  87. spotLight.penumbra = 1;
  88. spotLight.decay = 2;
  89. spotLight.distance = 0;
  90. spotLight.castShadow = true;
  91. spotLight.shadow.mapSize.width = 1024;
  92. spotLight.shadow.mapSize.height = 1024;
  93. spotLight.shadow.camera.near = 1;
  94. spotLight.shadow.camera.far = 10;
  95. spotLight.shadow.focus = 1;
  96. spotLight.shadow.bias = - .003;
  97. scene.add( spotLight );
  98. lightHelper = new THREE.SpotLightHelper( spotLight );
  99. scene.add( lightHelper );
  100. //
  101. const geometry = new THREE.PlaneGeometry( 200, 200 );
  102. const material = new THREE.MeshLambertMaterial( { color: 0xbcbcbc } );
  103. const mesh = new THREE.Mesh( geometry, material );
  104. mesh.position.set( 0, - 1, 0 );
  105. mesh.rotation.x = - Math.PI / 2;
  106. mesh.receiveShadow = true;
  107. scene.add( mesh );
  108. // Models
  109. new PLYLoader().load( 'models/ply/binary/Lucy100k.ply', function ( geometry ) {
  110. geometry.scale( 0.0024, 0.0024, 0.0024 );
  111. geometry.computeVertexNormals();
  112. const material = new THREE.MeshLambertMaterial();
  113. const mesh = new THREE.Mesh( geometry, material );
  114. mesh.rotation.y = - Math.PI / 2;
  115. mesh.position.y = 0.8;
  116. mesh.castShadow = true;
  117. mesh.receiveShadow = true;
  118. scene.add( mesh );
  119. } );
  120. window.addEventListener( 'resize', onWindowResize );
  121. // GUI
  122. const gui = new GUI();
  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. shadows: true,
  133. customAttenuation: 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, 'shadows' ).onChange( function ( val ) {
  160. renderer.shadowMap.enabled = val;
  161. scene.traverse( function ( child ) {
  162. if ( child.material ) {
  163. child.material.needsUpdate = true;
  164. }
  165. } );
  166. } );
  167. gui.add( params, 'customAttenuation' ).name( 'custom attenuation' ).onChange( function ( val ) {
  168. spotLight.attenuationNode = val ? boxAttenuationFn : null;
  169. aspectGUI.setValue( 1 ).enable( val );
  170. } );
  171. const aspectGUI = gui.add( spotLight.shadow, 'aspect', 0, 2 ).enable( false );
  172. gui.open();
  173. }
  174. function onWindowResize() {
  175. camera.aspect = window.innerWidth / window.innerHeight;
  176. camera.updateProjectionMatrix();
  177. renderer.setSize( window.innerWidth, window.innerHeight );
  178. }
  179. function animate() {
  180. const time = performance.now() / 3000;
  181. spotLight.position.x = Math.cos( time ) * 2.5;
  182. spotLight.position.z = Math.sin( time ) * 2.5;
  183. lightHelper.update();
  184. renderer.render( scene, camera );
  185. }
  186. </script>
  187. </body>
  188. </html>
粤ICP备19079148号