lights-rectarea.html 5.5 KB

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