webgpu_lights_ies_spotlight.html 5.7 KB

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