CSM.js 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349
  1. import {
  2. Vector2,
  3. Vector3,
  4. DirectionalLight,
  5. MathUtils,
  6. ShaderChunk,
  7. LineBasicMaterial,
  8. Object3D,
  9. BufferGeometry,
  10. BufferAttribute,
  11. Line
  12. } from '../../../build/three.module.js';
  13. import Frustum from './Frustum.js';
  14. import FrustumBoundingBox from './FrustumBoundingBox.js';
  15. import Shader from './Shader.js';
  16. export default class CSM {
  17. constructor( data ) {
  18. data = data || {};
  19. this.camera = data.camera;
  20. this.parent = data.parent;
  21. this.fov = data.fov || this.camera.fov;
  22. this.near = this.camera.near;
  23. this.far = data.far || this.camera.far;
  24. this.aspect = data.aspect || this.camera.aspect;
  25. this.cascades = data.cascades || 3;
  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.lights = [];
  36. this.materials = [];
  37. this.createLights();
  38. this.getBreaks();
  39. this.initCascades();
  40. this.injectInclude();
  41. }
  42. createLights() {
  43. for ( let i = 0; i < this.cascades; i ++ ) {
  44. const light = new DirectionalLight( 0xffffff, this.lightIntensity );
  45. light.castShadow = true;
  46. light.shadow.mapSize.width = this.shadowMapSize;
  47. light.shadow.mapSize.height = this.shadowMapSize;
  48. light.shadow.camera.near = this.lightNear;
  49. light.shadow.camera.far = this.lightFar;
  50. light.shadow.bias = this.shadowBias;
  51. this.parent.add( light );
  52. this.parent.add( light.target );
  53. this.lights.push( light );
  54. }
  55. }
  56. initCascades() {
  57. this.mainFrustum = new Frustum( {
  58. fov: this.fov,
  59. near: this.near,
  60. far: this.far,
  61. aspect: this.aspect
  62. } );
  63. this.mainFrustum.getViewSpaceVertices();
  64. this.frustums = this.mainFrustum.split( this.breaks );
  65. }
  66. getBreaks() {
  67. this.breaks = [];
  68. switch ( this.mode ) {
  69. case 'uniform':
  70. this.breaks = uniformSplit( this.cascades, this.near, this.far );
  71. break;
  72. case 'logarithmic':
  73. this.breaks = logarithmicSplit( this.cascades, this.near, this.far );
  74. break;
  75. case 'practical':
  76. this.breaks = practicalSplit( this.cascades, this.near, this.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, this.near, this.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. material.onBeforeCompile = function ( shader ) {
  151. shader.uniforms.CSM_cascades = { value: breaksVec2 };
  152. shader.uniforms.cameraNear = { value: self.camera.near };
  153. shader.uniforms.shadowFar = { value: self.far };
  154. self.materials.push( shader );
  155. };
  156. }
  157. updateUniforms() {
  158. for ( let i = 0; i < this.materials.length; i ++ ) {
  159. this.materials[ i ].uniforms.CSM_cascades.value = this.getExtendedBreaks();
  160. this.materials[ i ].uniforms.cameraNear.value = this.camera.near;
  161. this.materials[ i ].uniforms.shadowFar.value = this.far;
  162. }
  163. }
  164. getExtendedBreaks() {
  165. let breaksVec2 = [];
  166. for ( let i = 0; i < this.cascades; i ++ ) {
  167. let amount = this.breaks[ i ];
  168. let prev = this.breaks[ i - 1 ] || 0;
  169. breaksVec2.push( new Vector2( prev, amount ) );
  170. }
  171. return breaksVec2;
  172. }
  173. setAspect( aspect ) {
  174. this.aspect = aspect;
  175. this.initCascades();
  176. }
  177. updateFrustums() {
  178. this.getBreaks();
  179. this.initCascades();
  180. this.updateUniforms();
  181. }
  182. helper( cameraMatrix ) {
  183. let frustum;
  184. let geometry, vertices;
  185. const material = new LineBasicMaterial( { color: 0xffffff } );
  186. const object = new Object3D();
  187. for ( let i = 0; i < this.frustums.length; i ++ ) {
  188. frustum = this.frustums[ i ].toSpace( cameraMatrix );
  189. geometry = new BufferGeometry();
  190. vertices = [];
  191. for ( let i = 0; i < 5; i ++ ) {
  192. const point = frustum.vertices.near[ i === 4 ? 0 : i ];
  193. vertices.push( point.x, point.y, point.z );
  194. }
  195. geometry.setAttribute( 'position', new BufferAttribute( new Float32Array( vertices ), 3 ) );
  196. object.add( new Line( geometry, material ) );
  197. geometry = new BufferGeometry();
  198. vertices = [];
  199. for ( let i = 0; i < 5; i ++ ) {
  200. const point = frustum.vertices.far[ i === 4 ? 0 : i ];
  201. vertices.push( point.x, point.y, point.z );
  202. }
  203. geometry.setAttribute( 'position', new BufferAttribute( new Float32Array( vertices ), 3 ) );
  204. object.add( new Line( geometry, material ) );
  205. for ( let i = 0; i < 4; i ++ ) {
  206. geometry = new BufferGeometry();
  207. vertices = [];
  208. const near = frustum.vertices.near[ i ];
  209. const far = frustum.vertices.far[ i ];
  210. vertices.push( near.x, near.y, near.z );
  211. vertices.push( far.x, far.y, far.z );
  212. geometry.setAttribute( 'position', new BufferAttribute( new Float32Array( vertices ), 3 ) );
  213. object.add( new Line( geometry, material ) );
  214. }
  215. }
  216. return object;
  217. }
  218. remove() {
  219. for ( let i = 0; i < this.lights.length; i ++ ) {
  220. this.parent.remove( this.lights[ i ] );
  221. }
  222. }
  223. }