webgl_lights_rectarealight.html 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <title>three.js webgl - lights - rect area light</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> - THREE.RectAreaLight<br/>
  12. by <a href="http://github.com/abelnation" target="_blank" rel="noopener">abelnation</a>
  13. </div>
  14. <!-- Import maps polyfill -->
  15. <!-- Remove this when import maps will be widely supported -->
  16. <script async src="https://unpkg.com/[email protected]/dist/es-module-shims.js"></script>
  17. <script type="importmap">
  18. {
  19. "imports": {
  20. "three": "../build/three.module.js",
  21. "three/addons/": "./jsm/"
  22. }
  23. }
  24. </script>
  25. <script type="module">
  26. import * as THREE from 'three';
  27. import Stats from 'three/addons/libs/stats.module.js';
  28. import { OrbitControls } from 'three/addons/controls/OrbitControls.js';
  29. import { RectAreaLightHelper } from 'three/addons/helpers/RectAreaLightHelper.js';
  30. import { RectAreaLightUniformsLib } from 'three/addons/lights/RectAreaLightUniformsLib.js';
  31. THREE.ColorManagement.enabled = false; // TODO: Consider enabling color management.
  32. let renderer, scene, camera;
  33. let stats;
  34. init();
  35. function init() {
  36. renderer = new THREE.WebGLRenderer( { antialias: true } );
  37. renderer.setPixelRatio( window.devicePixelRatio );
  38. renderer.setSize( window.innerWidth, window.innerHeight );
  39. renderer.setAnimationLoop( animation );
  40. document.body.appendChild( renderer.domElement );
  41. camera = new THREE.PerspectiveCamera( 45, window.innerWidth / window.innerHeight, 1, 1000 );
  42. camera.position.set( 0, 5, - 15 );
  43. scene = new THREE.Scene();
  44. RectAreaLightUniformsLib.init();
  45. const rectLight1 = new THREE.RectAreaLight( 0xff0000, 5, 4, 10 );
  46. rectLight1.position.set( - 5, 5, 5 );
  47. scene.add( rectLight1 );
  48. const rectLight2 = new THREE.RectAreaLight( 0x00ff00, 5, 4, 10 );
  49. rectLight2.position.set( 0, 5, 5 );
  50. scene.add( rectLight2 );
  51. const rectLight3 = new THREE.RectAreaLight( 0x0000ff, 5, 4, 10 );
  52. rectLight3.position.set( 5, 5, 5 );
  53. scene.add( rectLight3 );
  54. scene.add( new RectAreaLightHelper( rectLight1 ) );
  55. scene.add( new RectAreaLightHelper( rectLight2 ) );
  56. scene.add( new RectAreaLightHelper( rectLight3 ) );
  57. const geoFloor = new THREE.BoxGeometry( 2000, 0.1, 2000 );
  58. const matStdFloor = new THREE.MeshStandardMaterial( { color: 0x808080, roughness: 0.1, metalness: 0 } );
  59. const mshStdFloor = new THREE.Mesh( geoFloor, matStdFloor );
  60. scene.add( mshStdFloor );
  61. const geoKnot = new THREE.TorusKnotGeometry( 1.5, 0.5, 200, 16 );
  62. const matKnot = new THREE.MeshStandardMaterial( { color: 0xffffff, roughness: 0, metalness: 0 } );
  63. const meshKnot = new THREE.Mesh( geoKnot, matKnot );
  64. meshKnot.name = 'meshKnot';
  65. meshKnot.position.set( 0, 5, 0 );
  66. scene.add( meshKnot );
  67. const controls = new OrbitControls( camera, renderer.domElement );
  68. controls.target.copy( meshKnot.position );
  69. controls.update();
  70. //
  71. window.addEventListener( 'resize', onWindowResize );
  72. stats = new Stats();
  73. document.body.appendChild( stats.dom );
  74. }
  75. function onWindowResize() {
  76. renderer.setSize( window.innerWidth, window.innerHeight );
  77. camera.aspect = ( window.innerWidth / window.innerHeight );
  78. camera.updateProjectionMatrix();
  79. }
  80. function animation( time ) {
  81. const mesh = scene.getObjectByName( 'meshKnot' );
  82. mesh.rotation.y = time / 1000;
  83. renderer.render( scene, camera );
  84. stats.update();
  85. }
  86. </script>
  87. </body>
  88. </html>