texture.d.ts 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372
  1. import { Observable } from "../../Misc/observable";
  2. import type { Nullable } from "../../types";
  3. import { Matrix } from "../../Maths/math.vector";
  4. import { BaseTexture } from "../../Materials/Textures/baseTexture";
  5. import type { IInspectable } from "../../Misc/iInspectable";
  6. import type { AbstractEngine } from "../../Engines/abstractEngine";
  7. import type { InternalTexture } from "./internalTexture";
  8. import type { CubeTexture } from "../../Materials/Textures/cubeTexture";
  9. import type { MirrorTexture } from "../../Materials/Textures/mirrorTexture";
  10. import type { RenderTargetTexture } from "../../Materials/Textures/renderTargetTexture";
  11. import type { Scene } from "../../scene";
  12. import type { VideoTexture, VideoTextureSettings } from "./videoTexture";
  13. /**
  14. * Defines the available options when creating a texture
  15. */
  16. export interface ITextureCreationOptions {
  17. /** Defines if the texture will require mip maps or not (default: false) */
  18. noMipmap?: boolean;
  19. /** Defines if the texture needs to be inverted on the y axis during loading (default: true) */
  20. invertY?: boolean;
  21. /** Defines the sampling mode we want for the texture while fetching from it (Texture.NEAREST_SAMPLINGMODE...) (default: Texture.TRILINEAR_SAMPLINGMODE) */
  22. samplingMode?: number;
  23. /** Defines a callback triggered when the texture has been loaded (default: null) */
  24. onLoad?: Nullable<() => void>;
  25. /** Defines a callback triggered when an error occurred during the loading session (default: null) */
  26. onError?: Nullable<(message?: string, exception?: any) => void>;
  27. /** Defines the buffer to load the texture from in case the texture is loaded from a buffer representation (default: null) */
  28. buffer?: Nullable<string | ArrayBuffer | ArrayBufferView | HTMLImageElement | Blob | ImageBitmap>;
  29. /** Defines if the buffer we are loading the texture from should be deleted after load (default: false) */
  30. deleteBuffer?: boolean;
  31. /** Defines the format of the texture we are trying to load (Engine.TEXTUREFORMAT_RGBA...) (default: ) */
  32. format?: number;
  33. /** Defines an optional mime type information (default: undefined) */
  34. mimeType?: string;
  35. /** Options to be passed to the loader (default: undefined) */
  36. loaderOptions?: any;
  37. /** Specific flags to use when creating the texture (Constants.TEXTURE_CREATIONFLAG_STORAGE for storage textures, for eg) (default: undefined) */
  38. creationFlags?: number;
  39. /** Defines if the texture must be loaded in a sRGB GPU buffer (if supported by the GPU) (default: false) */
  40. useSRGBBuffer?: boolean;
  41. /** Defines the underlying texture from an already existing one */
  42. internalTexture?: InternalTexture;
  43. /** Defines the underlying texture texture space */
  44. gammaSpace?: boolean;
  45. }
  46. /**
  47. * This represents a texture in babylon. It can be easily loaded from a network, base64 or html input.
  48. * @see https://doc.babylonjs.com/features/featuresDeepDive/materials/using/materials_introduction#texture
  49. */
  50. export declare class Texture extends BaseTexture {
  51. /**
  52. * Gets or sets a general boolean used to indicate that textures containing direct data (buffers) must be saved as part of the serialization process
  53. */
  54. static SerializeBuffers: boolean;
  55. /**
  56. * Gets or sets a general boolean used to indicate that texture buffers must be saved as part of the serialization process.
  57. * If no buffer exists, one will be created as base64 string from the internal webgl data.
  58. */
  59. static ForceSerializeBuffers: boolean;
  60. /**
  61. * This observable will notify when any texture had a loading error
  62. */
  63. static OnTextureLoadErrorObservable: Observable<BaseTexture>;
  64. /** @internal */
  65. static _SerializeInternalTextureUniqueId: boolean;
  66. /**
  67. * @internal
  68. */
  69. static _CubeTextureParser: (jsonTexture: any, scene: Scene, rootUrl: string) => CubeTexture;
  70. /**
  71. * @internal
  72. */
  73. static _CreateMirror: (name: string, renderTargetSize: number, scene: Scene, generateMipMaps: boolean) => MirrorTexture;
  74. /**
  75. * @internal
  76. */
  77. static _CreateRenderTargetTexture: (name: string, renderTargetSize: number, scene: Scene, generateMipMaps: boolean, creationFlags?: number) => RenderTargetTexture;
  78. /**
  79. * @internal
  80. */
  81. static _CreateVideoTexture(name: Nullable<string>, src: string | string[] | HTMLVideoElement, scene: Nullable<Scene>, generateMipMaps?: boolean, invertY?: boolean, samplingMode?: number, settings?: Partial<VideoTextureSettings>, onError?: Nullable<(message?: string, exception?: any) => void>, format?: number): VideoTexture;
  82. /** nearest is mag = nearest and min = nearest and no mip */
  83. static readonly NEAREST_SAMPLINGMODE = 1;
  84. /** nearest is mag = nearest and min = nearest and mip = linear */
  85. static readonly NEAREST_NEAREST_MIPLINEAR = 8;
  86. /** Bilinear is mag = linear and min = linear and no mip */
  87. static readonly BILINEAR_SAMPLINGMODE = 2;
  88. /** Bilinear is mag = linear and min = linear and mip = nearest */
  89. static readonly LINEAR_LINEAR_MIPNEAREST = 11;
  90. /** Trilinear is mag = linear and min = linear and mip = linear */
  91. static readonly TRILINEAR_SAMPLINGMODE = 3;
  92. /** Trilinear is mag = linear and min = linear and mip = linear */
  93. static readonly LINEAR_LINEAR_MIPLINEAR = 3;
  94. /** mag = nearest and min = nearest and mip = nearest */
  95. static readonly NEAREST_NEAREST_MIPNEAREST = 4;
  96. /** mag = nearest and min = linear and mip = nearest */
  97. static readonly NEAREST_LINEAR_MIPNEAREST = 5;
  98. /** mag = nearest and min = linear and mip = linear */
  99. static readonly NEAREST_LINEAR_MIPLINEAR = 6;
  100. /** mag = nearest and min = linear and mip = none */
  101. static readonly NEAREST_LINEAR = 7;
  102. /** mag = nearest and min = nearest and mip = none */
  103. static readonly NEAREST_NEAREST = 1;
  104. /** mag = linear and min = nearest and mip = nearest */
  105. static readonly LINEAR_NEAREST_MIPNEAREST = 9;
  106. /** mag = linear and min = nearest and mip = linear */
  107. static readonly LINEAR_NEAREST_MIPLINEAR = 10;
  108. /** mag = linear and min = linear and mip = none */
  109. static readonly LINEAR_LINEAR = 2;
  110. /** mag = linear and min = nearest and mip = none */
  111. static readonly LINEAR_NEAREST = 12;
  112. /** Explicit coordinates mode */
  113. static readonly EXPLICIT_MODE = 0;
  114. /** Spherical coordinates mode */
  115. static readonly SPHERICAL_MODE = 1;
  116. /** Planar coordinates mode */
  117. static readonly PLANAR_MODE = 2;
  118. /** Cubic coordinates mode */
  119. static readonly CUBIC_MODE = 3;
  120. /** Projection coordinates mode */
  121. static readonly PROJECTION_MODE = 4;
  122. /** Inverse Cubic coordinates mode */
  123. static readonly SKYBOX_MODE = 5;
  124. /** Inverse Cubic coordinates mode */
  125. static readonly INVCUBIC_MODE = 6;
  126. /** Equirectangular coordinates mode */
  127. static readonly EQUIRECTANGULAR_MODE = 7;
  128. /** Equirectangular Fixed coordinates mode */
  129. static readonly FIXED_EQUIRECTANGULAR_MODE = 8;
  130. /** Equirectangular Fixed Mirrored coordinates mode */
  131. static readonly FIXED_EQUIRECTANGULAR_MIRRORED_MODE = 9;
  132. /** Texture is not repeating outside of 0..1 UVs */
  133. static readonly CLAMP_ADDRESSMODE = 0;
  134. /** Texture is repeating outside of 0..1 UVs */
  135. static readonly WRAP_ADDRESSMODE = 1;
  136. /** Texture is repeating and mirrored */
  137. static readonly MIRROR_ADDRESSMODE = 2;
  138. /**
  139. * Gets or sets a boolean which defines if the texture url must be build from the serialized URL instead of just using the name and loading them side by side with the scene file
  140. */
  141. static UseSerializedUrlIfAny: boolean;
  142. /**
  143. * Define the url of the texture.
  144. */
  145. url: Nullable<string>;
  146. /**
  147. * Define an offset on the texture to offset the u coordinates of the UVs
  148. * @see https://doc.babylonjs.com/features/featuresDeepDive/materials/using/moreMaterials#offsetting
  149. */
  150. uOffset: number;
  151. /**
  152. * Define an offset on the texture to offset the v coordinates of the UVs
  153. * @see https://doc.babylonjs.com/features/featuresDeepDive/materials/using/moreMaterials#offsetting
  154. */
  155. vOffset: number;
  156. /**
  157. * Define an offset on the texture to scale the u coordinates of the UVs
  158. * @see https://doc.babylonjs.com/features/featuresDeepDive/materials/using/moreMaterials#tiling
  159. */
  160. uScale: number;
  161. /**
  162. * Define an offset on the texture to scale the v coordinates of the UVs
  163. * @see https://doc.babylonjs.com/features/featuresDeepDive/materials/using/moreMaterials#tiling
  164. */
  165. vScale: number;
  166. /**
  167. * Define an offset on the texture to rotate around the u coordinates of the UVs
  168. * The angle is defined in radians.
  169. * @see https://doc.babylonjs.com/features/featuresDeepDive/materials/using/moreMaterials
  170. */
  171. uAng: number;
  172. /**
  173. * Define an offset on the texture to rotate around the v coordinates of the UVs
  174. * The angle is defined in radians.
  175. * @see https://doc.babylonjs.com/features/featuresDeepDive/materials/using/moreMaterials
  176. */
  177. vAng: number;
  178. /**
  179. * Define an offset on the texture to rotate around the w coordinates of the UVs (in case of 3d texture)
  180. * The angle is defined in radians.
  181. * @see https://doc.babylonjs.com/features/featuresDeepDive/materials/using/moreMaterials
  182. */
  183. wAng: number;
  184. /**
  185. * Defines the center of rotation (U)
  186. */
  187. uRotationCenter: number;
  188. /**
  189. * Defines the center of rotation (V)
  190. */
  191. vRotationCenter: number;
  192. /**
  193. * Defines the center of rotation (W)
  194. */
  195. wRotationCenter: number;
  196. /**
  197. * Sets this property to true to avoid deformations when rotating the texture with non-uniform scaling
  198. */
  199. homogeneousRotationInUVTransform: boolean;
  200. /**
  201. * Are mip maps generated for this texture or not.
  202. */
  203. get noMipmap(): boolean;
  204. /**
  205. * List of inspectable custom properties (used by the Inspector)
  206. * @see https://doc.babylonjs.com/toolsAndResources/inspector#extensibility
  207. */
  208. inspectableCustomProperties: Nullable<IInspectable[]>;
  209. /** @internal */
  210. _noMipmap: boolean;
  211. /** @internal */
  212. _invertY: boolean;
  213. private _rowGenerationMatrix;
  214. private _cachedTextureMatrix;
  215. private _projectionModeMatrix;
  216. private _t0;
  217. private _t1;
  218. private _t2;
  219. private _cachedUOffset;
  220. private _cachedVOffset;
  221. private _cachedUScale;
  222. private _cachedVScale;
  223. private _cachedUAng;
  224. private _cachedVAng;
  225. private _cachedWAng;
  226. private _cachedReflectionProjectionMatrixId;
  227. private _cachedURotationCenter;
  228. private _cachedVRotationCenter;
  229. private _cachedWRotationCenter;
  230. private _cachedHomogeneousRotationInUVTransform;
  231. private _cachedIdentity3x2;
  232. private _cachedReflectionTextureMatrix;
  233. private _cachedReflectionUOffset;
  234. private _cachedReflectionVOffset;
  235. private _cachedReflectionUScale;
  236. private _cachedReflectionVScale;
  237. private _cachedReflectionCoordinatesMode;
  238. /** @internal */
  239. _buffer: Nullable<string | ArrayBuffer | ArrayBufferView | HTMLImageElement | Blob | ImageBitmap>;
  240. private _deleteBuffer;
  241. protected _format: Nullable<number>;
  242. private _delayedOnLoad;
  243. private _delayedOnError;
  244. private _mimeType?;
  245. private _loaderOptions?;
  246. private _creationFlags?;
  247. /** @internal */
  248. _useSRGBBuffer?: boolean;
  249. private _forcedExtension?;
  250. /** Returns the texture mime type if it was defined by a loader (undefined else) */
  251. get mimeType(): string | undefined;
  252. /**
  253. * Observable triggered once the texture has been loaded.
  254. */
  255. onLoadObservable: Observable<Texture>;
  256. protected _isBlocking: boolean;
  257. /**
  258. * Is the texture preventing material to render while loading.
  259. * If false, a default texture will be used instead of the loading one during the preparation step.
  260. */
  261. set isBlocking(value: boolean);
  262. get isBlocking(): boolean;
  263. /**
  264. * Gets a boolean indicating if the texture needs to be inverted on the y axis during loading
  265. */
  266. get invertY(): boolean;
  267. /**
  268. * Instantiates a new texture.
  269. * This represents a texture in babylon. It can be easily loaded from a network, base64 or html input.
  270. * @see https://doc.babylonjs.com/features/featuresDeepDive/materials/using/materials_introduction#texture
  271. * @param url defines the url of the picture to load as a texture
  272. * @param sceneOrEngine defines the scene or engine the texture will belong to
  273. * @param noMipmapOrOptions defines if the texture will require mip maps or not or set of all options to create the texture
  274. * @param invertY defines if the texture needs to be inverted on the y axis during loading
  275. * @param samplingMode defines the sampling mode we want for the texture while fetching from it (Texture.NEAREST_SAMPLINGMODE...)
  276. * @param onLoad defines a callback triggered when the texture has been loaded
  277. * @param onError defines a callback triggered when an error occurred during the loading session
  278. * @param buffer defines the buffer to load the texture from in case the texture is loaded from a buffer representation
  279. * @param deleteBuffer defines if the buffer we are loading the texture from should be deleted after load
  280. * @param format defines the format of the texture we are trying to load (Engine.TEXTUREFORMAT_RGBA...)
  281. * @param mimeType defines an optional mime type information
  282. * @param loaderOptions options to be passed to the loader
  283. * @param creationFlags specific flags to use when creating the texture (Constants.TEXTURE_CREATIONFLAG_STORAGE for storage textures, for eg)
  284. * @param forcedExtension defines the extension to use to pick the right loader
  285. */
  286. constructor(url: Nullable<string>, sceneOrEngine?: Nullable<Scene | AbstractEngine>, noMipmapOrOptions?: boolean | ITextureCreationOptions, invertY?: boolean, samplingMode?: number, onLoad?: Nullable<() => void>, onError?: Nullable<(message?: string, exception?: any) => void>, buffer?: Nullable<string | ArrayBuffer | ArrayBufferView | HTMLImageElement | Blob | ImageBitmap>, deleteBuffer?: boolean, format?: number, mimeType?: string, loaderOptions?: any, creationFlags?: number, forcedExtension?: string);
  287. /**
  288. * Update the url (and optional buffer) of this texture if url was null during construction.
  289. * @param url the url of the texture
  290. * @param buffer the buffer of the texture (defaults to null)
  291. * @param onLoad callback called when the texture is loaded (defaults to null)
  292. * @param forcedExtension defines the extension to use to pick the right loader
  293. */
  294. updateURL(url: string, buffer?: Nullable<string | ArrayBuffer | ArrayBufferView | HTMLImageElement | Blob | ImageBitmap>, onLoad?: () => void, forcedExtension?: string): void;
  295. /**
  296. * Finish the loading sequence of a texture flagged as delayed load.
  297. * @internal
  298. */
  299. delayLoad(): void;
  300. private _prepareRowForTextureGeneration;
  301. /**
  302. * Get the current texture matrix which includes the requested offsetting, tiling and rotation components.
  303. * @param uBase The horizontal base offset multiplier (1 by default)
  304. * @returns the transform matrix of the texture.
  305. */
  306. getTextureMatrix(uBase?: number): Matrix;
  307. /**
  308. * Get the current matrix used to apply reflection. This is useful to rotate an environment texture for instance.
  309. * @returns The reflection texture transform
  310. */
  311. getReflectionTextureMatrix(): Matrix;
  312. /**
  313. * Clones the texture.
  314. * @returns the cloned texture
  315. */
  316. clone(): Texture;
  317. /**
  318. * Serialize the texture to a JSON representation we can easily use in the respective Parse function.
  319. * @returns The JSON representation of the texture
  320. */
  321. serialize(): any;
  322. /**
  323. * Get the current class name of the texture useful for serialization or dynamic coding.
  324. * @returns "Texture"
  325. */
  326. getClassName(): string;
  327. /**
  328. * Dispose the texture and release its associated resources.
  329. */
  330. dispose(): void;
  331. /**
  332. * Parse the JSON representation of a texture in order to recreate the texture in the given scene.
  333. * @param parsedTexture Define the JSON representation of the texture
  334. * @param scene Define the scene the parsed texture should be instantiated in
  335. * @param rootUrl Define the root url of the parsing sequence in the case of relative dependencies
  336. * @returns The parsed texture if successful
  337. */
  338. static Parse(parsedTexture: any, scene: Scene, rootUrl: string): Nullable<BaseTexture>;
  339. /**
  340. * Creates a texture from its base 64 representation.
  341. * @param data Define the base64 payload without the data: prefix
  342. * @param name Define the name of the texture in the scene useful fo caching purpose for instance
  343. * @param scene Define the scene the texture should belong to
  344. * @param noMipmapOrOptions defines if the texture will require mip maps or not or set of all options to create the texture
  345. * @param invertY define if the texture needs to be inverted on the y axis during loading
  346. * @param samplingMode define the sampling mode we want for the texture while fetching from it (Texture.NEAREST_SAMPLINGMODE...)
  347. * @param onLoad define a callback triggered when the texture has been loaded
  348. * @param onError define a callback triggered when an error occurred during the loading session
  349. * @param format define the format of the texture we are trying to load (Engine.TEXTUREFORMAT_RGBA...)
  350. * @param creationFlags specific flags to use when creating the texture (Constants.TEXTURE_CREATIONFLAG_STORAGE for storage textures, for eg)
  351. * @param forcedExtension defines the extension to use to pick the right loader
  352. * @returns the created texture
  353. */
  354. static CreateFromBase64String(data: string, name: string, scene: Scene, noMipmapOrOptions?: boolean | ITextureCreationOptions, invertY?: boolean, samplingMode?: number, onLoad?: Nullable<() => void>, onError?: Nullable<() => void>, format?: number, creationFlags?: number, forcedExtension?: string): Texture;
  355. /**
  356. * Creates a texture from its data: representation. (data: will be added in case only the payload has been passed in)
  357. * @param name Define the name of the texture in the scene useful fo caching purpose for instance
  358. * @param buffer define the buffer to load the texture from in case the texture is loaded from a buffer representation
  359. * @param scene Define the scene the texture should belong to
  360. * @param deleteBuffer define if the buffer we are loading the texture from should be deleted after load
  361. * @param noMipmapOrOptions defines if the texture will require mip maps or not or set of all options to create the texture
  362. * @param invertY define if the texture needs to be inverted on the y axis during loading
  363. * @param samplingMode define the sampling mode we want for the texture while fetching from it (Texture.NEAREST_SAMPLINGMODE...)
  364. * @param onLoad define a callback triggered when the texture has been loaded
  365. * @param onError define a callback triggered when an error occurred during the loading session
  366. * @param format define the format of the texture we are trying to load (Engine.TEXTUREFORMAT_RGBA...)
  367. * @param creationFlags specific flags to use when creating the texture (Constants.TEXTURE_CREATIONFLAG_STORAGE for storage textures, for eg)
  368. * @param forcedExtension defines the extension to use to pick the right loader
  369. * @returns the created texture
  370. */
  371. static LoadFromDataString(name: string, buffer: any, scene: Scene, deleteBuffer?: boolean, noMipmapOrOptions?: boolean | ITextureCreationOptions, invertY?: boolean, samplingMode?: number, onLoad?: Nullable<() => void>, onError?: Nullable<(message?: string, exception?: any) => void>, format?: number, creationFlags?: number, forcedExtension?: string): Texture;
  372. }