lights-rectarea.html 5.2 KB

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