babylon.effectLayer.ts 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693
  1. module BABYLON {
  2. /**
  3. * Effect layer options. This helps customizing the behaviour
  4. * of the effect layer.
  5. */
  6. export interface IEffectLayerOptions {
  7. /**
  8. * Multiplication factor apply to the canvas size to compute the render target size
  9. * used to generated the objects (the smaller the faster).
  10. */
  11. mainTextureRatio: number;
  12. /**
  13. * Enforces a fixed size texture to ensure effect stability across devices.
  14. */
  15. mainTextureFixedSize?: number;
  16. /**
  17. * Alpha blending mode used to apply the blur. Default depends of the implementation.
  18. */
  19. alphaBlendingMode: number;
  20. /**
  21. * The camera attached to the layer.
  22. */
  23. camera: Nullable<Camera>;
  24. }
  25. /**
  26. * The effect layer Helps adding post process effect blended with the main pass.
  27. *
  28. * This can be for instance use to generate glow or higlight effects on the scene.
  29. *
  30. * The effect layer class can not be used directly and is intented to inherited from to be
  31. * customized per effects.
  32. */
  33. export abstract class EffectLayer {
  34. private _vertexBuffers: { [key: string]: Nullable<VertexBuffer> } = {};
  35. private _indexBuffer: Nullable<WebGLBuffer>;
  36. private _cachedDefines: string;
  37. private _effectLayerMapGenerationEffect: Effect;
  38. private _effectLayerOptions: IEffectLayerOptions;
  39. private _mergeEffect: Effect;
  40. protected _scene: Scene;
  41. protected _engine: Engine;
  42. protected _maxSize: number = 0;
  43. protected _mainTextureDesiredSize: ISize = { width: 0, height: 0 };
  44. protected _mainTexture: RenderTargetTexture;
  45. protected _shouldRender = true;
  46. protected _postProcesses: PostProcess[] = [];
  47. protected _textures: BaseTexture[] = [];
  48. protected _emissiveTextureAndColor: { texture: Nullable<BaseTexture>, color: Color4 } = { texture: null, color: new Color4() };
  49. /**
  50. * The name of the layer
  51. */
  52. @serialize()
  53. public name: string;
  54. /**
  55. * The clear color of the texture used to generate the glow map.
  56. */
  57. @serializeAsColor4()
  58. public neutralColor: Color4 = new Color4();
  59. /**
  60. * Specifies wether the highlight layer is enabled or not.
  61. */
  62. @serialize()
  63. public isEnabled: boolean = true;
  64. /**
  65. * Gets the camera attached to the layer.
  66. */
  67. @serializeAsCameraReference()
  68. public get camera(): Nullable<Camera> {
  69. return this._effectLayerOptions.camera;
  70. }
  71. /**
  72. * An event triggered when the effect layer has been disposed.
  73. */
  74. public onDisposeObservable = new Observable<EffectLayer>();
  75. /**
  76. * An event triggered when the effect layer is about rendering the main texture with the glowy parts.
  77. */
  78. public onBeforeRenderMainTextureObservable = new Observable<EffectLayer>();
  79. /**
  80. * An event triggered when the generated texture is being merged in the scene.
  81. */
  82. public onBeforeComposeObservable = new Observable<EffectLayer>();
  83. /**
  84. * An event triggered when the generated texture has been merged in the scene.
  85. */
  86. public onAfterComposeObservable = new Observable<EffectLayer>();
  87. /**
  88. * An event triggered when the efffect layer changes its size.
  89. */
  90. public onSizeChangedObservable = new Observable<EffectLayer>();
  91. /**
  92. * Instantiates a new effect Layer and references it in the scene.
  93. * @param name The name of the layer
  94. * @param scene The scene to use the layer in
  95. */
  96. constructor(
  97. /** The Friendly of the effect in the scene */
  98. name: string,
  99. scene: Scene) {
  100. this.name = name;
  101. this._scene = scene || Engine.LastCreatedScene;
  102. this._engine = scene.getEngine();
  103. this._maxSize = this._engine.getCaps().maxTextureSize;
  104. this._scene.effectLayers.push(this);
  105. // Generate Buffers
  106. this._generateIndexBuffer();
  107. this._genrateVertexBuffer();
  108. }
  109. /**
  110. * Get the effect name of the layer.
  111. * @return The effect name
  112. */
  113. public abstract getEffectName(): string;
  114. /**
  115. * Checks for the readiness of the element composing the layer.
  116. * @param subMesh the mesh to check for
  117. * @param useInstances specify wether or not to use instances to render the mesh
  118. * @return true if ready otherwise, false
  119. */
  120. public abstract isReady(subMesh: SubMesh, useInstances: boolean): boolean;
  121. /**
  122. * Returns wether or nood the layer needs stencil enabled during the mesh rendering.
  123. * @returns true if the effect requires stencil during the main canvas render pass.
  124. */
  125. public abstract needStencil(): boolean;
  126. /**
  127. * Create the merge effect. This is the shader use to blit the information back
  128. * to the main canvas at the end of the scene rendering.
  129. * @returns The effect containing the shader used to merge the effect on the main canvas
  130. */
  131. protected abstract _createMergeEffect(): Effect;
  132. /**
  133. * Creates the render target textures and post processes used in the effect layer.
  134. */
  135. protected abstract _createTextureAndPostProcesses(): void;
  136. /**
  137. * Implementation specific of rendering the generating effect on the main canvas.
  138. * @param effect The effect used to render through
  139. */
  140. protected abstract _internalRender(effect: Effect): void;
  141. /**
  142. * Sets the required values for both the emissive texture and and the main color.
  143. */
  144. protected abstract _setEmissiveTextureAndColor(mesh: Mesh, subMesh: SubMesh, material: Material): void;
  145. /**
  146. * Free any resources and references associated to a mesh.
  147. * Internal use
  148. * @param mesh The mesh to free.
  149. */
  150. public abstract _disposeMesh(mesh: Mesh): void;
  151. /**
  152. * Serializes this layer (Glow or Highlight for example)
  153. * @returns a serialized layer object
  154. */
  155. public abstract serialize?(): any;
  156. /**
  157. * Initializes the effect layer with the required options.
  158. * @param options Sets of none mandatory options to use with the layer (see IEffectLayerOptions for more information)
  159. */
  160. protected _init(options: Partial<IEffectLayerOptions>): void {
  161. // Adapt options
  162. this._effectLayerOptions = {
  163. mainTextureRatio: 0.5,
  164. alphaBlendingMode: Engine.ALPHA_COMBINE,
  165. camera: null,
  166. ...options,
  167. };
  168. this._setMainTextureSize();
  169. this._createMainTexture();
  170. this._createTextureAndPostProcesses();
  171. this._mergeEffect = this._createMergeEffect();
  172. }
  173. /**
  174. * Generates the index buffer of the full screen quad blending to the main canvas.
  175. */
  176. private _generateIndexBuffer(): void {
  177. // Indices
  178. var indices = [];
  179. indices.push(0);
  180. indices.push(1);
  181. indices.push(2);
  182. indices.push(0);
  183. indices.push(2);
  184. indices.push(3);
  185. this._indexBuffer = this._engine.createIndexBuffer(indices);
  186. }
  187. /**
  188. * Generates the vertex buffer of the full screen quad blending to the main canvas.
  189. */
  190. private _genrateVertexBuffer(): void {
  191. // VBO
  192. var vertices = [];
  193. vertices.push(1, 1);
  194. vertices.push(-1, 1);
  195. vertices.push(-1, -1);
  196. vertices.push(1, -1);
  197. var vertexBuffer = new VertexBuffer(this._engine, vertices, VertexBuffer.PositionKind, false, false, 2);
  198. this._vertexBuffers[VertexBuffer.PositionKind] = vertexBuffer;
  199. }
  200. /**
  201. * Sets the main texture desired size which is the closest power of two
  202. * of the engine canvas size.
  203. */
  204. private _setMainTextureSize(): void {
  205. if (this._effectLayerOptions.mainTextureFixedSize) {
  206. this._mainTextureDesiredSize.width = this._effectLayerOptions.mainTextureFixedSize;
  207. this._mainTextureDesiredSize.height = this._effectLayerOptions.mainTextureFixedSize;
  208. }
  209. else {
  210. this._mainTextureDesiredSize.width = this._engine.getRenderWidth() * this._effectLayerOptions.mainTextureRatio;
  211. this._mainTextureDesiredSize.height = this._engine.getRenderHeight() * this._effectLayerOptions.mainTextureRatio;
  212. this._mainTextureDesiredSize.width = this._engine.needPOTTextures ? Tools.GetExponentOfTwo(this._mainTextureDesiredSize.width, this._maxSize) : this._mainTextureDesiredSize.width;
  213. this._mainTextureDesiredSize.height = this._engine.needPOTTextures ? Tools.GetExponentOfTwo(this._mainTextureDesiredSize.height, this._maxSize) : this._mainTextureDesiredSize.height;
  214. }
  215. this._mainTextureDesiredSize.width = Math.floor(this._mainTextureDesiredSize.width);
  216. this._mainTextureDesiredSize.height = Math.floor(this._mainTextureDesiredSize.height);
  217. }
  218. /**
  219. * Creates the main texture for the effect layer.
  220. */
  221. protected _createMainTexture(): void {
  222. this._mainTexture = new RenderTargetTexture("HighlightLayerMainRTT",
  223. {
  224. width: this._mainTextureDesiredSize.width,
  225. height: this._mainTextureDesiredSize.height
  226. },
  227. this._scene,
  228. false,
  229. true,
  230. Engine.TEXTURETYPE_UNSIGNED_INT);
  231. this._mainTexture.activeCamera = this._effectLayerOptions.camera;
  232. this._mainTexture.wrapU = Texture.CLAMP_ADDRESSMODE;
  233. this._mainTexture.wrapV = Texture.CLAMP_ADDRESSMODE;
  234. this._mainTexture.anisotropicFilteringLevel = 1;
  235. this._mainTexture.updateSamplingMode(Texture.BILINEAR_SAMPLINGMODE);
  236. this._mainTexture.renderParticles = false;
  237. this._mainTexture.renderList = null;
  238. this._mainTexture.ignoreCameraViewport = true;
  239. // Custom render function
  240. this._mainTexture.customRenderFunction = (opaqueSubMeshes: SmartArray<SubMesh>, alphaTestSubMeshes: SmartArray<SubMesh>, transparentSubMeshes: SmartArray<SubMesh>, depthOnlySubMeshes: SmartArray<SubMesh>): void => {
  241. this.onBeforeRenderMainTextureObservable.notifyObservers(this);
  242. var index: number;
  243. let engine = this._scene.getEngine();
  244. if (depthOnlySubMeshes.length) {
  245. engine.setColorWrite(false);
  246. for (index = 0; index < depthOnlySubMeshes.length; index++) {
  247. this._renderSubMesh(depthOnlySubMeshes.data[index]);
  248. }
  249. engine.setColorWrite(true);
  250. }
  251. for (index = 0; index < opaqueSubMeshes.length; index++) {
  252. this._renderSubMesh(opaqueSubMeshes.data[index]);
  253. }
  254. for (index = 0; index < alphaTestSubMeshes.length; index++) {
  255. this._renderSubMesh(alphaTestSubMeshes.data[index]);
  256. }
  257. for (index = 0; index < transparentSubMeshes.length; index++) {
  258. this._renderSubMesh(transparentSubMeshes.data[index]);
  259. }
  260. };
  261. this._mainTexture.onClearObservable.add((engine: Engine) => {
  262. engine.clear(this.neutralColor, true, true, true);
  263. });
  264. }
  265. /**
  266. * Checks for the readiness of the element composing the layer.
  267. * @param subMesh the mesh to check for
  268. * @param useInstances specify wether or not to use instances to render the mesh
  269. * @param emissiveTexture the associated emissive texture used to generate the glow
  270. * @return true if ready otherwise, false
  271. */
  272. protected _isReady(subMesh: SubMesh, useInstances: boolean, emissiveTexture: Nullable<BaseTexture>): boolean {
  273. let material = subMesh.getMaterial();
  274. if (!material) {
  275. return false;
  276. }
  277. if (!material.isReady(subMesh.getMesh(), useInstances)) {
  278. return false;
  279. }
  280. var defines = [];
  281. var attribs = [VertexBuffer.PositionKind];
  282. var mesh = subMesh.getMesh();
  283. var uv1 = false;
  284. var uv2 = false;
  285. // Alpha test
  286. if (material && material.needAlphaTesting()) {
  287. var alphaTexture = material.getAlphaTestTexture();
  288. if (alphaTexture) {
  289. defines.push("#define ALPHATEST");
  290. if (mesh.isVerticesDataPresent(VertexBuffer.UV2Kind) &&
  291. alphaTexture.coordinatesIndex === 1) {
  292. defines.push("#define DIFFUSEUV2");
  293. uv2 = true;
  294. }
  295. else if (mesh.isVerticesDataPresent(VertexBuffer.UVKind)) {
  296. defines.push("#define DIFFUSEUV1");
  297. uv1 = true;
  298. }
  299. }
  300. }
  301. // Emissive
  302. if (emissiveTexture) {
  303. defines.push("#define EMISSIVE");
  304. if (mesh.isVerticesDataPresent(VertexBuffer.UV2Kind) &&
  305. emissiveTexture.coordinatesIndex === 1) {
  306. defines.push("#define EMISSIVEUV2");
  307. uv2 = true;
  308. }
  309. else if (mesh.isVerticesDataPresent(VertexBuffer.UVKind)) {
  310. defines.push("#define EMISSIVEUV1");
  311. uv1 = true;
  312. }
  313. }
  314. if (uv1) {
  315. attribs.push(VertexBuffer.UVKind);
  316. defines.push("#define UV1");
  317. }
  318. if (uv2) {
  319. attribs.push(VertexBuffer.UV2Kind);
  320. defines.push("#define UV2");
  321. }
  322. // Bones
  323. if (mesh.useBones && mesh.computeBonesUsingShaders) {
  324. attribs.push(VertexBuffer.MatricesIndicesKind);
  325. attribs.push(VertexBuffer.MatricesWeightsKind);
  326. if (mesh.numBoneInfluencers > 4) {
  327. attribs.push(VertexBuffer.MatricesIndicesExtraKind);
  328. attribs.push(VertexBuffer.MatricesWeightsExtraKind);
  329. }
  330. defines.push("#define NUM_BONE_INFLUENCERS " + mesh.numBoneInfluencers);
  331. defines.push("#define BonesPerMesh " + (mesh.skeleton ? (mesh.skeleton.bones.length + 1) : 0));
  332. } else {
  333. defines.push("#define NUM_BONE_INFLUENCERS 0");
  334. }
  335. // Morph targets
  336. var manager = (<Mesh>mesh).morphTargetManager;
  337. let morphInfluencers = 0;
  338. if (manager) {
  339. if (manager.numInfluencers > 0) {
  340. defines.push("#define MORPHTARGETS");
  341. morphInfluencers = manager.numInfluencers;
  342. defines.push("#define NUM_MORPH_INFLUENCERS " + morphInfluencers);
  343. MaterialHelper.PrepareAttributesForMorphTargets(attribs, mesh, {"NUM_MORPH_INFLUENCERS": morphInfluencers });
  344. }
  345. }
  346. // Instances
  347. if (useInstances) {
  348. defines.push("#define INSTANCES");
  349. attribs.push("world0");
  350. attribs.push("world1");
  351. attribs.push("world2");
  352. attribs.push("world3");
  353. }
  354. // Get correct effect
  355. var join = defines.join("\n");
  356. if (this._cachedDefines !== join) {
  357. this._cachedDefines = join;
  358. this._effectLayerMapGenerationEffect = this._scene.getEngine().createEffect("glowMapGeneration",
  359. attribs,
  360. ["world", "mBones", "viewProjection", "diffuseMatrix", "color", "emissiveMatrix", "morphTargetInfluences"],
  361. ["diffuseSampler", "emissiveSampler"], join,
  362. undefined, undefined, undefined, { maxSimultaneousMorphTargets: morphInfluencers });
  363. }
  364. return this._effectLayerMapGenerationEffect.isReady();
  365. }
  366. /**
  367. * Renders the glowing part of the scene by blending the blurred glowing meshes on top of the rendered scene.
  368. */
  369. public render(): void {
  370. var currentEffect = this._mergeEffect;
  371. // Check
  372. if (!currentEffect.isReady())
  373. return;
  374. for (var i = 0; i < this._postProcesses.length; i++) {
  375. if (!this._postProcesses[i].isReady()) {
  376. return;
  377. }
  378. }
  379. var engine = this._scene.getEngine();
  380. this.onBeforeComposeObservable.notifyObservers(this);
  381. // Render
  382. engine.enableEffect(currentEffect);
  383. engine.setState(false);
  384. // VBOs
  385. engine.bindBuffers(this._vertexBuffers, this._indexBuffer, currentEffect);
  386. // Cache
  387. var previousAlphaMode = engine.getAlphaMode();
  388. // Go Blend.
  389. engine.setAlphaMode(this._effectLayerOptions.alphaBlendingMode);
  390. // Blends the map on the main canvas.
  391. this._internalRender(currentEffect);
  392. // Restore Alpha
  393. engine.setAlphaMode(previousAlphaMode);
  394. this.onAfterComposeObservable.notifyObservers(this);
  395. // Handle size changes.
  396. var size = this._mainTexture.getSize();
  397. this._setMainTextureSize();
  398. if (size.width !== this._mainTextureDesiredSize.width || size.height !== this._mainTextureDesiredSize.height) {
  399. // Recreate RTT and post processes on size change.
  400. this.onSizeChangedObservable.notifyObservers(this);
  401. this._disposeTextureAndPostProcesses();
  402. this._createMainTexture();
  403. this._createTextureAndPostProcesses();
  404. }
  405. }
  406. /**
  407. * Determine if a given mesh will be used in the current effect.
  408. * @param mesh mesh to test
  409. * @returns true if the mesh will be used
  410. */
  411. public hasMesh(mesh: AbstractMesh): boolean {
  412. return true;
  413. }
  414. /**
  415. * Returns true if the layer contains information to display, otherwise false.
  416. * @returns true if the glow layer should be rendered
  417. */
  418. public shouldRender(): boolean {
  419. return this.isEnabled && this._shouldRender;
  420. }
  421. /**
  422. * Returns true if the mesh should render, otherwise false.
  423. * @param mesh The mesh to render
  424. * @returns true if it should render otherwise false
  425. */
  426. protected _shouldRenderMesh(mesh: Mesh): boolean {
  427. return true;
  428. }
  429. /**
  430. * Returns true if the mesh should render, otherwise false.
  431. * @param mesh The mesh to render
  432. * @returns true if it should render otherwise false
  433. */
  434. protected _shouldRenderEmissiveTextureForMesh(mesh: Mesh): boolean {
  435. return true;
  436. }
  437. /**
  438. * Renders the submesh passed in parameter to the generation map.
  439. */
  440. protected _renderSubMesh(subMesh: SubMesh): void {
  441. if (!this.shouldRender()) {
  442. return;
  443. }
  444. var material = subMesh.getMaterial();
  445. var mesh = subMesh.getRenderingMesh();
  446. var scene = this._scene;
  447. var engine = scene.getEngine();
  448. if (!material) {
  449. return;
  450. }
  451. // Do not block in blend mode.
  452. if (material.needAlphaBlendingForMesh(mesh)) {
  453. return;
  454. }
  455. // Culling
  456. engine.setState(material.backFaceCulling);
  457. // Managing instances
  458. var batch = mesh._getInstancesRenderList(subMesh._id);
  459. if (batch.mustReturn) {
  460. return;
  461. }
  462. // Early Exit per mesh
  463. if (!this._shouldRenderMesh(mesh)) {
  464. return;
  465. }
  466. var hardwareInstancedRendering = (engine.getCaps().instancedArrays) && (batch.visibleInstances[subMesh._id] !== null) && (batch.visibleInstances[subMesh._id] !== undefined);
  467. this._setEmissiveTextureAndColor(mesh, subMesh, material);
  468. if (this._isReady(subMesh, hardwareInstancedRendering, this._emissiveTextureAndColor.texture)) {
  469. engine.enableEffect(this._effectLayerMapGenerationEffect);
  470. mesh._bind(subMesh, this._effectLayerMapGenerationEffect, Material.TriangleFillMode);
  471. this._effectLayerMapGenerationEffect.setMatrix("viewProjection", scene.getTransformMatrix());
  472. this._effectLayerMapGenerationEffect.setFloat4("color",
  473. this._emissiveTextureAndColor.color.r,
  474. this._emissiveTextureAndColor.color.g,
  475. this._emissiveTextureAndColor.color.b,
  476. this._emissiveTextureAndColor.color.a);
  477. // Alpha test
  478. if (material && material.needAlphaTesting()) {
  479. var alphaTexture = material.getAlphaTestTexture();
  480. if (alphaTexture) {
  481. this._effectLayerMapGenerationEffect.setTexture("diffuseSampler", alphaTexture);
  482. let textureMatrix = alphaTexture.getTextureMatrix();
  483. if (textureMatrix) {
  484. this._effectLayerMapGenerationEffect.setMatrix("diffuseMatrix", textureMatrix);
  485. }
  486. }
  487. }
  488. // Glow emissive only
  489. if (this._emissiveTextureAndColor.texture) {
  490. this._effectLayerMapGenerationEffect.setTexture("emissiveSampler", this._emissiveTextureAndColor.texture);
  491. this._effectLayerMapGenerationEffect.setMatrix("emissiveMatrix", this._emissiveTextureAndColor.texture.getTextureMatrix());
  492. }
  493. // Bones
  494. if (mesh.useBones && mesh.computeBonesUsingShaders && mesh.skeleton) {
  495. this._effectLayerMapGenerationEffect.setMatrices("mBones", mesh.skeleton.getTransformMatrices(mesh));
  496. }
  497. // Morph targets
  498. MaterialHelper.BindMorphTargetParameters(mesh, this._effectLayerMapGenerationEffect);
  499. // Draw
  500. mesh._processRendering(subMesh, this._effectLayerMapGenerationEffect, Material.TriangleFillMode, batch, hardwareInstancedRendering,
  501. (isInstance, world) => this._effectLayerMapGenerationEffect.setMatrix("world", world));
  502. } else {
  503. // Need to reset refresh rate of the shadowMap
  504. this._mainTexture.resetRefreshCounter();
  505. }
  506. }
  507. /**
  508. * Rebuild the required buffers.
  509. * @ignore Internal use only.
  510. */
  511. public _rebuild(): void {
  512. let vb = this._vertexBuffers[VertexBuffer.PositionKind];
  513. if (vb) {
  514. vb._rebuild();
  515. }
  516. this._generateIndexBuffer();
  517. }
  518. /**
  519. * Dispose only the render target textures and post process.
  520. */
  521. private _disposeTextureAndPostProcesses(): void {
  522. this._mainTexture.dispose();
  523. for (var i = 0; i < this._postProcesses.length; i++) {
  524. if (this._postProcesses[i]) {
  525. this._postProcesses[i].dispose();
  526. }
  527. }
  528. this._postProcesses = [];
  529. for (var i = 0; i < this._textures.length; i++) {
  530. if (this._textures[i]) {
  531. this._textures[i].dispose();
  532. }
  533. }
  534. this._textures = [];
  535. }
  536. /**
  537. * Dispose the highlight layer and free resources.
  538. */
  539. public dispose(): void {
  540. var vertexBuffer = this._vertexBuffers[VertexBuffer.PositionKind];
  541. if (vertexBuffer) {
  542. vertexBuffer.dispose();
  543. this._vertexBuffers[VertexBuffer.PositionKind] = null;
  544. }
  545. if (this._indexBuffer) {
  546. this._scene.getEngine()._releaseBuffer(this._indexBuffer);
  547. this._indexBuffer = null;
  548. }
  549. // Clean textures and post processes
  550. this._disposeTextureAndPostProcesses();
  551. // Remove from scene
  552. var index = this._scene.effectLayers.indexOf(this, 0);
  553. if (index > -1) {
  554. this._scene.effectLayers.splice(index, 1);
  555. }
  556. // Callback
  557. this.onDisposeObservable.notifyObservers(this);
  558. this.onDisposeObservable.clear();
  559. this.onBeforeRenderMainTextureObservable.clear();
  560. this.onBeforeComposeObservable.clear();
  561. this.onAfterComposeObservable.clear();
  562. this.onSizeChangedObservable.clear();
  563. }
  564. /**
  565. * Gets the class name of the effect layer
  566. * @returns the string with the class name of the effect layer
  567. */
  568. public getClassName(): string {
  569. return "EffectLayer";
  570. }
  571. /**
  572. * Creates an effect layer from parsed effect layer data
  573. * @param parsedEffectLayer defines effect layer data
  574. * @param scene defines the current scene
  575. * @param rootUrl defines the root URL containing the effect layer information
  576. * @returns a parsed effect Layer
  577. */
  578. public static Parse(parsedEffectLayer: any, scene: Scene, rootUrl: string): EffectLayer {
  579. var effectLayerType = Tools.Instantiate(parsedEffectLayer.customType);
  580. return effectLayerType.Parse(parsedEffectLayer, scene, rootUrl);
  581. }
  582. }
  583. }