/
thinTexture.ts
305 lines (260 loc) · 9.54 KB
/
thinTexture.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
import type { Nullable } from "../../types";
import type { InternalTexture } from "../../Materials/Textures/internalTexture";
import { Constants } from "../../Engines/constants";
import type { ISize } from "../../Maths/math.size";
import { Size } from "../../Maths/math.size";
import type { ThinEngine } from "../../Engines/thinEngine";
import type { RenderTargetWrapper } from "core/Engines/renderTargetWrapper";
/**
* Base class of all the textures in babylon.
* It groups all the common properties required to work with Thin Engine.
*/
export class ThinTexture {
protected _wrapU = Constants.TEXTURE_WRAP_ADDRESSMODE;
/**
* | Value | Type | Description |
* | ----- | ------------------ | ----------- |
* | 0 | CLAMP_ADDRESSMODE | |
* | 1 | WRAP_ADDRESSMODE | |
* | 2 | MIRROR_ADDRESSMODE | |
*/
public get wrapU() {
return this._wrapU;
}
public set wrapU(value: number) {
this._wrapU = value;
}
protected _wrapV = Constants.TEXTURE_WRAP_ADDRESSMODE;
/**
* | Value | Type | Description |
* | ----- | ------------------ | ----------- |
* | 0 | CLAMP_ADDRESSMODE | |
* | 1 | WRAP_ADDRESSMODE | |
* | 2 | MIRROR_ADDRESSMODE | |
*/
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 | |
*/
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.
*/
public anisotropicFilteringLevel = 4;
/**
* Define the current state of the loading sequence when in delayed load mode.
*/
public delayLoadState = Constants.DELAYLOADSTATE_NONE;
/**
* How a texture is mapped.
* Unused in thin texture mode.
*/
public get coordinatesMode(): number {
return 0;
}
/**
* Define if the texture is a cube texture or if false a 2d texture.
*/
public get isCube(): boolean {
if (!this._texture) {
return false;
}
return this._texture.isCube;
}
protected set isCube(value: boolean) {
if (!this._texture) {
return;
}
this._texture.isCube = value;
}
/**
* Define if the texture is a 3d texture (webgl 2) or if false a 2d texture.
*/
public get is3D(): boolean {
if (!this._texture) {
return false;
}
return this._texture.is3D;
}
protected 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.
*/
public get is2DArray(): boolean {
if (!this._texture) {
return false;
}
return this._texture.is2DArray;
}
protected set is2DArray(value: boolean) {
if (!this._texture) {
return;
}
this._texture.is2DArray = value;
}
/**
* Get the class name of the texture.
* @returns "ThinTexture"
*/
public getClassName(): string {
return "ThinTexture";
}
/** @internal */
public _texture: Nullable<InternalTexture> = null;
protected _engine: Nullable<ThinEngine> = null;
private _cachedSize: ISize = Size.Zero();
private _cachedBaseSize: ISize = Size.Zero();
private static _IsRenderTargetWrapper(texture: Nullable<InternalTexture> | Nullable<RenderTargetWrapper>): texture is RenderTargetWrapper {
return (texture as RenderTargetWrapper)?._shareDepth !== undefined;
}
/**
* Instantiates a new ThinTexture.
* Base class of all the textures in babylon.
* This can be used as an internal texture wrapper in ThinEngine to benefit from the cache
* @param internalTexture Define the internalTexture to wrap. You can also pass a RenderTargetWrapper, in which case the texture will be the render target's texture
*/
constructor(internalTexture: Nullable<InternalTexture | RenderTargetWrapper>) {
this._texture = ThinTexture._IsRenderTargetWrapper(internalTexture) ? internalTexture.texture : internalTexture;
if (this._texture) {
this._engine = this._texture.getEngine();
}
}
/**
* Get if the texture is ready to be used (downloaded, converted, mip mapped...).
* @returns true if fully ready
*/
public isReady(): boolean {
if (this.delayLoadState === Constants.DELAYLOADSTATE_NOTLOADED) {
this.delayLoad();
return false;
}
if (this._texture) {
return this._texture.isReady;
}
return false;
}
/**
* Triggers the load sequence in delayed load mode.
*/
public delayLoad(): void {}
/**
* Get the underlying lower level texture from Babylon.
* @returns the internal texture
*/
public getInternalTexture(): Nullable<InternalTexture> {
return this._texture;
}
/**
* Get the size of the texture.
* @returns the texture size.
*/
public getSize(): ISize {
if (this._texture) {
if (this._texture.width) {
this._cachedSize.width = this._texture.width;
this._cachedSize.height = this._texture.height;
return this._cachedSize;
}
if (this._texture._size) {
this._cachedSize.width = this._texture._size;
this._cachedSize.height = this._texture._size;
return this._cachedSize;
}
}
return this._cachedSize;
}
/**
* Get the base size of the texture.
* It can be different from the size if the texture has been resized for POT for instance
* @returns the base size
*/
public getBaseSize(): ISize {
if (!this.isReady() || !this._texture) {
this._cachedBaseSize.width = 0;
this._cachedBaseSize.height = 0;
return this._cachedBaseSize;
}
if (this._texture._size) {
this._cachedBaseSize.width = this._texture._size;
this._cachedBaseSize.height = this._texture._size;
return this._cachedBaseSize;
}
this._cachedBaseSize.width = this._texture.baseWidth;
this._cachedBaseSize.height = this._texture.baseHeight;
return this._cachedBaseSize;
}
/** @internal */
protected _initialSamplingMode = Constants.TEXTURE_BILINEAR_SAMPLINGMODE;
/**
* Get the current sampling mode associated with the texture.
*/
public get samplingMode(): number {
if (!this._texture) {
return this._initialSamplingMode;
}
return this._texture.samplingMode;
}
/**
* Update the sampling mode of the texture.
* Default is Trilinear mode.
*
* | Value | Type | Description |
* | ----- | ------------------ | ----------- |
* | 1 | NEAREST_SAMPLINGMODE or NEAREST_NEAREST_MIPLINEAR | Nearest is: mag = nearest, min = nearest, mip = linear |
* | 2 | BILINEAR_SAMPLINGMODE or LINEAR_LINEAR_MIPNEAREST | Bilinear is: mag = linear, min = linear, mip = nearest |
* | 3 | TRILINEAR_SAMPLINGMODE or LINEAR_LINEAR_MIPLINEAR | Trilinear is: mag = linear, min = linear, mip = linear |
* | 4 | NEAREST_NEAREST_MIPNEAREST | |
* | 5 | NEAREST_LINEAR_MIPNEAREST | |
* | 6 | NEAREST_LINEAR_MIPLINEAR | |
* | 7 | NEAREST_LINEAR | |
* | 8 | NEAREST_NEAREST | |
* | 9 | LINEAR_NEAREST_MIPNEAREST | |
* | 10 | LINEAR_NEAREST_MIPLINEAR | |
* | 11 | LINEAR_LINEAR | |
* | 12 | LINEAR_NEAREST | |
*
* > _mag_: magnification filter (close to the viewer)
* > _min_: minification filter (far from the viewer)
* > _mip_: filter used between mip map levels
*@param samplingMode Define the new sampling mode of the texture
*/
public updateSamplingMode(samplingMode: number): void {
if (this._texture && this._engine) {
this._engine.updateTextureSamplingMode(samplingMode, this._texture);
}
}
/**
* Release and destroy the underlying lower level texture aka internalTexture.
*/
public releaseInternalTexture(): void {
if (this._texture) {
this._texture.dispose();
this._texture = null;
}
}
/**
* Dispose the texture and release its associated resources.
*/
public dispose(): void {
if (this._texture) {
this.releaseInternalTexture();
this._engine = null;
}
}
}