123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771 |
- import { serialize, SerializationHelper, serializeAsTexture } from "../../Misc/decorators";
- import { Observer, Observable } from "../../Misc/observable";
- import { Nullable } from "../../types";
- import { Scene } from "../../scene";
- import { Matrix } from "../../Maths/math.vector";
- import { EngineStore } from "../../Engines/engineStore";
- import { InternalTexture } from "../../Materials/Textures/internalTexture";
- import { Constants } from "../../Engines/constants";
- import { IAnimatable } from '../../Animations/animatable.interface';
- import { GUID } from '../../Misc/guid';
- import "../../Misc/fileTools";
- import { ThinEngine } from '../../Engines/thinEngine';
- import { ThinTexture } from './thinTexture';
- declare type Animation = import("../../Animations/animation").Animation;
- /**
- * Base class of all the textures in babylon.
- * It groups all the common properties the materials, post process, lights... might need
- * in order to make a correct use of the texture.
- */
- export class BaseTexture extends ThinTexture implements IAnimatable {
- /**
- * Default anisotropic filtering level for the application.
- * It is set to 4 as a good tradeoff between perf and quality.
- */
- public static DEFAULT_ANISOTROPIC_FILTERING_LEVEL = 4;
- /**
- * Gets or sets the unique id of the texture
- */
- @serialize()
- public uniqueId: number;
- /**
- * Define the name of the texture.
- */
- @serialize()
- public name: string;
- /**
- * Gets or sets an object used to store user defined information.
- */
- @serialize()
- public metadata: any = null;
- /**
- * For internal use only. Please do not use.
- */
- public reservedDataStore: any = null;
- @serialize("hasAlpha")
- private _hasAlpha = false;
- /**
- * Define if the texture is having a usable alpha value (can be use for transparency or glossiness for instance).
- */
- public set hasAlpha(value: boolean) {
- if (this._hasAlpha === value) {
- return;
- }
- this._hasAlpha = value;
- if (this._scene) {
- this._scene.markAllMaterialsAsDirty(Constants.MATERIAL_TextureDirtyFlag | Constants.MATERIAL_MiscDirtyFlag);
- }
- }
- public get hasAlpha(): boolean {
- return this._hasAlpha;
- }
- /**
- * Defines if the alpha value should be determined via the rgb values.
- * If true the luminance of the pixel might be used to find the corresponding alpha value.
- */
- @serialize()
- public getAlphaFromRGB = false;
- /**
- * Intensity or strength of the texture.
- * It is commonly used by materials to fine tune the intensity of the texture
- */
- @serialize()
- public level = 1;
- /**
- * Define the UV chanel to use starting from 0 and defaulting to 0.
- * This is part of the texture as textures usually maps to one uv set.
- */
- @serialize()
- public coordinatesIndex = 0;
- @serialize("coordinatesMode")
- protected _coordinatesMode = Constants.TEXTURE_EXPLICIT_MODE;
- /**
- * How a texture is mapped.
- *
- * | Value | Type | Description |
- * | ----- | ----------------------------------- | ----------- |
- * | 0 | EXPLICIT_MODE | |
- * | 1 | SPHERICAL_MODE | |
- * | 2 | PLANAR_MODE | |
- * | 3 | CUBIC_MODE | |
- * | 4 | PROJECTION_MODE | |
- * | 5 | SKYBOX_MODE | |
- * | 6 | INVCUBIC_MODE | |
- * | 7 | EQUIRECTANGULAR_MODE | |
- * | 8 | FIXED_EQUIRECTANGULAR_MODE | |
- * | 9 | FIXED_EQUIRECTANGULAR_MIRRORED_MODE | |
- */
- public set coordinatesMode(value: number) {
- if (this._coordinatesMode === value) {
- return;
- }
- this._coordinatesMode = value;
- if (this._scene) {
- this._scene.markAllMaterialsAsDirty(Constants.MATERIAL_TextureDirtyFlag);
- }
- }
- public get coordinatesMode(): number {
- return this._coordinatesMode;
- }
- /**
- * | Value | Type | Description |
- * | ----- | ------------------ | ----------- |
- * | 0 | CLAMP_ADDRESSMODE | |
- * | 1 | WRAP_ADDRESSMODE | |
- * | 2 | MIRROR_ADDRESSMODE | |
- */
- @serialize()
- public get wrapU() {
- return this._wrapU;
- }
- public set wrapU(value: number) {
- this._wrapU = value;
- }
- /**
- * | Value | Type | Description |
- * | ----- | ------------------ | ----------- |
- * | 0 | CLAMP_ADDRESSMODE | |
- * | 1 | WRAP_ADDRESSMODE | |
- * | 2 | MIRROR_ADDRESSMODE | |
- */
- @serialize()
- public get wrapV() {
- return this._wrapV;
- }
- public set wrapV(value: number) {
- this._wrapV = value;
- }
- /**
- * | Value | Type | Description |
- * | ----- | ------------------ | ----------- |
- * | 0 | CLAMP_ADDRESSMODE | |
- * | 1 | WRAP_ADDRESSMODE | |
- * | 2 | MIRROR_ADDRESSMODE | |
- */
- @serialize()
- public wrapR = Constants.TEXTURE_WRAP_ADDRESSMODE;
- /**
- * With compliant hardware and browser (supporting anisotropic filtering)
- * this defines the level of anisotropic filtering in the texture.
- * The higher the better but the slower. This defaults to 4 as it seems to be the best tradeoff.
- */
- @serialize()
- public anisotropicFilteringLevel = BaseTexture.DEFAULT_ANISOTROPIC_FILTERING_LEVEL;
- private _isCube = false;
- /**
- * Define if the texture is a cube texture or if false a 2d texture.
- */
- @serialize()
- public get isCube(): boolean {
- if (!this._texture) {
- return this._isCube;
- }
- return this._texture.isCube;
- }
- public set isCube(value: boolean) {
- if (!this._texture) {
- this._isCube = value;
- } else {
- this._texture.isCube = value;
- }
- }
- /**
- * Define if the texture is a 3d texture (webgl 2) or if false a 2d texture.
- */
- @serialize()
- public get is3D(): boolean {
- if (!this._texture) {
- return false;
- }
- return this._texture.is3D;
- }
- public set is3D(value: boolean) {
- if (!this._texture) {
- return;
- }
- this._texture.is3D = value;
- }
- /**
- * Define if the texture is a 2d array texture (webgl 2) or if false a 2d texture.
- */
- @serialize()
- public get is2DArray(): boolean {
- if (!this._texture) {
- return false;
- }
- return this._texture.is2DArray;
- }
- public set is2DArray(value: boolean) {
- if (!this._texture) {
- return;
- }
- this._texture.is2DArray = value;
- }
- private _gammaSpace = true;
- /**
- * Define if the texture contains data in gamma space (most of the png/jpg aside bump).
- * HDR texture are usually stored in linear space.
- * This only impacts the PBR and Background materials
- */
- @serialize()
- public get gammaSpace(): boolean {
- if (!this._texture) {
- return this._gammaSpace;
- } else {
- if (this._texture._gammaSpace === null) {
- this._texture._gammaSpace = this._gammaSpace;
- }
- }
- return this._texture._gammaSpace;
- }
- public set gammaSpace(gamma: boolean) {
- if (!this._texture) {
- if (this._gammaSpace === gamma) {
- return;
- }
- this._gammaSpace = gamma;
- } else {
- if (this._texture._gammaSpace === gamma) {
- return;
- }
- this._texture._gammaSpace = gamma;
- }
- this._markAllSubMeshesAsTexturesDirty();
- }
- /**
- * Gets or sets whether or not the texture contains RGBD data.
- */
- public get isRGBD(): boolean {
- return this._texture != null && this._texture._isRGBD;
- }
- public set isRGBD(value: boolean) {
- if (this._texture) { this._texture._isRGBD = value; }
- }
- /**
- * Is Z inverted in the texture (useful in a cube texture).
- */
- @serialize()
- public invertZ = false;
- /**
- * Are mip maps generated for this texture or not.
- */
- public get noMipmap(): boolean {
- return false;
- }
- /**
- * @hidden
- */
- @serialize()
- public lodLevelInAlpha = false;
- /**
- * With prefiltered texture, defined the offset used during the prefiltering steps.
- */
- @serialize()
- public get lodGenerationOffset(): number {
- if (this._texture) { return this._texture._lodGenerationOffset; }
- return 0.0;
- }
- public set lodGenerationOffset(value: number) {
- if (this._texture) { this._texture._lodGenerationOffset = value; }
- }
- /**
- * With prefiltered texture, defined the scale used during the prefiltering steps.
- */
- @serialize()
- public get lodGenerationScale(): number {
- if (this._texture) { return this._texture._lodGenerationScale; }
- return 0.0;
- }
- public set lodGenerationScale(value: number) {
- if (this._texture) { this._texture._lodGenerationScale = value; }
- }
- /**
- * With prefiltered texture, defined if the specular generation is based on a linear ramp.
- * By default we are using a log2 of the linear roughness helping to keep a better resolution for
- * average roughness values.
- */
- @serialize()
- public get linearSpecularLOD(): boolean {
- if (this._texture) { return this._texture._linearSpecularLOD; }
- return false;
- }
- public set linearSpecularLOD(value: boolean) {
- if (this._texture) { this._texture._linearSpecularLOD = value; }
- }
- /**
- * In case a better definition than spherical harmonics is required for the diffuse part of the environment.
- * You can set the irradiance texture to rely on a texture instead of the spherical approach.
- * This texture need to have the same characteristics than its parent (Cube vs 2d, coordinates mode, Gamma/Linear, RGBD).
- */
- @serializeAsTexture()
- public get irradianceTexture(): Nullable<BaseTexture> {
- if (this._texture) { return this._texture._irradianceTexture; }
- return null;
- }
- public set irradianceTexture(value: Nullable<BaseTexture>) {
- if (this._texture) { this._texture._irradianceTexture = value; }
- }
- /**
- * Define if the texture is a render target.
- */
- @serialize()
- public isRenderTarget = false;
- /**
- * Define the unique id of the texture in the scene.
- */
- public get uid(): string {
- if (!this._uid) {
- this._uid = GUID.RandomId();
- }
- return this._uid;
- }
- /** @hidden */
- public _prefiltered: boolean = false;
- /**
- * Return a string representation of the texture.
- * @returns the texture as a string
- */
- public toString(): string {
- return this.name;
- }
- /**
- * Get the class name of the texture.
- * @returns "BaseTexture"
- */
- public getClassName(): string {
- return "BaseTexture";
- }
- /**
- * Define the list of animation attached to the texture.
- */
- public animations = new Array<Animation>();
- /**
- * An event triggered when the texture is disposed.
- */
- public onDisposeObservable = new Observable<BaseTexture>();
- private _onDisposeObserver: Nullable<Observer<BaseTexture>> = null;
- /**
- * Callback triggered when the texture has been disposed.
- * Kept for back compatibility, you can use the onDisposeObservable instead.
- */
- public set onDispose(callback: () => void) {
- if (this._onDisposeObserver) {
- this.onDisposeObservable.remove(this._onDisposeObserver);
- }
- this._onDisposeObserver = this.onDisposeObservable.add(callback);
- }
- protected _scene: Nullable<Scene> = null;
- /** @hidden */
- public _texture: Nullable<InternalTexture> = null;
- private _uid: Nullable<string> = null;
- /**
- * Define if the texture is preventinga material to render or not.
- * If not and the texture is not ready, the engine will use a default black texture instead.
- */
- public get isBlocking(): boolean {
- return true;
- }
- /**
- * Instantiates a new BaseTexture.
- * Base class of all the textures in babylon.
- * It groups all the common properties the materials, post process, lights... might need
- * in order to make a correct use of the texture.
- * @param sceneOrEngine Define the scene or engine the texture blongs to
- */
- constructor(sceneOrEngine: Nullable<Scene | ThinEngine>) {
- super(null);
- if (sceneOrEngine) {
- if (BaseTexture._isScene(sceneOrEngine)) {
- this._scene = sceneOrEngine;
- }
- else {
- this._engine = sceneOrEngine;
- }
- }
- else {
- this._scene = EngineStore.LastCreatedScene;
- }
- if (this._scene) {
- this.uniqueId = this._scene.getUniqueId();
- this._scene.addTexture(this);
- this._engine = this._scene.getEngine();
- }
- this._uid = null;
- }
- /**
- * Get the scene the texture belongs to.
- * @returns the scene or null if undefined
- */
- public getScene(): Nullable<Scene> {
- return this._scene;
- }
- /** @hidden */
- protected _getEngine(): Nullable<ThinEngine> {
- return this._engine;
- }
- /**
- * Checks if the texture has the same transform matrix than another texture
- * @param texture texture to check against
- * @returns true if the transforms are the same, else false
- */
- public checkTransformsAreIdentical(texture: Nullable<BaseTexture>): boolean {
- return texture !== null;
- }
- /**
- * Get the texture transform matrix used to offset tile the texture for istance.
- * @returns the transformation matrix
- */
- public getTextureMatrix(): Matrix {
- return <Matrix>Matrix.IdentityReadOnly;
- }
- /**
- * Get the texture reflection matrix used to rotate/transform the reflection.
- * @returns the reflection matrix
- */
- public getReflectionTextureMatrix(): Matrix {
- return <Matrix>Matrix.IdentityReadOnly;
- }
- /**
- * Get if the texture is ready to be consumed (either it is ready or it is not blocking)
- * @returns true if ready or not blocking
- */
- public isReadyOrNotBlocking(): boolean {
- return !this.isBlocking || this.isReady();
- }
- /**
- * Scales the texture if is `canRescale()`
- * @param ratio the resize factor we want to use to rescale
- */
- public scale(ratio: number): void {
- }
- /**
- * Get if the texture can rescale.
- */
- public get canRescale(): boolean {
- return false;
- }
- /** @hidden */
- public _getFromCache(url: Nullable<string>, noMipmap: boolean, sampling?: number, invertY?: boolean): Nullable<InternalTexture> {
- const engine = this._getEngine();
- if (!engine) {
- return null;
- }
- var texturesCache = engine.getLoadedTexturesCache();
- for (var index = 0; index < texturesCache.length; index++) {
- var texturesCacheEntry = texturesCache[index];
- if (invertY === undefined || invertY === texturesCacheEntry.invertY) {
- if (texturesCacheEntry.url === url && texturesCacheEntry.generateMipMaps === !noMipmap) {
- if (!sampling || sampling === texturesCacheEntry.samplingMode) {
- texturesCacheEntry.incrementReferences();
- return texturesCacheEntry;
- }
- }
- }
- }
- return null;
- }
- /** @hidden */
- public _rebuild(): void {
- }
- /**
- * Clones the texture.
- * @returns the cloned texture
- */
- public clone(): Nullable<BaseTexture> {
- return null;
- }
- /**
- * Get the texture underlying type (INT, FLOAT...)
- */
- public get textureType(): number {
- if (!this._texture) {
- return Constants.TEXTURETYPE_UNSIGNED_INT;
- }
- return (this._texture.type !== undefined) ? this._texture.type : Constants.TEXTURETYPE_UNSIGNED_INT;
- }
- /**
- * Get the texture underlying format (RGB, RGBA...)
- */
- public get textureFormat(): number {
- if (!this._texture) {
- return Constants.TEXTUREFORMAT_RGBA;
- }
- return (this._texture.format !== undefined) ? this._texture.format : Constants.TEXTUREFORMAT_RGBA;
- }
- /**
- * Indicates that textures need to be re-calculated for all materials
- */
- protected _markAllSubMeshesAsTexturesDirty() {
- let scene = this.getScene();
- if (!scene) {
- return;
- }
- scene.markAllMaterialsAsDirty(Constants.MATERIAL_TextureDirtyFlag);
- }
- /**
- * Reads the pixels stored in the webgl texture and returns them as an ArrayBuffer.
- * This will returns an RGBA array buffer containing either in values (0-255) or
- * float values (0-1) depending of the underlying buffer type.
- * @param faceIndex defines the face of the texture to read (in case of cube texture)
- * @param level defines the LOD level of the texture to read (in case of Mip Maps)
- * @param buffer defines a user defined buffer to fill with data (can be null)
- * @param flushRenderer true to flush the renderer from the pending commands before reading the pixels
- * @returns The Array buffer promise containing the pixels data.
- */
- public readPixels(faceIndex = 0, level = 0, buffer: Nullable<ArrayBufferView> = null, flushRenderer = true): Nullable<Promise<ArrayBufferView>> {
- if (!this._texture) {
- return null;
- }
- var size = this.getSize();
- var width = size.width;
- var height = size.height;
- const engine = this._getEngine();
- if (!engine) {
- return null;
- }
- if (level != 0) {
- width = width / Math.pow(2, level);
- height = height / Math.pow(2, level);
- width = Math.round(width);
- height = Math.round(height);
- }
- try {
- if (this._texture.isCube) {
- return engine._readTexturePixels(this._texture, width, height, faceIndex, level, buffer, flushRenderer);
- }
- return engine._readTexturePixels(this._texture, width, height, -1, level, buffer, flushRenderer);
- } catch (e) {
- return null;
- }
- }
- /** @hidden */
- public _readPixelsSync(faceIndex = 0, level = 0, buffer: Nullable<ArrayBufferView> = null, flushRenderer = true): Nullable<ArrayBufferView> {
- if (!this._texture) {
- return null;
- }
- var size = this.getSize();
- var width = size.width;
- var height = size.height;
- const engine = this._getEngine();
- if (!engine) {
- return null;
- }
- if (level != 0) {
- width = width / Math.pow(2, level);
- height = height / Math.pow(2, level);
- width = Math.round(width);
- height = Math.round(height);
- }
- try {
- if (this._texture.isCube) {
- return engine._readTexturePixelsSync(this._texture, width, height, faceIndex, level, buffer, flushRenderer);
- }
- return engine._readTexturePixelsSync(this._texture, width, height, -1, level, buffer, flushRenderer);
- } catch (e) {
- return null;
- }
- }
- /** @hidden */
- public get _lodTextureHigh(): Nullable<BaseTexture> {
- if (this._texture) {
- return this._texture._lodTextureHigh;
- }
- return null;
- }
- /** @hidden */
- public get _lodTextureMid(): Nullable<BaseTexture> {
- if (this._texture) {
- return this._texture._lodTextureMid;
- }
- return null;
- }
- /** @hidden */
- public get _lodTextureLow(): Nullable<BaseTexture> {
- if (this._texture) {
- return this._texture._lodTextureLow;
- }
- return null;
- }
- /**
- * Dispose the texture and release its associated resources.
- */
- public dispose(): void {
- if (this._scene) {
- // Animations
- if (this._scene.stopAnimation) {
- this._scene.stopAnimation(this);
- }
- // Remove from scene
- this._scene._removePendingData(this);
- var index = this._scene.textures.indexOf(this);
- if (index >= 0) {
- this._scene.textures.splice(index, 1);
- }
- this._scene.onTextureRemovedObservable.notifyObservers(this);
- this._scene = null;
- }
- // Callback
- this.onDisposeObservable.notifyObservers(this);
- this.onDisposeObservable.clear();
- super.dispose();
- }
- /**
- * Serialize the texture into a JSON representation that can be parsed later on.
- * @returns the JSON representation of the texture
- */
- public serialize(): any {
- if (!this.name) {
- return null;
- }
- var serializationObject = SerializationHelper.Serialize(this);
- // Animations
- SerializationHelper.AppendSerializedAnimations(this, serializationObject);
- return serializationObject;
- }
- /**
- * Helper function to be called back once a list of texture contains only ready textures.
- * @param textures Define the list of textures to wait for
- * @param callback Define the callback triggered once the entire list will be ready
- */
- public static WhenAllReady(textures: BaseTexture[], callback: () => void): void {
- let numRemaining = textures.length;
- if (numRemaining === 0) {
- callback();
- return;
- }
- for (var i = 0; i < textures.length; i++) {
- var texture = textures[i];
- if (texture.isReady()) {
- if (--numRemaining === 0) {
- callback();
- }
- }
- else {
- var onLoadObservable = (texture as any).onLoadObservable as Observable<BaseTexture>;
- if (onLoadObservable) {
- onLoadObservable.addOnce(() => {
- if (--numRemaining === 0) {
- callback();
- }
- });
- }
- }
- }
- }
- private static _isScene(sceneOrEngine: Scene | ThinEngine): sceneOrEngine is Scene {
- return sceneOrEngine.getClassName() === "Scene";
- }
- }
|