webgpu_lights_ies_spotlight.html 6.0 KB

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