threejs-lights-rectarea.html 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  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 src="resources/threejs/r103/three.min.js"></script>
  24. <script src="resources/threejs/r103/js/lights/RectAreaLightUniformsLib.js"></script>
  25. <script src="resources/threejs/r103/js/controls/OrbitControls.js"></script>
  26. <script src="../3rdparty/dat.gui.min.js"></script>
  27. <script>
  28. 'use strict';
  29. /* global THREE, dat */
  30. function main() {
  31. const canvas = document.querySelector('#c');
  32. const renderer = new THREE.WebGLRenderer({canvas: canvas});
  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 THREE.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.PlaneBufferGeometry(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.BoxBufferGeometry(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.SphereBufferGeometry(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.Math.radToDeg(this.obj[this.prop]);
  99. }
  100. set value(v) {
  101. this.obj[this.prop] = THREE.Math.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.Math.degToRad(-90);
  119. scene.add(light);
  120. const helper = new THREE.RectAreaLightHelper(light);
  121. light.add(helper);
  122. function updateLight() {
  123. helper.update();
  124. }
  125. const gui = new dat.GUI();
  126. gui.addColor(new ColorGUIHelper(light, 'color'), 'value').name('color');
  127. gui.add(light, 'intensity', 0, 10, 0.01);
  128. gui.add(light, 'width', 0, 20).onChange(updateLight);
  129. gui.add(light, 'height', 0, 20).onChange(updateLight);
  130. gui.add(new DegRadHelper(light.rotation, 'x'), 'value', -180, 180).name('x rotation').onChange(updateLight);
  131. gui.add(new DegRadHelper(light.rotation, 'y'), 'value', -180, 180).name('y rotation').onChange(updateLight);
  132. gui.add(new DegRadHelper(light.rotation, 'z'), 'value', -180, 180).name('z rotation').onChange(updateLight);
  133. makeXYZGUI(gui, light.position, 'position', updateLight);
  134. }
  135. function resizeRendererToDisplaySize(renderer) {
  136. const canvas = renderer.domElement;
  137. const width = canvas.clientWidth;
  138. const height = canvas.clientHeight;
  139. const needResize = canvas.width !== width || canvas.height !== height;
  140. if (needResize) {
  141. renderer.setSize(width, height, false);
  142. }
  143. return needResize;
  144. }
  145. function render() {
  146. if (resizeRendererToDisplaySize(renderer)) {
  147. const canvas = renderer.domElement;
  148. camera.aspect = canvas.clientWidth / canvas.clientHeight;
  149. camera.updateProjectionMatrix();
  150. }
  151. renderer.render(scene, camera);
  152. requestAnimationFrame(render);
  153. }
  154. requestAnimationFrame(render);
  155. }
  156. main();
  157. </script>
  158. </html>