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

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