webgpu_lights_ies_spotlight.html 4.4 KB

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