babylon.shadowGenerator.ts 51 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250
  1. module BABYLON {
  2. /**
  3. * Interface to implement to create a shadow generator compatible with BJS.
  4. */
  5. export interface IShadowGenerator {
  6. /**
  7. * Gets the main RTT containing the shadow map (usually storing depth from the light point of view).
  8. * @returns The render target texture if present otherwise, null
  9. */
  10. getShadowMap(): Nullable<RenderTargetTexture>;
  11. /**
  12. * Gets the RTT used during rendering (can be a blurred version of the shadow map or the shadow map itself).
  13. * @returns The render target texture if the shadow map is present otherwise, null
  14. */
  15. getShadowMapForRendering(): Nullable<RenderTargetTexture>;
  16. /**
  17. * Determine wheter the shadow generator is ready or not (mainly all effects and related post processes needs to be ready).
  18. * @param subMesh The submesh we want to render in the shadow map
  19. * @param useInstances Defines wether will draw in the map using instances
  20. * @returns true if ready otherwise, false
  21. */
  22. isReady(subMesh: SubMesh, useInstances: boolean): boolean;
  23. /**
  24. * Prepare all the defines in a material relying on a shadow map at the specified light index.
  25. * @param defines Defines of the material we want to update
  26. * @param lightIndex Index of the light in the enabled light list of the material
  27. */
  28. prepareDefines(defines: MaterialDefines, lightIndex: number): void;
  29. /**
  30. * Binds the shadow related information inside of an effect (information like near, far, darkness...
  31. * defined in the generator but impacting the effect).
  32. * It implies the unifroms available on the materials are the standard BJS ones.
  33. * @param lightIndex Index of the light in the enabled light list of the material owning the effect
  34. * @param effect The effect we are binfing the information for
  35. */
  36. bindShadowLight(lightIndex: string, effect: Effect): void;
  37. /**
  38. * Gets the transformation matrix used to project the meshes into the map from the light point of view.
  39. * (eq to shadow prjection matrix * light transform matrix)
  40. * @returns The transform matrix used to create the shadow map
  41. */
  42. getTransformMatrix(): Matrix;
  43. /**
  44. * Recreates the shadow map dependencies like RTT and post processes. This can be used during the switch between
  45. * Cube and 2D textures for instance.
  46. */
  47. recreateShadowMap(): void;
  48. /**
  49. * Forces all the attached effect to compile to enable rendering only once ready vs. lazyly compiling effects.
  50. * @param onCompiled Callback triggered at the and of the effects compilation
  51. * @param options Sets of optional options forcing the compilation with different modes
  52. */
  53. forceCompilation(onCompiled?: (generator: ShadowGenerator) => void, options?: Partial<{ useInstances: boolean }>): void;
  54. /**
  55. * Serializes the shadow generator setup to a json object.
  56. * @returns The serialized JSON object
  57. */
  58. serialize(): any;
  59. /**
  60. * Disposes the Shadow map and related Textures and effects.
  61. */
  62. dispose(): void;
  63. }
  64. /**
  65. * Default implementation of @see IShadowGenerator.
  66. * This is the main object responsible of generating shadows in the framework.
  67. * Documentation: https://doc.babylonjs.com/babylon101/shadows
  68. */
  69. export class ShadowGenerator implements IShadowGenerator {
  70. private static _FILTER_NONE = 0;
  71. private static _FILTER_EXPONENTIALSHADOWMAP = 1;
  72. private static _FILTER_POISSONSAMPLING = 2;
  73. private static _FILTER_BLUREXPONENTIALSHADOWMAP = 3;
  74. private static _FILTER_CLOSEEXPONENTIALSHADOWMAP = 4;
  75. private static _FILTER_BLURCLOSEEXPONENTIALSHADOWMAP = 5;
  76. /**
  77. * Shadow generator mode None: no filtering applied.
  78. */
  79. public static get FILTER_NONE(): number {
  80. return ShadowGenerator._FILTER_NONE;
  81. }
  82. /**
  83. * Shadow generator mode Poisson Sampling: Percentage Closer Filtering.
  84. * (Multiple Tap around evenly distributed around the pixel are used to evaluate the shadow strength)
  85. */
  86. public static get FILTER_POISSONSAMPLING(): number {
  87. return ShadowGenerator._FILTER_POISSONSAMPLING;
  88. }
  89. /**
  90. * Shadow generator mode ESM: Exponential Shadow Mapping.
  91. * (http://developer.download.nvidia.com/presentations/2008/GDC/GDC08_SoftShadowMapping.pdf)
  92. */
  93. public static get FILTER_EXPONENTIALSHADOWMAP(): number {
  94. return ShadowGenerator._FILTER_EXPONENTIALSHADOWMAP;
  95. }
  96. /**
  97. * Shadow generator mode ESM: Blurred Exponential Shadow Mapping.
  98. * (http://developer.download.nvidia.com/presentations/2008/GDC/GDC08_SoftShadowMapping.pdf)
  99. */
  100. public static get FILTER_BLUREXPONENTIALSHADOWMAP(): number {
  101. return ShadowGenerator._FILTER_BLUREXPONENTIALSHADOWMAP;
  102. }
  103. /**
  104. * Shadow generator mode ESM: Exponential Shadow Mapping using the inverse of the exponential preventing
  105. * edge artifacts on steep falloff.
  106. * (http://developer.download.nvidia.com/presentations/2008/GDC/GDC08_SoftShadowMapping.pdf)
  107. */
  108. public static get FILTER_CLOSEEXPONENTIALSHADOWMAP(): number {
  109. return ShadowGenerator._FILTER_CLOSEEXPONENTIALSHADOWMAP;
  110. }
  111. /**
  112. * Shadow generator mode ESM: Blurred Exponential Shadow Mapping using the inverse of the exponential preventing
  113. * edge artifacts on steep falloff.
  114. * (http://developer.download.nvidia.com/presentations/2008/GDC/GDC08_SoftShadowMapping.pdf)
  115. */
  116. public static get FILTER_BLURCLOSEEXPONENTIALSHADOWMAP(): number {
  117. return ShadowGenerator._FILTER_BLURCLOSEEXPONENTIALSHADOWMAP;
  118. }
  119. private _bias = 0.00005;
  120. /**
  121. * Gets the bias: offset applied on the depth preventing acnea.
  122. */
  123. public get bias(): number {
  124. return this._bias;
  125. }
  126. /**
  127. * Sets the bias: offset applied on the depth preventing acnea.
  128. */
  129. public set bias(bias: number) {
  130. this._bias = bias;
  131. }
  132. private _blurBoxOffset = 1;
  133. /**
  134. * Gets the blur box offset: offset applied during the blur pass.
  135. * Only usefull if useKernelBlur = false
  136. */
  137. public get blurBoxOffset(): number {
  138. return this._blurBoxOffset;
  139. }
  140. /**
  141. * Sets the blur box offset: offset applied during the blur pass.
  142. * Only usefull if useKernelBlur = false
  143. */
  144. public set blurBoxOffset(value: number) {
  145. if (this._blurBoxOffset === value) {
  146. return;
  147. }
  148. this._blurBoxOffset = value;
  149. this._disposeBlurPostProcesses();
  150. }
  151. private _blurScale = 2;
  152. /**
  153. * Gets the blur scale: scale of the blurred texture compared to the main shadow map.
  154. * 2 means half of the size.
  155. */
  156. public get blurScale(): number {
  157. return this._blurScale;
  158. }
  159. /**
  160. * Sets the blur scale: scale of the blurred texture compared to the main shadow map.
  161. * 2 means half of the size.
  162. */
  163. public set blurScale(value: number) {
  164. if (this._blurScale === value) {
  165. return;
  166. }
  167. this._blurScale = value;
  168. this._disposeBlurPostProcesses();
  169. }
  170. private _blurKernel = 1;
  171. /**
  172. * Gets the blur kernel: kernel size of the blur pass.
  173. * Only usefull if useKernelBlur = true
  174. */
  175. public get blurKernel(): number {
  176. return this._blurKernel;
  177. }
  178. /**
  179. * Sets the blur kernel: kernel size of the blur pass.
  180. * Only usefull if useKernelBlur = true
  181. */
  182. public set blurKernel(value: number) {
  183. if (this._blurKernel === value) {
  184. return;
  185. }
  186. this._blurKernel = value;
  187. this._disposeBlurPostProcesses();
  188. }
  189. private _useKernelBlur = false;
  190. /**
  191. * Gets whether the blur pass is a kernel blur (if true) or box blur.
  192. * Only usefull in filtered mode (useBlurExponentialShadowMap...)
  193. */
  194. public get useKernelBlur(): boolean {
  195. return this._useKernelBlur;
  196. }
  197. /**
  198. * Sets whether the blur pass is a kernel blur (if true) or box blur.
  199. * Only usefull in filtered mode (useBlurExponentialShadowMap...)
  200. */
  201. public set useKernelBlur(value: boolean) {
  202. if (this._useKernelBlur === value) {
  203. return;
  204. }
  205. this._useKernelBlur = value;
  206. this._disposeBlurPostProcesses();
  207. }
  208. private _depthScale: number;
  209. /**
  210. * Gets the depth scale used in ESM mode.
  211. */
  212. public get depthScale(): number {
  213. return this._depthScale !== undefined ? this._depthScale : this._light.getDepthScale();
  214. }
  215. /**
  216. * Sets the depth scale used in ESM mode.
  217. * This can override the scale stored on the light.
  218. */
  219. public set depthScale(value: number) {
  220. this._depthScale = value;
  221. }
  222. private _filter = ShadowGenerator.FILTER_NONE;
  223. /**
  224. * Gets the current mode of the shadow generator (normal, PCF, ESM...).
  225. * The returned value is a number equal to one of the available mode defined in ShadowMap.FILTER_x like _FILTER_NONE
  226. */
  227. public get filter(): number {
  228. return this._filter;
  229. }
  230. /**
  231. * Sets the current mode of the shadow generator (normal, PCF, ESM...).
  232. * The returned value is a number equal to one of the available mode defined in ShadowMap.FILTER_x like _FILTER_NONE
  233. */
  234. public set filter(value: number) {
  235. // Blurring the cubemap is going to be too expensive. Reverting to unblurred version
  236. if (this._light.needCube()) {
  237. if (value === ShadowGenerator.FILTER_BLUREXPONENTIALSHADOWMAP) {
  238. this.useExponentialShadowMap = true;
  239. return;
  240. }
  241. else if (value === ShadowGenerator.FILTER_BLURCLOSEEXPONENTIALSHADOWMAP) {
  242. this.useCloseExponentialShadowMap = true;
  243. return;
  244. }
  245. }
  246. if (this._filter === value) {
  247. return;
  248. }
  249. this._filter = value;
  250. this._disposeBlurPostProcesses();
  251. this._applyFilterValues();
  252. this._light._markMeshesAsLightDirty();
  253. }
  254. /**
  255. * Gets if the current filter is set to Poisson Sampling aka PCF.
  256. */
  257. public get usePoissonSampling(): boolean {
  258. return this.filter === ShadowGenerator.FILTER_POISSONSAMPLING;
  259. }
  260. /**
  261. * Sets the current filter to Poisson Sampling aka PCF.
  262. */
  263. public set usePoissonSampling(value: boolean) {
  264. if (!value && this.filter !== ShadowGenerator.FILTER_POISSONSAMPLING) {
  265. return;
  266. }
  267. this.filter = (value ? ShadowGenerator.FILTER_POISSONSAMPLING : ShadowGenerator.FILTER_NONE);
  268. }
  269. /**
  270. * Gets if the current filter is set to VSM.
  271. * DEPRECATED. Should use useExponentialShadowMap instead.
  272. */
  273. public get useVarianceShadowMap(): boolean {
  274. Tools.Warn("VSM are now replaced by ESM. Please use useExponentialShadowMap instead.");
  275. return this.useExponentialShadowMap;
  276. }
  277. /**
  278. * Sets the current filter is to VSM.
  279. * DEPRECATED. Should use useExponentialShadowMap instead.
  280. */
  281. public set useVarianceShadowMap(value: boolean) {
  282. Tools.Warn("VSM are now replaced by ESM. Please use useExponentialShadowMap instead.");
  283. this.useExponentialShadowMap = value;
  284. }
  285. /**
  286. * Gets if the current filter is set to blurred VSM.
  287. * DEPRECATED. Should use useBlurExponentialShadowMap instead.
  288. */
  289. public get useBlurVarianceShadowMap(): boolean {
  290. Tools.Warn("VSM are now replaced by ESM. Please use useBlurExponentialShadowMap instead.");
  291. return this.useBlurExponentialShadowMap;
  292. }
  293. /**
  294. * Sets the current filter is to blurred VSM.
  295. * DEPRECATED. Should use useBlurExponentialShadowMap instead.
  296. */
  297. public set useBlurVarianceShadowMap(value: boolean) {
  298. Tools.Warn("VSM are now replaced by ESM. Please use useBlurExponentialShadowMap instead.");
  299. this.useBlurExponentialShadowMap = value;
  300. }
  301. /**
  302. * Gets if the current filter is set to ESM.
  303. */
  304. public get useExponentialShadowMap(): boolean {
  305. return this.filter === ShadowGenerator.FILTER_EXPONENTIALSHADOWMAP;
  306. }
  307. /**
  308. * Sets the current filter is to ESM.
  309. */
  310. public set useExponentialShadowMap(value: boolean) {
  311. if (!value && this.filter !== ShadowGenerator.FILTER_EXPONENTIALSHADOWMAP) {
  312. return;
  313. }
  314. this.filter = (value ? ShadowGenerator.FILTER_EXPONENTIALSHADOWMAP : ShadowGenerator.FILTER_NONE);
  315. }
  316. /**
  317. * Gets if the current filter is set to filtered ESM.
  318. */
  319. public get useBlurExponentialShadowMap(): boolean {
  320. return this.filter === ShadowGenerator.FILTER_BLUREXPONENTIALSHADOWMAP;
  321. }
  322. /**
  323. * Gets if the current filter is set to filtered ESM.
  324. */
  325. public set useBlurExponentialShadowMap(value: boolean) {
  326. if (!value && this.filter !== ShadowGenerator.FILTER_BLUREXPONENTIALSHADOWMAP) {
  327. return;
  328. }
  329. this.filter = (value ? ShadowGenerator.FILTER_BLUREXPONENTIALSHADOWMAP : ShadowGenerator.FILTER_NONE);
  330. }
  331. /**
  332. * Gets if the current filter is set to "close ESM" (using the inverse of the
  333. * exponential to prevent steep falloff artifacts).
  334. */
  335. public get useCloseExponentialShadowMap(): boolean {
  336. return this.filter === ShadowGenerator.FILTER_CLOSEEXPONENTIALSHADOWMAP;
  337. }
  338. /**
  339. * Sets the current filter to "close ESM" (using the inverse of the
  340. * exponential to prevent steep falloff artifacts).
  341. */
  342. public set useCloseExponentialShadowMap(value: boolean) {
  343. if (!value && this.filter !== ShadowGenerator.FILTER_CLOSEEXPONENTIALSHADOWMAP) {
  344. return;
  345. }
  346. this.filter = (value ? ShadowGenerator.FILTER_CLOSEEXPONENTIALSHADOWMAP : ShadowGenerator.FILTER_NONE);
  347. }
  348. /**
  349. * Gets if the current filter is set to filtered "close ESM" (using the inverse of the
  350. * exponential to prevent steep falloff artifacts).
  351. */
  352. public get useBlurCloseExponentialShadowMap(): boolean {
  353. return this.filter === ShadowGenerator.FILTER_BLURCLOSEEXPONENTIALSHADOWMAP;
  354. }
  355. /**
  356. * Sets the current filter to fileterd "close ESM" (using the inverse of the
  357. * exponential to prevent steep falloff artifacts).
  358. */
  359. public set useBlurCloseExponentialShadowMap(value: boolean) {
  360. if (!value && this.filter !== ShadowGenerator.FILTER_BLURCLOSEEXPONENTIALSHADOWMAP) {
  361. return;
  362. }
  363. this.filter = (value ? ShadowGenerator.FILTER_BLURCLOSEEXPONENTIALSHADOWMAP : ShadowGenerator.FILTER_NONE);
  364. }
  365. private _darkness = 0;
  366. /**
  367. * Returns the darkness value (float). This can only decrease the actual darkness of a shadow.
  368. * 0 means strongest and 1 would means no shadow.
  369. * @returns the darkness.
  370. */
  371. public getDarkness(): number {
  372. return this._darkness;
  373. }
  374. /**
  375. * Sets the darkness value (float). This can only decrease the actual darkness of a shadow.
  376. * @param darkness The darkness value 0 means strongest and 1 would means no shadow.
  377. * @returns the shadow generator allowing fluent coding.
  378. */
  379. public setDarkness(darkness: number): ShadowGenerator {
  380. if (darkness >= 1.0)
  381. this._darkness = 1.0;
  382. else if (darkness <= 0.0)
  383. this._darkness = 0.0;
  384. else
  385. this._darkness = darkness;
  386. return this;
  387. }
  388. private _transparencyShadow = false;
  389. /**
  390. * Sets the ability to have transparent shadow (boolean).
  391. * @param transparent True if transparent else False
  392. * @returns the shadow generator allowing fluent coding
  393. */
  394. public setTransparencyShadow(transparent: boolean): ShadowGenerator {
  395. this._transparencyShadow = transparent;
  396. return this;
  397. }
  398. private _shadowMap: Nullable<RenderTargetTexture>;
  399. private _shadowMap2: Nullable<RenderTargetTexture>;
  400. /**
  401. * Gets the main RTT containing the shadow map (usually storing depth from the light point of view).
  402. * @returns The render target texture if present otherwise, null
  403. */
  404. public getShadowMap(): Nullable<RenderTargetTexture> {
  405. return this._shadowMap;
  406. }
  407. /**
  408. * Gets the RTT used during rendering (can be a blurred version of the shadow map or the shadow map itself).
  409. * @returns The render target texture if the shadow map is present otherwise, null
  410. */
  411. public getShadowMapForRendering(): Nullable<RenderTargetTexture> {
  412. if (this._shadowMap2) {
  413. return this._shadowMap2;
  414. }
  415. return this._shadowMap;
  416. }
  417. /**
  418. * Helper function to add a mesh and its descendants to the list of shadow casters.
  419. * @param mesh Mesh to add
  420. * @param includeDescendants boolean indicating if the descendants should be added. Default to true
  421. * @returns the Shadow Generator itself
  422. */
  423. public addShadowCaster(mesh: AbstractMesh, includeDescendants = true): ShadowGenerator {
  424. if (!this._shadowMap) {
  425. return this;
  426. }
  427. if (!this._shadowMap.renderList) {
  428. this._shadowMap.renderList = [];
  429. }
  430. this._shadowMap.renderList.push(mesh);
  431. if (includeDescendants) {
  432. this._shadowMap.renderList.push(...mesh.getChildMeshes());
  433. }
  434. return this;
  435. }
  436. /**
  437. * Helper function to remove a mesh and its descendants from the list of shadow casters
  438. * @param mesh Mesh to remove
  439. * @param includeDescendants boolean indicating if the descendants should be removed. Default to true
  440. * @returns the Shadow Generator itself
  441. */
  442. public removeShadowCaster(mesh: AbstractMesh, includeDescendants = true): ShadowGenerator {
  443. if (!this._shadowMap || !this._shadowMap.renderList) {
  444. return this;
  445. }
  446. var index = this._shadowMap.renderList.indexOf(mesh);
  447. if (index !== -1) {
  448. this._shadowMap.renderList.splice(index, 1);
  449. }
  450. if (includeDescendants) {
  451. for (var child of mesh.getChildren()) {
  452. this.removeShadowCaster(<any>child);
  453. }
  454. }
  455. return this;
  456. }
  457. /**
  458. * Controls the extent to which the shadows fade out at the edge of the frustum
  459. * Used only by directionals and spots
  460. */
  461. public frustumEdgeFalloff = 0;
  462. private _light: IShadowLight;
  463. /**
  464. * Returns the associated light object.
  465. * @returns the light generating the shadow
  466. */
  467. public getLight(): IShadowLight {
  468. return this._light;
  469. }
  470. /**
  471. * If true the shadow map is generated by rendering the back face of the mesh instead of the front face.
  472. * This can help with self-shadowing as the geometry making up the back of objects is slightly offset.
  473. * It might on the other hand introduce peter panning.
  474. */
  475. public forceBackFacesOnly = false;
  476. private _scene: Scene;
  477. private _lightDirection = Vector3.Zero();
  478. private _effect: Effect;
  479. private _viewMatrix = Matrix.Zero();
  480. private _projectionMatrix = Matrix.Zero();
  481. private _transformMatrix = Matrix.Zero();
  482. private _cachedPosition: Vector3;
  483. private _cachedDirection: Vector3;
  484. private _cachedDefines: string;
  485. private _currentRenderID: number;
  486. private _boxBlurPostprocess: Nullable<PostProcess>;
  487. private _kernelBlurXPostprocess: Nullable<PostProcess>;
  488. private _kernelBlurYPostprocess: Nullable<PostProcess>;
  489. private _blurPostProcesses: PostProcess[];
  490. private _mapSize: number;
  491. private _currentFaceIndex = 0;
  492. private _currentFaceIndexCache = 0;
  493. private _textureType: number;
  494. private _defaultTextureMatrix = Matrix.Identity();
  495. /**
  496. * Creates a ShadowGenerator object.
  497. * A ShadowGenerator is the required tool to use the shadows.
  498. * Each light casting shadows needs to use its own ShadowGenerator.
  499. * Documentation : http://doc.babylonjs.com/tutorials/shadows
  500. * @param mapSize The size of the texture what stores the shadows. Example : 1024.
  501. * @param light The light object generating the shadows.
  502. * @param useFullFloatFirst 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.
  503. */
  504. constructor(mapSize: number, light: IShadowLight, useFullFloatFirst?: boolean) {
  505. this._mapSize = mapSize;
  506. this._light = light;
  507. this._scene = light.getScene();
  508. light._shadowGenerator = this;
  509. // Texture type fallback from float to int if not supported.
  510. var caps = this._scene.getEngine().getCaps();
  511. if (!useFullFloatFirst) {
  512. if (caps.textureHalfFloatRender && caps.textureHalfFloatLinearFiltering) {
  513. this._textureType = Engine.TEXTURETYPE_HALF_FLOAT;
  514. }
  515. else if (caps.textureFloatRender && caps.textureFloatLinearFiltering) {
  516. this._textureType = Engine.TEXTURETYPE_FLOAT;
  517. }
  518. else {
  519. this._textureType = Engine.TEXTURETYPE_UNSIGNED_INT;
  520. }
  521. } else {
  522. if (caps.textureFloatRender && caps.textureFloatLinearFiltering) {
  523. this._textureType = Engine.TEXTURETYPE_FLOAT;
  524. }
  525. else if (caps.textureHalfFloatRender && caps.textureHalfFloatLinearFiltering) {
  526. this._textureType = Engine.TEXTURETYPE_HALF_FLOAT;
  527. }
  528. else {
  529. this._textureType = Engine.TEXTURETYPE_UNSIGNED_INT;
  530. }
  531. }
  532. this._initializeGenerator();
  533. }
  534. private _initializeGenerator(): void {
  535. this._light._markMeshesAsLightDirty();
  536. this._initializeShadowMap();
  537. }
  538. private _initializeShadowMap(): void {
  539. // Render target
  540. this._shadowMap = new RenderTargetTexture(this._light.name + "_shadowMap", this._mapSize, this._scene, false, true, this._textureType, this._light.needCube());
  541. this._shadowMap.wrapU = Texture.CLAMP_ADDRESSMODE;
  542. this._shadowMap.wrapV = Texture.CLAMP_ADDRESSMODE;
  543. this._shadowMap.anisotropicFilteringLevel = 1;
  544. this._shadowMap.updateSamplingMode(Texture.BILINEAR_SAMPLINGMODE);
  545. this._shadowMap.renderParticles = false;
  546. this._shadowMap.ignoreCameraViewport = true;
  547. // Record Face Index before render.
  548. this._shadowMap.onBeforeRenderObservable.add((faceIndex: number) => {
  549. this._currentFaceIndex = faceIndex;
  550. });
  551. // Custom render function.
  552. this._shadowMap.customRenderFunction = this._renderForShadowMap.bind(this);
  553. // Blur if required afer render.
  554. this._shadowMap.onAfterUnbindObservable.add(() => {
  555. if (!this.useBlurExponentialShadowMap && !this.useBlurCloseExponentialShadowMap) {
  556. return;
  557. }
  558. let shadowMap = this.getShadowMapForRendering();
  559. if (shadowMap) {
  560. this._scene.postProcessManager.directRender(this._blurPostProcesses, shadowMap.getInternalTexture(), true);
  561. }
  562. });
  563. // Clear according to the chosen filter.
  564. this._shadowMap.onClearObservable.add((engine: Engine) => {
  565. if (this.useExponentialShadowMap || this.useBlurExponentialShadowMap) {
  566. engine.clear(new Color4(0, 0, 0, 0), true, true, true);
  567. }
  568. else {
  569. engine.clear(new Color4(1.0, 1.0, 1.0, 1.0), true, true, true);
  570. }
  571. });
  572. }
  573. private _initializeBlurRTTAndPostProcesses(): void {
  574. var engine = this._scene.getEngine();
  575. var targetSize = this._mapSize / this.blurScale;
  576. if (!this.useKernelBlur || this.blurScale !== 1.0) {
  577. this._shadowMap2 = new RenderTargetTexture(this._light.name + "_shadowMap2", targetSize, this._scene, false, true, this._textureType);
  578. this._shadowMap2.wrapU = Texture.CLAMP_ADDRESSMODE;
  579. this._shadowMap2.wrapV = Texture.CLAMP_ADDRESSMODE;
  580. this._shadowMap2.updateSamplingMode(Texture.BILINEAR_SAMPLINGMODE);
  581. }
  582. if (this.useKernelBlur) {
  583. this._kernelBlurXPostprocess = new BlurPostProcess(this._light.name + "KernelBlurX", new Vector2(1, 0), this.blurKernel, 1.0, null, Texture.BILINEAR_SAMPLINGMODE, engine, false, this._textureType);
  584. this._kernelBlurXPostprocess.width = targetSize;
  585. this._kernelBlurXPostprocess.height = targetSize;
  586. this._kernelBlurXPostprocess.onApplyObservable.add(effect => {
  587. effect.setTexture("textureSampler", this._shadowMap);
  588. });
  589. this._kernelBlurYPostprocess = new BlurPostProcess(this._light.name + "KernelBlurY", new Vector2(0, 1), this.blurKernel, 1.0, null, Texture.BILINEAR_SAMPLINGMODE, engine, false, this._textureType);
  590. this._kernelBlurXPostprocess.autoClear = false;
  591. this._kernelBlurYPostprocess.autoClear = false;
  592. if (this._textureType === Engine.TEXTURETYPE_UNSIGNED_INT) {
  593. (<BlurPostProcess>this._kernelBlurXPostprocess).packedFloat = true;
  594. (<BlurPostProcess>this._kernelBlurYPostprocess).packedFloat = true;
  595. }
  596. this._blurPostProcesses = [this._kernelBlurXPostprocess, this._kernelBlurYPostprocess];
  597. }
  598. else {
  599. 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);
  600. this._boxBlurPostprocess.onApplyObservable.add(effect => {
  601. effect.setFloat2("screenSize", targetSize, targetSize);
  602. effect.setTexture("textureSampler", this._shadowMap);
  603. });
  604. this._boxBlurPostprocess.autoClear = false;
  605. this._blurPostProcesses = [this._boxBlurPostprocess];
  606. }
  607. }
  608. private _renderForShadowMap(opaqueSubMeshes: SmartArray<SubMesh>, alphaTestSubMeshes: SmartArray<SubMesh>, transparentSubMeshes: SmartArray<SubMesh>, depthOnlySubMeshes: SmartArray<SubMesh>): void {
  609. var index: number;
  610. let engine = this._scene.getEngine();
  611. if (depthOnlySubMeshes.length) {
  612. engine.setColorWrite(false);
  613. for (index = 0; index < depthOnlySubMeshes.length; index++) {
  614. this._renderSubMeshForShadowMap(depthOnlySubMeshes.data[index]);
  615. }
  616. engine.setColorWrite(true);
  617. }
  618. for (index = 0; index < opaqueSubMeshes.length; index++) {
  619. this._renderSubMeshForShadowMap(opaqueSubMeshes.data[index]);
  620. }
  621. for (index = 0; index < alphaTestSubMeshes.length; index++) {
  622. this._renderSubMeshForShadowMap(alphaTestSubMeshes.data[index]);
  623. }
  624. if (this._transparencyShadow) {
  625. for (index = 0; index < transparentSubMeshes.length; index++) {
  626. this._renderSubMeshForShadowMap(transparentSubMeshes.data[index]);
  627. }
  628. }
  629. }
  630. private _renderSubMeshForShadowMap(subMesh: SubMesh): void {
  631. var mesh = subMesh.getRenderingMesh();
  632. var scene = this._scene;
  633. var engine = scene.getEngine();
  634. let material = subMesh.getMaterial();
  635. if (!material) {
  636. return;
  637. }
  638. // Culling
  639. engine.setState(material.backFaceCulling);
  640. // Managing instances
  641. var batch = mesh._getInstancesRenderList(subMesh._id);
  642. if (batch.mustReturn) {
  643. return;
  644. }
  645. var hardwareInstancedRendering = (engine.getCaps().instancedArrays) && (batch.visibleInstances[subMesh._id] !== null) && (batch.visibleInstances[subMesh._id] !== undefined);
  646. if (this.isReady(subMesh, hardwareInstancedRendering)) {
  647. engine.enableEffect(this._effect);
  648. mesh._bind(subMesh, this._effect, Material.TriangleFillMode);
  649. this._effect.setFloat2("biasAndScale", this.bias, this.depthScale);
  650. this._effect.setMatrix("viewProjection", this.getTransformMatrix());
  651. this._effect.setVector3("lightPosition", this.getLight().position);
  652. if (scene.activeCamera) {
  653. this._effect.setFloat2("depthValues", this.getLight().getDepthMinZ(scene.activeCamera), this.getLight().getDepthMinZ(scene.activeCamera) + this.getLight().getDepthMaxZ(scene.activeCamera));
  654. }
  655. // Alpha test
  656. if (material && material.needAlphaTesting()) {
  657. var alphaTexture = material.getAlphaTestTexture();
  658. if (alphaTexture) {
  659. this._effect.setTexture("diffuseSampler", alphaTexture);
  660. this._effect.setMatrix("diffuseMatrix", alphaTexture.getTextureMatrix() || this._defaultTextureMatrix);
  661. }
  662. }
  663. // Bones
  664. if (mesh.useBones && mesh.computeBonesUsingShaders) {
  665. this._effect.setMatrices("mBones", (<Skeleton>mesh.skeleton).getTransformMatrices((mesh)));
  666. }
  667. if (this.forceBackFacesOnly) {
  668. engine.setState(true, 0, false, true);
  669. }
  670. // Draw
  671. mesh._processRendering(subMesh, this._effect, Material.TriangleFillMode, batch, hardwareInstancedRendering,
  672. (isInstance, world) => this._effect.setMatrix("world", world));
  673. if (this.forceBackFacesOnly) {
  674. engine.setState(true, 0, false, false);
  675. }
  676. } else {
  677. // Need to reset refresh rate of the shadowMap
  678. if (this._shadowMap) {
  679. this._shadowMap.resetRefreshCounter();
  680. }
  681. }
  682. }
  683. private _applyFilterValues(): void {
  684. if (!this._shadowMap) {
  685. return;
  686. }
  687. if (this.filter === ShadowGenerator.FILTER_NONE) {
  688. this._shadowMap.updateSamplingMode(Texture.NEAREST_SAMPLINGMODE);
  689. } else {
  690. this._shadowMap.updateSamplingMode(Texture.BILINEAR_SAMPLINGMODE);
  691. }
  692. }
  693. /**
  694. * Forces all the attached effect to compile to enable rendering only once ready vs. lazyly compiling effects.
  695. * @param onCompiled Callback triggered at the and of the effects compilation
  696. * @param options Sets of optional options forcing the compilation with different modes
  697. */
  698. public forceCompilation(onCompiled?: (generator: ShadowGenerator) => void, options?: Partial<{ useInstances: boolean }>): void {
  699. let localOptions = {
  700. useInstances: false,
  701. ...options
  702. };
  703. let shadowMap = this.getShadowMap();
  704. if (!shadowMap) {
  705. if (onCompiled) {
  706. onCompiled(this);
  707. }
  708. return;
  709. }
  710. let renderList = shadowMap.renderList;
  711. if (!renderList) {
  712. if (onCompiled) {
  713. onCompiled(this);
  714. }
  715. return;
  716. }
  717. var subMeshes = new Array<SubMesh>();
  718. for (var mesh of renderList) {
  719. subMeshes.push(...mesh.subMeshes);
  720. }
  721. if (subMeshes.length === 0) {
  722. if (onCompiled) {
  723. onCompiled(this);
  724. }
  725. return;
  726. }
  727. var currentIndex = 0;
  728. var checkReady = () => {
  729. if (!this._scene || !this._scene.getEngine()) {
  730. return;
  731. }
  732. while (this.isReady(subMeshes[currentIndex], localOptions.useInstances)) {
  733. currentIndex++;
  734. if (currentIndex >= subMeshes.length) {
  735. if (onCompiled) {
  736. onCompiled(this);
  737. }
  738. return;
  739. }
  740. }
  741. setTimeout(checkReady, 16);
  742. };
  743. checkReady();
  744. }
  745. /**
  746. * Determine wheter the shadow generator is ready or not (mainly all effects and related post processes needs to be ready).
  747. * @param subMesh The submesh we want to render in the shadow map
  748. * @param useInstances Defines wether will draw in the map using instances
  749. * @returns true if ready otherwise, false
  750. */
  751. public isReady(subMesh: SubMesh, useInstances: boolean): boolean {
  752. var defines = [];
  753. if (this._textureType !== Engine.TEXTURETYPE_UNSIGNED_INT) {
  754. defines.push("#define FLOAT");
  755. }
  756. if (this.useExponentialShadowMap || this.useBlurExponentialShadowMap) {
  757. defines.push("#define ESM");
  758. }
  759. var attribs = [VertexBuffer.PositionKind];
  760. var mesh = subMesh.getMesh();
  761. var material = subMesh.getMaterial();
  762. // Alpha test
  763. if (material && material.needAlphaTesting()) {
  764. var alphaTexture = material.getAlphaTestTexture();
  765. if (alphaTexture) {
  766. defines.push("#define ALPHATEST");
  767. if (mesh.isVerticesDataPresent(VertexBuffer.UVKind)) {
  768. attribs.push(VertexBuffer.UVKind);
  769. defines.push("#define UV1");
  770. }
  771. if (mesh.isVerticesDataPresent(VertexBuffer.UV2Kind)) {
  772. if (alphaTexture.coordinatesIndex === 1) {
  773. attribs.push(VertexBuffer.UV2Kind);
  774. defines.push("#define UV2");
  775. }
  776. }
  777. }
  778. }
  779. // Bones
  780. if (mesh.useBones && mesh.computeBonesUsingShaders) {
  781. attribs.push(VertexBuffer.MatricesIndicesKind);
  782. attribs.push(VertexBuffer.MatricesWeightsKind);
  783. if (mesh.numBoneInfluencers > 4) {
  784. attribs.push(VertexBuffer.MatricesIndicesExtraKind);
  785. attribs.push(VertexBuffer.MatricesWeightsExtraKind);
  786. }
  787. defines.push("#define NUM_BONE_INFLUENCERS " + mesh.numBoneInfluencers);
  788. defines.push("#define BonesPerMesh " + ((<Skeleton>mesh.skeleton).bones.length + 1));
  789. } else {
  790. defines.push("#define NUM_BONE_INFLUENCERS 0");
  791. }
  792. // Instances
  793. if (useInstances) {
  794. defines.push("#define INSTANCES");
  795. attribs.push("world0");
  796. attribs.push("world1");
  797. attribs.push("world2");
  798. attribs.push("world3");
  799. }
  800. // Get correct effect
  801. var join = defines.join("\n");
  802. if (this._cachedDefines !== join) {
  803. this._cachedDefines = join;
  804. this._effect = this._scene.getEngine().createEffect("shadowMap",
  805. attribs,
  806. ["world", "mBones", "viewProjection", "diffuseMatrix", "lightPosition", "depthValues", "biasAndScale"],
  807. ["diffuseSampler"], join);
  808. }
  809. if (!this._effect.isReady()) {
  810. return false;
  811. }
  812. if (this.useBlurExponentialShadowMap || this.useBlurCloseExponentialShadowMap) {
  813. if (!this._blurPostProcesses || !this._blurPostProcesses.length) {
  814. this._initializeBlurRTTAndPostProcesses();
  815. }
  816. }
  817. if (this._kernelBlurXPostprocess && !this._kernelBlurXPostprocess.isReady()) {
  818. return false;
  819. }
  820. if (this._kernelBlurYPostprocess && !this._kernelBlurYPostprocess.isReady()) {
  821. return false;
  822. }
  823. if (this._boxBlurPostprocess && !this._boxBlurPostprocess.isReady()) {
  824. return false;
  825. }
  826. return true;
  827. }
  828. /**
  829. * Prepare all the defines in a material relying on a shadow map at the specified light index.
  830. * @param defines Defines of the material we want to update
  831. * @param lightIndex Index of the light in the enabled light list of the material
  832. */
  833. public prepareDefines(defines: any, lightIndex: number): void {
  834. var scene = this._scene;
  835. var light = this._light;
  836. if (!scene.shadowsEnabled || !light.shadowEnabled) {
  837. return;
  838. }
  839. defines["SHADOW" + lightIndex] = true;
  840. if (this.usePoissonSampling) {
  841. defines["SHADOWPCF" + lightIndex] = true;
  842. }
  843. else if (this.useExponentialShadowMap || this.useBlurExponentialShadowMap) {
  844. defines["SHADOWESM" + lightIndex] = true;
  845. }
  846. else if (this.useCloseExponentialShadowMap || this.useBlurCloseExponentialShadowMap) {
  847. defines["SHADOWCLOSEESM" + lightIndex] = true;
  848. }
  849. if (light.needCube()) {
  850. defines["SHADOWCUBE" + lightIndex] = true;
  851. }
  852. }
  853. /**
  854. * Binds the shadow related information inside of an effect (information like near, far, darkness...
  855. * defined in the generator but impacting the effect).
  856. * @param lightIndex Index of the light in the enabled light list of the material owning the effect
  857. * @param effect The effect we are binfing the information for
  858. */
  859. public bindShadowLight(lightIndex: string, effect: Effect): void {
  860. var light = this._light;
  861. var scene = this._scene;
  862. if (!scene.shadowsEnabled || !light.shadowEnabled) {
  863. return;
  864. }
  865. let camera = scene.activeCamera;
  866. if (!camera) {
  867. return;
  868. }
  869. let shadowMap = this.getShadowMap();
  870. if (!shadowMap) {
  871. return;
  872. }
  873. if (!light.needCube()) {
  874. effect.setMatrix("lightMatrix" + lightIndex, this.getTransformMatrix());
  875. }
  876. effect.setTexture("shadowSampler" + lightIndex, this.getShadowMapForRendering());
  877. light._uniformBuffer.updateFloat4("shadowsInfo", this.getDarkness(), this.blurScale / shadowMap.getSize().width, this.depthScale, this.frustumEdgeFalloff, lightIndex);
  878. light._uniformBuffer.updateFloat2("depthValues", this.getLight().getDepthMinZ(camera), this.getLight().getDepthMinZ(camera) + this.getLight().getDepthMaxZ(camera), lightIndex);
  879. }
  880. /**
  881. * Gets the transformation matrix used to project the meshes into the map from the light point of view.
  882. * (eq to shadow prjection matrix * light transform matrix)
  883. * @returns The transform matrix used to create the shadow map
  884. */
  885. public getTransformMatrix(): Matrix {
  886. var scene = this._scene;
  887. if (this._currentRenderID === scene.getRenderId() && this._currentFaceIndexCache === this._currentFaceIndex) {
  888. return this._transformMatrix;
  889. }
  890. this._currentRenderID = scene.getRenderId();
  891. this._currentFaceIndexCache = this._currentFaceIndex;
  892. var lightPosition = this._light.position;
  893. if (this._light.computeTransformedInformation()) {
  894. lightPosition = this._light.transformedPosition;
  895. }
  896. Vector3.NormalizeToRef(this._light.getShadowDirection(this._currentFaceIndex), this._lightDirection);
  897. if (Math.abs(Vector3.Dot(this._lightDirection, Vector3.Up())) === 1.0) {
  898. this._lightDirection.z = 0.0000000000001; // Required to avoid perfectly perpendicular light
  899. }
  900. if (this._light.needProjectionMatrixCompute() || !this._cachedPosition || !this._cachedDirection || !lightPosition.equals(this._cachedPosition) || !this._lightDirection.equals(this._cachedDirection)) {
  901. this._cachedPosition = lightPosition.clone();
  902. this._cachedDirection = this._lightDirection.clone();
  903. Matrix.LookAtLHToRef(lightPosition, lightPosition.add(this._lightDirection), Vector3.Up(), this._viewMatrix);
  904. let shadowMap = this.getShadowMap();
  905. if (shadowMap) {
  906. let renderList = shadowMap.renderList;
  907. if (renderList) {
  908. this._light.setShadowProjectionMatrix(this._projectionMatrix, this._viewMatrix, renderList);
  909. }
  910. }
  911. this._viewMatrix.multiplyToRef(this._projectionMatrix, this._transformMatrix);
  912. }
  913. return this._transformMatrix;
  914. }
  915. /**
  916. * Recreates the shadow map dependencies like RTT and post processes. This can be used during the switch between
  917. * Cube and 2D textures for instance.
  918. */
  919. public recreateShadowMap(): void {
  920. let shadowMap = this._shadowMap;
  921. if (!shadowMap) {
  922. return;
  923. }
  924. // Track render list.
  925. var renderList = shadowMap.renderList;
  926. // Clean up existing data.
  927. this._disposeRTTandPostProcesses();
  928. // Reinitializes.
  929. this._initializeGenerator();
  930. // Reaffect the filter to ensure a correct fallback if necessary.
  931. this.filter = this.filter;
  932. // Reaffect the filter.
  933. this._applyFilterValues();
  934. // Reaffect Render List.
  935. this._shadowMap!.renderList = renderList;
  936. }
  937. private _disposeBlurPostProcesses(): void {
  938. if (this._shadowMap2) {
  939. this._shadowMap2.dispose();
  940. this._shadowMap2 = null;
  941. }
  942. if (this._boxBlurPostprocess) {
  943. this._boxBlurPostprocess.dispose();
  944. this._boxBlurPostprocess = null;
  945. }
  946. if (this._kernelBlurXPostprocess) {
  947. this._kernelBlurXPostprocess.dispose();
  948. this._kernelBlurXPostprocess = null;
  949. }
  950. if (this._kernelBlurYPostprocess) {
  951. this._kernelBlurYPostprocess.dispose();
  952. this._kernelBlurYPostprocess = null;
  953. }
  954. this._blurPostProcesses = [];
  955. }
  956. private _disposeRTTandPostProcesses(): void {
  957. if (this._shadowMap) {
  958. this._shadowMap.dispose();
  959. this._shadowMap = null;
  960. }
  961. this._disposeBlurPostProcesses();
  962. }
  963. /**
  964. * Disposes the ShadowGenerator.
  965. * Returns nothing.
  966. */
  967. public dispose(): void {
  968. this._disposeRTTandPostProcesses();
  969. if (this._light) {
  970. this._light._shadowGenerator = null;
  971. this._light._markMeshesAsLightDirty();
  972. }
  973. }
  974. /**
  975. * Serializes the shadow generator setup to a json object.
  976. * @returns The serialized JSON object
  977. */
  978. public serialize(): any {
  979. var serializationObject: any = {};
  980. var shadowMap = this.getShadowMap();
  981. if (!shadowMap) {
  982. return serializationObject;
  983. }
  984. serializationObject.lightId = this._light.id;
  985. serializationObject.mapSize = shadowMap.getRenderSize();
  986. serializationObject.useExponentialShadowMap = this.useExponentialShadowMap;
  987. serializationObject.useBlurExponentialShadowMap = this.useBlurExponentialShadowMap;
  988. serializationObject.useCloseExponentialShadowMap = this.useBlurExponentialShadowMap;
  989. serializationObject.useBlurCloseExponentialShadowMap = this.useBlurExponentialShadowMap;
  990. serializationObject.usePoissonSampling = this.usePoissonSampling;
  991. serializationObject.forceBackFacesOnly = this.forceBackFacesOnly;
  992. serializationObject.depthScale = this.depthScale;
  993. serializationObject.darkness = this.getDarkness();
  994. serializationObject.blurBoxOffset = this.blurBoxOffset;
  995. serializationObject.blurKernel = this.blurKernel;
  996. serializationObject.blurScale = this.blurScale;
  997. serializationObject.useKernelBlur = this.useKernelBlur;
  998. serializationObject.transparencyShadow = this._transparencyShadow;
  999. serializationObject.renderList = [];
  1000. if (shadowMap.renderList) {
  1001. for (var meshIndex = 0; meshIndex < shadowMap.renderList.length; meshIndex++) {
  1002. var mesh = shadowMap.renderList[meshIndex];
  1003. serializationObject.renderList.push(mesh.id);
  1004. }
  1005. }
  1006. return serializationObject;
  1007. }
  1008. /**
  1009. * Parses a serialized ShadowGenerator and returns a new ShadowGenerator.
  1010. * @param parsedShadowGenerator The JSON object to parse
  1011. * @param scene The scene to create the shadow map for
  1012. * @returns The parsed shadow generator
  1013. */
  1014. public static Parse(parsedShadowGenerator: any, scene: Scene): ShadowGenerator {
  1015. //casting to point light, as light is missing the position attr and typescript complains.
  1016. var light = <PointLight>scene.getLightByID(parsedShadowGenerator.lightId);
  1017. var shadowGenerator = new ShadowGenerator(parsedShadowGenerator.mapSize, light);
  1018. var shadowMap = shadowGenerator.getShadowMap();
  1019. for (var meshIndex = 0; meshIndex < parsedShadowGenerator.renderList.length; meshIndex++) {
  1020. var meshes = scene.getMeshesByID(parsedShadowGenerator.renderList[meshIndex]);
  1021. meshes.forEach(function (mesh) {
  1022. if (!shadowMap) {
  1023. return;
  1024. }
  1025. if (!shadowMap.renderList) {
  1026. shadowMap.renderList = [];
  1027. }
  1028. shadowMap.renderList.push(mesh);
  1029. });
  1030. }
  1031. if (parsedShadowGenerator.usePoissonSampling) {
  1032. shadowGenerator.usePoissonSampling = true;
  1033. }
  1034. else if (parsedShadowGenerator.useExponentialShadowMap) {
  1035. shadowGenerator.useExponentialShadowMap = true;
  1036. }
  1037. else if (parsedShadowGenerator.useBlurExponentialShadowMap) {
  1038. shadowGenerator.useBlurExponentialShadowMap = true;
  1039. }
  1040. else if (parsedShadowGenerator.useCloseExponentialShadowMap) {
  1041. shadowGenerator.useCloseExponentialShadowMap = true;
  1042. }
  1043. else if (parsedShadowGenerator.useBlurCloseExponentialShadowMap) {
  1044. shadowGenerator.useBlurCloseExponentialShadowMap = true;
  1045. }
  1046. // Backward compat
  1047. else if (parsedShadowGenerator.useVarianceShadowMap) {
  1048. shadowGenerator.useExponentialShadowMap = true;
  1049. }
  1050. else if (parsedShadowGenerator.useBlurVarianceShadowMap) {
  1051. shadowGenerator.useBlurExponentialShadowMap = true;
  1052. }
  1053. if (parsedShadowGenerator.depthScale) {
  1054. shadowGenerator.depthScale = parsedShadowGenerator.depthScale;
  1055. }
  1056. if (parsedShadowGenerator.blurScale) {
  1057. shadowGenerator.blurScale = parsedShadowGenerator.blurScale;
  1058. }
  1059. if (parsedShadowGenerator.blurBoxOffset) {
  1060. shadowGenerator.blurBoxOffset = parsedShadowGenerator.blurBoxOffset;
  1061. }
  1062. if (parsedShadowGenerator.useKernelBlur) {
  1063. shadowGenerator.useKernelBlur = parsedShadowGenerator.useKernelBlur;
  1064. }
  1065. if (parsedShadowGenerator.blurKernel) {
  1066. shadowGenerator.blurKernel = parsedShadowGenerator.blurKernel;
  1067. }
  1068. if (parsedShadowGenerator.bias !== undefined) {
  1069. shadowGenerator.bias = parsedShadowGenerator.bias;
  1070. }
  1071. if (parsedShadowGenerator.darkness) {
  1072. shadowGenerator.setDarkness(parsedShadowGenerator.darkness);
  1073. }
  1074. if (parsedShadowGenerator.transparencyShadow) {
  1075. shadowGenerator.setTransparencyShadow(true);
  1076. }
  1077. shadowGenerator.forceBackFacesOnly = parsedShadowGenerator.forceBackFacesOnly;
  1078. return shadowGenerator;
  1079. }
  1080. }
  1081. }