CSM.js 7.6 KB

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