shadows-spot-light-with-shadow-radius.html 6.6 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 type="module">
  24. import * as THREE from '../../build/three.module.js';
  25. import {OrbitControls} from '../../examples/jsm/controls/OrbitControls.js';
  26. import {GUI} from '../../examples/jsm/libs/lil-gui.module.min.js';
  27. function main() {
  28. const canvas = document.querySelector('#c');
  29. const renderer = new THREE.WebGLRenderer({canvas});
  30. renderer.shadowMap.enabled = true;
  31. renderer.shadowMap.type = THREE.PCFSoftShadowMap; // default THREE.PCFShadowMap
  32. const fov = 45;
  33. const aspect = 2; // the canvas default
  34. const near = 0.1;
  35. const far = 100;
  36. const camera = new THREE.PerspectiveCamera(fov, aspect, near, far);
  37. camera.position.set(0, 10, 20);
  38. const controls = new OrbitControls(camera, canvas);
  39. controls.target.set(0, 5, 0);
  40. controls.update();
  41. const scene = new THREE.Scene();
  42. scene.background = new THREE.Color('black');
  43. {
  44. const planeSize = 40;
  45. const loader = new THREE.TextureLoader();
  46. const texture = loader.load('resources/images/checker.png');
  47. texture.wrapS = THREE.RepeatWrapping;
  48. texture.wrapT = THREE.RepeatWrapping;
  49. texture.magFilter = THREE.NearestFilter;
  50. const repeats = planeSize / 2;
  51. texture.repeat.set(repeats, repeats);
  52. const planeGeo = new THREE.PlaneGeometry(planeSize, planeSize);
  53. const planeMat = new THREE.MeshPhongMaterial({
  54. map: texture,
  55. side: THREE.DoubleSide,
  56. });
  57. const mesh = new THREE.Mesh(planeGeo, planeMat);
  58. mesh.receiveShadow = true;
  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.MeshPhongMaterial({color: '#8AC'});
  66. const mesh = new THREE.Mesh(cubeGeo, cubeMat);
  67. mesh.castShadow = true;
  68. mesh.receiveShadow = true;
  69. mesh.position.set(cubeSize + 1, cubeSize / 2, 0);
  70. scene.add(mesh);
  71. }
  72. {
  73. const sphereRadius = 3;
  74. const sphereWidthDivisions = 32;
  75. const sphereHeightDivisions = 16;
  76. const sphereGeo = new THREE.SphereGeometry(sphereRadius, sphereWidthDivisions, sphereHeightDivisions);
  77. const sphereMat = new THREE.MeshPhongMaterial({color: '#CA8'});
  78. const mesh = new THREE.Mesh(sphereGeo, sphereMat);
  79. mesh.castShadow = true;
  80. mesh.receiveShadow = true;
  81. mesh.position.set(-sphereRadius - 1, sphereRadius + 2, 0);
  82. scene.add(mesh);
  83. }
  84. class ColorGUIHelper {
  85. constructor(object, prop) {
  86. this.object = object;
  87. this.prop = prop;
  88. }
  89. get value() {
  90. return `#${this.object[this.prop].getHexString()}`;
  91. }
  92. set value(hexString) {
  93. this.object[this.prop].set(hexString);
  94. }
  95. }
  96. function makeXYZGUI(gui, vector3, name, onChangeFn) {
  97. const folder = gui.addFolder(name);
  98. folder.add(vector3, 'x', -10, 10).onChange(onChangeFn);
  99. folder.add(vector3, 'y', 0, 10).onChange(onChangeFn);
  100. folder.add(vector3, 'z', -10, 10).onChange(onChangeFn);
  101. // folder.open();
  102. }
  103. {
  104. const color = 0xFFFFFF;
  105. const intensity = 1;
  106. const light = new THREE.SpotLight(color, intensity);
  107. light.castShadow = true;
  108. light.position.set(0, 10, 0);
  109. light.target.position.set(-4, 0, -4);
  110. scene.add(light);
  111. scene.add(light.target);
  112. const cameraHelper = new THREE.CameraHelper(light.shadow.camera);
  113. scene.add(cameraHelper);
  114. function updateCamera() {
  115. // update the light target's matrixWorld because it's needed by the helper
  116. light.target.updateMatrixWorld();
  117. // update the light's shadow camera's projection matrix
  118. light.shadow.camera.updateProjectionMatrix();
  119. // and now update the camera helper we're using to show the light's shadow camera
  120. cameraHelper.update();
  121. }
  122. updateCamera();
  123. setTimeout(updateCamera);
  124. class MinMaxGUIHelper {
  125. constructor(obj, minProp, maxProp, minDif) {
  126. this.obj = obj;
  127. this.minProp = minProp;
  128. this.maxProp = maxProp;
  129. this.minDif = minDif;
  130. }
  131. get min() {
  132. return this.obj[this.minProp];
  133. }
  134. set min(v) {
  135. this.obj[this.minProp] = v;
  136. this.obj[this.maxProp] = Math.max(this.obj[this.maxProp], v + this.minDif);
  137. }
  138. get max() {
  139. return this.obj[this.maxProp];
  140. }
  141. set max(v) {
  142. this.obj[this.maxProp] = v;
  143. this.min = this.min; // this will call the min setter
  144. }
  145. }
  146. class DegRadHelper {
  147. constructor(obj, prop) {
  148. this.obj = obj;
  149. this.prop = prop;
  150. }
  151. get value() {
  152. return THREE.MathUtils.radToDeg(this.obj[this.prop]);
  153. }
  154. set value(v) {
  155. this.obj[this.prop] = THREE.MathUtils.degToRad(v);
  156. }
  157. }
  158. const gui = new GUI();
  159. gui.addColor(new ColorGUIHelper(light, 'color'), 'value').name('color');
  160. gui.add(light, 'intensity', 0, 2, 0.01);
  161. gui.add(light, 'distance', 0, 40).onChange(updateCamera);
  162. gui.add(new DegRadHelper(light, 'angle'), 'value', 0, 90).name('angle').onChange(updateCamera);
  163. gui.add(light, 'penumbra', 0, 1, 0.01);
  164. gui.add(light.shadow, 'radius', 0, 50, 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>