threejs-shadows-spot-light-with-shadow-radius.html 6.7 KB

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