lights-rectarea.html 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  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. renderer.useLegacyLights = false;
  44. RectAreaLightUniformsLib.init();
  45. const fov = 45;
  46. const aspect = 2; // the canvas default
  47. const near = 0.1;
  48. const far = 100;
  49. const camera = new THREE.PerspectiveCamera( fov, aspect, near, far );
  50. camera.position.set( 0, 10, 20 );
  51. const controls = new OrbitControls( camera, canvas );
  52. controls.target.set( 0, 5, 0 );
  53. controls.update();
  54. const scene = new THREE.Scene();
  55. scene.background = new THREE.Color( 'black' );
  56. {
  57. const planeSize = 40;
  58. const loader = new THREE.TextureLoader();
  59. const texture = loader.load( 'resources/images/checker.png' );
  60. texture.wrapS = THREE.RepeatWrapping;
  61. texture.wrapT = THREE.RepeatWrapping;
  62. texture.magFilter = THREE.NearestFilter;
  63. texture.colorSpace = THREE.SRGBColorSpace;
  64. const repeats = planeSize / 2;
  65. texture.repeat.set( repeats, repeats );
  66. const planeGeo = new THREE.PlaneGeometry( planeSize, planeSize );
  67. const planeMat = new THREE.MeshStandardMaterial( {
  68. map: texture,
  69. side: THREE.DoubleSide,
  70. } );
  71. const mesh = new THREE.Mesh( planeGeo, planeMat );
  72. mesh.rotation.x = Math.PI * - .5;
  73. scene.add( mesh );
  74. }
  75. {
  76. const cubeSize = 4;
  77. const cubeGeo = new THREE.BoxGeometry( cubeSize, cubeSize, cubeSize );
  78. const cubeMat = new THREE.MeshStandardMaterial( { color: '#8AC' } );
  79. const mesh = new THREE.Mesh( cubeGeo, cubeMat );
  80. mesh.position.set( cubeSize + 1, cubeSize / 2, 0 );
  81. scene.add( mesh );
  82. }
  83. {
  84. const sphereRadius = 3;
  85. const sphereWidthDivisions = 32;
  86. const sphereHeightDivisions = 16;
  87. const sphereGeo = new THREE.SphereGeometry( sphereRadius, sphereWidthDivisions, sphereHeightDivisions );
  88. const sphereMat = new THREE.MeshStandardMaterial( { color: '#CA8' } );
  89. const mesh = new THREE.Mesh( sphereGeo, sphereMat );
  90. mesh.position.set( - sphereRadius - 1, sphereRadius + 2, 0 );
  91. scene.add( mesh );
  92. }
  93. class ColorGUIHelper {
  94. constructor( object, prop ) {
  95. this.object = object;
  96. this.prop = prop;
  97. }
  98. get value() {
  99. return `#${this.object[ this.prop ].getHexString()}`;
  100. }
  101. set value( hexString ) {
  102. this.object[ this.prop ].set( hexString );
  103. }
  104. }
  105. class DegRadHelper {
  106. constructor( obj, prop ) {
  107. this.obj = obj;
  108. this.prop = prop;
  109. }
  110. get value() {
  111. return THREE.MathUtils.radToDeg( this.obj[ this.prop ] );
  112. }
  113. set value( v ) {
  114. this.obj[ this.prop ] = THREE.MathUtils.degToRad( v );
  115. }
  116. }
  117. function makeXYZGUI( gui, vector3, name, onChangeFn ) {
  118. const folder = gui.addFolder( name );
  119. folder.add( vector3, 'x', - 10, 10 ).onChange( onChangeFn );
  120. folder.add( vector3, 'y', 0, 10 ).onChange( onChangeFn );
  121. folder.add( vector3, 'z', - 10, 10 ).onChange( onChangeFn );
  122. folder.open();
  123. }
  124. {
  125. const color = 0xFFFFFF;
  126. const intensity = 5;
  127. const width = 12;
  128. const height = 4;
  129. const light = new THREE.RectAreaLight( color, intensity, width, height );
  130. light.position.set( 0, 10, 0 );
  131. light.rotation.x = THREE.MathUtils.degToRad( - 90 );
  132. scene.add( light );
  133. const helper = new RectAreaLightHelper( light );
  134. light.add( helper );
  135. const gui = new GUI();
  136. gui.addColor( new ColorGUIHelper( light, 'color' ), 'value' ).name( 'color' );
  137. gui.add( light, 'intensity', 0, 10, 0.01 );
  138. gui.add( light, 'width', 0, 20 );
  139. gui.add( light, 'height', 0, 20 );
  140. gui.add( new DegRadHelper( light.rotation, 'x' ), 'value', - 180, 180 ).name( 'x rotation' );
  141. gui.add( new DegRadHelper( light.rotation, 'y' ), 'value', - 180, 180 ).name( 'y rotation' );
  142. gui.add( new DegRadHelper( light.rotation, 'z' ), 'value', - 180, 180 ).name( 'z rotation' );
  143. makeXYZGUI( gui, light.position, 'position' );
  144. }
  145. function resizeRendererToDisplaySize( renderer ) {
  146. const canvas = renderer.domElement;
  147. const width = canvas.clientWidth;
  148. const height = canvas.clientHeight;
  149. const needResize = canvas.width !== width || canvas.height !== height;
  150. if ( needResize ) {
  151. renderer.setSize( width, height, false );
  152. }
  153. return needResize;
  154. }
  155. function render() {
  156. if ( resizeRendererToDisplaySize( renderer ) ) {
  157. const canvas = renderer.domElement;
  158. camera.aspect = canvas.clientWidth / canvas.clientHeight;
  159. camera.updateProjectionMatrix();
  160. }
  161. renderer.render( scene, camera );
  162. requestAnimationFrame( render );
  163. }
  164. requestAnimationFrame( render );
  165. }
  166. main();
  167. </script>
  168. </html>