threejs-shadows-directional-light-with-camera-gui.html 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  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 - Directional 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.DirectionalLight(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. const helper = new THREE.DirectionalLightHelper(light);
  116. scene.add(helper);
  117. function updateCamera() {
  118. // update the light target's matrixWorld because it's needed by the helper
  119. light.target.updateMatrixWorld();
  120. helper.update();
  121. // update the light's shadow camera's projection matrix
  122. light.shadow.camera.updateProjectionMatrix();
  123. // and now update the camera helper we're using to show the light's shadow camera
  124. cameraHelper.update();
  125. }
  126. updateCamera();
  127. class DimensionGUIHelper {
  128. constructor(obj, minProp, maxProp) {
  129. this.obj = obj;
  130. this.minProp = minProp;
  131. this.maxProp = maxProp;
  132. }
  133. get value() {
  134. return this.obj[this.maxProp] * 2;
  135. }
  136. set value(v) {
  137. this.obj[this.maxProp] = v / 2;
  138. this.obj[this.minProp] = v / -2;
  139. }
  140. }
  141. class MinMaxGUIHelper {
  142. constructor(obj, minProp, maxProp, minDif) {
  143. this.obj = obj;
  144. this.minProp = minProp;
  145. this.maxProp = maxProp;
  146. this.minDif = minDif;
  147. }
  148. get min() {
  149. return this.obj[this.minProp];
  150. }
  151. set min(v) {
  152. this.obj[this.minProp] = v;
  153. this.obj[this.maxProp] = Math.max(this.obj[this.maxProp], v + this.minDif);
  154. }
  155. get max() {
  156. return this.obj[this.maxProp];
  157. }
  158. set max(v) {
  159. this.obj[this.maxProp] = v;
  160. this.min = this.min; // this will call the min setter
  161. }
  162. }
  163. const gui = new dat.GUI();
  164. gui.addColor(new ColorGUIHelper(light, 'color'), 'value').name('color');
  165. gui.add(light, 'intensity', 0, 2, 0.01);
  166. {
  167. const folder = gui.addFolder('Shadow Camera');
  168. folder.open();
  169. folder.add(new DimensionGUIHelper(light.shadow.camera, 'left', 'right'), 'value', 1, 100)
  170. .name('width')
  171. .onChange(updateCamera);
  172. folder.add(new DimensionGUIHelper(light.shadow.camera, 'bottom', 'top'), 'value', 1, 100)
  173. .name('height')
  174. .onChange(updateCamera);
  175. const minMaxGUIHelper = new MinMaxGUIHelper(light.shadow.camera, 'near', 'far', 0.1);
  176. folder.add(minMaxGUIHelper, 'min', 0.1, 50, 0.1).name('near').onChange(updateCamera);
  177. folder.add(minMaxGUIHelper, 'max', 0.1, 50, 0.1).name('far').onChange(updateCamera);
  178. folder.add(light.shadow.camera, 'zoom', 0.01, 1.5, 0.01).onChange(updateCamera);
  179. }
  180. makeXYZGUI(gui, light.position, 'position', updateCamera);
  181. makeXYZGUI(gui, light.target.position, 'target', updateCamera);
  182. }
  183. function resizeRendererToDisplaySize(renderer) {
  184. const canvas = renderer.domElement;
  185. const width = canvas.clientWidth;
  186. const height = canvas.clientHeight;
  187. const needResize = canvas.width !== width || canvas.height !== height;
  188. if (needResize) {
  189. renderer.setSize(width, height, false);
  190. }
  191. return needResize;
  192. }
  193. function render() {
  194. resizeRendererToDisplaySize(renderer);
  195. {
  196. const canvas = renderer.domElement;
  197. camera.aspect = canvas.clientWidth / canvas.clientHeight;
  198. camera.updateProjectionMatrix();
  199. }
  200. renderer.render(scene, camera);
  201. requestAnimationFrame(render);
  202. }
  203. requestAnimationFrame(render);
  204. }
  205. main();
  206. </script>
  207. </html>