webgpu_lights_ies_spotlight.html 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <title>three.js WebGPU - lights - 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. <!-- Import maps polyfill -->
  14. <!-- Remove this when import maps will be widely supported -->
  15. <script async src="https://unpkg.com/[email protected]/dist/es-module-shims.js"></script>
  16. <script type="importmap">
  17. {
  18. "imports": {
  19. "three": "../build/three.module.js",
  20. "three/addons/": "./jsm/",
  21. "three/nodes": "./jsm/nodes/Nodes.js"
  22. }
  23. }
  24. </script>
  25. <script type="module">
  26. import * as THREE from 'three';
  27. import WebGPU from 'three/addons/capabilities/WebGPU.js';
  28. import WebGPURenderer from 'three/addons/renderers/webgpu/WebGPURenderer.js';
  29. import IESSpotLight from 'three/addons/lights/IESSpotLight.js';
  30. import { OrbitControls } from './jsm/controls/OrbitControls.js';
  31. import { IESLoader } from './jsm/loaders/IESLoader.js';
  32. let renderer, scene, camera;
  33. let lights;
  34. async function init() {
  35. if ( WebGPU.isAvailable() === false ) {
  36. document.body.appendChild( WebGPU.getErrorMessage() );
  37. throw new Error( 'No WebGPU support' );
  38. }
  39. //
  40. scene = new THREE.Scene();
  41. //
  42. const iesLoader = new IESLoader().setPath( './ies/' );
  43. //iesLoader.type = THREE.UnsignedByteType; // LDR
  44. const [ iesTexture1, iesTexture2, iesTexture3, iesTexture4 ] = await Promise.all( [
  45. iesLoader.loadAsync( '007cfb11e343e2f42e3b476be4ab684e.ies' ),
  46. iesLoader.loadAsync( '06b4cfdc8805709e767b5e2e904be8ad.ies' ),
  47. iesLoader.loadAsync( '02a7562c650498ebb301153dbbf59207.ies' ),
  48. iesLoader.loadAsync( '1a936937a49c63374e6d4fbed9252b29.ies' )
  49. ] );
  50. //
  51. const spotLight = new IESSpotLight( 0xff0000, 500 );
  52. spotLight.position.set( 6.5, 1.5, 6.5 );
  53. spotLight.angle = Math.PI / 8;
  54. spotLight.penumbra = 0.7;
  55. spotLight.distance = 20;
  56. spotLight.iesMap = iesTexture1;
  57. scene.add( spotLight );
  58. //
  59. const spotLight2 = new IESSpotLight( 0x00ff00, 500 );
  60. spotLight2.position.set( 6.5, 1.5, - 6.5 );
  61. spotLight2.angle = Math.PI / 8;
  62. spotLight2.penumbra = 0.7;
  63. spotLight2.distance = 20;
  64. spotLight2.iesMap = iesTexture2;
  65. scene.add( spotLight2 );
  66. //
  67. const spotLight3 = new IESSpotLight( 0x0000ff, 500 );
  68. spotLight3.position.set( - 6.5, 1.5, - 6.5 );
  69. spotLight3.angle = Math.PI / 8;
  70. spotLight3.penumbra = 0.7;
  71. spotLight3.distance = 20;
  72. spotLight3.iesMap = iesTexture3;
  73. scene.add( spotLight3 );
  74. //
  75. const spotLight4 = new IESSpotLight( 0xffffff, 500 );
  76. spotLight4.position.set( - 6.5, 1.5, 6.5 );
  77. spotLight4.angle = Math.PI / 8;
  78. spotLight4.penumbra = 0.7;
  79. spotLight4.distance = 20;
  80. spotLight4.iesMap = iesTexture4;
  81. scene.add( spotLight4 );
  82. //
  83. lights = [ spotLight, spotLight2, spotLight3, spotLight4 ];
  84. //
  85. const material = new THREE.MeshPhongMaterial( { color: 0x808080/*, dithering: true*/ } );
  86. const geometry = new THREE.PlaneGeometry( 200, 200 );
  87. const mesh = new THREE.Mesh( geometry, material );
  88. mesh.rotation.x = - Math.PI * 0.5;
  89. scene.add( mesh );
  90. //
  91. renderer = new WebGPURenderer();
  92. renderer.setPixelRatio( window.devicePixelRatio );
  93. renderer.setSize( window.innerWidth, window.innerHeight );
  94. renderer.setAnimationLoop( render );
  95. renderer.outputEncoding = THREE.sRGBEncoding;
  96. document.body.appendChild( renderer.domElement );
  97. camera = new THREE.PerspectiveCamera( 35, window.innerWidth / window.innerHeight, .1, 100 );
  98. camera.position.set( 16, 4, 1 );
  99. const controls = new OrbitControls( camera, renderer.domElement );
  100. controls.addEventListener( 'change', render );
  101. controls.minDistance = 2;
  102. controls.maxDistance = 50;
  103. controls.enablePan = false;
  104. //
  105. window.addEventListener( 'resize', onWindowResize );
  106. }
  107. function onWindowResize() {
  108. camera.aspect = window.innerWidth / window.innerHeight;
  109. camera.updateProjectionMatrix();
  110. renderer.setSize( window.innerWidth, window.innerHeight );
  111. }
  112. function render( time ) {
  113. time = ( time / 1000 ) * 2.0;
  114. for ( let i = 0; i < lights.length; i ++ ) {
  115. lights[ i ].position.y = Math.sin( time + i ) + 0.97;
  116. }
  117. renderer.render( scene, camera );
  118. }
  119. init();
  120. </script>
  121. </body>
  122. </html>