spriteManager.ts 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582
  1. import { IDisposable, Scene } from "../scene";
  2. import { Nullable } from "../types";
  3. import { Observable, Observer } from "../Misc/observable";
  4. import { Buffer } from "../Meshes/buffer";
  5. import { VertexBuffer } from "../Meshes/buffer";
  6. import { Vector3, TmpVectors } from "../Maths/math.vector";
  7. import { Sprite } from "./sprite";
  8. import { SpriteSceneComponent } from "./spriteSceneComponent";
  9. import { PickingInfo } from "../Collisions/pickingInfo";
  10. import { Camera } from "../Cameras/camera";
  11. import { Texture } from "../Materials/Textures/texture";
  12. import { Effect } from "../Materials/effect";
  13. import { Material } from "../Materials/material";
  14. import { SceneComponentConstants } from "../sceneComponent";
  15. import { Constants } from "../Engines/constants";
  16. import { Logger } from "../Misc/logger";
  17. import "../Shaders/sprites.fragment";
  18. import "../Shaders/sprites.vertex";
  19. import { DataBuffer } from '../Meshes/dataBuffer';
  20. declare type Ray = import("../Culling/ray").Ray;
  21. /**
  22. * Defines the minimum interface to fullfil in order to be a sprite manager.
  23. */
  24. export interface ISpriteManager extends IDisposable {
  25. /**
  26. * Restricts the camera to viewing objects with the same layerMask.
  27. * A camera with a layerMask of 1 will render spriteManager.layerMask & camera.layerMask!== 0
  28. */
  29. layerMask: number;
  30. /**
  31. * Gets or sets a boolean indicating if the mesh can be picked (by scene.pick for instance or through actions). Default is true
  32. */
  33. isPickable: boolean;
  34. /**
  35. * Specifies the rendering group id for this mesh (0 by default)
  36. * @see http://doc.babylonjs.com/resources/transparency_and_how_meshes_are_rendered#rendering-groups
  37. */
  38. renderingGroupId: number;
  39. /**
  40. * Defines the list of sprites managed by the manager.
  41. */
  42. sprites: Array<Sprite>;
  43. /**
  44. * Tests the intersection of a sprite with a specific ray.
  45. * @param ray The ray we are sending to test the collision
  46. * @param camera The camera space we are sending rays in
  47. * @param predicate A predicate allowing excluding sprites from the list of object to test
  48. * @param fastCheck Is the hit test done in a OOBB or AOBB fashion the faster, the less precise
  49. * @returns picking info or null.
  50. */
  51. intersects(ray: Ray, camera: Camera, predicate?: (sprite: Sprite) => boolean, fastCheck?: boolean): Nullable<PickingInfo>;
  52. /**
  53. * Intersects the sprites with a ray
  54. * @param ray defines the ray to intersect with
  55. * @param camera defines the current active camera
  56. * @param predicate defines a predicate used to select candidate sprites
  57. * @returns null if no hit or a PickingInfo array
  58. */
  59. multiIntersects(ray: Ray, camera: Camera, predicate?: (sprite: Sprite) => boolean): Nullable<PickingInfo[]>;
  60. /**
  61. * Renders the list of sprites on screen.
  62. */
  63. render(): void;
  64. }
  65. /**
  66. * Class used to manage multiple sprites on the same spritesheet
  67. * @see http://doc.babylonjs.com/babylon101/sprites
  68. */
  69. export class SpriteManager implements ISpriteManager {
  70. /** Gets the list of sprites */
  71. public sprites = new Array<Sprite>();
  72. /** Gets or sets the rendering group id (0 by default) */
  73. public renderingGroupId = 0;
  74. /** Gets or sets camera layer mask */
  75. public layerMask: number = 0x0FFFFFFF;
  76. /** Gets or sets a boolean indicating if the manager must consider scene fog when rendering */
  77. public fogEnabled = true;
  78. /** Gets or sets a boolean indicating if the sprites are pickable */
  79. public isPickable = false;
  80. /** Defines the default width of a cell in the spritesheet */
  81. public cellWidth: number;
  82. /** Defines the default height of a cell in the spritesheet */
  83. public cellHeight: number;
  84. /** Associative array from JSON sprite data file */
  85. private _cellData: any;
  86. /** Array of sprite names from JSON sprite data file */
  87. private _spriteMap: Array<string>;
  88. /** True when packed cell data from JSON file is ready*/
  89. private _packedAndReady: boolean = false;
  90. /**
  91. * An event triggered when the manager is disposed.
  92. */
  93. public onDisposeObservable = new Observable<SpriteManager>();
  94. private _onDisposeObserver: Nullable<Observer<SpriteManager>>;
  95. /**
  96. * Callback called when the manager is disposed
  97. */
  98. public set onDispose(callback: () => void) {
  99. if (this._onDisposeObserver) {
  100. this.onDisposeObservable.remove(this._onDisposeObserver);
  101. }
  102. this._onDisposeObserver = this.onDisposeObservable.add(callback);
  103. }
  104. private _capacity: number;
  105. private _fromPacked: boolean;
  106. private _spriteTexture: Texture;
  107. private _epsilon: number;
  108. private _scene: Scene;
  109. private _vertexData: Float32Array;
  110. private _buffer: Buffer;
  111. private _vertexBuffers: { [key: string]: VertexBuffer } = {};
  112. private _indexBuffer: DataBuffer;
  113. private _effectBase: Effect;
  114. private _effectFog: Effect;
  115. /**
  116. * Gets or sets the spritesheet texture
  117. */
  118. public get texture(): Texture {
  119. return this._spriteTexture;
  120. }
  121. public set texture(value: Texture) {
  122. this._spriteTexture = value;
  123. }
  124. /**
  125. * Creates a new sprite manager
  126. * @param name defines the manager's name
  127. * @param imgUrl defines the sprite sheet url
  128. * @param capacity defines the maximum allowed number of sprites
  129. * @param cellSize defines the size of a sprite cell
  130. * @param scene defines the hosting scene
  131. * @param epsilon defines the epsilon value to align texture (0.01 by default)
  132. * @param samplingMode defines the smapling mode to use with spritesheet
  133. * @param fromPacked set to false; do not alter
  134. * @param spriteJSON null otherwise a JSON object defining sprite sheet data; do not alter
  135. */
  136. constructor(
  137. /** defines the manager's name */
  138. public name: string,
  139. imgUrl: string, capacity: number, cellSize: any, scene: Scene, epsilon: number = 0.01, samplingMode: number = Texture.TRILINEAR_SAMPLINGMODE, fromPacked: boolean = false, spriteJSON: string | null = null) {
  140. if (!scene._getComponent(SceneComponentConstants.NAME_SPRITE)) {
  141. scene._addComponent(new SpriteSceneComponent(scene));
  142. }
  143. this._capacity = capacity;
  144. this._fromPacked = fromPacked;
  145. this._spriteTexture = new Texture(imgUrl, scene, true, false, samplingMode);
  146. this._spriteTexture.wrapU = Texture.CLAMP_ADDRESSMODE;
  147. this._spriteTexture.wrapV = Texture.CLAMP_ADDRESSMODE;
  148. if (cellSize.width && cellSize.height) {
  149. this.cellWidth = cellSize.width;
  150. this.cellHeight = cellSize.height;
  151. } else if (cellSize !== undefined) {
  152. this.cellWidth = cellSize;
  153. this.cellHeight = cellSize;
  154. } else {
  155. return;
  156. }
  157. this._epsilon = epsilon;
  158. this._scene = scene;
  159. this._scene.spriteManagers.push(this);
  160. var indices = [];
  161. var index = 0;
  162. for (var count = 0; count < capacity; count++) {
  163. indices.push(index);
  164. indices.push(index + 1);
  165. indices.push(index + 2);
  166. indices.push(index);
  167. indices.push(index + 2);
  168. indices.push(index + 3);
  169. index += 4;
  170. }
  171. this._indexBuffer = scene.getEngine().createIndexBuffer(indices);
  172. // VBO
  173. // 18 floats per sprite (x, y, z, angle, sizeX, sizeY, offsetX, offsetY, invertU, invertV, cellLeft, cellTop, cellWidth, cellHeight, color r, color g, color b, color a)
  174. this._vertexData = new Float32Array(capacity * 18 * 4);
  175. this._buffer = new Buffer(scene.getEngine(), this._vertexData, true, 18);
  176. var positions = this._buffer.createVertexBuffer(VertexBuffer.PositionKind, 0, 4);
  177. var options = this._buffer.createVertexBuffer("options", 4, 4);
  178. var inverts = this._buffer.createVertexBuffer("inverts", 8, 2);
  179. var cellInfo = this._buffer.createVertexBuffer("cellInfo", 10, 4);
  180. var colors = this._buffer.createVertexBuffer(VertexBuffer.ColorKind, 14, 4);
  181. this._vertexBuffers[VertexBuffer.PositionKind] = positions;
  182. this._vertexBuffers["options"] = options;
  183. this._vertexBuffers["inverts"] = inverts;
  184. this._vertexBuffers["cellInfo"] = cellInfo;
  185. this._vertexBuffers[VertexBuffer.ColorKind] = colors;
  186. // Effects
  187. this._effectBase = this._scene.getEngine().createEffect("sprites",
  188. [VertexBuffer.PositionKind, "options", "inverts", "cellInfo", VertexBuffer.ColorKind],
  189. ["view", "projection", "textureInfos", "alphaTest"],
  190. ["diffuseSampler"], "");
  191. this._effectFog = this._scene.getEngine().createEffect("sprites",
  192. [VertexBuffer.PositionKind, "options", "inverts", "cellInfo", VertexBuffer.ColorKind],
  193. ["view", "projection", "textureInfos", "alphaTest", "vFogInfos", "vFogColor"],
  194. ["diffuseSampler"], "#define FOG");
  195. if (this._fromPacked) {
  196. this._makePacked(imgUrl, spriteJSON);
  197. }
  198. }
  199. private _makePacked(imgUrl: string, spriteJSON: string | null) {
  200. if (spriteJSON !== null) {
  201. try {
  202. let celldata = JSON.parse(spriteJSON);
  203. let spritemap = (<string[]>(<any>Reflect).ownKeys(celldata.frames));
  204. this._spriteMap = spritemap;
  205. this._packedAndReady = true;
  206. this._cellData = celldata.frames;
  207. }
  208. catch (e) {
  209. this._fromPacked = false;
  210. this._packedAndReady = false;
  211. throw new Error("Invalid JSON from string. Spritesheet managed with constant cell size.");
  212. }
  213. }
  214. else {
  215. let re = /\./g;
  216. let li: number;
  217. do {
  218. li = re.lastIndex;
  219. re.test(imgUrl);
  220. } while (re.lastIndex > 0);
  221. let jsonUrl = imgUrl.substring(0, li - 1) + ".json";
  222. let xmlhttp = new XMLHttpRequest();
  223. xmlhttp.open("GET", jsonUrl, true);
  224. xmlhttp.onerror = () => {
  225. Logger.Error("Unable to Load Sprite JSON Data. Spritesheet managed with constant cell size.");
  226. this._fromPacked = false;
  227. this._packedAndReady = false;
  228. };
  229. xmlhttp.onload = () => {
  230. try {
  231. let celldata = JSON.parse(xmlhttp.response);
  232. let spritemap = (<string[]>(<any>Reflect).ownKeys(celldata.frames));
  233. this._spriteMap = spritemap;
  234. this._packedAndReady = true;
  235. this._cellData = celldata.frames;
  236. }
  237. catch (e) {
  238. this._fromPacked = false;
  239. this._packedAndReady = false;
  240. throw new Error("Invalid JSON from file. Spritesheet managed with constant cell size.");
  241. }
  242. };
  243. xmlhttp.send();
  244. }
  245. }
  246. private _appendSpriteVertex(index: number, sprite: Sprite, offsetX: number, offsetY: number, baseSize: any): void {
  247. var arrayOffset = index * 18;
  248. if (offsetX === 0) {
  249. offsetX = this._epsilon;
  250. }
  251. else if (offsetX === 1) {
  252. offsetX = 1 - this._epsilon;
  253. }
  254. if (offsetY === 0) {
  255. offsetY = this._epsilon;
  256. }
  257. else if (offsetY === 1) {
  258. offsetY = 1 - this._epsilon;
  259. }
  260. // Positions
  261. this._vertexData[arrayOffset] = sprite.position.x;
  262. this._vertexData[arrayOffset + 1] = sprite.position.y;
  263. this._vertexData[arrayOffset + 2] = sprite.position.z;
  264. this._vertexData[arrayOffset + 3] = sprite.angle;
  265. // Options
  266. this._vertexData[arrayOffset + 4] = sprite.width;
  267. this._vertexData[arrayOffset + 5] = sprite.height;
  268. this._vertexData[arrayOffset + 6] = offsetX;
  269. this._vertexData[arrayOffset + 7] = offsetY;
  270. // Inverts
  271. this._vertexData[arrayOffset + 8] = sprite.invertU ? 1 : 0;
  272. this._vertexData[arrayOffset + 9] = sprite.invertV ? 1 : 0;
  273. // CellIfo
  274. if (this._packedAndReady) {
  275. if (!sprite.cellRef) {
  276. sprite.cellIndex = 0;
  277. }
  278. let num = sprite.cellIndex;
  279. if (typeof (num) === "number" && isFinite(num) && Math.floor(num) === num) {
  280. sprite.cellRef = this._spriteMap[sprite.cellIndex];
  281. }
  282. this._vertexData[arrayOffset + 10] = this._cellData[sprite.cellRef].frame.x / baseSize.width;
  283. this._vertexData[arrayOffset + 11] = this._cellData[sprite.cellRef].frame.y / baseSize.height;
  284. this._vertexData[arrayOffset + 12] = this._cellData[sprite.cellRef].frame.w / baseSize.width;
  285. this._vertexData[arrayOffset + 13] = this._cellData[sprite.cellRef].frame.h / baseSize.height;
  286. }
  287. else {
  288. if (!sprite.cellIndex) {
  289. sprite.cellIndex = 0;
  290. }
  291. var rowSize = baseSize.width / this.cellWidth;
  292. var offset = (sprite.cellIndex / rowSize) >> 0;
  293. this._vertexData[arrayOffset + 10] = (sprite.cellIndex - offset * rowSize) * this.cellWidth / baseSize.width;
  294. this._vertexData[arrayOffset + 11] = offset * this.cellHeight / baseSize.height;
  295. this._vertexData[arrayOffset + 12] = this.cellWidth / baseSize.width;
  296. this._vertexData[arrayOffset + 13] = this.cellHeight / baseSize.height;
  297. }
  298. // Color
  299. this._vertexData[arrayOffset + 14] = sprite.color.r;
  300. this._vertexData[arrayOffset + 15] = sprite.color.g;
  301. this._vertexData[arrayOffset + 16] = sprite.color.b;
  302. this._vertexData[arrayOffset + 17] = sprite.color.a;
  303. }
  304. /**
  305. * Intersects the sprites with a ray
  306. * @param ray defines the ray to intersect with
  307. * @param camera defines the current active camera
  308. * @param predicate defines a predicate used to select candidate sprites
  309. * @param fastCheck defines if a fast check only must be done (the first potential sprite is will be used and not the closer)
  310. * @returns null if no hit or a PickingInfo
  311. */
  312. public intersects(ray: Ray, camera: Camera, predicate?: (sprite: Sprite) => boolean, fastCheck?: boolean): Nullable<PickingInfo> {
  313. var count = Math.min(this._capacity, this.sprites.length);
  314. var min = TmpVectors.Vector3[0].copyFromFloats(0, 0, 0);
  315. var max = TmpVectors.Vector3[1].copyFromFloats(0, 0, 0);
  316. var distance = Number.MAX_VALUE;
  317. var currentSprite: Nullable<Sprite> = null;
  318. var pickedPoint = TmpVectors.Vector3[2].copyFromFloats(0, 0, 0);
  319. var cameraSpacePosition = TmpVectors.Vector3[3].copyFromFloats(0, 0, 0);;
  320. var cameraView = camera.getViewMatrix();
  321. for (var index = 0; index < count; index++) {
  322. var sprite = this.sprites[index];
  323. if (!sprite) {
  324. continue;
  325. }
  326. if (predicate) {
  327. if (!predicate(sprite)) {
  328. continue;
  329. }
  330. } else if (!sprite.isPickable) {
  331. continue;
  332. }
  333. Vector3.TransformCoordinatesToRef(sprite.position, cameraView, cameraSpacePosition);
  334. min.copyFromFloats(cameraSpacePosition.x - sprite.width / 2, cameraSpacePosition.y - sprite.height / 2, cameraSpacePosition.z);
  335. max.copyFromFloats(cameraSpacePosition.x + sprite.width / 2, cameraSpacePosition.y + sprite.height / 2, cameraSpacePosition.z);
  336. if (ray.intersectsBoxMinMax(min, max)) {
  337. var currentDistance = Vector3.Distance(cameraSpacePosition, ray.origin);
  338. if (distance > currentDistance) {
  339. distance = currentDistance;
  340. currentSprite = sprite;
  341. if (fastCheck) {
  342. break;
  343. }
  344. }
  345. }
  346. }
  347. if (currentSprite) {
  348. var result = new PickingInfo();
  349. cameraView.invertToRef(TmpVectors.Matrix[0]);
  350. result.hit = true;
  351. result.pickedSprite = currentSprite;
  352. result.distance = distance;
  353. // Get picked point
  354. let direction = TmpVectors.Vector3[4];
  355. direction.copyFrom(ray.direction);
  356. direction.normalize();
  357. direction.scaleInPlace(distance);
  358. ray.origin.addToRef(direction, pickedPoint);
  359. result.pickedPoint = Vector3.TransformCoordinates(pickedPoint, TmpVectors.Matrix[0]);
  360. return result;
  361. }
  362. return null;
  363. }
  364. /**
  365. * Intersects the sprites with a ray
  366. * @param ray defines the ray to intersect with
  367. * @param camera defines the current active camera
  368. * @param predicate defines a predicate used to select candidate sprites
  369. * @returns null if no hit or a PickingInfo array
  370. */
  371. public multiIntersects(ray: Ray, camera: Camera, predicate?: (sprite: Sprite) => boolean): Nullable<PickingInfo[]> {
  372. var count = Math.min(this._capacity, this.sprites.length);
  373. var min = TmpVectors.Vector3[0].copyFromFloats(0, 0, 0);
  374. var max = TmpVectors.Vector3[1].copyFromFloats(0, 0, 0);
  375. var distance: number;
  376. var results: Nullable<PickingInfo[]> = [];
  377. var pickedPoint = TmpVectors.Vector3[2].copyFromFloats(0, 0, 0);
  378. var cameraSpacePosition = TmpVectors.Vector3[3].copyFromFloats(0, 0, 0);;
  379. var cameraView = camera.getViewMatrix();
  380. for (var index = 0; index < count; index++) {
  381. var sprite = this.sprites[index];
  382. if (!sprite) {
  383. continue;
  384. }
  385. if (predicate) {
  386. if (!predicate(sprite)) {
  387. continue;
  388. }
  389. } else if (!sprite.isPickable) {
  390. continue;
  391. }
  392. Vector3.TransformCoordinatesToRef(sprite.position, cameraView, cameraSpacePosition);
  393. min.copyFromFloats(cameraSpacePosition.x - sprite.width / 2, cameraSpacePosition.y - sprite.height / 2, cameraSpacePosition.z);
  394. max.copyFromFloats(cameraSpacePosition.x + sprite.width / 2, cameraSpacePosition.y + sprite.height / 2, cameraSpacePosition.z);
  395. if (ray.intersectsBoxMinMax(min, max)) {
  396. distance = Vector3.Distance(cameraSpacePosition, ray.origin);
  397. var result = new PickingInfo();
  398. results.push(result);
  399. cameraView.invertToRef(TmpVectors.Matrix[0]);
  400. result.hit = true;
  401. result.pickedSprite = sprite;
  402. result.distance = distance;
  403. // Get picked point
  404. let direction = TmpVectors.Vector3[4];
  405. direction.copyFrom(ray.direction);
  406. direction.normalize();
  407. direction.scaleInPlace(distance);
  408. ray.origin.addToRef(direction, pickedPoint);
  409. result.pickedPoint = Vector3.TransformCoordinates(pickedPoint, TmpVectors.Matrix[0]);
  410. }
  411. }
  412. return results;
  413. }
  414. /**
  415. * Render all child sprites
  416. */
  417. public render(): void {
  418. // Check
  419. if (!this._effectBase.isReady() || !this._effectFog.isReady() || !this._spriteTexture
  420. || !this._spriteTexture.isReady() || !this.sprites.length) {
  421. return;
  422. }
  423. if (this._fromPacked && (!this._packedAndReady || !this._spriteMap || !this._cellData)) {
  424. return;
  425. }
  426. var engine = this._scene.getEngine();
  427. var baseSize = this._spriteTexture.getBaseSize();
  428. // Sprites
  429. var deltaTime = engine.getDeltaTime();
  430. var max = Math.min(this._capacity, this.sprites.length);
  431. var offset = 0;
  432. let noSprite = true;
  433. for (var index = 0; index < max; index++) {
  434. var sprite = this.sprites[index];
  435. if (!sprite || !sprite.isVisible) {
  436. continue;
  437. }
  438. noSprite = false;
  439. sprite._animate(deltaTime);
  440. this._appendSpriteVertex(offset++, sprite, 0, 0, baseSize);
  441. this._appendSpriteVertex(offset++, sprite, 1, 0, baseSize);
  442. this._appendSpriteVertex(offset++, sprite, 1, 1, baseSize);
  443. this._appendSpriteVertex(offset++, sprite, 0, 1, baseSize);
  444. }
  445. if (noSprite) {
  446. return;
  447. }
  448. this._buffer.update(this._vertexData);
  449. // Render
  450. var effect = this._effectBase;
  451. if (this._scene.fogEnabled && this._scene.fogMode !== Scene.FOGMODE_NONE && this.fogEnabled) {
  452. effect = this._effectFog;
  453. }
  454. engine.enableEffect(effect);
  455. var viewMatrix = this._scene.getViewMatrix();
  456. effect.setTexture("diffuseSampler", this._spriteTexture);
  457. effect.setMatrix("view", viewMatrix);
  458. effect.setMatrix("projection", this._scene.getProjectionMatrix());
  459. // Fog
  460. if (this._scene.fogEnabled && this._scene.fogMode !== Scene.FOGMODE_NONE && this.fogEnabled) {
  461. effect.setFloat4("vFogInfos", this._scene.fogMode, this._scene.fogStart, this._scene.fogEnd, this._scene.fogDensity);
  462. effect.setColor3("vFogColor", this._scene.fogColor);
  463. }
  464. // VBOs
  465. engine.bindBuffers(this._vertexBuffers, this._indexBuffer, effect);
  466. // Draw order
  467. engine.setDepthFunctionToLessOrEqual();
  468. effect.setBool("alphaTest", true);
  469. engine.setColorWrite(false);
  470. engine.drawElementsType(Material.TriangleFillMode, 0, (offset / 4) * 6);
  471. engine.setColorWrite(true);
  472. effect.setBool("alphaTest", false);
  473. engine.setAlphaMode(Constants.ALPHA_COMBINE);
  474. engine.drawElementsType(Material.TriangleFillMode, 0, (offset / 4) * 6);
  475. engine.setAlphaMode(Constants.ALPHA_DISABLE);
  476. }
  477. /**
  478. * Release associated resources
  479. */
  480. public dispose(): void {
  481. if (this._buffer) {
  482. this._buffer.dispose();
  483. (<any>this._buffer) = null;
  484. }
  485. if (this._indexBuffer) {
  486. this._scene.getEngine()._releaseBuffer(this._indexBuffer);
  487. (<any>this._indexBuffer) = null;
  488. }
  489. if (this._spriteTexture) {
  490. this._spriteTexture.dispose();
  491. (<any>this._spriteTexture) = null;
  492. }
  493. // Remove from scene
  494. var index = this._scene.spriteManagers.indexOf(this);
  495. this._scene.spriteManagers.splice(index, 1);
  496. // Callback
  497. this.onDisposeObservable.notifyObservers(this);
  498. this.onDisposeObservable.clear();
  499. }
  500. }