lights-rectarea.html 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  1. <!-- Licensed under a BSD license. See license.html for license -->
  2. <!DOCTYPE html>
  3. <html>
  4. <head>
  5. <meta charset="utf-8">
  6. <meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=yes">
  7. <title>Three.js - Lights - RectArea</title>
  8. <style>
  9. html, body {
  10. margin: 0;
  11. height: 100%;
  12. }
  13. #c {
  14. width: 100%;
  15. height: 100%;
  16. display: block;
  17. }
  18. </style>
  19. </head>
  20. <body>
  21. <canvas id="c"></canvas>
  22. </body>
  23. <!-- Import maps polyfill -->
  24. <!-- Remove this when import maps will be widely supported -->
  25. <script async src="https://unpkg.com/[email protected]/dist/es-module-shims.js"></script>
  26. <script type="importmap">
  27. {
  28. "imports": {
  29. "three": "../../build/three.module.js",
  30. "three/addons/": "../../examples/jsm/"
  31. }
  32. }
  33. </script>
  34. <script type="module">
  35. import * as THREE from 'three';
  36. import { OrbitControls } from 'three/addons/controls/OrbitControls.js';
  37. import { RectAreaLightUniformsLib } from 'three/addons/lights/RectAreaLightUniformsLib.js';
  38. import { RectAreaLightHelper } from 'three/addons/helpers/RectAreaLightHelper.js';
  39. import { GUI } from 'three/addons/libs/lil-gui.module.min.js';
  40. function main() {
  41. const canvas = document.querySelector( '#c' );
  42. const renderer = new THREE.WebGLRenderer( { antialias: true, canvas } );
  43. RectAreaLightUniformsLib.init();
  44. const fov = 45;
  45. const aspect = 2; // the canvas default
  46. const near = 0.1;
  47. const far = 100;
  48. const camera = new THREE.PerspectiveCamera( fov, aspect, near, far );
  49. camera.position.set( 0, 10, 20 );
  50. const controls = new OrbitControls( camera, canvas );
  51. controls.target.set( 0, 5, 0 );
  52. controls.update();
  53. const scene = new THREE.Scene();
  54. scene.background = new THREE.Color( 'black' );
  55. {
  56. const planeSize = 40;
  57. const loader = new THREE.TextureLoader();
  58. const texture = loader.load( 'resources/images/checker.png' );
  59. texture.wrapS = THREE.RepeatWrapping;
  60. texture.wrapT = THREE.RepeatWrapping;
  61. texture.magFilter = THREE.NearestFilter;
  62. texture.colorSpace = THREE.SRGBColorSpace;
  63. const repeats = planeSize / 2;
  64. texture.repeat.set( repeats, repeats );
  65. const planeGeo = new THREE.PlaneGeometry( planeSize, planeSize );
  66. const planeMat = new THREE.MeshStandardMaterial( {
  67. map: texture,
  68. side: THREE.DoubleSide,
  69. } );
  70. const mesh = new THREE.Mesh( planeGeo, planeMat );
  71. mesh.rotation.x = Math.PI * - .5;
  72. scene.add( mesh );
  73. }
  74. {
  75. const cubeSize = 4;
  76. const cubeGeo = new THREE.BoxGeometry( cubeSize, cubeSize, cubeSize );
  77. const cubeMat = new THREE.MeshStandardMaterial( { color: '#8AC' } );
  78. const mesh = new THREE.Mesh( cubeGeo, cubeMat );
  79. mesh.position.set( cubeSize + 1, cubeSize / 2, 0 );
  80. scene.add( mesh );
  81. }
  82. {
  83. const sphereRadius = 3;
  84. const sphereWidthDivisions = 32;
  85. const sphereHeightDivisions = 16;
  86. const sphereGeo = new THREE.SphereGeometry( sphereRadius, sphereWidthDivisions, sphereHeightDivisions );
  87. const sphereMat = new THREE.MeshStandardMaterial( { color: '#CA8' } );
  88. const mesh = new THREE.Mesh( sphereGeo, sphereMat );
  89. mesh.position.set( - sphereRadius - 1, sphereRadius + 2, 0 );
  90. scene.add( mesh );
  91. }
  92. class ColorGUIHelper {
  93. constructor( object, prop ) {
  94. this.object = object;
  95. this.prop = prop;
  96. }
  97. get value() {
  98. return `#${this.object[ this.prop ].getHexString()}`;
  99. }
  100. set value( hexString ) {
  101. this.object[ this.prop ].set( hexString );
  102. }
  103. }
  104. class DegRadHelper {
  105. constructor( obj, prop ) {
  106. this.obj = obj;
  107. this.prop = prop;
  108. }
  109. get value() {
  110. return THREE.MathUtils.radToDeg( this.obj[ this.prop ] );
  111. }
  112. set value( v ) {
  113. this.obj[ this.prop ] = THREE.MathUtils.degToRad( v );
  114. }
  115. }
  116. function makeXYZGUI( gui, vector3, name, onChangeFn ) {
  117. const folder = gui.addFolder( name );
  118. folder.add( vector3, 'x', - 10, 10 ).onChange( onChangeFn );
  119. folder.add( vector3, 'y', 0, 10 ).onChange( onChangeFn );
  120. folder.add( vector3, 'z', - 10, 10 ).onChange( onChangeFn );
  121. folder.open();
  122. }
  123. {
  124. const color = 0xFFFFFF;
  125. const intensity = 5;
  126. const width = 12;
  127. const height = 4;
  128. const light = new THREE.RectAreaLight( color, intensity, width, height );
  129. light.position.set( 0, 10, 0 );
  130. light.rotation.x = THREE.MathUtils.degToRad( - 90 );
  131. scene.add( light );
  132. const helper = new RectAreaLightHelper( light );
  133. light.add( helper );
  134. const gui = new GUI();
  135. gui.addColor( new ColorGUIHelper( light, 'color' ), 'value' ).name( 'color' );
  136. gui.add( light, 'intensity', 0, 10, 0.01 );
  137. gui.add( light, 'width', 0, 20 );
  138. gui.add( light, 'height', 0, 20 );
  139. gui.add( new DegRadHelper( light.rotation, 'x' ), 'value', - 180, 180 ).name( 'x rotation' );
  140. gui.add( new DegRadHelper( light.rotation, 'y' ), 'value', - 180, 180 ).name( 'y rotation' );
  141. gui.add( new DegRadHelper( light.rotation, 'z' ), 'value', - 180, 180 ).name( 'z rotation' );
  142. makeXYZGUI( gui, light.position, 'position' );
  143. }
  144. function resizeRendererToDisplaySize( renderer ) {
  145. const canvas = renderer.domElement;
  146. const width = canvas.clientWidth;
  147. const height = canvas.clientHeight;
  148. const needResize = canvas.width !== width || canvas.height !== height;
  149. if ( needResize ) {
  150. renderer.setSize( width, height, false );
  151. }
  152. return needResize;
  153. }
  154. function render() {
  155. if ( resizeRendererToDisplaySize( renderer ) ) {
  156. const canvas = renderer.domElement;
  157. camera.aspect = canvas.clientWidth / canvas.clientHeight;
  158. camera.updateProjectionMatrix();
  159. }
  160. renderer.render( scene, camera );
  161. requestAnimationFrame( render );
  162. }
  163. requestAnimationFrame( render );
  164. }
  165. main();
  166. </script>
  167. </html>