cascadedShadowGenerator.ts 44 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049
  1. import { Nullable } from "../../types";
  2. import { Scene } from "../../scene";
  3. import { Matrix, Vector3 } from "../../Maths/math.vector";
  4. import { SubMesh } from "../../Meshes/subMesh";
  5. import { AbstractMesh } from "../../Meshes/abstractMesh";
  6. import { IShadowLight } from "../../Lights/shadowLight";
  7. import { Effect } from "../../Materials/effect";
  8. import { RenderTargetTexture } from "../../Materials/Textures/renderTargetTexture";
  9. import { Constants } from "../../Engines/constants";
  10. import "../../Shaders/shadowMap.fragment";
  11. import "../../Shaders/shadowMap.vertex";
  12. import "../../Shaders/depthBoxBlur.fragment";
  13. import { Observer } from '../../Misc/observable';
  14. import { _DevTools } from '../../Misc/devTools';
  15. import { ShadowGenerator } from './shadowGenerator';
  16. import { DirectionalLight } from '../directionalLight';
  17. import { BoundingInfo } from '../../Culling/boundingInfo';
  18. import { DepthRenderer } from '../../Rendering/depthRenderer';
  19. import { DepthReducer } from '../../Misc/depthReducer';
  20. import { Logger } from "../../Misc/logger";
  21. import { EngineStore } from '../../Engines/engineStore';
  22. interface ICascade {
  23. prevBreakDistance: number;
  24. breakDistance: number;
  25. }
  26. const UpDir = Vector3.Up();
  27. const ZeroVec = Vector3.Zero();
  28. let tmpv1 = new Vector3(),
  29. tmpv2 = new Vector3(),
  30. tmpMatrix = new Matrix(),
  31. tmpMatrix2 = new Matrix();
  32. /**
  33. * A CSM implementation allowing casting shadows on large scenes.
  34. * Documentation : https://doc.babylonjs.com/babylon101/cascadedShadows
  35. * Based on: https://github.com/TheRealMJP/Shadows and https://johanmedestrom.wordpress.com/2016/03/18/opengl-cascaded-shadow-maps/
  36. */
  37. export class CascadedShadowGenerator extends ShadowGenerator {
  38. private static readonly frustumCornersNDCSpace = [
  39. new Vector3(-1.0, +1.0, -1.0),
  40. new Vector3(+1.0, +1.0, -1.0),
  41. new Vector3(+1.0, -1.0, -1.0),
  42. new Vector3(-1.0, -1.0, -1.0),
  43. new Vector3(-1.0, +1.0, +1.0),
  44. new Vector3(+1.0, +1.0, +1.0),
  45. new Vector3(+1.0, -1.0, +1.0),
  46. new Vector3(-1.0, -1.0, +1.0),
  47. ];
  48. /**
  49. * Name of the CSM class
  50. */
  51. public static CLASSNAME = "CascadedShadowGenerator";
  52. /**
  53. * Defines the default number of cascades used by the CSM.
  54. */
  55. public static readonly DEFAULT_CASCADES_COUNT = 4;
  56. /**
  57. * Defines the minimum number of cascades used by the CSM.
  58. */
  59. public static readonly MIN_CASCADES_COUNT = 2;
  60. /**
  61. * Defines the maximum number of cascades used by the CSM.
  62. */
  63. public static readonly MAX_CASCADES_COUNT = 4;
  64. protected _validateFilter(filter: number): number {
  65. if (filter === ShadowGenerator.FILTER_NONE ||
  66. filter === ShadowGenerator.FILTER_PCF ||
  67. filter === ShadowGenerator.FILTER_PCSS)
  68. {
  69. return filter;
  70. }
  71. console.error('Unsupported filter "' + filter + '"!');
  72. return ShadowGenerator.FILTER_NONE;
  73. }
  74. /**
  75. * Gets or sets the actual darkness of the soft shadows while using PCSS filtering (value between 0. and 1.)
  76. */
  77. public penumbraDarkness: number;
  78. private _numCascades: number;
  79. /**
  80. * Gets or set the number of cascades used by the CSM.
  81. */
  82. public get numCascades(): number {
  83. return this._numCascades;
  84. }
  85. public set numCascades(value: number) {
  86. value = Math.min(Math.max(value, CascadedShadowGenerator.MIN_CASCADES_COUNT), CascadedShadowGenerator.MAX_CASCADES_COUNT);
  87. if (value === this._numCascades) {
  88. return;
  89. }
  90. this._numCascades = value;
  91. this.recreateShadowMap();
  92. }
  93. /**
  94. * Sets this to true if you want that the edges of the shadows don't "swimm" / "shimmer" when rotating the camera.
  95. * The trade off is that you loose some precision in the shadow rendering when enabling this setting.
  96. */
  97. public stabilizeCascades: boolean;
  98. private _freezeShadowCastersBoundingInfo: boolean;
  99. private _freezeShadowCastersBoundingInfoObservable: Nullable<Observer<Scene>>;
  100. /**
  101. * Enables or disables the shadow casters bounding info computation.
  102. * If your shadow casters don't move, you can disable this feature.
  103. * If it is enabled, the bounding box computation is done every frame.
  104. */
  105. public get freezeShadowCastersBoundingInfo(): boolean {
  106. return this._freezeShadowCastersBoundingInfo;
  107. }
  108. public set freezeShadowCastersBoundingInfo(freeze: boolean) {
  109. if (this._freezeShadowCastersBoundingInfoObservable && freeze) {
  110. this._scene.onBeforeRenderObservable.remove(this._freezeShadowCastersBoundingInfoObservable);
  111. this._freezeShadowCastersBoundingInfoObservable = null;
  112. }
  113. if (!this._freezeShadowCastersBoundingInfoObservable && !freeze) {
  114. this._freezeShadowCastersBoundingInfoObservable = this._scene.onBeforeRenderObservable.add(this._computeShadowCastersBoundingInfo.bind(this));
  115. }
  116. this._freezeShadowCastersBoundingInfo = freeze;
  117. if (freeze) {
  118. this._computeShadowCastersBoundingInfo();
  119. }
  120. }
  121. private _scbiMin: Vector3;
  122. private _scbiMax: Vector3;
  123. protected _computeShadowCastersBoundingInfo(): void {
  124. this._scbiMin.copyFromFloats(Number.MAX_VALUE, Number.MAX_VALUE, Number.MAX_VALUE);
  125. this._scbiMax.copyFromFloats(Number.MIN_VALUE, Number.MIN_VALUE, Number.MIN_VALUE);
  126. if (this._shadowMap && this._shadowMap.renderList) {
  127. const renderList = this._shadowMap.renderList;
  128. for (let meshIndex = 0; meshIndex < renderList.length; meshIndex++) {
  129. const mesh = renderList[meshIndex];
  130. if (!mesh) {
  131. continue;
  132. }
  133. const boundingInfo = mesh.getBoundingInfo(),
  134. boundingBox = boundingInfo.boundingBox;
  135. this._scbiMin.minimizeInPlace(boundingBox.minimumWorld);
  136. this._scbiMax.maximizeInPlace(boundingBox.maximumWorld);
  137. }
  138. const meshes = this._scene.meshes;
  139. for (let meshIndex = 0; meshIndex < meshes.length; meshIndex++) {
  140. const mesh = meshes[meshIndex];
  141. if (!mesh || !mesh.isVisible || !mesh.isEnabled || !mesh.receiveShadows) {
  142. continue;
  143. }
  144. const boundingInfo = mesh.getBoundingInfo(),
  145. boundingBox = boundingInfo.boundingBox;
  146. this._scbiMin.minimizeInPlace(boundingBox.minimumWorld);
  147. this._scbiMax.maximizeInPlace(boundingBox.maximumWorld);
  148. }
  149. }
  150. this._shadowCastersBoundingInfo.reConstruct(this._scbiMin, this._scbiMax);
  151. }
  152. protected _shadowCastersBoundingInfo: BoundingInfo;
  153. /**
  154. * Gets or sets the shadow casters bounding info.
  155. * If you provide your own shadow casters bounding info, first enable freezeShadowCastersBoundingInfo
  156. * so that the system won't overwrite the bounds you provide
  157. */
  158. public get shadowCastersBoundingInfo(): BoundingInfo {
  159. return this._shadowCastersBoundingInfo;
  160. }
  161. public set shadowCastersBoundingInfo(boundingInfo: BoundingInfo) {
  162. this._shadowCastersBoundingInfo = boundingInfo;
  163. }
  164. protected _breaksAreDirty: boolean;
  165. protected _minDistance: number;
  166. protected _maxDistance: number;
  167. /**
  168. * Sets the minimal and maximal distances to use when computing the cascade breaks.
  169. *
  170. * The values of min / max are typically the depth zmin and zmax values of your scene, for a given frame.
  171. * If you don't know these values, simply leave them to their defaults and don't call this function.
  172. * @param min minimal distance for the breaks (default to 0.)
  173. * @param max maximal distance for the breaks (default to 1.)
  174. */
  175. public setMinMaxDistance(min: number, max: number): void {
  176. if (this._minDistance === min && this._maxDistance === max) {
  177. return;
  178. }
  179. if (min > max) {
  180. min = 0;
  181. max = 1;
  182. }
  183. if (min < 0) {
  184. min = 0;
  185. }
  186. if (max > 1) {
  187. max = 1;
  188. }
  189. this._minDistance = min;
  190. this._maxDistance = max;
  191. this._breaksAreDirty = true;
  192. }
  193. /** Gets the minimal distance used in the cascade break computation */
  194. public get minDistance(): number {
  195. return this._minDistance;
  196. }
  197. /** Gets the maximal distance used in the cascade break computation */
  198. public get maxDistance(): number {
  199. return this._maxDistance;
  200. }
  201. /**
  202. * Gets the class name of that object
  203. * @returns "CascadedShadowGenerator"
  204. */
  205. public getClassName(): string {
  206. return CascadedShadowGenerator.CLASSNAME;
  207. }
  208. private _cascadeMinExtents: Array<Vector3>;
  209. private _cascadeMaxExtents: Array<Vector3>;
  210. /**
  211. * Gets a cascade minimum extents
  212. * @param cascadeIndex index of the cascade
  213. * @returns the minimum cascade extents
  214. */
  215. public getCascadeMinExtents(cascadeIndex: number): Nullable<Vector3> {
  216. return cascadeIndex >= 0 && cascadeIndex < this._numCascades ? this._cascadeMinExtents[cascadeIndex] : null;
  217. }
  218. /**
  219. * Gets a cascade maximum extents
  220. * @param cascadeIndex index of the cascade
  221. * @returns the maximum cascade extents
  222. */
  223. public getCascadeMaxExtents(cascadeIndex: number): Nullable<Vector3> {
  224. return cascadeIndex >= 0 && cascadeIndex < this._numCascades ? this._cascadeMaxExtents[cascadeIndex] : null;
  225. }
  226. private _cascades: Array<ICascade>;
  227. private _currentLayer: number;
  228. private _viewSpaceFrustumsZ: Array<number>;
  229. private _viewMatrices: Array<Matrix>;
  230. private _projectionMatrices: Array<Matrix>;
  231. private _transformMatrices: Array<Matrix>;
  232. private _transformMatricesAsArray: Float32Array;
  233. private _frustumLengths: Array<number>;
  234. private _lightSizeUVCorrection: Array<number>;
  235. private _depthCorrection: Array<number>;
  236. private _frustumCornersWorldSpace: Array<Array<Vector3>>;
  237. private _frustumCenter: Array<Vector3>;
  238. private _shadowCameraPos: Array<Vector3>;
  239. private _shadowMaxZ: number;
  240. /**
  241. * Gets the shadow max z distance. It's the limit beyond which shadows are not displayed.
  242. * It defaults to camera.maxZ
  243. */
  244. public get shadowMaxZ(): number {
  245. if (!this._scene || !this._scene.activeCamera) {
  246. return 0;
  247. }
  248. return this._shadowMaxZ;
  249. }
  250. /**
  251. * Sets the shadow max z distance.
  252. */
  253. public set shadowMaxZ(value: number) {
  254. if (!this._scene || !this._scene.activeCamera) {
  255. this._shadowMaxZ = value;
  256. return;
  257. }
  258. if (this._shadowMaxZ === value || value < this._scene.activeCamera.minZ || value > this._scene.activeCamera.maxZ) {
  259. return;
  260. }
  261. this._shadowMaxZ = value;
  262. this._light._markMeshesAsLightDirty();
  263. this._breaksAreDirty = true;
  264. }
  265. protected _debug: boolean;
  266. /**
  267. * Gets or sets the debug flag.
  268. * When enabled, the cascades are materialized by different colors on the screen.
  269. */
  270. public get debug(): boolean {
  271. return this._debug;
  272. }
  273. public set debug(dbg: boolean) {
  274. this._debug = dbg;
  275. this._light._markMeshesAsLightDirty();
  276. }
  277. private _depthClamp: boolean;
  278. /**
  279. * Gets or sets the depth clamping value.
  280. *
  281. * When enabled, it improves the shadow quality because the near z plane of the light frustum don't need to be adjusted
  282. * to account for the shadow casters far away.
  283. *
  284. * Note that this property is incompatible with PCSS filtering, so it won't be used in that case.
  285. */
  286. public get depthClamp(): boolean {
  287. return this._depthClamp;
  288. }
  289. public set depthClamp(value: boolean) {
  290. this._depthClamp = value;
  291. }
  292. private _cascadeBlendPercentage: number;
  293. /**
  294. * Gets or sets the percentage of blending between two cascades (value between 0. and 1.).
  295. * It defaults to 0.1 (10% blending).
  296. */
  297. public get cascadeBlendPercentage(): number {
  298. return this._cascadeBlendPercentage;
  299. }
  300. public set cascadeBlendPercentage(value: number) {
  301. this._cascadeBlendPercentage = value;
  302. this._light._markMeshesAsLightDirty();
  303. }
  304. private _lambda: number;
  305. /**
  306. * Gets or set the lambda parameter.
  307. * This parameter is used to split the camera frustum and create the cascades.
  308. * It's a value between 0. and 1.: If 0, the split is a uniform split of the frustum, if 1 it is a logarithmic split.
  309. * For all values in-between, it's a linear combination of the uniform and logarithm split algorithm.
  310. */
  311. public get lambda(): number {
  312. return this._lambda;
  313. }
  314. public set lambda(value: number) {
  315. const lambda = Math.min(Math.max(value, 0), 1);
  316. if (this._lambda == lambda) {
  317. return;
  318. }
  319. this._lambda = lambda;
  320. this._breaksAreDirty = true;
  321. }
  322. /**
  323. * Gets the view matrix corresponding to a given cascade
  324. * @param cascadeNum cascade to retrieve the view matrix from
  325. * @returns the cascade view matrix
  326. */
  327. public getCascadeViewMatrix(cascadeNum: number): Nullable<Matrix> {
  328. return cascadeNum >= 0 && cascadeNum < this._numCascades ? this._viewMatrices[cascadeNum] : null;
  329. }
  330. /**
  331. * Gets the projection matrix corresponding to a given cascade
  332. * @param cascadeNum cascade to retrieve the projection matrix from
  333. * @returns the cascade projection matrix
  334. */
  335. public getCascadeProjectionMatrix(cascadeNum: number): Nullable<Matrix> {
  336. return cascadeNum >= 0 && cascadeNum < this._numCascades ? this._projectionMatrices[cascadeNum] : null;
  337. }
  338. /**
  339. * Gets the transformation matrix corresponding to a given cascade
  340. * @param cascadeNum cascade to retrieve the transformation matrix from
  341. * @returns the cascade transformation matrix
  342. */
  343. public getCascadeTransformMatrix(cascadeNum: number): Nullable<Matrix> {
  344. return cascadeNum >= 0 && cascadeNum < this._numCascades ? this._transformMatrices[cascadeNum] : null;
  345. }
  346. private _depthRenderer: Nullable<DepthRenderer>;
  347. /**
  348. * Sets the depth renderer to use when autoCalcDepthBounds is enabled.
  349. *
  350. * Note that if no depth renderer is set, a new one will be automatically created internally when necessary.
  351. *
  352. * You should call this function if you already have a depth renderer enabled in your scene, to avoid
  353. * doing multiple depth rendering each frame. If you provide your own depth renderer, make sure it stores linear depth!
  354. * @param depthRenderer The depth renderer to use when autoCalcDepthBounds is enabled. If you pass null or don't call this function at all, a depth renderer will be automatically created
  355. */
  356. public setDepthRenderer(depthRenderer: Nullable<DepthRenderer>): void {
  357. this._depthRenderer = depthRenderer;
  358. if (this._depthReducer) {
  359. this._depthReducer.setDepthRenderer(this._depthRenderer);
  360. }
  361. }
  362. private _depthReducer: Nullable<DepthReducer>;
  363. private _autoCalcDepthBounds: boolean;
  364. /**
  365. * Gets or sets the autoCalcDepthBounds property.
  366. *
  367. * When enabled, a depth rendering pass is first performed (with an internally created depth renderer or with the one
  368. * you provide by calling setDepthRenderer). Then, a min/max reducing is applied on the depth map to compute the
  369. * minimal and maximal depth of the map and those values are used as inputs for the setMinMaxDistance() function.
  370. * It can greatly enhance the shadow quality, at the expense of more GPU works.
  371. * When using this option, you should increase the value of the lambda parameter, and even set it to 1 for best results.
  372. */
  373. public get autoCalcDepthBounds(): boolean {
  374. return this._autoCalcDepthBounds;
  375. }
  376. public set autoCalcDepthBounds(value: boolean) {
  377. const camera = this._scene.activeCamera;
  378. if (!camera) {
  379. return;
  380. }
  381. this._autoCalcDepthBounds = value;
  382. if (!value) {
  383. if (this._depthReducer) {
  384. this._depthReducer.deactivate();
  385. }
  386. this.setMinMaxDistance(0, 1);
  387. return;
  388. }
  389. if (!this._depthReducer) {
  390. this._depthReducer = new DepthReducer(camera);
  391. this._depthReducer.onAfterReductionPerformed.add((minmax: { min: number, max: number}) => {
  392. let min = minmax.min, max = minmax.max;
  393. if (min >= max) {
  394. min = 0;
  395. max = 1;
  396. }
  397. if (min != this._minDistance || max != this._maxDistance) {
  398. this.setMinMaxDistance(min, max);
  399. }
  400. });
  401. this._depthReducer.setDepthRenderer(this._depthRenderer);
  402. }
  403. this._depthReducer.activate();
  404. }
  405. /**
  406. * Defines the refresh rate of the min/max computation used when autoCalcDepthBounds is set to true
  407. * Use 0 to compute just once, 1 to compute on every frame, 2 to compute every two frames and so on...
  408. * Note that if you provided your own depth renderer through a call to setDepthRenderer, you are responsible
  409. * for setting the refresh rate on the renderer yourself!
  410. */
  411. public get autoCalcDepthBoundsRefreshRate(): number {
  412. return this._depthReducer?.depthRenderer?.getDepthMap().refreshRate ?? -1;
  413. }
  414. public set autoCalcDepthBoundsRefreshRate(value: number) {
  415. if (this._depthReducer?.depthRenderer) {
  416. this._depthReducer.depthRenderer.getDepthMap().refreshRate = value;
  417. }
  418. }
  419. /**
  420. * Create the cascade breaks according to the lambda, shadowMaxZ and min/max distance properties, as well as the camera near and far planes.
  421. * This function is automatically called when updating lambda, shadowMaxZ and min/max distances, however you should call it yourself if
  422. * you change the camera near/far planes!
  423. */
  424. public splitFrustum(): void {
  425. this._breaksAreDirty = true;
  426. }
  427. private _splitFrustum(): void {
  428. let camera = this._scene.activeCamera;
  429. if (!camera) {
  430. return;
  431. }
  432. const near = camera.minZ,
  433. far = camera.maxZ,
  434. cameraRange = far - near,
  435. minDistance = this._minDistance,
  436. maxDistance = this._shadowMaxZ < far && this._shadowMaxZ >= near ? Math.min((this._shadowMaxZ - near) / (far - near), this._maxDistance) : this._maxDistance;
  437. const minZ = near + minDistance * cameraRange,
  438. maxZ = near + maxDistance * cameraRange;
  439. const range = maxZ - minZ,
  440. ratio = maxZ / minZ;
  441. for (let cascadeIndex = 0; cascadeIndex < this._cascades.length; ++cascadeIndex) {
  442. const p = (cascadeIndex + 1) / this._numCascades,
  443. log = minZ * (ratio ** p),
  444. uniform = minZ + range * p;
  445. const d = this._lambda * (log - uniform) + uniform;
  446. this._cascades[cascadeIndex].prevBreakDistance = cascadeIndex === 0 ? minDistance : this._cascades[cascadeIndex - 1].breakDistance;
  447. this._cascades[cascadeIndex].breakDistance = (d - near) / cameraRange;
  448. this._viewSpaceFrustumsZ[cascadeIndex] = near + this._cascades[cascadeIndex].breakDistance * cameraRange;
  449. this._frustumLengths[cascadeIndex] = (this._cascades[cascadeIndex].breakDistance - this._cascades[cascadeIndex].prevBreakDistance) * cameraRange;
  450. }
  451. this._breaksAreDirty = false;
  452. }
  453. private _computeMatrices(): void {
  454. var scene = this._scene;
  455. let camera = scene.activeCamera;
  456. if (!camera) {
  457. return;
  458. }
  459. Vector3.NormalizeToRef(this._light.getShadowDirection(0), this._lightDirection);
  460. if (Math.abs(Vector3.Dot(this._lightDirection, Vector3.Up())) === 1.0) {
  461. this._lightDirection.z = 0.0000000000001; // Required to avoid perfectly perpendicular light
  462. }
  463. this._cachedDirection.copyFrom(this._lightDirection);
  464. for (let cascadeIndex = 0; cascadeIndex < this._numCascades; ++cascadeIndex) {
  465. this._computeFrustumInWorldSpace(cascadeIndex);
  466. this._computeCascadeFrustum(cascadeIndex);
  467. this._cascadeMaxExtents[cascadeIndex].subtractToRef(this._cascadeMinExtents[cascadeIndex], tmpv1); // tmpv1 = cascadeExtents
  468. // Get position of the shadow camera
  469. this._frustumCenter[cascadeIndex].addToRef(this._lightDirection.scale(this._cascadeMinExtents[cascadeIndex].z), this._shadowCameraPos[cascadeIndex]);
  470. // Come up with a new orthographic camera for the shadow caster
  471. Matrix.LookAtLHToRef(this._shadowCameraPos[cascadeIndex], this._frustumCenter[cascadeIndex], UpDir, this._viewMatrices[cascadeIndex]);
  472. let minZ = 0, maxZ = tmpv1.z;
  473. // Try to tighten minZ and maxZ based on the bounding box of the shadow casters
  474. const boundingInfo = this._shadowCastersBoundingInfo;
  475. boundingInfo.update(this._viewMatrices[cascadeIndex]);
  476. maxZ = Math.min(maxZ, boundingInfo.boundingBox.maximumWorld.z);
  477. if (!this._depthClamp || this.filter === ShadowGenerator.FILTER_PCSS) {
  478. // If we don't use depth clamping, we must set minZ so that all shadow casters are in the light frustum
  479. minZ = Math.min(minZ, boundingInfo.boundingBox.minimumWorld.z);
  480. } else {
  481. // If using depth clamping, we can adjust minZ to reduce the [minZ, maxZ] range (and get some additional precision in the shadow map)
  482. minZ = Math.max(minZ, boundingInfo.boundingBox.minimumWorld.z);
  483. }
  484. Matrix.OrthoOffCenterLHToRef(this._cascadeMinExtents[cascadeIndex].x, this._cascadeMaxExtents[cascadeIndex].x, this._cascadeMinExtents[cascadeIndex].y, this._cascadeMaxExtents[cascadeIndex].y, minZ, maxZ, this._projectionMatrices[cascadeIndex]);
  485. this._cascadeMinExtents[cascadeIndex].z = minZ;
  486. this._cascadeMaxExtents[cascadeIndex].z = maxZ;
  487. this._viewMatrices[cascadeIndex].multiplyToRef(this._projectionMatrices[cascadeIndex], this._transformMatrices[cascadeIndex]);
  488. // Create the rounding matrix, by projecting the world-space origin and determining
  489. // the fractional offset in texel space
  490. Vector3.TransformCoordinatesToRef(ZeroVec, this._transformMatrices[cascadeIndex], tmpv1); // tmpv1 = shadowOrigin
  491. tmpv1.scaleInPlace(this._mapSize / 2);
  492. tmpv2.copyFromFloats(Math.round(tmpv1.x), Math.round(tmpv1.y), Math.round(tmpv1.z)); // tmpv2 = roundedOrigin
  493. tmpv2.subtractInPlace(tmpv1).scaleInPlace(2 / this._mapSize); // tmpv2 = roundOffset
  494. Matrix.TranslationToRef(tmpv2.x, tmpv2.y, 0.0, tmpMatrix);
  495. this._projectionMatrices[cascadeIndex].multiplyToRef(tmpMatrix, this._projectionMatrices[cascadeIndex]);
  496. this._viewMatrices[cascadeIndex].multiplyToRef(this._projectionMatrices[cascadeIndex], this._transformMatrices[cascadeIndex]);
  497. this._transformMatrices[cascadeIndex].copyToArray(this._transformMatricesAsArray, cascadeIndex * 16);
  498. }
  499. }
  500. // Get the 8 points of the view frustum in world space
  501. private _computeFrustumInWorldSpace(cascadeIndex: number): void {
  502. if (!this._scene.activeCamera) {
  503. return;
  504. }
  505. const prevSplitDist = this._cascades[cascadeIndex].prevBreakDistance,
  506. splitDist = this._cascades[cascadeIndex].breakDistance;
  507. this._scene.activeCamera.getViewMatrix(); // make sure the transformation matrix we get when calling 'getTransformationMatrix()' is calculated with an up to date view matrix
  508. const invViewProj = Matrix.Invert(this._scene.activeCamera.getTransformationMatrix());
  509. for (let cornerIndex = 0; cornerIndex < CascadedShadowGenerator.frustumCornersNDCSpace.length; ++cornerIndex) {
  510. Vector3.TransformCoordinatesToRef(CascadedShadowGenerator.frustumCornersNDCSpace[cornerIndex], invViewProj, this._frustumCornersWorldSpace[cascadeIndex][cornerIndex]);
  511. }
  512. // Get the corners of the current cascade slice of the view frustum
  513. for (let cornerIndex = 0; cornerIndex < CascadedShadowGenerator.frustumCornersNDCSpace.length / 2; ++cornerIndex) {
  514. tmpv1.copyFrom(this._frustumCornersWorldSpace[cascadeIndex][cornerIndex + 4]).subtractInPlace(this._frustumCornersWorldSpace[cascadeIndex][cornerIndex]);
  515. tmpv2.copyFrom(tmpv1).scaleInPlace(prevSplitDist); // near corner ray
  516. tmpv1.scaleInPlace(splitDist); // far corner ray
  517. tmpv1.addInPlace(this._frustumCornersWorldSpace[cascadeIndex][cornerIndex]);
  518. this._frustumCornersWorldSpace[cascadeIndex][cornerIndex + 4].copyFrom(tmpv1);
  519. this._frustumCornersWorldSpace[cascadeIndex][cornerIndex].addInPlace(tmpv2);
  520. }
  521. }
  522. private _computeCascadeFrustum(cascadeIndex: number): void {
  523. this._cascadeMinExtents[cascadeIndex].copyFromFloats(Number.MAX_VALUE, Number.MAX_VALUE, Number.MAX_VALUE);
  524. this._cascadeMaxExtents[cascadeIndex].copyFromFloats(Number.MIN_VALUE, Number.MIN_VALUE, Number.MIN_VALUE);
  525. this._frustumCenter[cascadeIndex].copyFromFloats(0, 0, 0);
  526. const camera = this._scene.activeCamera;
  527. if (!camera) {
  528. return;
  529. }
  530. // Calculate the centroid of the view frustum slice
  531. for (let cornerIndex = 0; cornerIndex < this._frustumCornersWorldSpace[cascadeIndex].length; ++cornerIndex) {
  532. this._frustumCenter[cascadeIndex].addInPlace(this._frustumCornersWorldSpace[cascadeIndex][cornerIndex]);
  533. }
  534. this._frustumCenter[cascadeIndex].scaleInPlace(1 / this._frustumCornersWorldSpace[cascadeIndex].length);
  535. if (this.stabilizeCascades) {
  536. // Calculate the radius of a bounding sphere surrounding the frustum corners
  537. let sphereRadius = 0;
  538. for (let cornerIndex = 0; cornerIndex < this._frustumCornersWorldSpace[cascadeIndex].length; ++cornerIndex) {
  539. const dist = this._frustumCornersWorldSpace[cascadeIndex][cornerIndex].subtractToRef(this._frustumCenter[cascadeIndex], tmpv1).length();
  540. sphereRadius = Math.max(sphereRadius, dist);
  541. }
  542. sphereRadius = Math.ceil(sphereRadius * 16) / 16;
  543. this._cascadeMaxExtents[cascadeIndex].copyFromFloats(sphereRadius, sphereRadius, sphereRadius);
  544. this._cascadeMinExtents[cascadeIndex].copyFromFloats(-sphereRadius, -sphereRadius, -sphereRadius);
  545. } else {
  546. // Create a temporary view matrix for the light
  547. const lightCameraPos = this._frustumCenter[cascadeIndex];
  548. this._frustumCenter[cascadeIndex].addToRef(this._lightDirection, tmpv1); // tmpv1 = look at
  549. Matrix.LookAtLHToRef(lightCameraPos, tmpv1, UpDir, tmpMatrix); // matrix = lightView
  550. // Calculate an AABB around the frustum corners
  551. for (let cornerIndex = 0; cornerIndex < this._frustumCornersWorldSpace[cascadeIndex].length; ++cornerIndex) {
  552. Vector3.TransformCoordinatesToRef(this._frustumCornersWorldSpace[cascadeIndex][cornerIndex], tmpMatrix, tmpv1);
  553. this._cascadeMinExtents[cascadeIndex].minimizeInPlace(tmpv1);
  554. this._cascadeMaxExtents[cascadeIndex].maximizeInPlace(tmpv1);
  555. }
  556. }
  557. }
  558. /**
  559. * Support test.
  560. */
  561. public static get IsSupported(): boolean {
  562. var engine = EngineStore.LastCreatedEngine;
  563. if (!engine) {
  564. return false;
  565. }
  566. return engine.webGLVersion != 1;
  567. }
  568. /** @hidden */
  569. public static _SceneComponentInitialization: (scene: Scene) => void = (_) => {
  570. throw _DevTools.WarnImport("ShadowGeneratorSceneComponent");
  571. }
  572. /**
  573. * Creates a Cascaded Shadow Generator object.
  574. * A ShadowGenerator is the required tool to use the shadows.
  575. * Each directional light casting shadows needs to use its own ShadowGenerator.
  576. * Documentation : https://doc.babylonjs.com/babylon101/cascadedShadows
  577. * @param mapSize The size of the texture what stores the shadows. Example : 1024.
  578. * @param light The directional light object generating the shadows.
  579. * @param usefulFloatFirst By default the generator will try to use half float textures but if you need precision (for self shadowing for instance), you can use this option to enforce full float texture.
  580. */
  581. constructor(mapSize: number, light: DirectionalLight, usefulFloatFirst?: boolean) {
  582. super(mapSize, light, usefulFloatFirst);
  583. if (!CascadedShadowGenerator.IsSupported) {
  584. Logger.Error("CascadedShadowMap needs WebGL 2 support.");
  585. return;
  586. }
  587. this.usePercentageCloserFiltering = true;
  588. }
  589. protected _initializeGenerator(): void {
  590. this.penumbraDarkness = this.penumbraDarkness ?? 1.0;
  591. this._numCascades = this._numCascades ?? CascadedShadowGenerator.DEFAULT_CASCADES_COUNT;
  592. this.stabilizeCascades = this.stabilizeCascades ?? false;
  593. this._freezeShadowCastersBoundingInfoObservable = this._freezeShadowCastersBoundingInfoObservable ?? null;
  594. this.freezeShadowCastersBoundingInfo = this.freezeShadowCastersBoundingInfo ?? false;
  595. this._scbiMin = this._scbiMin ?? new Vector3(0, 0, 0);
  596. this._scbiMax = this._scbiMax ?? new Vector3(0, 0, 0);
  597. this._shadowCastersBoundingInfo = this._shadowCastersBoundingInfo ?? new BoundingInfo(new Vector3(0, 0, 0), new Vector3(0, 0, 0));
  598. this._breaksAreDirty = this._breaksAreDirty ?? true;
  599. this._minDistance = this._minDistance ?? 0;
  600. this._maxDistance = this._maxDistance ?? 1;
  601. this._currentLayer = this._currentLayer ?? 0;
  602. this._shadowMaxZ = this._shadowMaxZ ?? this._scene.activeCamera?.maxZ ?? 10000;
  603. this._debug = this._debug ?? false;
  604. this._depthClamp = this._depthClamp ?? true;
  605. this._cascadeBlendPercentage = this._cascadeBlendPercentage ?? 0.1;
  606. this._lambda = this._lambda ?? 0.5;
  607. this._autoCalcDepthBounds = this._autoCalcDepthBounds ?? false;
  608. super._initializeGenerator();
  609. }
  610. protected _createTargetRenderTexture(): void {
  611. const size = { width: this._mapSize, height: this._mapSize, layers: this.numCascades };
  612. this._shadowMap = new RenderTargetTexture(this._light.name + "_shadowMap", size, this._scene, false, true, this._textureType, false, undefined, false, false, undefined/*, Constants.TEXTUREFORMAT_RED*/);
  613. this._shadowMap.createDepthStencilTexture(Constants.LESS, true);
  614. }
  615. protected _initializeShadowMap(): void {
  616. super._initializeShadowMap();
  617. if (this._shadowMap === null) {
  618. return;
  619. }
  620. this._transformMatricesAsArray = new Float32Array(this._numCascades * 16);
  621. this._viewSpaceFrustumsZ = new Array(this._numCascades);
  622. this._frustumLengths = new Array(this._numCascades);
  623. this._lightSizeUVCorrection = new Array(this._numCascades * 2);
  624. this._depthCorrection = new Array(this._numCascades);
  625. this._cascades = [];
  626. this._viewMatrices = [];
  627. this._projectionMatrices = [];
  628. this._transformMatrices = [];
  629. this._cascadeMinExtents = [];
  630. this._cascadeMaxExtents = [];
  631. this._frustumCenter = [];
  632. this._shadowCameraPos = [];
  633. this._frustumCornersWorldSpace = [];
  634. for (let cascadeIndex = 0; cascadeIndex < this._numCascades; ++cascadeIndex) {
  635. this._cascades[cascadeIndex] = {
  636. prevBreakDistance: 0,
  637. breakDistance: 0,
  638. };
  639. this._viewMatrices[cascadeIndex] = Matrix.Zero();
  640. this._projectionMatrices[cascadeIndex] = Matrix.Zero();
  641. this._transformMatrices[cascadeIndex] = Matrix.Zero();
  642. this._cascadeMinExtents[cascadeIndex] = new Vector3();
  643. this._cascadeMaxExtents[cascadeIndex] = new Vector3();
  644. this._frustumCenter[cascadeIndex] = new Vector3();
  645. this._shadowCameraPos[cascadeIndex] = new Vector3();
  646. this._frustumCornersWorldSpace[cascadeIndex] = new Array(CascadedShadowGenerator.frustumCornersNDCSpace.length);
  647. for (let i = 0; i < CascadedShadowGenerator.frustumCornersNDCSpace.length; ++i) {
  648. this._frustumCornersWorldSpace[cascadeIndex][i] = new Vector3();
  649. }
  650. }
  651. this._shadowMap.onBeforeRenderObservable.add((layer: number) => {
  652. this._currentLayer = layer;
  653. if (this._scene.getSceneUniformBuffer().useUbo) {
  654. const sceneUBO = this._scene.getSceneUniformBuffer();
  655. sceneUBO.updateMatrix("viewProjection", this.getCascadeTransformMatrix(layer)!);
  656. sceneUBO.updateMatrix("view", this.getCascadeViewMatrix(layer)!);
  657. sceneUBO.update();
  658. }
  659. });
  660. this._shadowMap.onBeforeBindObservable.add(() => {
  661. if (this._breaksAreDirty) {
  662. this._splitFrustum();
  663. }
  664. this._computeMatrices();
  665. });
  666. this._splitFrustum();
  667. }
  668. protected _bindCustomEffectForRenderSubMeshForShadowMap(subMesh: SubMesh, effect: Effect, matriceNames: any, mesh: AbstractMesh): void {
  669. effect.setMatrix(matriceNames?.viewProjection ?? "viewProjection", this.getCascadeTransformMatrix(this._currentLayer)!);
  670. effect.setMatrix(matriceNames?.view ?? "view", this.getCascadeViewMatrix(this._currentLayer)!);
  671. effect.setMatrix(matriceNames?.projection ?? "projection", this.getCascadeProjectionMatrix(this._currentLayer)!);
  672. const world = mesh.getWorldMatrix();
  673. world.multiplyToRef(this.getCascadeTransformMatrix(this._currentLayer)!, tmpMatrix);
  674. effect.setMatrix(matriceNames?.worldViewProjection ?? "worldViewProjection", tmpMatrix);
  675. world.multiplyToRef(this.getCascadeViewMatrix(this._currentLayer)!, tmpMatrix2);
  676. effect.setMatrix(matriceNames?.worldView ?? "worldView", tmpMatrix2);
  677. }
  678. protected _isReadyCustomDefines(defines: any, subMesh: SubMesh, useInstances: boolean): void {
  679. defines.push("#define SM_DEPTHCLAMP " + (this._depthClamp && this._filter !== ShadowGenerator.FILTER_PCSS ? "1" : "0"));
  680. }
  681. /**
  682. * Prepare all the defines in a material relying on a shadow map at the specified light index.
  683. * @param defines Defines of the material we want to update
  684. * @param lightIndex Index of the light in the enabled light list of the material
  685. */
  686. public prepareDefines(defines: any, lightIndex: number): void {
  687. super.prepareDefines(defines, lightIndex);
  688. var scene = this._scene;
  689. var light = this._light;
  690. if (!scene.shadowsEnabled || !light.shadowEnabled) {
  691. return;
  692. }
  693. defines["SHADOWCSM" + lightIndex] = true;
  694. defines["SHADOWCSMDEBUG" + lightIndex] = this.debug;
  695. defines["SHADOWCSMNUM_CASCADES" + lightIndex] = this.numCascades;
  696. defines["SHADOWCSM_RIGHTHANDED" + lightIndex] = scene.useRightHandedSystem;
  697. const camera = scene.activeCamera;
  698. if (camera && this._shadowMaxZ < camera.maxZ) {
  699. defines["SHADOWCSMUSESHADOWMAXZ" + lightIndex] = true;
  700. }
  701. if (this.cascadeBlendPercentage === 0) {
  702. defines["SHADOWCSMNOBLEND" + lightIndex] = true;
  703. }
  704. }
  705. /**
  706. * Binds the shadow related information inside of an effect (information like near, far, darkness...
  707. * defined in the generator but impacting the effect).
  708. * @param lightIndex Index of the light in the enabled light list of the material owning the effect
  709. * @param effect The effect we are binfing the information for
  710. */
  711. public bindShadowLight(lightIndex: string, effect: Effect): void {
  712. const light = this._light;
  713. const scene = this._scene;
  714. if (!scene.shadowsEnabled || !light.shadowEnabled) {
  715. return;
  716. }
  717. const camera = scene.activeCamera;
  718. if (!camera) {
  719. return;
  720. }
  721. const shadowMap = this.getShadowMap();
  722. if (!shadowMap) {
  723. return;
  724. }
  725. const width = shadowMap.getSize().width;
  726. effect.setMatrices("lightMatrix" + lightIndex, this._transformMatricesAsArray);
  727. effect.setArray("viewFrustumZ" + lightIndex, this._viewSpaceFrustumsZ);
  728. effect.setFloat("cascadeBlendFactor" + lightIndex, this.cascadeBlendPercentage === 0 ? 10000 : 1 / this.cascadeBlendPercentage);
  729. effect.setArray("frustumLengths" + lightIndex, this._frustumLengths);
  730. // Only PCF uses depth stencil texture.
  731. if (this._filter === ShadowGenerator.FILTER_PCF) {
  732. effect.setDepthStencilTexture("shadowSampler" + lightIndex, shadowMap);
  733. light._uniformBuffer.updateFloat4("shadowsInfo", this.getDarkness(), width, 1 / width, this.frustumEdgeFalloff, lightIndex);
  734. } else if (this._filter === ShadowGenerator.FILTER_PCSS) {
  735. for (let cascadeIndex = 0; cascadeIndex < this._numCascades; ++cascadeIndex) {
  736. this._lightSizeUVCorrection[cascadeIndex * 2 + 0] = cascadeIndex === 0 ? 1 : (this._cascadeMaxExtents[0].x - this._cascadeMinExtents[0].x) / (this._cascadeMaxExtents[cascadeIndex].x - this._cascadeMinExtents[cascadeIndex].x); // x correction
  737. this._lightSizeUVCorrection[cascadeIndex * 2 + 1] = cascadeIndex === 0 ? 1 : (this._cascadeMaxExtents[0].y - this._cascadeMinExtents[0].y) / (this._cascadeMaxExtents[cascadeIndex].y - this._cascadeMinExtents[cascadeIndex].y); // y correction
  738. this._depthCorrection[cascadeIndex] = cascadeIndex === 0 ? 1 : (this._cascadeMaxExtents[cascadeIndex].z - this._cascadeMinExtents[cascadeIndex].z) / (this._cascadeMaxExtents[0].z - this._cascadeMinExtents[0].z);
  739. }
  740. effect.setDepthStencilTexture("shadowSampler" + lightIndex, shadowMap);
  741. effect.setTexture("depthSampler" + lightIndex, shadowMap);
  742. effect.setArray2("lightSizeUVCorrection" + lightIndex, this._lightSizeUVCorrection);
  743. effect.setArray("depthCorrection" + lightIndex, this._depthCorrection);
  744. effect.setFloat("penumbraDarkness" + lightIndex, this.penumbraDarkness);
  745. light._uniformBuffer.updateFloat4("shadowsInfo", this.getDarkness(), 1 / width, this._contactHardeningLightSizeUVRatio * width, this.frustumEdgeFalloff, lightIndex);
  746. }
  747. else {
  748. effect.setTexture("shadowSampler" + lightIndex, shadowMap);
  749. light._uniformBuffer.updateFloat4("shadowsInfo", this.getDarkness(), width, 1 / width, this.frustumEdgeFalloff, lightIndex);
  750. }
  751. light._uniformBuffer.updateFloat2("depthValues", this.getLight().getDepthMinZ(camera), this.getLight().getDepthMinZ(camera) + this.getLight().getDepthMaxZ(camera), lightIndex);
  752. }
  753. /**
  754. * Gets the transformation matrix of the first cascade used to project the meshes into the map from the light point of view.
  755. * (eq to view projection * shadow projection matrices)
  756. * @returns The transform matrix used to create the shadow map
  757. */
  758. public getTransformMatrix(): Matrix {
  759. return this.getCascadeTransformMatrix(0)!;
  760. }
  761. /**
  762. * Disposes the ShadowGenerator.
  763. * Returns nothing.
  764. */
  765. public dispose(): void {
  766. super.dispose();
  767. if (this._freezeShadowCastersBoundingInfoObservable) {
  768. this._scene.onBeforeRenderObservable.remove(this._freezeShadowCastersBoundingInfoObservable);
  769. this._freezeShadowCastersBoundingInfoObservable = null;
  770. }
  771. if (this._depthReducer) {
  772. this._depthReducer.dispose();
  773. this._depthReducer = null;
  774. }
  775. }
  776. /**
  777. * Serializes the shadow generator setup to a json object.
  778. * @returns The serialized JSON object
  779. */
  780. public serialize(): any {
  781. var serializationObject: any = super.serialize();
  782. var shadowMap = this.getShadowMap();
  783. if (!shadowMap) {
  784. return serializationObject;
  785. }
  786. serializationObject.numCascades = this._numCascades;
  787. serializationObject.debug = this._debug;
  788. serializationObject.stabilizeCascades = this.stabilizeCascades;
  789. serializationObject.lambda = this._lambda;
  790. serializationObject.cascadeBlendPercentage = this.cascadeBlendPercentage;
  791. serializationObject.depthClamp = this._depthClamp;
  792. serializationObject.autoCalcDepthBounds = this.autoCalcDepthBounds;
  793. serializationObject.shadowMaxZ = this._shadowMaxZ;
  794. serializationObject.penumbraDarkness = this.penumbraDarkness;
  795. serializationObject.freezeShadowCastersBoundingInfo = this._freezeShadowCastersBoundingInfo;
  796. serializationObject.minDistance = this.minDistance;
  797. serializationObject.maxDistance = this.maxDistance;
  798. serializationObject.renderList = [];
  799. if (shadowMap.renderList) {
  800. for (var meshIndex = 0; meshIndex < shadowMap.renderList.length; meshIndex++) {
  801. var mesh = shadowMap.renderList[meshIndex];
  802. serializationObject.renderList.push(mesh.id);
  803. }
  804. }
  805. return serializationObject;
  806. }
  807. /**
  808. * Parses a serialized ShadowGenerator and returns a new ShadowGenerator.
  809. * @param parsedShadowGenerator The JSON object to parse
  810. * @param scene The scene to create the shadow map for
  811. * @returns The parsed shadow generator
  812. */
  813. public static Parse(parsedShadowGenerator: any, scene: Scene): ShadowGenerator {
  814. var shadowGenerator = ShadowGenerator.Parse(parsedShadowGenerator, scene, (mapSize: number, light: IShadowLight) => new CascadedShadowGenerator(mapSize, <DirectionalLight>light)) as CascadedShadowGenerator;
  815. if (parsedShadowGenerator.numCascades !== undefined) {
  816. shadowGenerator.numCascades = parsedShadowGenerator.numCascades;
  817. }
  818. if (parsedShadowGenerator.debug !== undefined) {
  819. shadowGenerator.debug = parsedShadowGenerator.debug;
  820. }
  821. if (parsedShadowGenerator.stabilizeCascades !== undefined) {
  822. shadowGenerator.stabilizeCascades = parsedShadowGenerator.stabilizeCascades;
  823. }
  824. if (parsedShadowGenerator.lambda !== undefined) {
  825. shadowGenerator.lambda = parsedShadowGenerator.lambda;
  826. }
  827. if (parsedShadowGenerator.cascadeBlendPercentage !== undefined) {
  828. shadowGenerator.cascadeBlendPercentage = parsedShadowGenerator.cascadeBlendPercentage;
  829. }
  830. if (parsedShadowGenerator.depthClamp !== undefined) {
  831. shadowGenerator.depthClamp = parsedShadowGenerator.depthClamp;
  832. }
  833. if (parsedShadowGenerator.autoCalcDepthBounds !== undefined) {
  834. shadowGenerator.autoCalcDepthBounds = parsedShadowGenerator.autoCalcDepthBounds;
  835. }
  836. if (parsedShadowGenerator.shadowMaxZ !== undefined) {
  837. shadowGenerator.shadowMaxZ = parsedShadowGenerator.shadowMaxZ;
  838. }
  839. if (parsedShadowGenerator.penumbraDarkness !== undefined) {
  840. shadowGenerator.penumbraDarkness = parsedShadowGenerator.penumbraDarkness;
  841. }
  842. if (parsedShadowGenerator.freezeShadowCastersBoundingInfo !== undefined) {
  843. shadowGenerator.freezeShadowCastersBoundingInfo = parsedShadowGenerator.freezeShadowCastersBoundingInfo;
  844. }
  845. if (parsedShadowGenerator.minDistance !== undefined && parsedShadowGenerator.maxDistance !== undefined) {
  846. shadowGenerator.setMinMaxDistance(parsedShadowGenerator.minDistance, parsedShadowGenerator.maxDistance);
  847. }
  848. return shadowGenerator;
  849. }
  850. }