webgpu_lights_ies_spotlight.html 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <title>three.js webgpu - ies 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. <meta property="og:title" content="three.js webgpu - ies spotlight">
  8. <meta property="og:type" content="website">
  9. <meta property="og:url" content="https://threejs.org/examples/webgpu_lights_ies_spotlight.html">
  10. <meta property="og:image" content="https://threejs.org/examples/screenshots/webgpu_lights_ies_spotlight.jpg">
  11. <link type="text/css" rel="stylesheet" href="example.css">
  12. </head>
  13. <body>
  14. <div id="info">
  15. <a href="https://threejs.org/" target="_blank" rel="noopener" class="logo-link"></a>
  16. <div class="title-wrapper">
  17. <a href="https://threejs.org/" target="_blank" rel="noopener">three.js</a><span>IES Spot Light</span>
  18. </div>
  19. <small>
  20. IES Spot Light with IES texture files.
  21. </small>
  22. </div>
  23. <script type="importmap">
  24. {
  25. "imports": {
  26. "three": "../build/three.webgpu.js",
  27. "three/webgpu": "../build/three.webgpu.js",
  28. "three/tsl": "../build/three.tsl.js",
  29. "three/addons/": "./jsm/"
  30. }
  31. }
  32. </script>
  33. <script type="module">
  34. import * as THREE from 'three/webgpu';
  35. import { OrbitControls } from './jsm/controls/OrbitControls.js';
  36. import { IESLoader } from 'three/addons/loaders/IESLoader.js';
  37. import { Inspector } from 'three/addons/inspector/Inspector.js';
  38. let renderer, scene, camera;
  39. let lights;
  40. async function init() {
  41. const iesLoader = new IESLoader().setPath( './ies/' );
  42. //iesLoader.type = THREE.UnsignedByteType; // LDR
  43. const [ iesTexture1, iesTexture2, iesTexture3, iesTexture4 ] = await Promise.all( [
  44. iesLoader.loadAsync( '007cfb11e343e2f42e3b476be4ab684e.ies' ),
  45. iesLoader.loadAsync( '06b4cfdc8805709e767b5e2e904be8ad.ies' ),
  46. iesLoader.loadAsync( '02a7562c650498ebb301153dbbf59207.ies' ),
  47. iesLoader.loadAsync( '1a936937a49c63374e6d4fbed9252b29.ies' )
  48. ] );
  49. //
  50. scene = new THREE.Scene();
  51. //
  52. const spotLight = new THREE.IESSpotLight( 0xff0000, 500 );
  53. spotLight.position.set( 6.5, 3, 6.5 );
  54. spotLight.angle = Math.PI / 8;
  55. spotLight.penumbra = 0.7;
  56. spotLight.distance = 20;
  57. spotLight.castShadow = true;
  58. spotLight.iesMap = iesTexture1;
  59. spotLight.userData.helper = new THREE.SpotLightHelper( spotLight );
  60. scene.add( spotLight );
  61. scene.add( spotLight.target );
  62. scene.add( spotLight.userData.helper );
  63. //
  64. const spotLight2 = new THREE.IESSpotLight( 0x00ff00, 500 );
  65. spotLight2.position.set( - 6.5, 3, 6.5 );
  66. spotLight2.angle = Math.PI / 8;
  67. spotLight2.penumbra = 0.7;
  68. spotLight2.distance = 20;
  69. spotLight2.castShadow = true;
  70. spotLight2.iesMap = iesTexture2;
  71. spotLight2.userData.helper = new THREE.SpotLightHelper( spotLight2 );
  72. scene.add( spotLight2 );
  73. scene.add( spotLight2.target );
  74. scene.add( spotLight2.userData.helper );
  75. //
  76. const spotLight3 = new THREE.IESSpotLight( 0x0000ff, 500 );
  77. spotLight3.position.set( - 6.5, 3, - 6.5 );
  78. spotLight3.angle = Math.PI / 8;
  79. spotLight3.penumbra = 0.7;
  80. spotLight3.distance = 20;
  81. spotLight3.castShadow = true;
  82. spotLight3.iesMap = iesTexture3;
  83. spotLight3.userData.helper = new THREE.SpotLightHelper( spotLight3 );
  84. scene.add( spotLight3 );
  85. scene.add( spotLight3.target );
  86. scene.add( spotLight3.userData.helper );
  87. //
  88. const spotLight4 = new THREE.IESSpotLight( 0xffffff, 500 );
  89. spotLight4.position.set( 6.5, 3, - 6.5 );
  90. spotLight4.angle = Math.PI / 8;
  91. spotLight4.penumbra = 0.7;
  92. spotLight4.distance = 20;
  93. spotLight4.castShadow = true;
  94. spotLight4.iesMap = iesTexture4;
  95. spotLight4.userData.helper = new THREE.SpotLightHelper( spotLight4 );
  96. scene.add( spotLight4 );
  97. scene.add( spotLight4.target );
  98. scene.add( spotLight4.userData.helper );
  99. //
  100. lights = [ spotLight, spotLight2, spotLight3, spotLight4 ];
  101. //
  102. const material = new THREE.MeshPhongMaterial( { color: 0x999999/*, dithering: true*/ } );
  103. const geometry = new THREE.PlaneGeometry( 200, 200 );
  104. const mesh = new THREE.Mesh( geometry, material );
  105. mesh.rotation.x = - Math.PI * 0.5;
  106. mesh.receiveShadow = true;
  107. scene.add( mesh );
  108. const geometry2 = new THREE.BoxGeometry( 2, 2, 2 );
  109. //const geometry2 = new THREE.IcosahedronGeometry( 1, 5 );
  110. const mesh2 = new THREE.Mesh( geometry2, material );
  111. mesh2.position.y = 1;
  112. mesh2.castShadow = true;
  113. scene.add( mesh2 );
  114. //
  115. renderer = new THREE.WebGPURenderer( { antialias: true } );
  116. renderer.setPixelRatio( window.devicePixelRatio );
  117. renderer.setSize( window.innerWidth, window.innerHeight );
  118. renderer.setAnimationLoop( render );
  119. renderer.shadowMap.enabled = true;
  120. renderer.inspector = new Inspector();
  121. document.body.appendChild( renderer.domElement );
  122. camera = new THREE.PerspectiveCamera( 35, window.innerWidth / window.innerHeight, .1, 100 );
  123. camera.position.set( 16, 4, 1 );
  124. const controls = new OrbitControls( camera, renderer.domElement );
  125. controls.minDistance = 2;
  126. controls.maxDistance = 50;
  127. controls.enablePan = false;
  128. //
  129. function setHelperVisible( value ) {
  130. for ( let i = 0; i < lights.length; i ++ ) {
  131. lights[ i ].userData.helper.visible = value;
  132. }
  133. }
  134. setHelperVisible( false );
  135. //
  136. const gui = renderer.inspector.createParameters( 'Settings' );
  137. gui.add( { helper: false }, 'helper' ).onChange( ( v ) => setHelperVisible( v ) );
  138. //
  139. window.addEventListener( 'resize', onWindowResize );
  140. }
  141. function onWindowResize() {
  142. camera.aspect = window.innerWidth / window.innerHeight;
  143. camera.updateProjectionMatrix();
  144. renderer.setSize( window.innerWidth, window.innerHeight );
  145. }
  146. function render( time ) {
  147. time = time / 1000;
  148. for ( let i = 0; i < lights.length; i ++ ) {
  149. const t = ( Math.sin( ( time + i ) * ( Math.PI / 2 ) ) + 1 ) / 2;
  150. const x = THREE.MathUtils.lerp( lights[ i ].position.x, 0, t );
  151. const z = THREE.MathUtils.lerp( lights[ i ].position.z, 0, t );
  152. lights[ i ].target.position.x = x;
  153. lights[ i ].target.position.z = z;
  154. if ( lights[ i ].userData.helper ) lights[ i ].userData.helper.update();
  155. }
  156. renderer.render( scene, camera );
  157. }
  158. init();
  159. </script>
  160. </body>
  161. </html>
粤ICP备19079148号