CSM.js 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382
  1. ( function () {
  2. const _cameraToLightMatrix = new THREE.Matrix4();
  3. const _lightSpaceFrustum = new THREE.CSMFrustum();
  4. const _center = new THREE.Vector3();
  5. const _bbox = new THREE.Box3();
  6. const _uniformArray = [];
  7. const _logArray = [];
  8. class CSM {
  9. constructor( data ) {
  10. data = data || {};
  11. this.camera = data.camera;
  12. this.parent = data.parent;
  13. this.cascades = data.cascades || 3;
  14. this.maxFar = data.maxFar || 100000;
  15. this.mode = data.mode || 'practical';
  16. this.shadowMapSize = data.shadowMapSize || 2048;
  17. this.shadowBias = data.shadowBias || 0.000001;
  18. this.lightDirection = data.lightDirection || new THREE.Vector3( 1, - 1, 1 ).normalize();
  19. this.lightIntensity = data.lightIntensity || 1;
  20. this.lightNear = data.lightNear || 1;
  21. this.lightFar = data.lightFar || 2000;
  22. this.lightMargin = data.lightMargin || 200;
  23. this.customSplitsCallback = data.customSplitsCallback;
  24. this.fade = false;
  25. this.mainFrustum = new THREE.CSMFrustum();
  26. this.frustums = [];
  27. this.breaks = [];
  28. this.lights = [];
  29. this.shaders = new Map();
  30. this.createLights();
  31. this.updateFrustums();
  32. this.injectInclude();
  33. }
  34. createLights() {
  35. for ( let i = 0; i < this.cascades; i ++ ) {
  36. const light = new THREE.DirectionalLight( 0xffffff, this.lightIntensity );
  37. light.castShadow = true;
  38. light.shadow.mapSize.width = this.shadowMapSize;
  39. light.shadow.mapSize.height = this.shadowMapSize;
  40. light.shadow.camera.near = this.lightNear;
  41. light.shadow.camera.far = this.lightFar;
  42. light.shadow.bias = this.shadowBias;
  43. this.parent.add( light );
  44. this.parent.add( light.target );
  45. this.lights.push( light );
  46. }
  47. }
  48. initCascades() {
  49. const camera = this.camera;
  50. camera.updateProjectionMatrix();
  51. this.mainFrustum.setFromProjectionMatrix( camera.projectionMatrix, this.maxFar );
  52. this.mainFrustum.split( this.breaks, this.frustums );
  53. }
  54. updateShadowBounds() {
  55. const frustums = this.frustums;
  56. for ( let i = 0; i < frustums.length; i ++ ) {
  57. const light = this.lights[ i ];
  58. const shadowCam = light.shadow.camera;
  59. const frustum = this.frustums[ i ]; // Get the two points that represent that furthest points on the frustum assuming
  60. // that's either the diagonal across the far plane or the diagonal across the whole
  61. // frustum itself.
  62. const nearVerts = frustum.vertices.near;
  63. const farVerts = frustum.vertices.far;
  64. const point1 = farVerts[ 0 ];
  65. let point2;
  66. if ( point1.distanceTo( farVerts[ 2 ] ) > point1.distanceTo( nearVerts[ 2 ] ) ) {
  67. point2 = farVerts[ 2 ];
  68. } else {
  69. point2 = nearVerts[ 2 ];
  70. }
  71. let squaredBBWidth = point1.distanceTo( point2 );
  72. if ( this.fade ) {
  73. // expand the shadow extents by the fade margin if fade is enabled.
  74. const camera = this.camera;
  75. const far = Math.max( camera.far, this.maxFar );
  76. const linearDepth = frustum.vertices.far[ 0 ].z / ( far - camera.near );
  77. const margin = 0.25 * Math.pow( linearDepth, 2.0 ) * ( far - camera.near );
  78. squaredBBWidth += margin;
  79. }
  80. shadowCam.left = - squaredBBWidth / 2;
  81. shadowCam.right = squaredBBWidth / 2;
  82. shadowCam.top = squaredBBWidth / 2;
  83. shadowCam.bottom = - squaredBBWidth / 2;
  84. shadowCam.updateProjectionMatrix();
  85. }
  86. }
  87. getBreaks() {
  88. const camera = this.camera;
  89. const far = Math.min( camera.far, this.maxFar );
  90. this.breaks.length = 0;
  91. switch ( this.mode ) {
  92. case 'uniform':
  93. uniformSplit( this.cascades, camera.near, far, this.breaks );
  94. break;
  95. case 'logarithmic':
  96. logarithmicSplit( this.cascades, camera.near, far, this.breaks );
  97. break;
  98. case 'practical':
  99. practicalSplit( this.cascades, camera.near, far, 0.5, this.breaks );
  100. break;
  101. case 'custom':
  102. if ( this.customSplitsCallback === undefined ) console.error( 'CSM: Custom split scheme callback not defined.' );
  103. this.customSplitsCallback( this.cascades, camera.near, far, this.breaks );
  104. break;
  105. }
  106. function uniformSplit( amount, near, far, target ) {
  107. for ( let i = 1; i < amount; i ++ ) {
  108. target.push( ( near + ( far - near ) * i / amount ) / far );
  109. }
  110. target.push( 1 );
  111. }
  112. function logarithmicSplit( amount, near, far, target ) {
  113. for ( let i = 1; i < amount; i ++ ) {
  114. target.push( near * ( far / near ) ** ( i / amount ) / far );
  115. }
  116. target.push( 1 );
  117. }
  118. function practicalSplit( amount, near, far, lambda, target ) {
  119. _uniformArray.length = 0;
  120. _logArray.length = 0;
  121. logarithmicSplit( amount, near, far, _logArray );
  122. uniformSplit( amount, near, far, _uniformArray );
  123. for ( let i = 1; i < amount; i ++ ) {
  124. target.push( THREE.MathUtils.lerp( _uniformArray[ i - 1 ], _logArray[ i - 1 ], lambda ) );
  125. }
  126. target.push( 1 );
  127. }
  128. }
  129. update() {
  130. const camera = this.camera;
  131. const frustums = this.frustums;
  132. for ( let i = 0; i < frustums.length; i ++ ) {
  133. const light = this.lights[ i ];
  134. const shadowCam = light.shadow.camera;
  135. const texelWidth = ( shadowCam.right - shadowCam.left ) / this.shadowMapSize;
  136. const texelHeight = ( shadowCam.top - shadowCam.bottom ) / this.shadowMapSize;
  137. light.shadow.camera.updateMatrixWorld( true );
  138. _cameraToLightMatrix.multiplyMatrices( light.shadow.camera.matrixWorldInverse, camera.matrixWorld );
  139. frustums[ i ].toSpace( _cameraToLightMatrix, _lightSpaceFrustum );
  140. const nearVerts = _lightSpaceFrustum.vertices.near;
  141. const farVerts = _lightSpaceFrustum.vertices.far;
  142. _bbox.makeEmpty();
  143. for ( let j = 0; j < 4; j ++ ) {
  144. _bbox.expandByPoint( nearVerts[ j ] );
  145. _bbox.expandByPoint( farVerts[ j ] );
  146. }
  147. _bbox.getCenter( _center );
  148. _center.z = _bbox.max.z + this.lightMargin;
  149. _center.x = Math.floor( _center.x / texelWidth ) * texelWidth;
  150. _center.y = Math.floor( _center.y / texelHeight ) * texelHeight;
  151. _center.applyMatrix4( light.shadow.camera.matrixWorld );
  152. light.position.copy( _center );
  153. light.target.position.copy( _center );
  154. light.target.position.x += this.lightDirection.x;
  155. light.target.position.y += this.lightDirection.y;
  156. light.target.position.z += this.lightDirection.z;
  157. }
  158. }
  159. injectInclude() {
  160. THREE.ShaderChunk.lights_fragment_begin = THREE.CSMShader.lights_fragment_begin;
  161. THREE.ShaderChunk.lights_pars_begin = THREE.CSMShader.lights_pars_begin;
  162. }
  163. setupMaterial( material ) {
  164. material.defines = material.defines || {};
  165. material.defines.USE_CSM = 1;
  166. material.defines.CSM_CASCADES = this.cascades;
  167. if ( this.fade ) {
  168. material.defines.CSM_FADE = '';
  169. }
  170. const breaksVec2 = [];
  171. const scope = this;
  172. const shaders = this.shaders;
  173. material.onBeforeCompile = function ( shader ) {
  174. const far = Math.min( scope.camera.far, scope.maxFar );
  175. scope.getExtendedBreaks( breaksVec2 );
  176. shader.uniforms.CSM_cascades = {
  177. value: breaksVec2
  178. };
  179. shader.uniforms.cameraNear = {
  180. value: scope.camera.near
  181. };
  182. shader.uniforms.shadowFar = {
  183. value: far
  184. };
  185. shaders.set( material, shader );
  186. };
  187. shaders.set( material, null );
  188. }
  189. updateUniforms() {
  190. const far = Math.min( this.camera.far, this.maxFar );
  191. const shaders = this.shaders;
  192. shaders.forEach( function ( shader, material ) {
  193. if ( shader !== null ) {
  194. const uniforms = shader.uniforms;
  195. this.getExtendedBreaks( uniforms.CSM_cascades.value );
  196. uniforms.cameraNear.value = this.camera.near;
  197. uniforms.shadowFar.value = far;
  198. }
  199. if ( ! this.fade && 'CSM_FADE' in material.defines ) {
  200. delete material.defines.CSM_FADE;
  201. material.needsUpdate = true;
  202. } else if ( this.fade && ! ( 'CSM_FADE' in material.defines ) ) {
  203. material.defines.CSM_FADE = '';
  204. material.needsUpdate = true;
  205. }
  206. }, this );
  207. }
  208. getExtendedBreaks( target ) {
  209. while ( target.length < this.breaks.length ) {
  210. target.push( new THREE.Vector2() );
  211. }
  212. target.length = this.breaks.length;
  213. for ( let i = 0; i < this.cascades; i ++ ) {
  214. const amount = this.breaks[ i ];
  215. const prev = this.breaks[ i - 1 ] || 0;
  216. target[ i ].x = prev;
  217. target[ i ].y = amount;
  218. }
  219. }
  220. updateFrustums() {
  221. this.getBreaks();
  222. this.initCascades();
  223. this.updateShadowBounds();
  224. this.updateUniforms();
  225. }
  226. remove() {
  227. for ( let i = 0; i < this.lights.length; i ++ ) {
  228. this.parent.remove( this.lights[ i ] );
  229. }
  230. }
  231. dispose() {
  232. const shaders = this.shaders;
  233. shaders.forEach( function ( shader, material ) {
  234. delete material.onBeforeCompile;
  235. delete material.defines.USE_CSM;
  236. delete material.defines.CSM_CASCADES;
  237. delete material.defines.CSM_FADE;
  238. if ( shader !== null ) {
  239. delete shader.uniforms.CSM_cascades;
  240. delete shader.uniforms.cameraNear;
  241. delete shader.uniforms.shadowFar;
  242. }
  243. material.needsUpdate = true;
  244. } );
  245. shaders.clear();
  246. }
  247. }
  248. THREE.CSM = CSM;
  249. } )();