shadowGenerator.ts 71 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818
  1. import { SmartArray } from "../../Misc/smartArray";
  2. import { Nullable } from "../../types";
  3. import { Scene } from "../../scene";
  4. import { Matrix, Vector3, Vector2 } from "../../Maths/math.vector";
  5. import { Color4 } from "../../Maths/math.color";
  6. import { VertexBuffer } from "../../Meshes/buffer";
  7. import { SubMesh } from "../../Meshes/subMesh";
  8. import { AbstractMesh } from "../../Meshes/abstractMesh";
  9. import { Mesh } from "../../Meshes/mesh";
  10. import { IShadowLight } from "../../Lights/shadowLight";
  11. import { Light } from "../../Lights/light";
  12. import { MaterialDefines } from "../../Materials/materialDefines";
  13. import { MaterialHelper } from "../../Materials/materialHelper";
  14. import { Effect } from "../../Materials/effect";
  15. import { Texture } from "../../Materials/Textures/texture";
  16. import { RenderTargetTexture } from "../../Materials/Textures/renderTargetTexture";
  17. import { PostProcess } from "../../PostProcesses/postProcess";
  18. import { BlurPostProcess } from "../../PostProcesses/blurPostProcess";
  19. import { Constants } from "../../Engines/constants";
  20. import "../../Shaders/shadowMap.fragment";
  21. import "../../Shaders/shadowMap.vertex";
  22. import "../../Shaders/depthBoxBlur.fragment";
  23. import { Observable } from '../../Misc/observable';
  24. import { _DevTools } from '../../Misc/devTools';
  25. import { EffectFallbacks } from '../../Materials/effectFallbacks';
  26. import { RenderingManager } from '../../Rendering/renderingManager';
  27. /**
  28. * Defines the options associated with the creation of a custom shader for a shadow generator.
  29. */
  30. export interface ICustomShaderOptions {
  31. /**
  32. * Gets or sets the custom shader name to use
  33. */
  34. shaderName: string;
  35. /**
  36. * The list of attribute names used in the shader
  37. */
  38. attributes?: string[];
  39. /**
  40. * The list of unifrom names used in the shader
  41. */
  42. uniforms?: string[];
  43. /**
  44. * The list of sampler names used in the shader
  45. */
  46. samplers?: string[];
  47. /**
  48. * The list of defines used in the shader
  49. */
  50. defines?: string[];
  51. }
  52. /**
  53. * Interface to implement to create a shadow generator compatible with BJS.
  54. */
  55. export interface IShadowGenerator {
  56. /**
  57. * Gets the main RTT containing the shadow map (usually storing depth from the light point of view).
  58. * @returns The render target texture if present otherwise, null
  59. */
  60. getShadowMap(): Nullable<RenderTargetTexture>;
  61. /**
  62. * Determine wheter the shadow generator is ready or not (mainly all effects and related post processes needs to be ready).
  63. * @param subMesh The submesh we want to render in the shadow map
  64. * @param useInstances Defines wether will draw in the map using instances
  65. * @returns true if ready otherwise, false
  66. */
  67. isReady(subMesh: SubMesh, useInstances: boolean): boolean;
  68. /**
  69. * Prepare all the defines in a material relying on a shadow map at the specified light index.
  70. * @param defines Defines of the material we want to update
  71. * @param lightIndex Index of the light in the enabled light list of the material
  72. */
  73. prepareDefines(defines: MaterialDefines, lightIndex: number): void;
  74. /**
  75. * Binds the shadow related information inside of an effect (information like near, far, darkness...
  76. * defined in the generator but impacting the effect).
  77. * It implies the unifroms available on the materials are the standard BJS ones.
  78. * @param lightIndex Index of the light in the enabled light list of the material owning the effect
  79. * @param effect The effect we are binfing the information for
  80. */
  81. bindShadowLight(lightIndex: string, effect: Effect): void;
  82. /**
  83. * Gets the transformation matrix used to project the meshes into the map from the light point of view.
  84. * (eq to shadow prjection matrix * light transform matrix)
  85. * @returns The transform matrix used to create the shadow map
  86. */
  87. getTransformMatrix(): Matrix;
  88. /**
  89. * Recreates the shadow map dependencies like RTT and post processes. This can be used during the switch between
  90. * Cube and 2D textures for instance.
  91. */
  92. recreateShadowMap(): void;
  93. /**
  94. * Forces all the attached effect to compile to enable rendering only once ready vs. lazyly compiling effects.
  95. * @param onCompiled Callback triggered at the and of the effects compilation
  96. * @param options Sets of optional options forcing the compilation with different modes
  97. */
  98. forceCompilation(onCompiled?: (generator: IShadowGenerator) => void, options?: Partial<{ useInstances: boolean }>): void;
  99. /**
  100. * Forces all the attached effect to compile to enable rendering only once ready vs. lazyly compiling effects.
  101. * @param options Sets of optional options forcing the compilation with different modes
  102. * @returns A promise that resolves when the compilation completes
  103. */
  104. forceCompilationAsync(options?: Partial<{ useInstances: boolean }>): Promise<void>;
  105. /**
  106. * Serializes the shadow generator setup to a json object.
  107. * @returns The serialized JSON object
  108. */
  109. serialize(): any;
  110. /**
  111. * Disposes the Shadow map and related Textures and effects.
  112. */
  113. dispose(): void;
  114. }
  115. /**
  116. * Default implementation IShadowGenerator.
  117. * This is the main object responsible of generating shadows in the framework.
  118. * Documentation: https://doc.babylonjs.com/babylon101/shadows
  119. */
  120. export class ShadowGenerator implements IShadowGenerator {
  121. /**
  122. * Name of the shadow generator class
  123. */
  124. public static CLASSNAME = "ShadowGenerator";
  125. /**
  126. * Shadow generator mode None: no filtering applied.
  127. */
  128. public static readonly FILTER_NONE = 0;
  129. /**
  130. * Shadow generator mode ESM: Exponential Shadow Mapping.
  131. * (http://developer.download.nvidia.com/presentations/2008/GDC/GDC08_SoftShadowMapping.pdf)
  132. */
  133. public static readonly FILTER_EXPONENTIALSHADOWMAP = 1;
  134. /**
  135. * Shadow generator mode Poisson Sampling: Percentage Closer Filtering.
  136. * (Multiple Tap around evenly distributed around the pixel are used to evaluate the shadow strength)
  137. */
  138. public static readonly FILTER_POISSONSAMPLING = 2;
  139. /**
  140. * Shadow generator mode ESM: Blurred Exponential Shadow Mapping.
  141. * (http://developer.download.nvidia.com/presentations/2008/GDC/GDC08_SoftShadowMapping.pdf)
  142. */
  143. public static readonly FILTER_BLUREXPONENTIALSHADOWMAP = 3;
  144. /**
  145. * Shadow generator mode ESM: Exponential Shadow Mapping using the inverse of the exponential preventing
  146. * edge artifacts on steep falloff.
  147. * (http://developer.download.nvidia.com/presentations/2008/GDC/GDC08_SoftShadowMapping.pdf)
  148. */
  149. public static readonly FILTER_CLOSEEXPONENTIALSHADOWMAP = 4;
  150. /**
  151. * Shadow generator mode ESM: Blurred Exponential Shadow Mapping using the inverse of the exponential preventing
  152. * edge artifacts on steep falloff.
  153. * (http://developer.download.nvidia.com/presentations/2008/GDC/GDC08_SoftShadowMapping.pdf)
  154. */
  155. public static readonly FILTER_BLURCLOSEEXPONENTIALSHADOWMAP = 5;
  156. /**
  157. * Shadow generator mode PCF: Percentage Closer Filtering
  158. * benefits from Webgl 2 shadow samplers. Fallback to Poisson Sampling in Webgl 1
  159. * (https://developer.nvidia.com/gpugems/GPUGems/gpugems_ch11.html)
  160. */
  161. public static readonly FILTER_PCF = 6;
  162. /**
  163. * Shadow generator mode PCSS: Percentage Closering Soft Shadow.
  164. * benefits from Webgl 2 shadow samplers. Fallback to Poisson Sampling in Webgl 1
  165. * Contact Hardening
  166. */
  167. public static readonly FILTER_PCSS = 7;
  168. /**
  169. * Reserved for PCF and PCSS
  170. * Highest Quality.
  171. *
  172. * Execute PCF on a 5*5 kernel improving a lot the shadow aliasing artifacts.
  173. *
  174. * Execute PCSS with 32 taps blocker search and 64 taps PCF.
  175. */
  176. public static readonly QUALITY_HIGH = 0;
  177. /**
  178. * Reserved for PCF and PCSS
  179. * Good tradeoff for quality/perf cross devices
  180. *
  181. * Execute PCF on a 3*3 kernel.
  182. *
  183. * Execute PCSS with 16 taps blocker search and 32 taps PCF.
  184. */
  185. public static readonly QUALITY_MEDIUM = 1;
  186. /**
  187. * Reserved for PCF and PCSS
  188. * The lowest quality but the fastest.
  189. *
  190. * Execute PCF on a 1*1 kernel.
  191. *
  192. * Execute PCSS with 16 taps blocker search and 16 taps PCF.
  193. */
  194. public static readonly QUALITY_LOW = 2;
  195. /** Gets or sets the custom shader name to use */
  196. public customShaderOptions: ICustomShaderOptions;
  197. /**
  198. * Observable triggered before the shadow is rendered. Can be used to update internal effect state
  199. */
  200. public onBeforeShadowMapRenderObservable = new Observable<Effect>();
  201. /**
  202. * Observable triggered after the shadow is rendered. Can be used to restore internal effect state
  203. */
  204. public onAfterShadowMapRenderObservable = new Observable<Effect>();
  205. /**
  206. * Observable triggered before a mesh is rendered in the shadow map.
  207. * Can be used to update internal effect state (that you can get from the onBeforeShadowMapRenderObservable)
  208. */
  209. public onBeforeShadowMapRenderMeshObservable = new Observable<Mesh>();
  210. /**
  211. * Observable triggered after a mesh is rendered in the shadow map.
  212. * Can be used to update internal effect state (that you can get from the onAfterShadowMapRenderObservable)
  213. */
  214. public onAfterShadowMapRenderMeshObservable = new Observable<Mesh>();
  215. protected _bias = 0.00005;
  216. /**
  217. * Gets the bias: offset applied on the depth preventing acnea (in light direction).
  218. */
  219. public get bias(): number {
  220. return this._bias;
  221. }
  222. /**
  223. * Sets the bias: offset applied on the depth preventing acnea (in light direction).
  224. */
  225. public set bias(bias: number) {
  226. this._bias = bias;
  227. }
  228. protected _normalBias = 0;
  229. /**
  230. * Gets the normalBias: offset applied on the depth preventing acnea (along side the normal direction and proportinal to the light/normal angle).
  231. */
  232. public get normalBias(): number {
  233. return this._normalBias;
  234. }
  235. /**
  236. * Sets the normalBias: offset applied on the depth preventing acnea (along side the normal direction and proportinal to the light/normal angle).
  237. */
  238. public set normalBias(normalBias: number) {
  239. this._normalBias = normalBias;
  240. }
  241. protected _blurBoxOffset = 1;
  242. /**
  243. * Gets the blur box offset: offset applied during the blur pass.
  244. * Only useful if useKernelBlur = false
  245. */
  246. public get blurBoxOffset(): number {
  247. return this._blurBoxOffset;
  248. }
  249. /**
  250. * Sets the blur box offset: offset applied during the blur pass.
  251. * Only useful if useKernelBlur = false
  252. */
  253. public set blurBoxOffset(value: number) {
  254. if (this._blurBoxOffset === value) {
  255. return;
  256. }
  257. this._blurBoxOffset = value;
  258. this._disposeBlurPostProcesses();
  259. }
  260. protected _blurScale = 2;
  261. /**
  262. * Gets the blur scale: scale of the blurred texture compared to the main shadow map.
  263. * 2 means half of the size.
  264. */
  265. public get blurScale(): number {
  266. return this._blurScale;
  267. }
  268. /**
  269. * Sets the blur scale: scale of the blurred texture compared to the main shadow map.
  270. * 2 means half of the size.
  271. */
  272. public set blurScale(value: number) {
  273. if (this._blurScale === value) {
  274. return;
  275. }
  276. this._blurScale = value;
  277. this._disposeBlurPostProcesses();
  278. }
  279. protected _blurKernel = 1;
  280. /**
  281. * Gets the blur kernel: kernel size of the blur pass.
  282. * Only useful if useKernelBlur = true
  283. */
  284. public get blurKernel(): number {
  285. return this._blurKernel;
  286. }
  287. /**
  288. * Sets the blur kernel: kernel size of the blur pass.
  289. * Only useful if useKernelBlur = true
  290. */
  291. public set blurKernel(value: number) {
  292. if (this._blurKernel === value) {
  293. return;
  294. }
  295. this._blurKernel = value;
  296. this._disposeBlurPostProcesses();
  297. }
  298. protected _useKernelBlur = false;
  299. /**
  300. * Gets whether the blur pass is a kernel blur (if true) or box blur.
  301. * Only useful in filtered mode (useBlurExponentialShadowMap...)
  302. */
  303. public get useKernelBlur(): boolean {
  304. return this._useKernelBlur;
  305. }
  306. /**
  307. * Sets whether the blur pass is a kernel blur (if true) or box blur.
  308. * Only useful in filtered mode (useBlurExponentialShadowMap...)
  309. */
  310. public set useKernelBlur(value: boolean) {
  311. if (this._useKernelBlur === value) {
  312. return;
  313. }
  314. this._useKernelBlur = value;
  315. this._disposeBlurPostProcesses();
  316. }
  317. protected _depthScale: number;
  318. /**
  319. * Gets the depth scale used in ESM mode.
  320. */
  321. public get depthScale(): number {
  322. return this._depthScale !== undefined ? this._depthScale : this._light.getDepthScale();
  323. }
  324. /**
  325. * Sets the depth scale used in ESM mode.
  326. * This can override the scale stored on the light.
  327. */
  328. public set depthScale(value: number) {
  329. this._depthScale = value;
  330. }
  331. protected _validateFilter(filter: number): number {
  332. return filter;
  333. }
  334. protected _filter = ShadowGenerator.FILTER_NONE;
  335. /**
  336. * Gets the current mode of the shadow generator (normal, PCF, ESM...).
  337. * The returned value is a number equal to one of the available mode defined in ShadowMap.FILTER_x like _FILTER_NONE
  338. */
  339. public get filter(): number {
  340. return this._filter;
  341. }
  342. /**
  343. * Sets the current mode of the shadow generator (normal, PCF, ESM...).
  344. * The returned value is a number equal to one of the available mode defined in ShadowMap.FILTER_x like _FILTER_NONE
  345. */
  346. public set filter(value: number) {
  347. value = this._validateFilter(value);
  348. // Blurring the cubemap is going to be too expensive. Reverting to unblurred version
  349. if (this._light.needCube()) {
  350. if (value === ShadowGenerator.FILTER_BLUREXPONENTIALSHADOWMAP) {
  351. this.useExponentialShadowMap = true;
  352. return;
  353. }
  354. else if (value === ShadowGenerator.FILTER_BLURCLOSEEXPONENTIALSHADOWMAP) {
  355. this.useCloseExponentialShadowMap = true;
  356. return;
  357. }
  358. // PCF on cubemap would also be expensive
  359. else if (value === ShadowGenerator.FILTER_PCF || value === ShadowGenerator.FILTER_PCSS) {
  360. this.usePoissonSampling = true;
  361. return;
  362. }
  363. }
  364. // Weblg1 fallback for PCF.
  365. if (value === ShadowGenerator.FILTER_PCF || value === ShadowGenerator.FILTER_PCSS) {
  366. if (this._scene.getEngine().webGLVersion === 1) {
  367. this.usePoissonSampling = true;
  368. return;
  369. }
  370. }
  371. if (this._filter === value) {
  372. return;
  373. }
  374. this._filter = value;
  375. this._disposeBlurPostProcesses();
  376. this._applyFilterValues();
  377. this._light._markMeshesAsLightDirty();
  378. }
  379. /**
  380. * Gets if the current filter is set to Poisson Sampling.
  381. */
  382. public get usePoissonSampling(): boolean {
  383. return this.filter === ShadowGenerator.FILTER_POISSONSAMPLING;
  384. }
  385. /**
  386. * Sets the current filter to Poisson Sampling.
  387. */
  388. public set usePoissonSampling(value: boolean) {
  389. let filter = this._validateFilter(ShadowGenerator.FILTER_POISSONSAMPLING);
  390. if (!value && this.filter !== ShadowGenerator.FILTER_POISSONSAMPLING) {
  391. return;
  392. }
  393. this.filter = (value ? filter : ShadowGenerator.FILTER_NONE);
  394. }
  395. /**
  396. * Gets if the current filter is set to ESM.
  397. */
  398. public get useExponentialShadowMap(): boolean {
  399. return this.filter === ShadowGenerator.FILTER_EXPONENTIALSHADOWMAP;
  400. }
  401. /**
  402. * Sets the current filter is to ESM.
  403. */
  404. public set useExponentialShadowMap(value: boolean) {
  405. let filter = this._validateFilter(ShadowGenerator.FILTER_EXPONENTIALSHADOWMAP);
  406. if (!value && this.filter !== ShadowGenerator.FILTER_EXPONENTIALSHADOWMAP) {
  407. return;
  408. }
  409. this.filter = (value ? filter : ShadowGenerator.FILTER_NONE);
  410. }
  411. /**
  412. * Gets if the current filter is set to filtered ESM.
  413. */
  414. public get useBlurExponentialShadowMap(): boolean {
  415. return this.filter === ShadowGenerator.FILTER_BLUREXPONENTIALSHADOWMAP;
  416. }
  417. /**
  418. * Gets if the current filter is set to filtered ESM.
  419. */
  420. public set useBlurExponentialShadowMap(value: boolean) {
  421. let filter = this._validateFilter(ShadowGenerator.FILTER_BLUREXPONENTIALSHADOWMAP);
  422. if (!value && this.filter !== ShadowGenerator.FILTER_BLUREXPONENTIALSHADOWMAP) {
  423. return;
  424. }
  425. this.filter = (value ? filter : ShadowGenerator.FILTER_NONE);
  426. }
  427. /**
  428. * Gets if the current filter is set to "close ESM" (using the inverse of the
  429. * exponential to prevent steep falloff artifacts).
  430. */
  431. public get useCloseExponentialShadowMap(): boolean {
  432. return this.filter === ShadowGenerator.FILTER_CLOSEEXPONENTIALSHADOWMAP;
  433. }
  434. /**
  435. * Sets the current filter to "close ESM" (using the inverse of the
  436. * exponential to prevent steep falloff artifacts).
  437. */
  438. public set useCloseExponentialShadowMap(value: boolean) {
  439. let filter = this._validateFilter(ShadowGenerator.FILTER_CLOSEEXPONENTIALSHADOWMAP);
  440. if (!value && this.filter !== ShadowGenerator.FILTER_CLOSEEXPONENTIALSHADOWMAP) {
  441. return;
  442. }
  443. this.filter = (value ? filter : ShadowGenerator.FILTER_NONE);
  444. }
  445. /**
  446. * Gets if the current filter is set to filtered "close ESM" (using the inverse of the
  447. * exponential to prevent steep falloff artifacts).
  448. */
  449. public get useBlurCloseExponentialShadowMap(): boolean {
  450. return this.filter === ShadowGenerator.FILTER_BLURCLOSEEXPONENTIALSHADOWMAP;
  451. }
  452. /**
  453. * Sets the current filter to filtered "close ESM" (using the inverse of the
  454. * exponential to prevent steep falloff artifacts).
  455. */
  456. public set useBlurCloseExponentialShadowMap(value: boolean) {
  457. let filter = this._validateFilter(ShadowGenerator.FILTER_BLURCLOSEEXPONENTIALSHADOWMAP);
  458. if (!value && this.filter !== ShadowGenerator.FILTER_BLURCLOSEEXPONENTIALSHADOWMAP) {
  459. return;
  460. }
  461. this.filter = (value ? filter : ShadowGenerator.FILTER_NONE);
  462. }
  463. /**
  464. * Gets if the current filter is set to "PCF" (percentage closer filtering).
  465. */
  466. public get usePercentageCloserFiltering(): boolean {
  467. return this.filter === ShadowGenerator.FILTER_PCF;
  468. }
  469. /**
  470. * Sets the current filter to "PCF" (percentage closer filtering).
  471. */
  472. public set usePercentageCloserFiltering(value: boolean) {
  473. let filter = this._validateFilter(ShadowGenerator.FILTER_PCF);
  474. if (!value && this.filter !== ShadowGenerator.FILTER_PCF) {
  475. return;
  476. }
  477. this.filter = (value ? filter : ShadowGenerator.FILTER_NONE);
  478. }
  479. protected _filteringQuality = ShadowGenerator.QUALITY_HIGH;
  480. /**
  481. * Gets the PCF or PCSS Quality.
  482. * Only valid if usePercentageCloserFiltering or usePercentageCloserFiltering is true.
  483. */
  484. public get filteringQuality(): number {
  485. return this._filteringQuality;
  486. }
  487. /**
  488. * Sets the PCF or PCSS Quality.
  489. * Only valid if usePercentageCloserFiltering or usePercentageCloserFiltering is true.
  490. */
  491. public set filteringQuality(filteringQuality: number) {
  492. if (this._filteringQuality === filteringQuality) {
  493. return;
  494. }
  495. this._filteringQuality = filteringQuality;
  496. this._disposeBlurPostProcesses();
  497. this._applyFilterValues();
  498. this._light._markMeshesAsLightDirty();
  499. }
  500. /**
  501. * Gets if the current filter is set to "PCSS" (contact hardening).
  502. */
  503. public get useContactHardeningShadow(): boolean {
  504. return this.filter === ShadowGenerator.FILTER_PCSS;
  505. }
  506. /**
  507. * Sets the current filter to "PCSS" (contact hardening).
  508. */
  509. public set useContactHardeningShadow(value: boolean) {
  510. let filter = this._validateFilter(ShadowGenerator.FILTER_PCSS);
  511. if (!value && this.filter !== ShadowGenerator.FILTER_PCSS) {
  512. return;
  513. }
  514. this.filter = (value ? filter : ShadowGenerator.FILTER_NONE);
  515. }
  516. protected _contactHardeningLightSizeUVRatio = 0.1;
  517. /**
  518. * Gets the Light Size (in shadow map uv unit) used in PCSS to determine the blocker search area and the penumbra size.
  519. * Using a ratio helps keeping shape stability independently of the map size.
  520. *
  521. * It does not account for the light projection as it was having too much
  522. * instability during the light setup or during light position changes.
  523. *
  524. * Only valid if useContactHardeningShadow is true.
  525. */
  526. public get contactHardeningLightSizeUVRatio(): number {
  527. return this._contactHardeningLightSizeUVRatio;
  528. }
  529. /**
  530. * Sets the Light Size (in shadow map uv unit) used in PCSS to determine the blocker search area and the penumbra size.
  531. * Using a ratio helps keeping shape stability independently of the map size.
  532. *
  533. * It does not account for the light projection as it was having too much
  534. * instability during the light setup or during light position changes.
  535. *
  536. * Only valid if useContactHardeningShadow is true.
  537. */
  538. public set contactHardeningLightSizeUVRatio(contactHardeningLightSizeUVRatio: number) {
  539. this._contactHardeningLightSizeUVRatio = contactHardeningLightSizeUVRatio;
  540. }
  541. protected _darkness = 0;
  542. /** Gets or sets the actual darkness of a shadow */
  543. public get darkness() {
  544. return this._darkness;
  545. }
  546. public set darkness(value: number) {
  547. this.setDarkness(value);
  548. }
  549. /**
  550. * Returns the darkness value (float). This can only decrease the actual darkness of a shadow.
  551. * 0 means strongest and 1 would means no shadow.
  552. * @returns the darkness.
  553. */
  554. public getDarkness(): number {
  555. return this._darkness;
  556. }
  557. /**
  558. * Sets the darkness value (float). This can only decrease the actual darkness of a shadow.
  559. * @param darkness The darkness value 0 means strongest and 1 would means no shadow.
  560. * @returns the shadow generator allowing fluent coding.
  561. */
  562. public setDarkness(darkness: number): ShadowGenerator {
  563. if (darkness >= 1.0) {
  564. this._darkness = 1.0;
  565. }
  566. else if (darkness <= 0.0) {
  567. this._darkness = 0.0;
  568. }
  569. else {
  570. this._darkness = darkness;
  571. }
  572. return this;
  573. }
  574. protected _transparencyShadow = false;
  575. /** Gets or sets the ability to have transparent shadow */
  576. public get transparencyShadow() {
  577. return this._transparencyShadow;
  578. }
  579. public set transparencyShadow(value: boolean) {
  580. this.setTransparencyShadow(value);
  581. }
  582. /**
  583. * Sets the ability to have transparent shadow (boolean).
  584. * @param transparent True if transparent else False
  585. * @returns the shadow generator allowing fluent coding
  586. */
  587. public setTransparencyShadow(transparent: boolean): ShadowGenerator {
  588. this._transparencyShadow = transparent;
  589. return this;
  590. }
  591. protected _shadowMap: Nullable<RenderTargetTexture>;
  592. protected _shadowMap2: Nullable<RenderTargetTexture>;
  593. /**
  594. * Gets the main RTT containing the shadow map (usually storing depth from the light point of view).
  595. * @returns The render target texture if present otherwise, null
  596. */
  597. public getShadowMap(): Nullable<RenderTargetTexture> {
  598. return this._shadowMap;
  599. }
  600. /**
  601. * Gets the RTT used during rendering (can be a blurred version of the shadow map or the shadow map itself).
  602. * @returns The render target texture if the shadow map is present otherwise, null
  603. */
  604. public getShadowMapForRendering(): Nullable<RenderTargetTexture> {
  605. if (this._shadowMap2) {
  606. return this._shadowMap2;
  607. }
  608. return this._shadowMap;
  609. }
  610. /**
  611. * Gets the class name of that object
  612. * @returns "ShadowGenerator"
  613. */
  614. public getClassName(): string {
  615. return ShadowGenerator.CLASSNAME;
  616. }
  617. /**
  618. * Helper function to add a mesh and its descendants to the list of shadow casters.
  619. * @param mesh Mesh to add
  620. * @param includeDescendants boolean indicating if the descendants should be added. Default to true
  621. * @returns the Shadow Generator itself
  622. */
  623. public addShadowCaster(mesh: AbstractMesh, includeDescendants = true): ShadowGenerator {
  624. if (!this._shadowMap) {
  625. return this;
  626. }
  627. if (!this._shadowMap.renderList) {
  628. this._shadowMap.renderList = [];
  629. }
  630. this._shadowMap.renderList.push(mesh);
  631. if (includeDescendants) {
  632. this._shadowMap.renderList.push(...mesh.getChildMeshes());
  633. }
  634. return this;
  635. }
  636. /**
  637. * Helper function to remove a mesh and its descendants from the list of shadow casters
  638. * @param mesh Mesh to remove
  639. * @param includeDescendants boolean indicating if the descendants should be removed. Default to true
  640. * @returns the Shadow Generator itself
  641. */
  642. public removeShadowCaster(mesh: AbstractMesh, includeDescendants = true): ShadowGenerator {
  643. if (!this._shadowMap || !this._shadowMap.renderList) {
  644. return this;
  645. }
  646. var index = this._shadowMap.renderList.indexOf(mesh);
  647. if (index !== -1) {
  648. this._shadowMap.renderList.splice(index, 1);
  649. }
  650. if (includeDescendants) {
  651. for (var child of mesh.getChildren()) {
  652. this.removeShadowCaster(<any>child);
  653. }
  654. }
  655. return this;
  656. }
  657. /**
  658. * Controls the extent to which the shadows fade out at the edge of the frustum
  659. */
  660. public frustumEdgeFalloff = 0;
  661. protected _light: IShadowLight;
  662. /**
  663. * Returns the associated light object.
  664. * @returns the light generating the shadow
  665. */
  666. public getLight(): IShadowLight {
  667. return this._light;
  668. }
  669. /**
  670. * If true the shadow map is generated by rendering the back face of the mesh instead of the front face.
  671. * This can help with self-shadowing as the geometry making up the back of objects is slightly offset.
  672. * It might on the other hand introduce peter panning.
  673. */
  674. public forceBackFacesOnly = false;
  675. protected _scene: Scene;
  676. protected _lightDirection = Vector3.Zero();
  677. protected _effect: Effect;
  678. protected _viewMatrix = Matrix.Zero();
  679. protected _projectionMatrix = Matrix.Zero();
  680. protected _transformMatrix = Matrix.Zero();
  681. protected _cachedPosition: Vector3 = new Vector3(Number.MAX_VALUE, Number.MAX_VALUE, Number.MAX_VALUE);
  682. protected _cachedDirection: Vector3 = new Vector3(Number.MAX_VALUE, Number.MAX_VALUE, Number.MAX_VALUE);
  683. protected _cachedDefines: string;
  684. protected _currentRenderID: number;
  685. protected _boxBlurPostprocess: Nullable<PostProcess>;
  686. protected _kernelBlurXPostprocess: Nullable<PostProcess>;
  687. protected _kernelBlurYPostprocess: Nullable<PostProcess>;
  688. protected _blurPostProcesses: PostProcess[];
  689. protected _mapSize: number;
  690. protected _currentFaceIndex = 0;
  691. protected _currentFaceIndexCache = 0;
  692. protected _textureType: number;
  693. protected _defaultTextureMatrix = Matrix.Identity();
  694. protected _storedUniqueId: Nullable<number>;
  695. /** @hidden */
  696. public static _SceneComponentInitialization: (scene: Scene) => void = (_) => {
  697. throw _DevTools.WarnImport("ShadowGeneratorSceneComponent");
  698. }
  699. /**
  700. * Creates a ShadowGenerator object.
  701. * A ShadowGenerator is the required tool to use the shadows.
  702. * Each light casting shadows needs to use its own ShadowGenerator.
  703. * Documentation : https://doc.babylonjs.com/babylon101/shadows
  704. * @param mapSize The size of the texture what stores the shadows. Example : 1024.
  705. * @param light The light object generating the shadows.
  706. * @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.
  707. */
  708. constructor(mapSize: number, light: IShadowLight, usefulFloatFirst?: boolean) {
  709. this._mapSize = mapSize;
  710. this._light = light;
  711. this._scene = light.getScene();
  712. light._shadowGenerator = this;
  713. ShadowGenerator._SceneComponentInitialization(this._scene);
  714. // Texture type fallback from float to int if not supported.
  715. var caps = this._scene.getEngine().getCaps();
  716. if (!usefulFloatFirst) {
  717. if (caps.textureHalfFloatRender && caps.textureHalfFloatLinearFiltering) {
  718. this._textureType = Constants.TEXTURETYPE_HALF_FLOAT;
  719. }
  720. else if (caps.textureFloatRender && caps.textureFloatLinearFiltering) {
  721. this._textureType = Constants.TEXTURETYPE_FLOAT;
  722. }
  723. else {
  724. this._textureType = Constants.TEXTURETYPE_UNSIGNED_INT;
  725. }
  726. } else {
  727. if (caps.textureFloatRender && caps.textureFloatLinearFiltering) {
  728. this._textureType = Constants.TEXTURETYPE_FLOAT;
  729. }
  730. else if (caps.textureHalfFloatRender && caps.textureHalfFloatLinearFiltering) {
  731. this._textureType = Constants.TEXTURETYPE_HALF_FLOAT;
  732. }
  733. else {
  734. this._textureType = Constants.TEXTURETYPE_UNSIGNED_INT;
  735. }
  736. }
  737. this._initializeGenerator();
  738. this._applyFilterValues();
  739. }
  740. protected _initializeGenerator(): void {
  741. this._light._markMeshesAsLightDirty();
  742. this._initializeShadowMap();
  743. }
  744. protected _createTargetRenderTexture(): void {
  745. let engine = this._scene.getEngine();
  746. if (engine.webGLVersion > 1) {
  747. this._shadowMap = new RenderTargetTexture(this._light.name + "_shadowMap", this._mapSize, this._scene, false, true, this._textureType, this._light.needCube(), undefined, false, false);
  748. this._shadowMap.createDepthStencilTexture(Constants.LESS, true);
  749. }
  750. else {
  751. this._shadowMap = new RenderTargetTexture(this._light.name + "_shadowMap", this._mapSize, this._scene, false, true, this._textureType, this._light.needCube());
  752. }
  753. }
  754. protected _initializeShadowMap(): void {
  755. this._createTargetRenderTexture();
  756. if (this._shadowMap === null) {
  757. return;
  758. }
  759. this._shadowMap.wrapU = Texture.CLAMP_ADDRESSMODE;
  760. this._shadowMap.wrapV = Texture.CLAMP_ADDRESSMODE;
  761. this._shadowMap.anisotropicFilteringLevel = 1;
  762. this._shadowMap.updateSamplingMode(Texture.BILINEAR_SAMPLINGMODE);
  763. this._shadowMap.renderParticles = false;
  764. this._shadowMap.ignoreCameraViewport = true;
  765. if (this._storedUniqueId) {
  766. this._shadowMap.uniqueId = this._storedUniqueId;
  767. }
  768. // Custom render function.
  769. this._shadowMap.customRenderFunction = this._renderForShadowMap.bind(this);
  770. // Force the mesh is ready funcion to true as we are double checking it
  771. // in the custom render function. Also it prevents side effects and useless
  772. // shader variations in DEPTHPREPASS mode.
  773. this._shadowMap.customIsReadyFunction = (m: AbstractMesh, r: number) => {
  774. return true;
  775. };
  776. let engine = this._scene.getEngine();
  777. // Record Face Index before render.
  778. this._shadowMap.onBeforeRenderObservable.add((faceIndex: number) => {
  779. this._currentFaceIndex = faceIndex;
  780. if (this._filter === ShadowGenerator.FILTER_PCF) {
  781. engine.setColorWrite(false);
  782. }
  783. if (this._scene.getSceneUniformBuffer().useUbo) {
  784. const sceneUBO = this._scene.getSceneUniformBuffer();
  785. sceneUBO.updateMatrix("viewProjection", this.getTransformMatrix());
  786. sceneUBO.updateMatrix("view", this._viewMatrix);
  787. sceneUBO.update();
  788. }
  789. });
  790. // Blur if required afer render.
  791. this._shadowMap.onAfterUnbindObservable.add(() => {
  792. if (this._scene.getSceneUniformBuffer().useUbo) {
  793. const sceneUBO = this._scene.getSceneUniformBuffer();
  794. sceneUBO.updateMatrix("viewProjection", this._scene.getTransformMatrix());
  795. sceneUBO.updateMatrix("view", this._scene.getViewMatrix());
  796. sceneUBO.update();
  797. }
  798. if (this._filter === ShadowGenerator.FILTER_PCF) {
  799. engine.setColorWrite(true);
  800. }
  801. if (!this.useBlurExponentialShadowMap && !this.useBlurCloseExponentialShadowMap) {
  802. return;
  803. }
  804. let shadowMap = this.getShadowMapForRendering();
  805. if (shadowMap) {
  806. this._scene.postProcessManager.directRender(this._blurPostProcesses, shadowMap.getInternalTexture(), true);
  807. }
  808. });
  809. // Clear according to the chosen filter.
  810. var clearZero = new Color4(0, 0, 0, 0);
  811. var clearOne = new Color4(1.0, 1.0, 1.0, 1.0);
  812. this._shadowMap.onClearObservable.add((engine) => {
  813. if (this._filter === ShadowGenerator.FILTER_PCF) {
  814. engine.clear(clearOne, false, true, false);
  815. }
  816. else if (this.useExponentialShadowMap || this.useBlurExponentialShadowMap) {
  817. engine.clear(clearZero, true, true, false);
  818. }
  819. else {
  820. engine.clear(clearOne, true, true, false);
  821. }
  822. });
  823. // Recreate on resize.
  824. this._shadowMap.onResizeObservable.add((RTT) => {
  825. this._storedUniqueId = this._shadowMap!.uniqueId;
  826. this._mapSize = RTT.getRenderSize();
  827. this._light._markMeshesAsLightDirty();
  828. this.recreateShadowMap();
  829. });
  830. // Ensures rendering groupids do not erase the depth buffer
  831. // or we would lose the shadows information.
  832. for (let i = RenderingManager.MIN_RENDERINGGROUPS; i < RenderingManager.MAX_RENDERINGGROUPS; i++) {
  833. this._shadowMap.setRenderingAutoClearDepthStencil(i, false);
  834. }
  835. }
  836. protected _initializeBlurRTTAndPostProcesses(): void {
  837. var engine = this._scene.getEngine();
  838. var targetSize = this._mapSize / this.blurScale;
  839. if (!this.useKernelBlur || this.blurScale !== 1.0) {
  840. this._shadowMap2 = new RenderTargetTexture(this._light.name + "_shadowMap2", targetSize, this._scene, false, true, this._textureType);
  841. this._shadowMap2.wrapU = Texture.CLAMP_ADDRESSMODE;
  842. this._shadowMap2.wrapV = Texture.CLAMP_ADDRESSMODE;
  843. this._shadowMap2.updateSamplingMode(Texture.BILINEAR_SAMPLINGMODE);
  844. }
  845. if (this.useKernelBlur) {
  846. this._kernelBlurXPostprocess = new BlurPostProcess(this._light.name + "KernelBlurX", new Vector2(1, 0), this.blurKernel, 1.0, null, Texture.BILINEAR_SAMPLINGMODE, engine, false, this._textureType);
  847. this._kernelBlurXPostprocess.width = targetSize;
  848. this._kernelBlurXPostprocess.height = targetSize;
  849. this._kernelBlurXPostprocess.onApplyObservable.add((effect) => {
  850. effect.setTexture("textureSampler", this._shadowMap);
  851. });
  852. this._kernelBlurYPostprocess = new BlurPostProcess(this._light.name + "KernelBlurY", new Vector2(0, 1), this.blurKernel, 1.0, null, Texture.BILINEAR_SAMPLINGMODE, engine, false, this._textureType);
  853. this._kernelBlurXPostprocess.autoClear = false;
  854. this._kernelBlurYPostprocess.autoClear = false;
  855. if (this._textureType === Constants.TEXTURETYPE_UNSIGNED_INT) {
  856. (<BlurPostProcess>this._kernelBlurXPostprocess).packedFloat = true;
  857. (<BlurPostProcess>this._kernelBlurYPostprocess).packedFloat = true;
  858. }
  859. this._blurPostProcesses = [this._kernelBlurXPostprocess, this._kernelBlurYPostprocess];
  860. }
  861. else {
  862. this._boxBlurPostprocess = new PostProcess(this._light.name + "DepthBoxBlur", "depthBoxBlur", ["screenSize", "boxOffset"], [], 1.0, null, Texture.BILINEAR_SAMPLINGMODE, engine, false, "#define OFFSET " + this._blurBoxOffset, this._textureType);
  863. this._boxBlurPostprocess.onApplyObservable.add((effect) => {
  864. effect.setFloat2("screenSize", targetSize, targetSize);
  865. effect.setTexture("textureSampler", this._shadowMap);
  866. });
  867. this._boxBlurPostprocess.autoClear = false;
  868. this._blurPostProcesses = [this._boxBlurPostprocess];
  869. }
  870. }
  871. protected _renderForShadowMap(opaqueSubMeshes: SmartArray<SubMesh>, alphaTestSubMeshes: SmartArray<SubMesh>, transparentSubMeshes: SmartArray<SubMesh>, depthOnlySubMeshes: SmartArray<SubMesh>): void {
  872. var index: number;
  873. let engine = this._scene.getEngine();
  874. const colorWrite = engine.getColorWrite();
  875. if (depthOnlySubMeshes.length) {
  876. engine.setColorWrite(false);
  877. for (index = 0; index < depthOnlySubMeshes.length; index++) {
  878. this._renderSubMeshForShadowMap(depthOnlySubMeshes.data[index]);
  879. }
  880. engine.setColorWrite(colorWrite);
  881. }
  882. for (index = 0; index < opaqueSubMeshes.length; index++) {
  883. this._renderSubMeshForShadowMap(opaqueSubMeshes.data[index]);
  884. }
  885. for (index = 0; index < alphaTestSubMeshes.length; index++) {
  886. this._renderSubMeshForShadowMap(alphaTestSubMeshes.data[index]);
  887. }
  888. if (this._transparencyShadow) {
  889. for (index = 0; index < transparentSubMeshes.length; index++) {
  890. this._renderSubMeshForShadowMap(transparentSubMeshes.data[index]);
  891. }
  892. }
  893. }
  894. protected _bindCustomEffectForRenderSubMeshForShadowMap(subMesh: SubMesh, effect: Effect, matriceNames: any): void {
  895. effect.setMatrix(matriceNames?.viewProjection ?? "viewProjection", this.getTransformMatrix());
  896. effect.setMatrix(matriceNames?.view ?? "view", this._viewMatrix);
  897. effect.setMatrix(matriceNames?.projection ?? "projection", this._projectionMatrix);
  898. }
  899. protected _renderSubMeshForShadowMap(subMesh: SubMesh): void {
  900. var ownerMesh = subMesh.getMesh();
  901. var replacementMesh = ownerMesh._internalAbstractMeshDataInfo._actAsRegularMesh ? ownerMesh : null;
  902. var renderingMesh = subMesh.getRenderingMesh();
  903. var effectiveMesh = replacementMesh ? replacementMesh : renderingMesh;
  904. var scene = this._scene;
  905. var engine = scene.getEngine();
  906. let material = subMesh.getMaterial();
  907. effectiveMesh._internalAbstractMeshDataInfo._isActiveIntermediate = false;
  908. if (!material || subMesh.verticesCount === 0) {
  909. return;
  910. }
  911. // Culling
  912. engine.setState(material.backFaceCulling);
  913. // Managing instances
  914. var batch = renderingMesh._getInstancesRenderList(subMesh._id, !!replacementMesh);
  915. if (batch.mustReturn) {
  916. return;
  917. }
  918. var hardwareInstancedRendering = (engine.getCaps().instancedArrays) && (batch.visibleInstances[subMesh._id] !== null) && (batch.visibleInstances[subMesh._id] !== undefined);
  919. if (this.isReady(subMesh, hardwareInstancedRendering)) {
  920. const shadowDepthWrapper = renderingMesh.material?.shadowDepthWrapper;
  921. let effect = shadowDepthWrapper?.getEffect(subMesh, this) ?? this._effect;
  922. engine.enableEffect(effect);
  923. renderingMesh._bind(subMesh, effect, material.fillMode);
  924. this.getTransformMatrix(); // make sur _cachedDirection et _cachedPosition are up to date
  925. effect.setFloat3("biasAndScaleSM", this.bias, this.normalBias, this.depthScale);
  926. if (this.getLight().getTypeID() === Light.LIGHTTYPEID_DIRECTIONALLIGHT) {
  927. effect.setVector3("lightDataSM", this._cachedDirection);
  928. }
  929. else {
  930. effect.setVector3("lightDataSM", this._cachedPosition);
  931. }
  932. if (scene.activeCamera) {
  933. effect.setFloat2("depthValuesSM", this.getLight().getDepthMinZ(scene.activeCamera), this.getLight().getDepthMinZ(scene.activeCamera) + this.getLight().getDepthMaxZ(scene.activeCamera));
  934. }
  935. if (shadowDepthWrapper) {
  936. subMesh._effectOverride = effect;
  937. if (shadowDepthWrapper.standalone) {
  938. shadowDepthWrapper.baseMaterial.bindForSubMesh(effectiveMesh.getWorldMatrix(), renderingMesh, subMesh);
  939. } else {
  940. material.bindForSubMesh(effectiveMesh.getWorldMatrix(), renderingMesh, subMesh);
  941. }
  942. subMesh._effectOverride = null;
  943. } else {
  944. effect.setMatrix("viewProjection", this.getTransformMatrix());
  945. // Alpha test
  946. if (material && material.needAlphaTesting()) {
  947. var alphaTexture = material.getAlphaTestTexture();
  948. if (alphaTexture) {
  949. effect.setTexture("diffuseSampler", alphaTexture);
  950. effect.setMatrix("diffuseMatrix", alphaTexture.getTextureMatrix() || this._defaultTextureMatrix);
  951. }
  952. }
  953. // Bones
  954. if (renderingMesh.useBones && renderingMesh.computeBonesUsingShaders && renderingMesh.skeleton) {
  955. const skeleton = renderingMesh.skeleton;
  956. if (skeleton.isUsingTextureForMatrices) {
  957. const boneTexture = skeleton.getTransformMatrixTexture(renderingMesh);
  958. if (!boneTexture) {
  959. return;
  960. }
  961. effect.setTexture("boneSampler", boneTexture);
  962. effect.setFloat("boneTextureWidth", 4.0 * (skeleton.bones.length + 1));
  963. } else {
  964. effect.setMatrices("mBones", skeleton.getTransformMatrices((renderingMesh)));
  965. }
  966. }
  967. // Morph targets
  968. MaterialHelper.BindMorphTargetParameters(renderingMesh, effect);
  969. // Clip planes
  970. MaterialHelper.BindClipPlane(effect, scene);
  971. }
  972. this._bindCustomEffectForRenderSubMeshForShadowMap(subMesh, effect, shadowDepthWrapper?._matriceNames);
  973. if (this.forceBackFacesOnly) {
  974. engine.setState(true, 0, false, true);
  975. }
  976. // Observables
  977. this.onBeforeShadowMapRenderMeshObservable.notifyObservers(renderingMesh);
  978. this.onBeforeShadowMapRenderObservable.notifyObservers(effect);
  979. // Draw
  980. renderingMesh._processRendering(effectiveMesh, subMesh, effect, material.fillMode, batch, hardwareInstancedRendering,
  981. (isInstance, world) => effect.setMatrix("world", world));
  982. if (this.forceBackFacesOnly) {
  983. engine.setState(true, 0, false, false);
  984. }
  985. // Observables
  986. this.onAfterShadowMapRenderObservable.notifyObservers(effect);
  987. this.onAfterShadowMapRenderMeshObservable.notifyObservers(renderingMesh);
  988. } else {
  989. // Need to reset refresh rate of the shadowMap
  990. if (this._shadowMap) {
  991. this._shadowMap.resetRefreshCounter();
  992. }
  993. }
  994. }
  995. protected _applyFilterValues(): void {
  996. if (!this._shadowMap) {
  997. return;
  998. }
  999. if (this.filter === ShadowGenerator.FILTER_NONE || this.filter === ShadowGenerator.FILTER_PCSS) {
  1000. this._shadowMap.updateSamplingMode(Texture.NEAREST_SAMPLINGMODE);
  1001. } else {
  1002. this._shadowMap.updateSamplingMode(Texture.BILINEAR_SAMPLINGMODE);
  1003. }
  1004. }
  1005. /**
  1006. * Forces all the attached effect to compile to enable rendering only once ready vs. lazyly compiling effects.
  1007. * @param onCompiled Callback triggered at the and of the effects compilation
  1008. * @param options Sets of optional options forcing the compilation with different modes
  1009. */
  1010. public forceCompilation(onCompiled?: (generator: IShadowGenerator) => void, options?: Partial<{ useInstances: boolean }>): void {
  1011. let localOptions = {
  1012. useInstances: false,
  1013. ...options
  1014. };
  1015. let shadowMap = this.getShadowMap();
  1016. if (!shadowMap) {
  1017. if (onCompiled) {
  1018. onCompiled(this);
  1019. }
  1020. return;
  1021. }
  1022. let renderList = shadowMap.renderList;
  1023. if (!renderList) {
  1024. if (onCompiled) {
  1025. onCompiled(this);
  1026. }
  1027. return;
  1028. }
  1029. var subMeshes = new Array<SubMesh>();
  1030. for (var mesh of renderList) {
  1031. subMeshes.push(...mesh.subMeshes);
  1032. }
  1033. if (subMeshes.length === 0) {
  1034. if (onCompiled) {
  1035. onCompiled(this);
  1036. }
  1037. return;
  1038. }
  1039. var currentIndex = 0;
  1040. var checkReady = () => {
  1041. if (!this._scene || !this._scene.getEngine()) {
  1042. return;
  1043. }
  1044. while (this.isReady(subMeshes[currentIndex], localOptions.useInstances)) {
  1045. currentIndex++;
  1046. if (currentIndex >= subMeshes.length) {
  1047. if (onCompiled) {
  1048. onCompiled(this);
  1049. }
  1050. return;
  1051. }
  1052. }
  1053. setTimeout(checkReady, 16);
  1054. };
  1055. checkReady();
  1056. }
  1057. /**
  1058. * Forces all the attached effect to compile to enable rendering only once ready vs. lazyly compiling effects.
  1059. * @param options Sets of optional options forcing the compilation with different modes
  1060. * @returns A promise that resolves when the compilation completes
  1061. */
  1062. public forceCompilationAsync(options?: Partial<{ useInstances: boolean }>): Promise<void> {
  1063. return new Promise((resolve) => {
  1064. this.forceCompilation(() => {
  1065. resolve();
  1066. }, options);
  1067. });
  1068. }
  1069. protected _isReadyCustomDefines(defines: any, subMesh: SubMesh, useInstances: boolean): void {
  1070. }
  1071. private _prepareShadowDefines(subMesh: SubMesh, useInstances: boolean, defines: string[]): string[] {
  1072. defines.push("#define SM_FLOAT " + (this._textureType !== Constants.TEXTURETYPE_UNSIGNED_INT ? "1" : "0"));
  1073. defines.push("#define SM_ESM " + (this.useExponentialShadowMap || this.useBlurExponentialShadowMap ? "1" : "0"));
  1074. defines.push("#define SM_DEPTHTEXTURE " + (this.usePercentageCloserFiltering || this.useContactHardeningShadow ? "1" : "0"));
  1075. var mesh = subMesh.getMesh();
  1076. // Normal bias.
  1077. defines.push("#define SM_NORMALBIAS " + (this.normalBias && mesh.isVerticesDataPresent(VertexBuffer.NormalKind) ? "1" : "0"));
  1078. defines.push("#define SM_DIRECTIONINLIGHTDATA " + (this.getLight().getTypeID() === Light.LIGHTTYPEID_DIRECTIONALLIGHT ? "1" : "0"));
  1079. // Point light
  1080. defines.push("#define SM_USEDISTANCE " + (this._light.needCube() ? "1" : "0"));
  1081. this._isReadyCustomDefines(defines, subMesh, useInstances);
  1082. return defines;
  1083. }
  1084. /**
  1085. * Determine wheter the shadow generator is ready or not (mainly all effects and related post processes needs to be ready).
  1086. * @param subMesh The submesh we want to render in the shadow map
  1087. * @param useInstances Defines wether will draw in the map using instances
  1088. * @returns true if ready otherwise, false
  1089. */
  1090. public isReady(subMesh: SubMesh, useInstances: boolean): boolean {
  1091. const material = subMesh.getMaterial(),
  1092. shadowDepthWrapper = material?.shadowDepthWrapper;
  1093. const defines: string[] = [];
  1094. this._prepareShadowDefines(subMesh, useInstances, defines);
  1095. if (shadowDepthWrapper) {
  1096. if (!shadowDepthWrapper.isReadyForSubMesh(subMesh, defines, this, useInstances)) {
  1097. return false;
  1098. }
  1099. } else {
  1100. var attribs = [VertexBuffer.PositionKind];
  1101. var mesh = subMesh.getMesh();
  1102. // Normal bias.
  1103. if (this.normalBias && mesh.isVerticesDataPresent(VertexBuffer.NormalKind)) {
  1104. attribs.push(VertexBuffer.NormalKind);
  1105. defines.push("#define NORMAL");
  1106. if (mesh.nonUniformScaling) {
  1107. defines.push("#define NONUNIFORMSCALING");
  1108. }
  1109. }
  1110. // Alpha test
  1111. if (material && material.needAlphaTesting()) {
  1112. var alphaTexture = material.getAlphaTestTexture();
  1113. if (alphaTexture) {
  1114. defines.push("#define ALPHATEST");
  1115. if (mesh.isVerticesDataPresent(VertexBuffer.UVKind)) {
  1116. attribs.push(VertexBuffer.UVKind);
  1117. defines.push("#define UV1");
  1118. }
  1119. if (mesh.isVerticesDataPresent(VertexBuffer.UV2Kind)) {
  1120. if (alphaTexture.coordinatesIndex === 1) {
  1121. attribs.push(VertexBuffer.UV2Kind);
  1122. defines.push("#define UV2");
  1123. }
  1124. }
  1125. }
  1126. }
  1127. // Bones
  1128. const fallbacks = new EffectFallbacks();
  1129. if (mesh.useBones && mesh.computeBonesUsingShaders && mesh.skeleton) {
  1130. attribs.push(VertexBuffer.MatricesIndicesKind);
  1131. attribs.push(VertexBuffer.MatricesWeightsKind);
  1132. if (mesh.numBoneInfluencers > 4) {
  1133. attribs.push(VertexBuffer.MatricesIndicesExtraKind);
  1134. attribs.push(VertexBuffer.MatricesWeightsExtraKind);
  1135. }
  1136. const skeleton = mesh.skeleton;
  1137. defines.push("#define NUM_BONE_INFLUENCERS " + mesh.numBoneInfluencers);
  1138. if (mesh.numBoneInfluencers > 0) {
  1139. fallbacks.addCPUSkinningFallback(0, mesh);
  1140. }
  1141. if (skeleton.isUsingTextureForMatrices) {
  1142. defines.push("#define BONETEXTURE");
  1143. } else {
  1144. defines.push("#define BonesPerMesh " + (skeleton.bones.length + 1));
  1145. }
  1146. } else {
  1147. defines.push("#define NUM_BONE_INFLUENCERS 0");
  1148. }
  1149. // Morph targets
  1150. var manager = (<Mesh>mesh).morphTargetManager;
  1151. let morphInfluencers = 0;
  1152. if (manager) {
  1153. if (manager.numInfluencers > 0) {
  1154. defines.push("#define MORPHTARGETS");
  1155. morphInfluencers = manager.numInfluencers;
  1156. defines.push("#define NUM_MORPH_INFLUENCERS " + morphInfluencers);
  1157. MaterialHelper.PrepareAttributesForMorphTargetsInfluencers(attribs, mesh, morphInfluencers);
  1158. }
  1159. }
  1160. // ClipPlanes
  1161. const scene = this._scene;
  1162. if (scene.clipPlane) {
  1163. defines.push("#define CLIPPLANE");
  1164. }
  1165. if (scene.clipPlane2) {
  1166. defines.push("#define CLIPPLANE2");
  1167. }
  1168. if (scene.clipPlane3) {
  1169. defines.push("#define CLIPPLANE3");
  1170. }
  1171. if (scene.clipPlane4) {
  1172. defines.push("#define CLIPPLANE4");
  1173. }
  1174. if (scene.clipPlane5) {
  1175. defines.push("#define CLIPPLANE5");
  1176. }
  1177. if (scene.clipPlane6) {
  1178. defines.push("#define CLIPPLANE6");
  1179. }
  1180. // Instances
  1181. if (useInstances) {
  1182. defines.push("#define INSTANCES");
  1183. MaterialHelper.PushAttributesForInstances(attribs);
  1184. }
  1185. if (this.customShaderOptions) {
  1186. if (this.customShaderOptions.defines) {
  1187. for (var define of this.customShaderOptions.defines) {
  1188. if (defines.indexOf(define) === -1) {
  1189. defines.push(define);
  1190. }
  1191. }
  1192. }
  1193. }
  1194. // Get correct effect
  1195. var join = defines.join("\n");
  1196. if (this._cachedDefines !== join) {
  1197. this._cachedDefines = join;
  1198. let shaderName = "shadowMap";
  1199. let uniforms = ["world", "mBones", "viewProjection", "diffuseMatrix", "lightDataSM", "depthValuesSM", "biasAndScaleSM", "morphTargetInfluences", "boneTextureWidth",
  1200. "vClipPlane", "vClipPlane2", "vClipPlane3", "vClipPlane4", "vClipPlane5", "vClipPlane6"];
  1201. let samplers = ["diffuseSampler", "boneSampler"];
  1202. // Custom shader?
  1203. if (this.customShaderOptions) {
  1204. shaderName = this.customShaderOptions.shaderName;
  1205. if (this.customShaderOptions.attributes) {
  1206. for (var attrib of this.customShaderOptions.attributes) {
  1207. if (attribs.indexOf(attrib) === -1) {
  1208. attribs.push(attrib);
  1209. }
  1210. }
  1211. }
  1212. if (this.customShaderOptions.uniforms) {
  1213. for (var uniform of this.customShaderOptions.uniforms) {
  1214. if (uniforms.indexOf(uniform) === -1) {
  1215. uniforms.push(uniform);
  1216. }
  1217. }
  1218. }
  1219. if (this.customShaderOptions.samplers) {
  1220. for (var sampler of this.customShaderOptions.samplers) {
  1221. if (samplers.indexOf(sampler) === -1) {
  1222. samplers.push(sampler);
  1223. }
  1224. }
  1225. }
  1226. }
  1227. this._effect = this._scene.getEngine().createEffect(shaderName,
  1228. attribs, uniforms,
  1229. samplers, join,
  1230. fallbacks, undefined, undefined, { maxSimultaneousMorphTargets: morphInfluencers });
  1231. }
  1232. if (!this._effect.isReady()) {
  1233. return false;
  1234. }
  1235. }
  1236. if (this.useBlurExponentialShadowMap || this.useBlurCloseExponentialShadowMap) {
  1237. if (!this._blurPostProcesses || !this._blurPostProcesses.length) {
  1238. this._initializeBlurRTTAndPostProcesses();
  1239. }
  1240. }
  1241. if (this._kernelBlurXPostprocess && !this._kernelBlurXPostprocess.isReady()) {
  1242. return false;
  1243. }
  1244. if (this._kernelBlurYPostprocess && !this._kernelBlurYPostprocess.isReady()) {
  1245. return false;
  1246. }
  1247. if (this._boxBlurPostprocess && !this._boxBlurPostprocess.isReady()) {
  1248. return false;
  1249. }
  1250. return true;
  1251. }
  1252. /**
  1253. * Prepare all the defines in a material relying on a shadow map at the specified light index.
  1254. * @param defines Defines of the material we want to update
  1255. * @param lightIndex Index of the light in the enabled light list of the material
  1256. */
  1257. public prepareDefines(defines: any, lightIndex: number): void {
  1258. var scene = this._scene;
  1259. var light = this._light;
  1260. if (!scene.shadowsEnabled || !light.shadowEnabled) {
  1261. return;
  1262. }
  1263. defines["SHADOW" + lightIndex] = true;
  1264. if (this.useContactHardeningShadow) {
  1265. defines["SHADOWPCSS" + lightIndex] = true;
  1266. if (this._filteringQuality === ShadowGenerator.QUALITY_LOW) {
  1267. defines["SHADOWLOWQUALITY" + lightIndex] = true;
  1268. }
  1269. else if (this._filteringQuality === ShadowGenerator.QUALITY_MEDIUM) {
  1270. defines["SHADOWMEDIUMQUALITY" + lightIndex] = true;
  1271. }
  1272. // else default to high.
  1273. }
  1274. else if (this.usePercentageCloserFiltering) {
  1275. defines["SHADOWPCF" + lightIndex] = true;
  1276. if (this._filteringQuality === ShadowGenerator.QUALITY_LOW) {
  1277. defines["SHADOWLOWQUALITY" + lightIndex] = true;
  1278. }
  1279. else if (this._filteringQuality === ShadowGenerator.QUALITY_MEDIUM) {
  1280. defines["SHADOWMEDIUMQUALITY" + lightIndex] = true;
  1281. }
  1282. // else default to high.
  1283. }
  1284. else if (this.usePoissonSampling) {
  1285. defines["SHADOWPOISSON" + lightIndex] = true;
  1286. }
  1287. else if (this.useExponentialShadowMap || this.useBlurExponentialShadowMap) {
  1288. defines["SHADOWESM" + lightIndex] = true;
  1289. }
  1290. else if (this.useCloseExponentialShadowMap || this.useBlurCloseExponentialShadowMap) {
  1291. defines["SHADOWCLOSEESM" + lightIndex] = true;
  1292. }
  1293. if (light.needCube()) {
  1294. defines["SHADOWCUBE" + lightIndex] = true;
  1295. }
  1296. }
  1297. /**
  1298. * Binds the shadow related information inside of an effect (information like near, far, darkness...
  1299. * defined in the generator but impacting the effect).
  1300. * @param lightIndex Index of the light in the enabled light list of the material owning the effect
  1301. * @param effect The effect we are binfing the information for
  1302. */
  1303. public bindShadowLight(lightIndex: string, effect: Effect): void {
  1304. const light = this._light;
  1305. const scene = this._scene;
  1306. if (!scene.shadowsEnabled || !light.shadowEnabled) {
  1307. return;
  1308. }
  1309. const camera = scene.activeCamera;
  1310. if (!camera) {
  1311. return;
  1312. }
  1313. const shadowMap = this.getShadowMap();
  1314. if (!shadowMap) {
  1315. return;
  1316. }
  1317. if (!light.needCube()) {
  1318. effect.setMatrix("lightMatrix" + lightIndex, this.getTransformMatrix());
  1319. }
  1320. // Only PCF uses depth stencil texture.
  1321. if (this._filter === ShadowGenerator.FILTER_PCF) {
  1322. effect.setDepthStencilTexture("shadowSampler" + lightIndex, this.getShadowMapForRendering());
  1323. light._uniformBuffer.updateFloat4("shadowsInfo", this.getDarkness(), shadowMap.getSize().width, 1 / shadowMap.getSize().width, this.frustumEdgeFalloff, lightIndex);
  1324. }
  1325. else if (this._filter === ShadowGenerator.FILTER_PCSS) {
  1326. effect.setDepthStencilTexture("shadowSampler" + lightIndex, this.getShadowMapForRendering());
  1327. effect.setTexture("depthSampler" + lightIndex, this.getShadowMapForRendering());
  1328. light._uniformBuffer.updateFloat4("shadowsInfo", this.getDarkness(), 1 / shadowMap.getSize().width, this._contactHardeningLightSizeUVRatio * shadowMap.getSize().width, this.frustumEdgeFalloff, lightIndex);
  1329. }
  1330. else {
  1331. effect.setTexture("shadowSampler" + lightIndex, this.getShadowMapForRendering());
  1332. light._uniformBuffer.updateFloat4("shadowsInfo", this.getDarkness(), this.blurScale / shadowMap.getSize().width, this.depthScale, this.frustumEdgeFalloff, lightIndex);
  1333. }
  1334. light._uniformBuffer.updateFloat2("depthValues", this.getLight().getDepthMinZ(camera), this.getLight().getDepthMinZ(camera) + this.getLight().getDepthMaxZ(camera), lightIndex);
  1335. }
  1336. /**
  1337. * Gets the transformation matrix used to project the meshes into the map from the light point of view.
  1338. * (eq to shadow prjection matrix * light transform matrix)
  1339. * @returns The transform matrix used to create the shadow map
  1340. */
  1341. public getTransformMatrix(): Matrix {
  1342. var scene = this._scene;
  1343. if (this._currentRenderID === scene.getRenderId() && this._currentFaceIndexCache === this._currentFaceIndex) {
  1344. return this._transformMatrix;
  1345. }
  1346. this._currentRenderID = scene.getRenderId();
  1347. this._currentFaceIndexCache = this._currentFaceIndex;
  1348. var lightPosition = this._light.position;
  1349. if (this._light.computeTransformedInformation()) {
  1350. lightPosition = this._light.transformedPosition;
  1351. }
  1352. Vector3.NormalizeToRef(this._light.getShadowDirection(this._currentFaceIndex), this._lightDirection);
  1353. if (Math.abs(Vector3.Dot(this._lightDirection, Vector3.Up())) === 1.0) {
  1354. this._lightDirection.z = 0.0000000000001; // Required to avoid perfectly perpendicular light
  1355. }
  1356. if (this._light.needProjectionMatrixCompute() || !this._cachedPosition || !this._cachedDirection || !lightPosition.equals(this._cachedPosition) || !this._lightDirection.equals(this._cachedDirection)) {
  1357. this._cachedPosition.copyFrom(lightPosition);
  1358. this._cachedDirection.copyFrom(this._lightDirection);
  1359. Matrix.LookAtLHToRef(lightPosition, lightPosition.add(this._lightDirection), Vector3.Up(), this._viewMatrix);
  1360. let shadowMap = this.getShadowMap();
  1361. if (shadowMap) {
  1362. let renderList = shadowMap.renderList;
  1363. if (renderList) {
  1364. this._light.setShadowProjectionMatrix(this._projectionMatrix, this._viewMatrix, renderList);
  1365. }
  1366. }
  1367. this._viewMatrix.multiplyToRef(this._projectionMatrix, this._transformMatrix);
  1368. }
  1369. return this._transformMatrix;
  1370. }
  1371. /**
  1372. * Recreates the shadow map dependencies like RTT and post processes. This can be used during the switch between
  1373. * Cube and 2D textures for instance.
  1374. */
  1375. public recreateShadowMap(): void {
  1376. let shadowMap = this._shadowMap;
  1377. if (!shadowMap) {
  1378. return;
  1379. }
  1380. // Track render list.
  1381. var renderList = shadowMap.renderList;
  1382. // Clean up existing data.
  1383. this._disposeRTTandPostProcesses();
  1384. // Reinitializes.
  1385. this._initializeGenerator();
  1386. // Reaffect the filter to ensure a correct fallback if necessary.
  1387. this.filter = this.filter;
  1388. // Reaffect the filter.
  1389. this._applyFilterValues();
  1390. // Reaffect Render List.
  1391. this._shadowMap!.renderList = renderList;
  1392. }
  1393. protected _disposeBlurPostProcesses(): void {
  1394. if (this._shadowMap2) {
  1395. this._shadowMap2.dispose();
  1396. this._shadowMap2 = null;
  1397. }
  1398. if (this._boxBlurPostprocess) {
  1399. this._boxBlurPostprocess.dispose();
  1400. this._boxBlurPostprocess = null;
  1401. }
  1402. if (this._kernelBlurXPostprocess) {
  1403. this._kernelBlurXPostprocess.dispose();
  1404. this._kernelBlurXPostprocess = null;
  1405. }
  1406. if (this._kernelBlurYPostprocess) {
  1407. this._kernelBlurYPostprocess.dispose();
  1408. this._kernelBlurYPostprocess = null;
  1409. }
  1410. this._blurPostProcesses = [];
  1411. }
  1412. protected _disposeRTTandPostProcesses(): void {
  1413. if (this._shadowMap) {
  1414. this._shadowMap.dispose();
  1415. this._shadowMap = null;
  1416. }
  1417. this._disposeBlurPostProcesses();
  1418. }
  1419. /**
  1420. * Disposes the ShadowGenerator.
  1421. * Returns nothing.
  1422. */
  1423. public dispose(): void {
  1424. this._disposeRTTandPostProcesses();
  1425. if (this._light) {
  1426. this._light._shadowGenerator = null;
  1427. this._light._markMeshesAsLightDirty();
  1428. }
  1429. this.onBeforeShadowMapRenderMeshObservable.clear();
  1430. this.onBeforeShadowMapRenderObservable.clear();
  1431. this.onAfterShadowMapRenderMeshObservable.clear();
  1432. this.onAfterShadowMapRenderObservable.clear();
  1433. }
  1434. /**
  1435. * Serializes the shadow generator setup to a json object.
  1436. * @returns The serialized JSON object
  1437. */
  1438. public serialize(): any {
  1439. var serializationObject: any = {};
  1440. var shadowMap = this.getShadowMap();
  1441. if (!shadowMap) {
  1442. return serializationObject;
  1443. }
  1444. serializationObject.className = this.getClassName();
  1445. serializationObject.lightId = this._light.id;
  1446. serializationObject.mapSize = shadowMap.getRenderSize();
  1447. serializationObject.forceBackFacesOnly = this.forceBackFacesOnly;
  1448. serializationObject.darkness = this.getDarkness();
  1449. serializationObject.transparencyShadow = this._transparencyShadow;
  1450. serializationObject.frustumEdgeFalloff = this.frustumEdgeFalloff;
  1451. serializationObject.bias = this.bias;
  1452. serializationObject.normalBias = this.normalBias;
  1453. serializationObject.usePercentageCloserFiltering = this.usePercentageCloserFiltering;
  1454. serializationObject.useContactHardeningShadow = this.useContactHardeningShadow;
  1455. serializationObject.contactHardeningLightSizeUVRatio = this.contactHardeningLightSizeUVRatio;
  1456. serializationObject.filteringQuality = this.filteringQuality;
  1457. serializationObject.useExponentialShadowMap = this.useExponentialShadowMap;
  1458. serializationObject.useBlurExponentialShadowMap = this.useBlurExponentialShadowMap;
  1459. serializationObject.useCloseExponentialShadowMap = this.useBlurExponentialShadowMap;
  1460. serializationObject.useBlurCloseExponentialShadowMap = this.useBlurExponentialShadowMap;
  1461. serializationObject.usePoissonSampling = this.usePoissonSampling;
  1462. serializationObject.depthScale = this.depthScale;
  1463. serializationObject.blurBoxOffset = this.blurBoxOffset;
  1464. serializationObject.blurKernel = this.blurKernel;
  1465. serializationObject.blurScale = this.blurScale;
  1466. serializationObject.useKernelBlur = this.useKernelBlur;
  1467. serializationObject.renderList = [];
  1468. if (shadowMap.renderList) {
  1469. for (var meshIndex = 0; meshIndex < shadowMap.renderList.length; meshIndex++) {
  1470. var mesh = shadowMap.renderList[meshIndex];
  1471. serializationObject.renderList.push(mesh.id);
  1472. }
  1473. }
  1474. return serializationObject;
  1475. }
  1476. /**
  1477. * Parses a serialized ShadowGenerator and returns a new ShadowGenerator.
  1478. * @param parsedShadowGenerator The JSON object to parse
  1479. * @param scene The scene to create the shadow map for
  1480. * @param constr A function that builds a shadow generator or undefined to create an instance of the default shadow generator
  1481. * @returns The parsed shadow generator
  1482. */
  1483. public static Parse(parsedShadowGenerator: any, scene: Scene, constr?: (mapSize: number, light: IShadowLight) => ShadowGenerator): ShadowGenerator {
  1484. var light = <IShadowLight>scene.getLightByID(parsedShadowGenerator.lightId);
  1485. var shadowGenerator = constr ? constr(parsedShadowGenerator.mapSize, light) : new ShadowGenerator(parsedShadowGenerator.mapSize, light);
  1486. var shadowMap = shadowGenerator.getShadowMap();
  1487. for (var meshIndex = 0; meshIndex < parsedShadowGenerator.renderList.length; meshIndex++) {
  1488. var meshes = scene.getMeshesByID(parsedShadowGenerator.renderList[meshIndex]);
  1489. meshes.forEach(function(mesh) {
  1490. if (!shadowMap) {
  1491. return;
  1492. }
  1493. if (!shadowMap.renderList) {
  1494. shadowMap.renderList = [];
  1495. }
  1496. shadowMap.renderList.push(mesh);
  1497. });
  1498. }
  1499. shadowGenerator.forceBackFacesOnly = !!parsedShadowGenerator.forceBackFacesOnly;
  1500. if (parsedShadowGenerator.darkness !== undefined) {
  1501. shadowGenerator.setDarkness(parsedShadowGenerator.darkness);
  1502. }
  1503. if (parsedShadowGenerator.transparencyShadow) {
  1504. shadowGenerator.setTransparencyShadow(true);
  1505. }
  1506. if (parsedShadowGenerator.frustumEdgeFalloff !== undefined) {
  1507. shadowGenerator.frustumEdgeFalloff = parsedShadowGenerator.frustumEdgeFalloff;
  1508. }
  1509. if (parsedShadowGenerator.bias !== undefined) {
  1510. shadowGenerator.bias = parsedShadowGenerator.bias;
  1511. }
  1512. if (parsedShadowGenerator.normalBias !== undefined) {
  1513. shadowGenerator.normalBias = parsedShadowGenerator.normalBias;
  1514. }
  1515. if (parsedShadowGenerator.usePercentageCloserFiltering) {
  1516. shadowGenerator.usePercentageCloserFiltering = true;
  1517. } else if (parsedShadowGenerator.useContactHardeningShadow) {
  1518. shadowGenerator.useContactHardeningShadow = true;
  1519. } else if (parsedShadowGenerator.usePoissonSampling) {
  1520. shadowGenerator.usePoissonSampling = true;
  1521. } else if (parsedShadowGenerator.useExponentialShadowMap) {
  1522. shadowGenerator.useExponentialShadowMap = true;
  1523. } else if (parsedShadowGenerator.useBlurExponentialShadowMap) {
  1524. shadowGenerator.useBlurExponentialShadowMap = true;
  1525. } else if (parsedShadowGenerator.useCloseExponentialShadowMap) {
  1526. shadowGenerator.useCloseExponentialShadowMap = true;
  1527. } else if (parsedShadowGenerator.useBlurCloseExponentialShadowMap) {
  1528. shadowGenerator.useBlurCloseExponentialShadowMap = true;
  1529. } else
  1530. // Backward compat
  1531. if (parsedShadowGenerator.useVarianceShadowMap) {
  1532. shadowGenerator.useExponentialShadowMap = true;
  1533. } else if (parsedShadowGenerator.useBlurVarianceShadowMap) {
  1534. shadowGenerator.useBlurExponentialShadowMap = true;
  1535. }
  1536. if (parsedShadowGenerator.contactHardeningLightSizeUVRatio !== undefined) {
  1537. shadowGenerator.contactHardeningLightSizeUVRatio = parsedShadowGenerator.contactHardeningLightSizeUVRatio;
  1538. }
  1539. if (parsedShadowGenerator.filteringQuality !== undefined) {
  1540. shadowGenerator.filteringQuality = parsedShadowGenerator.filteringQuality;
  1541. }
  1542. if (parsedShadowGenerator.depthScale) {
  1543. shadowGenerator.depthScale = parsedShadowGenerator.depthScale;
  1544. }
  1545. if (parsedShadowGenerator.blurScale) {
  1546. shadowGenerator.blurScale = parsedShadowGenerator.blurScale;
  1547. }
  1548. if (parsedShadowGenerator.blurBoxOffset) {
  1549. shadowGenerator.blurBoxOffset = parsedShadowGenerator.blurBoxOffset;
  1550. }
  1551. if (parsedShadowGenerator.useKernelBlur) {
  1552. shadowGenerator.useKernelBlur = parsedShadowGenerator.useKernelBlur;
  1553. }
  1554. if (parsedShadowGenerator.blurKernel) {
  1555. shadowGenerator.blurKernel = parsedShadowGenerator.blurKernel;
  1556. }
  1557. return shadowGenerator;
  1558. }
  1559. }