babylon.baseTexture.ts 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357
  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 gammaSpace = true;
  46. @serialize()
  47. public invertZ = false;
  48. @serialize()
  49. public lodLevelInAlpha = false;
  50. @serialize()
  51. public lodGenerationOffset = 1.0;
  52. @serialize()
  53. public lodGenerationScale = 0.8;
  54. @serialize()
  55. public isRenderTarget = false;
  56. public get uid(): string {
  57. if (!this._uid) {
  58. this._uid = Tools.RandomId();
  59. }
  60. return this._uid;
  61. }
  62. public toString(): string {
  63. return this.name;
  64. }
  65. public animations = new Array<Animation>();
  66. /**
  67. * An event triggered when the texture is disposed.
  68. * @type {BABYLON.Observable}
  69. */
  70. public onDisposeObservable = new Observable<BaseTexture>();
  71. private _onDisposeObserver: Observer<BaseTexture>;
  72. public set onDispose(callback: () => void) {
  73. if (this._onDisposeObserver) {
  74. this.onDisposeObservable.remove(this._onDisposeObserver);
  75. }
  76. this._onDisposeObserver = this.onDisposeObservable.add(callback);
  77. }
  78. public delayLoadState = Engine.DELAYLOADSTATE_NONE;
  79. public _cachedAnisotropicFilteringLevel: number;
  80. private _scene: Scene;
  81. public _texture: WebGLTexture;
  82. private _uid: string;
  83. public get isBlocking(): boolean {
  84. return true;
  85. }
  86. constructor(scene: Scene) {
  87. this._scene = scene || Engine.LastCreatedScene;
  88. this._scene.textures.push(this);
  89. this._uid = null;
  90. }
  91. public getScene(): Scene {
  92. return this._scene;
  93. }
  94. public getTextureMatrix(): Matrix {
  95. return null;
  96. }
  97. public getReflectionTextureMatrix(): Matrix {
  98. return null;
  99. }
  100. public getInternalTexture(): WebGLTexture {
  101. return this._texture;
  102. }
  103. public isReadyOrNotBlocking(): boolean {
  104. return !this.isBlocking || this.isReady();
  105. }
  106. public isReady(): boolean {
  107. if (this.delayLoadState === Engine.DELAYLOADSTATE_NOTLOADED) {
  108. this.delayLoad();
  109. return false;
  110. }
  111. if (this._texture) {
  112. return this._texture.isReady;
  113. }
  114. return false;
  115. }
  116. public getSize(): ISize {
  117. if (this._texture._width) {
  118. return new Size(this._texture._width, this._texture._height);
  119. }
  120. if (this._texture._size) {
  121. return new Size(this._texture._size, this._texture._size);
  122. }
  123. return Size.Zero();
  124. }
  125. public getBaseSize(): ISize {
  126. if (!this.isReady() || !this._texture)
  127. return Size.Zero();
  128. if (this._texture._size) {
  129. return new Size(this._texture._size, this._texture._size);
  130. }
  131. return new Size(this._texture._baseWidth, this._texture._baseHeight);
  132. }
  133. public scale(ratio: number): void {
  134. }
  135. public get canRescale(): boolean {
  136. return false;
  137. }
  138. public _removeFromCache(url: string, noMipmap: boolean): void {
  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. texturesCache.splice(index, 1);
  144. return;
  145. }
  146. }
  147. }
  148. public _getFromCache(url: string, noMipmap: boolean, sampling?: number): WebGLTexture {
  149. var texturesCache = this._scene.getEngine().getLoadedTexturesCache();
  150. for (var index = 0; index < texturesCache.length; index++) {
  151. var texturesCacheEntry = texturesCache[index];
  152. if (texturesCacheEntry.url === url && texturesCacheEntry.noMipmap === noMipmap) {
  153. if (!sampling || sampling === texturesCacheEntry.samplingMode) {
  154. texturesCacheEntry.references++;
  155. return texturesCacheEntry;
  156. }
  157. }
  158. }
  159. return null;
  160. }
  161. public delayLoad(): void {
  162. }
  163. public clone(): BaseTexture {
  164. return null;
  165. }
  166. public get textureType(): number {
  167. if (!this._texture) {
  168. return Engine.TEXTURETYPE_UNSIGNED_INT;
  169. }
  170. return (this._texture.type !== undefined) ? this._texture.type : Engine.TEXTURETYPE_UNSIGNED_INT;
  171. }
  172. public get textureFormat(): number {
  173. if (!this._texture) {
  174. return Engine.TEXTUREFORMAT_RGBA;
  175. }
  176. return (this._texture.format !== undefined) ? this._texture.format : Engine.TEXTUREFORMAT_RGBA;
  177. }
  178. public readPixels(faceIndex = 0): ArrayBufferView {
  179. if (!this._texture) {
  180. return null;
  181. }
  182. var size = this.getSize();
  183. var engine = this.getScene().getEngine();
  184. if (this._texture.isCube) {
  185. return engine._readTexturePixels(this._texture, size.width, size.height, faceIndex);
  186. }
  187. return engine._readTexturePixels(this._texture, size.width, size.height, -1);
  188. }
  189. public releaseInternalTexture(): void {
  190. if (this._texture) {
  191. this._scene.getEngine().releaseInternalTexture(this._texture);
  192. delete this._texture;
  193. }
  194. }
  195. public get sphericalPolynomial(): SphericalPolynomial {
  196. if (!this._texture || !Internals.CubeMapToSphericalPolynomialTools || !this.isReady()) {
  197. return null;
  198. }
  199. if (!this._texture._sphericalPolynomial) {
  200. this._texture._sphericalPolynomial =
  201. Internals.CubeMapToSphericalPolynomialTools.ConvertCubeMapTextureToSphericalPolynomial(this);
  202. }
  203. return this._texture._sphericalPolynomial;
  204. }
  205. public set sphericalPolynomial(value: SphericalPolynomial) {
  206. if (this._texture) {
  207. this._texture._sphericalPolynomial = value;
  208. }
  209. }
  210. public get lodTextureHigh(): BaseTexture {
  211. if (this._texture) {
  212. return this._texture._lodTextureHigh;
  213. }
  214. return null;
  215. }
  216. public get lodTextureMid(): BaseTexture {
  217. if (this._texture) {
  218. return this._texture._lodTextureMid;
  219. }
  220. return null;
  221. }
  222. public get lodTextureLow(): BaseTexture {
  223. if (this._texture) {
  224. return this._texture._lodTextureLow;
  225. }
  226. return null;
  227. }
  228. public dispose(): void {
  229. // Animations
  230. this.getScene().stopAnimation(this);
  231. // Remove from scene
  232. this._scene._removePendingData(this);
  233. var index = this._scene.textures.indexOf(this);
  234. if (index >= 0) {
  235. this._scene.textures.splice(index, 1);
  236. }
  237. if (this._texture === undefined) {
  238. return;
  239. }
  240. // Release
  241. this.releaseInternalTexture();
  242. // Callback
  243. this.onDisposeObservable.notifyObservers(this);
  244. this.onDisposeObservable.clear();
  245. }
  246. public serialize(): any {
  247. if (!this.name) {
  248. return null;
  249. }
  250. var serializationObject = SerializationHelper.Serialize(this);
  251. // Animations
  252. Animation.AppendSerializedAnimations(this, serializationObject);
  253. return serializationObject;
  254. }
  255. public static WhenAllReady(textures: BaseTexture[], onLoad: () => void): void {
  256. var numReady = 0;
  257. for (var i = 0; i < textures.length; i++) {
  258. var texture = textures[i];
  259. if (texture.isReady()) {
  260. if (++numReady === textures.length) {
  261. onLoad();
  262. }
  263. }
  264. else {
  265. var observable = (texture as any).onLoadObservable as Observable<Texture>;
  266. let callback = () => {
  267. observable.removeCallback(callback);
  268. if (++numReady === textures.length) {
  269. onLoad();
  270. }
  271. };
  272. observable.add(callback);
  273. }
  274. }
  275. }
  276. }
  277. }