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

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