babylon.baseTexture.ts 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286
  1. module BABYLON {
  2. export class BaseTexture {
  3. public static DEFAULT_ANISOTROPIC_FILTERING_LEVEL = 4;
  4. @serialize()
  5. public name: string;
  6. @serialize("hasAlpha")
  7. private _hasAlpha = false;
  8. public set hasAlpha(value : boolean) {
  9. if (this._hasAlpha === value) {
  10. return;
  11. }
  12. this._hasAlpha = value;
  13. this._scene.markAllMaterialsAsDirty(Material.TextureDirtyFlag);
  14. }
  15. public get hasAlpha(): boolean {
  16. return this._hasAlpha;
  17. }
  18. @serialize()
  19. public getAlphaFromRGB = false;
  20. @serialize()
  21. public level = 1;
  22. @serialize()
  23. public coordinatesIndex = 0;
  24. @serialize("coordinatesMode")
  25. private _coordinatesMode = Texture.EXPLICIT_MODE;
  26. public set coordinatesMode(value : number) {
  27. if (this._coordinatesMode === value) {
  28. return;
  29. }
  30. this._coordinatesMode = value;
  31. this._scene.markAllMaterialsAsDirty(Material.TextureDirtyFlag);
  32. }
  33. public get coordinatesMode(): number {
  34. return this._coordinatesMode;
  35. }
  36. @serialize()
  37. public wrapU = Texture.WRAP_ADDRESSMODE;
  38. @serialize()
  39. public wrapV = Texture.WRAP_ADDRESSMODE;
  40. @serialize()
  41. public anisotropicFilteringLevel = BaseTexture.DEFAULT_ANISOTROPIC_FILTERING_LEVEL;
  42. @serialize()
  43. public isCube = false;
  44. @serialize()
  45. public isRenderTarget = false;
  46. public get uid(): string {
  47. if (!this._uid) {
  48. this._uid = Tools.RandomId();
  49. }
  50. return this._uid;
  51. }
  52. public toString(): string {
  53. return this.name;
  54. }
  55. public animations = new Array<Animation>();
  56. /**
  57. * An event triggered when the texture is disposed.
  58. * @type {BABYLON.Observable}
  59. */
  60. public onDisposeObservable = new Observable<BaseTexture>();
  61. private _onDisposeObserver: Observer<BaseTexture>;
  62. public set onDispose(callback: () => void) {
  63. if (this._onDisposeObserver) {
  64. this.onDisposeObservable.remove(this._onDisposeObserver);
  65. }
  66. this._onDisposeObserver = this.onDisposeObservable.add(callback);
  67. }
  68. public delayLoadState = Engine.DELAYLOADSTATE_NONE;
  69. public _cachedAnisotropicFilteringLevel: number;
  70. private _scene: Scene;
  71. public _texture: WebGLTexture;
  72. private _uid: string;
  73. public get isBlocking(): boolean {
  74. return true;
  75. }
  76. constructor(scene: Scene) {
  77. this._scene = scene || Engine.LastCreatedScene;
  78. this._scene.textures.push(this);
  79. this._uid = null;
  80. }
  81. public getScene(): Scene {
  82. return this._scene;
  83. }
  84. public getTextureMatrix(): Matrix {
  85. return null;
  86. }
  87. public getReflectionTextureMatrix(): Matrix {
  88. return null;
  89. }
  90. public getInternalTexture(): WebGLTexture {
  91. return this._texture;
  92. }
  93. public isReadyOrNotBlocking(): boolean {
  94. return !this.isBlocking || this.isReady();
  95. }
  96. public isReady(): boolean {
  97. if (this.delayLoadState === Engine.DELAYLOADSTATE_NOTLOADED) {
  98. this.delayLoad();
  99. return false;
  100. }
  101. if (this._texture) {
  102. return this._texture.isReady;
  103. }
  104. return false;
  105. }
  106. public getSize(): ISize {
  107. if (this._texture._width) {
  108. return new Size(this._texture._width, this._texture._height);
  109. }
  110. if (this._texture._size) {
  111. return new Size(this._texture._size, this._texture._size);
  112. }
  113. return Size.Zero();
  114. }
  115. public getBaseSize(): ISize {
  116. if (!this.isReady() || !this._texture)
  117. return Size.Zero();
  118. if (this._texture._size) {
  119. return new Size(this._texture._size, this._texture._size);
  120. }
  121. return new Size(this._texture._baseWidth, this._texture._baseHeight);
  122. }
  123. public scale(ratio: number): void {
  124. }
  125. public get canRescale(): boolean {
  126. return false;
  127. }
  128. public _removeFromCache(url: string, noMipmap: boolean): void {
  129. var texturesCache = this._scene.getEngine().getLoadedTexturesCache();
  130. for (var index = 0; index < texturesCache.length; index++) {
  131. var texturesCacheEntry = texturesCache[index];
  132. if (texturesCacheEntry.url === url && texturesCacheEntry.noMipmap === noMipmap) {
  133. texturesCache.splice(index, 1);
  134. return;
  135. }
  136. }
  137. }
  138. public _getFromCache(url: string, noMipmap: boolean, sampling?: number): WebGLTexture {
  139. var texturesCache = this._scene.getEngine().getLoadedTexturesCache();
  140. for (var index = 0; index < texturesCache.length; index++) {
  141. var texturesCacheEntry = texturesCache[index];
  142. if (texturesCacheEntry.url === url && texturesCacheEntry.noMipmap === noMipmap) {
  143. if (!sampling || sampling === texturesCacheEntry.samplingMode) {
  144. texturesCacheEntry.references++;
  145. return texturesCacheEntry;
  146. }
  147. }
  148. }
  149. return null;
  150. }
  151. public delayLoad(): void {
  152. }
  153. public clone(): BaseTexture {
  154. return null;
  155. }
  156. public readPixels(faceIndex = 0): Uint8Array {
  157. if (!this._texture) {
  158. return null;
  159. }
  160. var size = this.getSize();
  161. var engine = this.getScene().getEngine();
  162. if (this._texture.isCube) {
  163. return engine._readTexturePixels(this._texture, size.width, size.height, faceIndex);
  164. }
  165. return engine._readTexturePixels(this._texture, size.width, size.height);
  166. }
  167. public releaseInternalTexture(): void {
  168. if (this._texture) {
  169. this._scene.getEngine().releaseInternalTexture(this._texture);
  170. delete this._texture;
  171. }
  172. }
  173. public dispose(): void {
  174. // Animations
  175. this.getScene().stopAnimation(this);
  176. // Remove from scene
  177. this._scene._removePendingData(this);
  178. var index = this._scene.textures.indexOf(this);
  179. if (index >= 0) {
  180. this._scene.textures.splice(index, 1);
  181. }
  182. if (this._texture === undefined) {
  183. return;
  184. }
  185. // Release
  186. this.releaseInternalTexture();
  187. // Callback
  188. this.onDisposeObservable.notifyObservers(this);
  189. this.onDisposeObservable.clear();
  190. }
  191. public serialize(): any {
  192. if (!this.name) {
  193. return null;
  194. }
  195. var serializationObject = SerializationHelper.Serialize(this);
  196. // Animations
  197. Animation.AppendSerializedAnimations(this, serializationObject);
  198. return serializationObject;
  199. }
  200. public static WhenAllReady(textures: BaseTexture[], onLoad: () => void): void {
  201. var numReady = 0;
  202. for (var i = 0; i < textures.length; i++) {
  203. var texture = textures[i];
  204. if (texture.isReady()) {
  205. if (++numReady === textures.length) {
  206. onLoad();
  207. }
  208. }
  209. else {
  210. var observable = (texture as any).onLoadObservable as Observable<Texture>;
  211. let callback = () => {
  212. observable.removeCallback(callback);
  213. if (++numReady === textures.length) {
  214. onLoad();
  215. }
  216. };
  217. observable.add(callback);
  218. }
  219. }
  220. }
  221. }
  222. }