threejs-shadows-directional-light-shadow-acne.html 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271
  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/r98/three.min.js"></script>
  24. <script src="resources/threejs/r98/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(-1.4, 14, 12);
  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, planeSize, planeSize);
  54. const positionAttribute = planeGeo.attributes.position;
  55. for (let i = 0; i < positionAttribute.count; ++i) {
  56. positionAttribute.setZ(i, Math.random() * .2);
  57. }
  58. planeGeo.computeVertexNormals();
  59. const planeMat = new THREE.MeshPhongMaterial({
  60. map: texture,
  61. flatShading: true,
  62. });
  63. const mesh = new THREE.Mesh(planeGeo, planeMat);
  64. mesh.castShadow = true;
  65. mesh.receiveShadow = true;
  66. mesh.rotation.x = Math.PI * -.5;
  67. scene.add(mesh);
  68. }
  69. /*
  70. {
  71. const sphereRadius = 4;
  72. const sphereWidthDivisions = 32;
  73. const sphereHeightDivisions = 16;
  74. const phiStart = 0;
  75. const phiLength = Math.PI * 2;
  76. const thetaStart = Math.PI * .5;
  77. const thetaLength = Math.PI * .5;
  78. const sphereGeo = new THREE.SphereBufferGeometry(
  79. sphereRadius,
  80. sphereWidthDivisions,
  81. sphereHeightDivisions,
  82. phiStart,
  83. phiLength,
  84. thetaStart,
  85. thetaLength,
  86. );
  87. const sphereMat = new THREE.MeshPhongMaterial({
  88. color: '#AC8',
  89. side: THREE.DoubleSide,
  90. });
  91. const mesh = new THREE.Mesh(sphereGeo, sphereMat);
  92. mesh.castShadow = true;
  93. mesh.receiveShadow = true;
  94. mesh.position.set(0, sphereRadius + 2, 0);
  95. scene.add(mesh);
  96. }
  97. */
  98. {
  99. const sphereRadius = 4;
  100. const sphereWidthDivisions = 32;
  101. const sphereHeightDivisions = 16;
  102. const sphereGeo = new THREE.SphereBufferGeometry(
  103. sphereRadius,
  104. sphereWidthDivisions,
  105. sphereHeightDivisions,
  106. );
  107. const sphereMat = new THREE.MeshPhongMaterial({
  108. color: '#AC8',
  109. });
  110. const mesh = new THREE.Mesh(sphereGeo, sphereMat);
  111. mesh.castShadow = true;
  112. mesh.receiveShadow = true;
  113. mesh.position.set(0, 0, 0);
  114. scene.add(mesh);
  115. }
  116. class ColorGUIHelper {
  117. constructor(object, prop) {
  118. this.object = object;
  119. this.prop = prop;
  120. }
  121. get value() {
  122. return `#${this.object[this.prop].getHexString()}`;
  123. }
  124. set value(hexString) {
  125. this.object[this.prop].set(hexString);
  126. }
  127. }
  128. function makeXYZGUI(gui, vector3, name, onChangeFn) {
  129. const folder = gui.addFolder(name);
  130. folder.add(vector3, 'x', -10, 10).onChange(onChangeFn);
  131. folder.add(vector3, 'y', 0, 10).onChange(onChangeFn);
  132. folder.add(vector3, 'z', -10, 10).onChange(onChangeFn);
  133. // folder.open();
  134. }
  135. {
  136. const color = 0xFFFFFF;
  137. const intensity = 1;
  138. const light = new THREE.DirectionalLight(color, intensity);
  139. light.castShadow = true;
  140. light.position.set(4, 10, 5);
  141. light.target.position.set(-4, 1, -4);
  142. scene.add(light);
  143. scene.add(light.target);
  144. //light.shadow.bias = -0.001;
  145. light.shadow.camera.near = 0.000001;
  146. light.shadow.camera.far = 1000000;
  147. const cameraHelper = new THREE.CameraHelper(light.shadow.camera);
  148. scene.add(cameraHelper);
  149. const helper = new THREE.DirectionalLightHelper(light);
  150. scene.add(helper);
  151. function updateCamera() {
  152. // update the light target's matrixWorld because it's needed by the helper
  153. light.target.updateMatrixWorld();
  154. helper.update();
  155. // update the light's shadow camera's projection matrix
  156. light.shadow.camera.updateProjectionMatrix();
  157. // and now update the camera helper we're using to show the light's shadow camera
  158. cameraHelper.update();
  159. }
  160. updateCamera();
  161. class DimensionGUIHelper {
  162. constructor(obj, minProp, maxProp) {
  163. this.obj = obj;
  164. this.minProp = minProp;
  165. this.maxProp = maxProp;
  166. }
  167. get value() {
  168. return this.obj[this.maxProp] * 2;
  169. }
  170. set value(v) {
  171. this.obj[this.maxProp] = v / 2;
  172. this.obj[this.minProp] = v / -2;
  173. }
  174. }
  175. class MinMaxGUIHelper {
  176. constructor(obj, minProp, maxProp, minDif) {
  177. this.obj = obj;
  178. this.minProp = minProp;
  179. this.maxProp = maxProp;
  180. this.minDif = minDif;
  181. }
  182. get min() {
  183. return this.obj[this.minProp];
  184. }
  185. set min(v) {
  186. this.obj[this.minProp] = v;
  187. this.obj[this.maxProp] = Math.max(this.obj[this.maxProp], v + this.minDif);
  188. }
  189. get max() {
  190. return this.obj[this.maxProp];
  191. }
  192. set max(v) {
  193. this.obj[this.maxProp] = v;
  194. this.min = this.min; // this will call the min setter
  195. }
  196. }
  197. const gui = new dat.GUI();
  198. gui.addColor(new ColorGUIHelper(light, 'color'), 'value').name('color');
  199. gui.add(light, 'intensity', 0, 2);
  200. {
  201. const folder = gui.addFolder('Shadow Camera');
  202. folder.open();
  203. folder.add(new DimensionGUIHelper(light.shadow.camera, 'left', 'right'), 'value', 1, 100)
  204. .name('width')
  205. .onChange(updateCamera);
  206. folder.add(new DimensionGUIHelper(light.shadow.camera, 'bottom', 'top'), 'value', 1, 100)
  207. .name('height')
  208. .onChange(updateCamera);
  209. const minMaxGUIHelper = new MinMaxGUIHelper(light.shadow.camera, 'near', 'far', 0.1);
  210. folder.add(minMaxGUIHelper, 'min', 0.1, 50, 0.1).name('near').onChange(updateCamera);
  211. folder.add(minMaxGUIHelper, 'max', 0.1, 50, 0.1).name('far').onChange(updateCamera);
  212. folder.add(light.shadow.camera, 'zoom', 0.01, 1.5, 0.01).onChange(updateCamera);
  213. }
  214. makeXYZGUI(gui, light.position, 'position', updateCamera);
  215. makeXYZGUI(gui, light.target.position, 'target', updateCamera);
  216. }
  217. function resizeRendererToDisplaySize(renderer) {
  218. const canvas = renderer.domElement;
  219. const width = canvas.clientWidth;
  220. const height = canvas.clientHeight;
  221. const needResize = canvas.width !== width || canvas.height !== height;
  222. if (needResize) {
  223. renderer.setSize(width, height, false);
  224. }
  225. return needResize;
  226. }
  227. function render() {
  228. resizeRendererToDisplaySize(renderer);
  229. {
  230. const canvas = renderer.domElement;
  231. camera.aspect = canvas.clientWidth / canvas.clientHeight;
  232. camera.updateProjectionMatrix();
  233. }
  234. renderer.render(scene, camera);
  235. requestAnimationFrame(render);
  236. }
  237. requestAnimationFrame(render);
  238. }
  239. main();
  240. </script>
  241. </html>