CSM.js 7.5 KB

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