threejs-shadows-spot-light-with-camera-gui.html 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  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 - Shadows - Spot Light w/CameraHelper</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/r102/three.min.js"></script>
  24. <script src="resources/threejs/r102/js/controls/OrbitControls.js"></script>
  25. <script src="../3rdparty/dat.gui.min.js"></script>
  26. <script>
  27. 'use strict';
  28. /* global THREE, dat */
  29. function main() {
  30. const canvas = document.querySelector('#c');
  31. const renderer = new THREE.WebGLRenderer({canvas: canvas});
  32. renderer.shadowMap.enabled = true;
  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.MeshPhongMaterial({
  55. map: texture,
  56. side: THREE.DoubleSide,
  57. });
  58. const mesh = new THREE.Mesh(planeGeo, planeMat);
  59. mesh.receiveShadow = true;
  60. mesh.rotation.x = Math.PI * -.5;
  61. scene.add(mesh);
  62. }
  63. {
  64. const cubeSize = 4;
  65. const cubeGeo = new THREE.BoxBufferGeometry(cubeSize, cubeSize, cubeSize);
  66. const cubeMat = new THREE.MeshPhongMaterial({color: '#8AC'});
  67. const mesh = new THREE.Mesh(cubeGeo, cubeMat);
  68. mesh.castShadow = true;
  69. mesh.receiveShadow = true;
  70. mesh.position.set(cubeSize + 1, cubeSize / 2, 0);
  71. scene.add(mesh);
  72. }
  73. {
  74. const sphereRadius = 3;
  75. const sphereWidthDivisions = 32;
  76. const sphereHeightDivisions = 16;
  77. const sphereGeo = new THREE.SphereBufferGeometry(sphereRadius, sphereWidthDivisions, sphereHeightDivisions);
  78. const sphereMat = new THREE.MeshPhongMaterial({color: '#CA8'});
  79. const mesh = new THREE.Mesh(sphereGeo, sphereMat);
  80. mesh.castShadow = true;
  81. mesh.receiveShadow = true;
  82. mesh.position.set(-sphereRadius - 1, sphereRadius + 2, 0);
  83. scene.add(mesh);
  84. }
  85. class ColorGUIHelper {
  86. constructor(object, prop) {
  87. this.object = object;
  88. this.prop = prop;
  89. }
  90. get value() {
  91. return `#${this.object[this.prop].getHexString()}`;
  92. }
  93. set value(hexString) {
  94. this.object[this.prop].set(hexString);
  95. }
  96. }
  97. function makeXYZGUI(gui, vector3, name, onChangeFn) {
  98. const folder = gui.addFolder(name);
  99. folder.add(vector3, 'x', -10, 10).onChange(onChangeFn);
  100. folder.add(vector3, 'y', 0, 10).onChange(onChangeFn);
  101. folder.add(vector3, 'z', -10, 10).onChange(onChangeFn);
  102. // folder.open();
  103. }
  104. {
  105. const color = 0xFFFFFF;
  106. const intensity = 1;
  107. const light = new THREE.SpotLight(color, intensity);
  108. light.castShadow = true;
  109. light.position.set(0, 10, 0);
  110. light.target.position.set(-4, 0, -4);
  111. scene.add(light);
  112. scene.add(light.target);
  113. const cameraHelper = new THREE.CameraHelper(light.shadow.camera);
  114. scene.add(cameraHelper);
  115. function updateCamera() {
  116. // update the light target's matrixWorld because it's needed by the helper
  117. light.target.updateMatrixWorld();
  118. // update the light's shadow camera's projection matrix
  119. light.shadow.camera.updateProjectionMatrix();
  120. // and now update the camera helper we're using to show the light's shadow camera
  121. cameraHelper.update();
  122. }
  123. updateCamera();
  124. setTimeout(updateCamera);
  125. class MinMaxGUIHelper {
  126. constructor(obj, minProp, maxProp, minDif) {
  127. this.obj = obj;
  128. this.minProp = minProp;
  129. this.maxProp = maxProp;
  130. this.minDif = minDif;
  131. }
  132. get min() {
  133. return this.obj[this.minProp];
  134. }
  135. set min(v) {
  136. this.obj[this.minProp] = v;
  137. this.obj[this.maxProp] = Math.max(this.obj[this.maxProp], v + this.minDif);
  138. }
  139. get max() {
  140. return this.obj[this.maxProp];
  141. }
  142. set max(v) {
  143. this.obj[this.maxProp] = v;
  144. this.min = this.min; // this will call the min setter
  145. }
  146. }
  147. class DegRadHelper {
  148. constructor(obj, prop) {
  149. this.obj = obj;
  150. this.prop = prop;
  151. }
  152. get value() {
  153. return THREE.Math.radToDeg(this.obj[this.prop]);
  154. }
  155. set value(v) {
  156. this.obj[this.prop] = THREE.Math.degToRad(v);
  157. }
  158. }
  159. const gui = new dat.GUI();
  160. gui.addColor(new ColorGUIHelper(light, 'color'), 'value').name('color');
  161. gui.add(light, 'intensity', 0, 2, 0.01);
  162. gui.add(light, 'distance', 0, 40).onChange(updateCamera);
  163. gui.add(new DegRadHelper(light, 'angle'), 'value', 0, 90).name('angle').onChange(updateCamera);
  164. gui.add(light, 'penumbra', 0, 1, 0.01);
  165. {
  166. const folder = gui.addFolder('Shadow Camera');
  167. folder.open();
  168. const minMaxGUIHelper = new MinMaxGUIHelper(light.shadow.camera, 'near', 'far', 0.1);
  169. folder.add(minMaxGUIHelper, 'min', 0.1, 50, 0.1).name('near').onChange(updateCamera);
  170. folder.add(minMaxGUIHelper, 'max', 0.1, 50, 0.1).name('far').onChange(updateCamera);
  171. }
  172. makeXYZGUI(gui, light.position, 'position', updateCamera);
  173. makeXYZGUI(gui, light.target.position, 'target', updateCamera);
  174. }
  175. function resizeRendererToDisplaySize(renderer) {
  176. const canvas = renderer.domElement;
  177. const width = canvas.clientWidth;
  178. const height = canvas.clientHeight;
  179. const needResize = canvas.width !== width || canvas.height !== height;
  180. if (needResize) {
  181. renderer.setSize(width, height, false);
  182. }
  183. return needResize;
  184. }
  185. function render() {
  186. resizeRendererToDisplaySize(renderer);
  187. {
  188. const canvas = renderer.domElement;
  189. camera.aspect = canvas.clientWidth / canvas.clientHeight;
  190. camera.updateProjectionMatrix();
  191. }
  192. renderer.render(scene, camera);
  193. requestAnimationFrame(render);
  194. }
  195. requestAnimationFrame(render);
  196. }
  197. main();
  198. </script>
  199. </html>