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

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