babylon.effectLayer.ts 28 KB

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