CSM.js 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345
  1. /**
  2. * @author vHawk / https://github.com/vHawk/
  3. */
  4. import {
  5. Vector2,
  6. Vector3,
  7. DirectionalLight,
  8. MathUtils,
  9. ShaderChunk,
  10. LineBasicMaterial,
  11. Object3D,
  12. BufferGeometry,
  13. BufferAttribute,
  14. Line,
  15. Matrix4
  16. } from '../../../build/three.module.js';
  17. import Frustum from './Frustum.js';
  18. import FrustumBoundingBox from './FrustumBoundingBox.js';
  19. import Shader from './Shader.js';
  20. const _cameraToLightMatrix = new Matrix4();
  21. const _lightSpaceFrustum = new Frustum();
  22. const _frustum = new Frustum();
  23. const _center = new Vector3();
  24. const _bbox = new FrustumBoundingBox();
  25. const _uniformArray = [];
  26. const _logArray = [];
  27. export default class CSM {
  28. constructor( data ) {
  29. data = data || {};
  30. this.camera = data.camera;
  31. this.parent = data.parent;
  32. this.cascades = data.cascades || 3;
  33. this.maxFar = data.maxFar || 100000;
  34. this.mode = data.mode || 'practical';
  35. this.shadowMapSize = data.shadowMapSize || 2048;
  36. this.shadowBias = data.shadowBias || 0.000001;
  37. this.lightDirection = data.lightDirection || new Vector3( 1, - 1, 1 ).normalize();
  38. this.lightIntensity = data.lightIntensity || 1;
  39. this.lightNear = data.lightNear || 1;
  40. this.lightFar = data.lightFar || 2000;
  41. this.lightMargin = data.lightMargin || 200;
  42. this.customSplitsCallback = data.customSplitsCallback;
  43. this.mainFrustum = new Frustum();
  44. this.frustums = [];
  45. this.breaks = [];
  46. this.lights = [];
  47. this.materials = [];
  48. this.createLights();
  49. this.getBreaks();
  50. this.initCascades();
  51. this.injectInclude();
  52. }
  53. createLights() {
  54. for ( let i = 0; i < this.cascades; i ++ ) {
  55. const light = new DirectionalLight( 0xffffff, this.lightIntensity );
  56. light.castShadow = true;
  57. light.shadow.mapSize.width = this.shadowMapSize;
  58. light.shadow.mapSize.height = this.shadowMapSize;
  59. light.shadow.camera.near = this.lightNear;
  60. light.shadow.camera.far = this.lightFar;
  61. light.shadow.bias = this.shadowBias;
  62. this.parent.add( light );
  63. this.parent.add( light.target );
  64. this.lights.push( light );
  65. }
  66. }
  67. initCascades() {
  68. const camera = this.camera;
  69. camera.updateProjectionMatrix();
  70. this.mainFrustum.setFromProjectionMatrix( camera.projectionMatrix, this.maxFar );
  71. this.mainFrustum.split( this.breaks, this.frustums );
  72. }
  73. getBreaks() {
  74. const camera = this.camera;
  75. const far = Math.min(camera.far, this.maxFar);
  76. this.breaks.length = 0;
  77. switch ( this.mode ) {
  78. case 'uniform':
  79. uniformSplit( this.cascades, camera.near, far, this.breaks );
  80. break;
  81. case 'logarithmic':
  82. logarithmicSplit( this.cascades, camera.near, far, this.breaks );
  83. break;
  84. case 'practical':
  85. practicalSplit( this.cascades, camera.near, far, 0.5, this.breaks );
  86. break;
  87. case 'custom':
  88. if ( this.customSplitsCallback === undefined ) console.error( 'CSM: Custom split scheme callback not defined.' );
  89. this.customSplitsCallback( this.cascades, camera.near, far, this.breaks );
  90. break;
  91. }
  92. function uniformSplit( amount, near, far, target ) {
  93. for ( let i = 1; i < amount; i ++ ) {
  94. target.push( ( near + ( far - near ) * i / amount ) / far );
  95. }
  96. target.push( 1 );
  97. }
  98. function logarithmicSplit( amount, near, far, target ) {
  99. for ( let i = 1; i < amount; i ++ ) {
  100. target.push( ( near * ( far / near ) ** ( i / amount ) ) / far );
  101. }
  102. target.push( 1 );
  103. }
  104. function practicalSplit( amount, near, far, lambda, target ) {
  105. _uniformArray.length = 0;
  106. _logArray.length = 0;
  107. const log = logarithmicSplit( amount, near, far, _logArray );
  108. const uni = uniformSplit( amount, near, far, _uniformArray );
  109. for ( let i = 1; i < amount; i ++ ) {
  110. target.push( MathUtils.lerp( _uniformArray[ i - 1 ], _logArray[ i - 1 ], lambda ) );
  111. }
  112. target.push( 1 );
  113. }
  114. }
  115. update( cameraMatrix ) {
  116. for ( let i = 0; i < this.frustums.length; i ++ ) {
  117. const light = this.lights[ i ];
  118. _cameraToLightMatrix.multiplyMatrices( light.shadow.camera.matrixWorldInverse, cameraMatrix );
  119. this.frustums[ i ].toSpace( _cameraToLightMatrix, _lightSpaceFrustum );
  120. light.shadow.camera.updateMatrixWorld( true );
  121. _bbox.fromFrustum( _lightSpaceFrustum );
  122. _bbox.getSize();
  123. _bbox.getCenter( this.lightMargin );
  124. const squaredBBWidth = Math.max( _bbox.size.x, _bbox.size.y );
  125. _center.copy( _bbox.center );
  126. _center.applyMatrix4( light.shadow.camera.matrixWorld );
  127. light.shadow.camera.left = - squaredBBWidth / 2;
  128. light.shadow.camera.right = squaredBBWidth / 2;
  129. light.shadow.camera.top = squaredBBWidth / 2;
  130. light.shadow.camera.bottom = - squaredBBWidth / 2;
  131. light.position.copy( _center );
  132. light.target.position.copy( _center );
  133. light.target.position.x += this.lightDirection.x;
  134. light.target.position.y += this.lightDirection.y;
  135. light.target.position.z += this.lightDirection.z;
  136. light.shadow.camera.updateProjectionMatrix();
  137. light.shadow.camera.updateMatrixWorld();
  138. }
  139. }
  140. injectInclude() {
  141. ShaderChunk.lights_fragment_begin = Shader.lights_fragment_begin;
  142. ShaderChunk.lights_pars_begin = Shader.lights_pars_begin;
  143. }
  144. setupMaterial( material ) {
  145. material.defines = material.defines || {};
  146. material.defines.USE_CSM = 1;
  147. material.defines.CSM_CASCADES = this.cascades;
  148. const breaksVec2 = [];
  149. this.getExtendedBreaks( breaksVec2 );
  150. const self = this;
  151. const far = Math.min(this.camera.far, this.maxFar);
  152. material.onBeforeCompile = function ( shader ) {
  153. shader.uniforms.CSM_cascades = { value: breaksVec2 };
  154. shader.uniforms.cameraNear = { value: self.camera.near };
  155. shader.uniforms.shadowFar = { value: far };
  156. self.materials.push( shader );
  157. };
  158. }
  159. updateUniforms() {
  160. const far = Math.min(this.camera.far, this.maxFar);
  161. for ( let i = 0; i < this.materials.length; i ++ ) {
  162. const uniforms = this.materials[ i ].uniforms;
  163. this.getExtendedBreaks( uniforms.CSM_cascades.value );
  164. uniforms.cameraNear.value = this.camera.near;
  165. uniforms.shadowFar.value = far;
  166. }
  167. }
  168. getExtendedBreaks( target ) {
  169. while ( target.length < this.breaks.length ) {
  170. target.push( new Vector2() );
  171. }
  172. target.length = this.breaks.length;
  173. for ( let i = 0; i < this.cascades; i ++ ) {
  174. let amount = this.breaks[ i ];
  175. let prev = this.breaks[ i - 1 ] || 0;
  176. target[ i ].x = prev;
  177. target[ i ].y = amount;
  178. }
  179. }
  180. updateFrustums() {
  181. this.getBreaks();
  182. this.initCascades();
  183. this.updateUniforms();
  184. }
  185. helper( cameraMatrix ) {
  186. let geometry, vertices;
  187. const material = new LineBasicMaterial( { color: 0xffffff } );
  188. const object = new Object3D();
  189. for ( let i = 0; i < this.frustums.length; i ++ ) {
  190. this.frustums[ i ].toSpace( cameraMatrix, _frustum );
  191. geometry = new BufferGeometry();
  192. vertices = [];
  193. for ( let i = 0; i < 5; i ++ ) {
  194. const point = _frustum.vertices.near[ i === 4 ? 0 : i ];
  195. vertices.push( point.x, point.y, point.z );
  196. }
  197. geometry.setAttribute( 'position', new BufferAttribute( new Float32Array( vertices ), 3 ) );
  198. object.add( new Line( geometry, material ) );
  199. geometry = new BufferGeometry();
  200. vertices = [];
  201. for ( let i = 0; i < 5; i ++ ) {
  202. const point = _frustum.vertices.far[ i === 4 ? 0 : i ];
  203. vertices.push( point.x, point.y, point.z );
  204. }
  205. geometry.setAttribute( 'position', new BufferAttribute( new Float32Array( vertices ), 3 ) );
  206. object.add( new Line( geometry, material ) );
  207. for ( let i = 0; i < 4; i ++ ) {
  208. geometry = new BufferGeometry();
  209. vertices = [];
  210. const near = _frustum.vertices.near[ i ];
  211. const far = _frustum.vertices.far[ i ];
  212. vertices.push( near.x, near.y, near.z );
  213. vertices.push( far.x, far.y, far.z );
  214. geometry.setAttribute( 'position', new BufferAttribute( new Float32Array( vertices ), 3 ) );
  215. object.add( new Line( geometry, material ) );
  216. }
  217. }
  218. return object;
  219. }
  220. remove() {
  221. for ( let i = 0; i < this.lights.length; i ++ ) {
  222. this.parent.remove( this.lights[ i ] );
  223. }
  224. }
  225. }