CSM.js 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350
  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 _frustum = new Frustum();
  22. const _center = new Vector3();
  23. const _bbox = new FrustumBoundingBox();
  24. export default class CSM {
  25. constructor( data ) {
  26. data = data || {};
  27. this.camera = data.camera;
  28. this.parent = data.parent;
  29. this.cascades = data.cascades || 3;
  30. this.maxFar = data.maxFar || 100000;
  31. this.mode = data.mode || 'practical';
  32. this.shadowMapSize = data.shadowMapSize || 2048;
  33. this.shadowBias = data.shadowBias || 0.000001;
  34. this.lightDirection = data.lightDirection || new Vector3( 1, - 1, 1 ).normalize();
  35. this.lightIntensity = data.lightIntensity || 1;
  36. this.lightNear = data.lightNear || 1;
  37. this.lightFar = data.lightFar || 2000;
  38. this.lightMargin = data.lightMargin || 200;
  39. this.customSplitsCallback = data.customSplitsCallback;
  40. this.mainFrustum = new Frustum();
  41. this.frustums = [];
  42. this.lights = [];
  43. this.materials = [];
  44. this.createLights();
  45. this.getBreaks();
  46. this.initCascades();
  47. this.injectInclude();
  48. }
  49. createLights() {
  50. for ( let i = 0; i < this.cascades; i ++ ) {
  51. const light = new DirectionalLight( 0xffffff, this.lightIntensity );
  52. light.castShadow = true;
  53. light.shadow.mapSize.width = this.shadowMapSize;
  54. light.shadow.mapSize.height = this.shadowMapSize;
  55. light.shadow.camera.near = this.lightNear;
  56. light.shadow.camera.far = this.lightFar;
  57. light.shadow.bias = this.shadowBias;
  58. this.parent.add( light );
  59. this.parent.add( light.target );
  60. this.lights.push( light );
  61. }
  62. }
  63. initCascades() {
  64. const camera = this.camera;
  65. camera.updateProjectionMatrix();
  66. this.mainFrustum.setFromProjectionMatrix( camera.projectionMatrix, this.maxFar );
  67. this.mainFrustum.split( this.breaks, this.frustums );
  68. }
  69. getBreaks() {
  70. const camera = this.camera;
  71. const far = Math.min(camera.far, this.maxFar);
  72. this.breaks = [];
  73. switch ( this.mode ) {
  74. case 'uniform':
  75. this.breaks = uniformSplit( this.cascades, camera.near, far );
  76. break;
  77. case 'logarithmic':
  78. this.breaks = logarithmicSplit( this.cascades, camera.near, far );
  79. break;
  80. case 'practical':
  81. this.breaks = practicalSplit( this.cascades, camera.near, far, 0.5 );
  82. break;
  83. case 'custom':
  84. if ( this.customSplitsCallback === undefined ) console.error( 'CSM: Custom split scheme callback not defined.' );
  85. this.breaks = this.customSplitsCallback( this.cascades, camera.near, far );
  86. break;
  87. }
  88. function uniformSplit( amount, near, far ) {
  89. const r = [];
  90. for ( let i = 1; i < amount; i ++ ) {
  91. r.push( ( near + ( far - near ) * i / amount ) / far );
  92. }
  93. r.push( 1 );
  94. return r;
  95. }
  96. function logarithmicSplit( amount, near, far ) {
  97. const r = [];
  98. for ( let i = 1; i < amount; i ++ ) {
  99. r.push( ( near * ( far / near ) ** ( i / amount ) ) / far );
  100. }
  101. r.push( 1 );
  102. return r;
  103. }
  104. function practicalSplit( amount, near, far, lambda ) {
  105. const log = logarithmicSplit( amount, near, far );
  106. const uni = uniformSplit( amount, near, far );
  107. const r = [];
  108. for ( let i = 1; i < amount; i ++ ) {
  109. r.push( MathUtils.lerp( uni[ i - 1 ], log[ i - 1 ], lambda ) );
  110. }
  111. r.push( 1 );
  112. return r;
  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, _frustum );
  120. light.shadow.camera.updateMatrixWorld( true );
  121. _bbox.fromFrustum( _frustum );
  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. for ( let i = 0; i < this.cascades; i ++ ) {
  150. let amount = this.breaks[ i ];
  151. let prev = this.breaks[ i - 1 ] || 0;
  152. breaksVec2.push( new Vector2( prev, amount ) );
  153. }
  154. const self = this;
  155. const far = Math.min(this.camera.far, this.maxFar);
  156. material.onBeforeCompile = function ( shader ) {
  157. shader.uniforms.CSM_cascades = { value: breaksVec2 };
  158. shader.uniforms.cameraNear = { value: self.camera.near };
  159. shader.uniforms.shadowFar = { value: far };
  160. self.materials.push( shader );
  161. };
  162. }
  163. updateUniforms() {
  164. const far = Math.min(this.camera.far, this.maxFar);
  165. for ( let i = 0; i < this.materials.length; i ++ ) {
  166. this.materials[ i ].uniforms.CSM_cascades.value = this.getExtendedBreaks();
  167. this.materials[ i ].uniforms.cameraNear.value = this.camera.near;
  168. this.materials[ i ].uniforms.shadowFar.value = far;
  169. }
  170. }
  171. getExtendedBreaks() {
  172. let breaksVec2 = [];
  173. for ( let i = 0; i < this.cascades; i ++ ) {
  174. let amount = this.breaks[ i ];
  175. let prev = this.breaks[ i - 1 ] || 0;
  176. breaksVec2.push( new Vector2( prev, amount ) );
  177. }
  178. return breaksVec2;
  179. }
  180. updateFrustums() {
  181. this.getBreaks();
  182. this.initCascades();
  183. this.updateUniforms();
  184. }
  185. helper( cameraMatrix ) {
  186. let frustum;
  187. let geometry, vertices;
  188. const material = new LineBasicMaterial( { color: 0xffffff } );
  189. const object = new Object3D();
  190. for ( let i = 0; i < this.frustums.length; i ++ ) {
  191. frustum = this.frustums[ i ].toSpace( cameraMatrix );
  192. geometry = new BufferGeometry();
  193. vertices = [];
  194. for ( let i = 0; i < 5; i ++ ) {
  195. const point = frustum.vertices.near[ i === 4 ? 0 : i ];
  196. vertices.push( point.x, point.y, point.z );
  197. }
  198. geometry.setAttribute( 'position', new BufferAttribute( new Float32Array( vertices ), 3 ) );
  199. object.add( new Line( geometry, material ) );
  200. geometry = new BufferGeometry();
  201. vertices = [];
  202. for ( let i = 0; i < 5; i ++ ) {
  203. const point = frustum.vertices.far[ i === 4 ? 0 : i ];
  204. vertices.push( point.x, point.y, point.z );
  205. }
  206. geometry.setAttribute( 'position', new BufferAttribute( new Float32Array( vertices ), 3 ) );
  207. object.add( new Line( geometry, material ) );
  208. for ( let i = 0; i < 4; i ++ ) {
  209. geometry = new BufferGeometry();
  210. vertices = [];
  211. const near = frustum.vertices.near[ i ];
  212. const far = frustum.vertices.far[ i ];
  213. vertices.push( near.x, near.y, near.z );
  214. vertices.push( far.x, far.y, far.z );
  215. geometry.setAttribute( 'position', new BufferAttribute( new Float32Array( vertices ), 3 ) );
  216. object.add( new Line( geometry, material ) );
  217. }
  218. }
  219. return object;
  220. }
  221. remove() {
  222. for ( let i = 0; i < this.lights.length; i ++ ) {
  223. this.parent.remove( this.lights[ i ] );
  224. }
  225. }
  226. }