/
texture.ts
1191 lines (1052 loc) · 52 KB
/
texture.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
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import { serialize, SerializationHelper } from "../../Misc/decorators";
import { Observable } from "../../Misc/observable";
import type { Nullable } from "../../types";
import { Matrix, TmpVectors, Vector3 } from "../../Maths/math.vector";
import { BaseTexture } from "../../Materials/Textures/baseTexture";
import { Constants } from "../../Engines/constants";
import { GetClass, RegisterClass } from "../../Misc/typeStore";
import { _WarnImport } from "../../Misc/devTools";
import type { IInspectable } from "../../Misc/iInspectable";
import type { ThinEngine } from "../../Engines/thinEngine";
import { TimingTools } from "../../Misc/timingTools";
import { InstantiationTools } from "../../Misc/instantiationTools";
import { Plane } from "../../Maths/math.plane";
import { EncodeArrayBufferToBase64 } from "../../Misc/stringTools";
import { GenerateBase64StringFromTexture, GenerateBase64StringFromTextureAsync } from "../../Misc/copyTools";
import { CompatibilityOptions } from "../../Compat/compatibilityOptions";
import type { InternalTexture } from "./internalTexture";
import type { CubeTexture } from "../../Materials/Textures/cubeTexture";
import type { MirrorTexture } from "../../Materials/Textures/mirrorTexture";
import type { RenderTargetTexture } from "../../Materials/Textures/renderTargetTexture";
import type { Scene } from "../../scene";
import type { VideoTexture, VideoTextureSettings } from "./videoTexture";
/**
* Defines the available options when creating a texture
*/
export interface ITextureCreationOptions {
/** Defines if the texture will require mip maps or not (default: false) */
noMipmap?: boolean;
/** Defines if the texture needs to be inverted on the y axis during loading (default: true) */
invertY?: boolean;
/** Defines the sampling mode we want for the texture while fetching from it (Texture.NEAREST_SAMPLINGMODE...) (default: Texture.TRILINEAR_SAMPLINGMODE) */
samplingMode?: number;
/** Defines a callback triggered when the texture has been loaded (default: null) */
onLoad?: Nullable<() => void>;
/** Defines a callback triggered when an error occurred during the loading session (default: null) */
onError?: Nullable<(message?: string, exception?: any) => void>;
/** Defines the buffer to load the texture from in case the texture is loaded from a buffer representation (default: null) */
buffer?: Nullable<string | ArrayBuffer | ArrayBufferView | HTMLImageElement | Blob | ImageBitmap>;
/** Defines if the buffer we are loading the texture from should be deleted after load (default: false) */
deleteBuffer?: boolean;
/** Defines the format of the texture we are trying to load (Engine.TEXTUREFORMAT_RGBA...) (default: ) */
format?: number;
/** Defines an optional mime type information (default: undefined) */
mimeType?: string;
/** Options to be passed to the loader (default: undefined) */
loaderOptions?: any;
/** Specific flags to use when creating the texture (Constants.TEXTURE_CREATIONFLAG_STORAGE for storage textures, for eg) (default: undefined) */
creationFlags?: number;
/** Defines if the texture must be loaded in a sRGB GPU buffer (if supported by the GPU) (default: false) */
useSRGBBuffer?: boolean;
/** Defines the underlying texture from an already existing one */
internalTexture?: InternalTexture;
/** Defines the underlying texture texture space */
gammaSpace?: boolean;
}
/**
* This represents a texture in babylon. It can be easily loaded from a network, base64 or html input.
* @see https://doc.babylonjs.com/features/featuresDeepDive/materials/using/materials_introduction#texture
*/
export class Texture extends BaseTexture {
/**
* Gets or sets a general boolean used to indicate that textures containing direct data (buffers) must be saved as part of the serialization process
*/
public static SerializeBuffers = true;
/**
* Gets or sets a general boolean used to indicate that texture buffers must be saved as part of the serialization process.
* If no buffer exists, one will be created as base64 string from the internal webgl data.
*/
public static ForceSerializeBuffers = false;
/**
* This observable will notify when any texture had a loading error
*/
public static OnTextureLoadErrorObservable = new Observable<BaseTexture>();
/** @internal */
public static _SerializeInternalTextureUniqueId = false;
/**
* @internal
*/
// eslint-disable-next-line @typescript-eslint/no-unused-vars
public static _CubeTextureParser = (jsonTexture: any, scene: Scene, rootUrl: string): CubeTexture => {
throw _WarnImport("CubeTexture");
};
/**
* @internal
*/
// eslint-disable-next-line @typescript-eslint/no-unused-vars
public static _CreateMirror = (name: string, renderTargetSize: number, scene: Scene, generateMipMaps: boolean): MirrorTexture => {
throw _WarnImport("MirrorTexture");
};
/**
* @internal
*/
// eslint-disable-next-line @typescript-eslint/no-unused-vars
public static _CreateRenderTargetTexture = (name: string, renderTargetSize: number, scene: Scene, generateMipMaps: boolean, creationFlags?: number): RenderTargetTexture => {
throw _WarnImport("RenderTargetTexture");
};
public static _CreateVideoTexture(
name: Nullable<string>,
src: string | string[] | HTMLVideoElement,
scene: Nullable<Scene>,
generateMipMaps = false,
invertY = false,
samplingMode: number = Texture.TRILINEAR_SAMPLINGMODE,
settings: Partial<VideoTextureSettings> = {},
onError?: Nullable<(message?: string, exception?: any) => void>,
format: number = Constants.TEXTUREFORMAT_RGBA
): VideoTexture {
throw _WarnImport("VideoTexture");
}
/** nearest is mag = nearest and min = nearest and no mip */
public static readonly NEAREST_SAMPLINGMODE = Constants.TEXTURE_NEAREST_SAMPLINGMODE;
/** nearest is mag = nearest and min = nearest and mip = linear */
public static readonly NEAREST_NEAREST_MIPLINEAR = Constants.TEXTURE_NEAREST_NEAREST_MIPLINEAR; // nearest is mag = nearest and min = nearest and mip = linear
/** Bilinear is mag = linear and min = linear and no mip */
public static readonly BILINEAR_SAMPLINGMODE = Constants.TEXTURE_BILINEAR_SAMPLINGMODE;
/** Bilinear is mag = linear and min = linear and mip = nearest */
public static readonly LINEAR_LINEAR_MIPNEAREST = Constants.TEXTURE_LINEAR_LINEAR_MIPNEAREST; // Bilinear is mag = linear and min = linear and mip = nearest
/** Trilinear is mag = linear and min = linear and mip = linear */
public static readonly TRILINEAR_SAMPLINGMODE = Constants.TEXTURE_TRILINEAR_SAMPLINGMODE;
/** Trilinear is mag = linear and min = linear and mip = linear */
public static readonly LINEAR_LINEAR_MIPLINEAR = Constants.TEXTURE_LINEAR_LINEAR_MIPLINEAR; // Trilinear is mag = linear and min = linear and mip = linear
/** mag = nearest and min = nearest and mip = nearest */
public static readonly NEAREST_NEAREST_MIPNEAREST = Constants.TEXTURE_NEAREST_NEAREST_MIPNEAREST;
/** mag = nearest and min = linear and mip = nearest */
public static readonly NEAREST_LINEAR_MIPNEAREST = Constants.TEXTURE_NEAREST_LINEAR_MIPNEAREST;
/** mag = nearest and min = linear and mip = linear */
public static readonly NEAREST_LINEAR_MIPLINEAR = Constants.TEXTURE_NEAREST_LINEAR_MIPLINEAR;
/** mag = nearest and min = linear and mip = none */
public static readonly NEAREST_LINEAR = Constants.TEXTURE_NEAREST_LINEAR;
/** mag = nearest and min = nearest and mip = none */
public static readonly NEAREST_NEAREST = Constants.TEXTURE_NEAREST_NEAREST;
/** mag = linear and min = nearest and mip = nearest */
public static readonly LINEAR_NEAREST_MIPNEAREST = Constants.TEXTURE_LINEAR_NEAREST_MIPNEAREST;
/** mag = linear and min = nearest and mip = linear */
public static readonly LINEAR_NEAREST_MIPLINEAR = Constants.TEXTURE_LINEAR_NEAREST_MIPLINEAR;
/** mag = linear and min = linear and mip = none */
public static readonly LINEAR_LINEAR = Constants.TEXTURE_LINEAR_LINEAR;
/** mag = linear and min = nearest and mip = none */
public static readonly LINEAR_NEAREST = Constants.TEXTURE_LINEAR_NEAREST;
/** Explicit coordinates mode */
public static readonly EXPLICIT_MODE = Constants.TEXTURE_EXPLICIT_MODE;
/** Spherical coordinates mode */
public static readonly SPHERICAL_MODE = Constants.TEXTURE_SPHERICAL_MODE;
/** Planar coordinates mode */
public static readonly PLANAR_MODE = Constants.TEXTURE_PLANAR_MODE;
/** Cubic coordinates mode */
public static readonly CUBIC_MODE = Constants.TEXTURE_CUBIC_MODE;
/** Projection coordinates mode */
public static readonly PROJECTION_MODE = Constants.TEXTURE_PROJECTION_MODE;
/** Inverse Cubic coordinates mode */
public static readonly SKYBOX_MODE = Constants.TEXTURE_SKYBOX_MODE;
/** Inverse Cubic coordinates mode */
public static readonly INVCUBIC_MODE = Constants.TEXTURE_INVCUBIC_MODE;
/** Equirectangular coordinates mode */
public static readonly EQUIRECTANGULAR_MODE = Constants.TEXTURE_EQUIRECTANGULAR_MODE;
/** Equirectangular Fixed coordinates mode */
public static readonly FIXED_EQUIRECTANGULAR_MODE = Constants.TEXTURE_FIXED_EQUIRECTANGULAR_MODE;
/** Equirectangular Fixed Mirrored coordinates mode */
public static readonly FIXED_EQUIRECTANGULAR_MIRRORED_MODE = Constants.TEXTURE_FIXED_EQUIRECTANGULAR_MIRRORED_MODE;
/** Texture is not repeating outside of 0..1 UVs */
public static readonly CLAMP_ADDRESSMODE = Constants.TEXTURE_CLAMP_ADDRESSMODE;
/** Texture is repeating outside of 0..1 UVs */
public static readonly WRAP_ADDRESSMODE = Constants.TEXTURE_WRAP_ADDRESSMODE;
/** Texture is repeating and mirrored */
public static readonly MIRROR_ADDRESSMODE = Constants.TEXTURE_MIRROR_ADDRESSMODE;
/**
* 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
*/
public static UseSerializedUrlIfAny = false;
/**
* Define the url of the texture.
*/
@serialize()
public url: Nullable<string> = null;
/**
* Define an offset on the texture to offset the u coordinates of the UVs
* @see https://doc.babylonjs.com/features/featuresDeepDive/materials/using/moreMaterials#offsetting
*/
@serialize()
public uOffset = 0;
/**
* Define an offset on the texture to offset the v coordinates of the UVs
* @see https://doc.babylonjs.com/features/featuresDeepDive/materials/using/moreMaterials#offsetting
*/
@serialize()
public vOffset = 0;
/**
* Define an offset on the texture to scale the u coordinates of the UVs
* @see https://doc.babylonjs.com/features/featuresDeepDive/materials/using/moreMaterials#tiling
*/
@serialize()
public uScale = 1.0;
/**
* Define an offset on the texture to scale the v coordinates of the UVs
* @see https://doc.babylonjs.com/features/featuresDeepDive/materials/using/moreMaterials#tiling
*/
@serialize()
public vScale = 1.0;
/**
* Define an offset on the texture to rotate around the u coordinates of the UVs
* The angle is defined in radians.
* @see https://doc.babylonjs.com/features/featuresDeepDive/materials/using/moreMaterials
*/
@serialize()
public uAng = 0;
/**
* Define an offset on the texture to rotate around the v coordinates of the UVs
* The angle is defined in radians.
* @see https://doc.babylonjs.com/features/featuresDeepDive/materials/using/moreMaterials
*/
@serialize()
public vAng = 0;
/**
* Define an offset on the texture to rotate around the w coordinates of the UVs (in case of 3d texture)
* The angle is defined in radians.
* @see https://doc.babylonjs.com/features/featuresDeepDive/materials/using/moreMaterials
*/
@serialize()
public wAng = 0;
/**
* Defines the center of rotation (U)
*/
@serialize()
public uRotationCenter = 0.5;
/**
* Defines the center of rotation (V)
*/
@serialize()
public vRotationCenter = 0.5;
/**
* Defines the center of rotation (W)
*/
@serialize()
public wRotationCenter = 0.5;
/**
* Sets this property to true to avoid deformations when rotating the texture with non-uniform scaling
*/
@serialize()
public homogeneousRotationInUVTransform = false;
/**
* Are mip maps generated for this texture or not.
*/
get noMipmap(): boolean {
return this._noMipmap;
}
/**
* List of inspectable custom properties (used by the Inspector)
* @see https://doc.babylonjs.com/toolsAndResources/inspector#extensibility
*/
public inspectableCustomProperties: Nullable<IInspectable[]> = null;
/** @internal */
public _noMipmap: boolean = false;
/** @internal */
public _invertY: boolean = false;
private _rowGenerationMatrix: Nullable<Matrix> = null;
private _cachedTextureMatrix: Nullable<Matrix> = null;
private _projectionModeMatrix: Nullable<Matrix> = null;
private _t0: Nullable<Vector3> = null;
private _t1: Nullable<Vector3> = null;
private _t2: Nullable<Vector3> = null;
private _cachedUOffset: number = -1;
private _cachedVOffset: number = -1;
private _cachedUScale: number = 0;
private _cachedVScale: number = 0;
private _cachedUAng: number = -1;
private _cachedVAng: number = -1;
private _cachedWAng: number = -1;
private _cachedReflectionProjectionMatrixId: number = -1;
private _cachedURotationCenter: number = -1;
private _cachedVRotationCenter: number = -1;
private _cachedWRotationCenter: number = -1;
private _cachedHomogeneousRotationInUVTransform: boolean = false;
private _cachedReflectionTextureMatrix: Nullable<Matrix> = null;
private _cachedReflectionUOffset = -1;
private _cachedReflectionVOffset = -1;
private _cachedReflectionUScale = 0;
private _cachedReflectionVScale = 0;
private _cachedReflectionCoordinatesMode = -1;
/** @internal */
public _buffer: Nullable<string | ArrayBuffer | ArrayBufferView | HTMLImageElement | Blob | ImageBitmap> = null;
private _deleteBuffer: boolean = false;
protected _format: Nullable<number> = null;
private _delayedOnLoad: Nullable<() => void> = null;
private _delayedOnError: Nullable<() => void> = null;
private _mimeType?: string;
private _loaderOptions?: any;
private _creationFlags?: number;
/** @internal */
public _useSRGBBuffer?: boolean;
private _forcedExtension?: string;
/** Returns the texture mime type if it was defined by a loader (undefined else) */
public get mimeType() {
return this._mimeType;
}
/**
* Observable triggered once the texture has been loaded.
*/
public onLoadObservable: Observable<Texture> = new Observable<Texture>();
protected _isBlocking: boolean = true;
/**
* Is the texture preventing material to render while loading.
* If false, a default texture will be used instead of the loading one during the preparation step.
*/
public set isBlocking(value: boolean) {
this._isBlocking = value;
}
@serialize()
public get isBlocking(): boolean {
return this._isBlocking;
}
/**
* Gets a boolean indicating if the texture needs to be inverted on the y axis during loading
*/
public get invertY(): boolean {
return this._invertY;
}
/**
* Instantiates a new texture.
* This represents a texture in babylon. It can be easily loaded from a network, base64 or html input.
* @see https://doc.babylonjs.com/features/featuresDeepDive/materials/using/materials_introduction#texture
* @param url defines the url of the picture to load as a texture
* @param sceneOrEngine defines the scene or engine the texture will belong to
* @param noMipmapOrOptions defines if the texture will require mip maps or not or set of all options to create the texture
* @param invertY defines if the texture needs to be inverted on the y axis during loading
* @param samplingMode defines the sampling mode we want for the texture while fetching from it (Texture.NEAREST_SAMPLINGMODE...)
* @param onLoad defines a callback triggered when the texture has been loaded
* @param onError defines a callback triggered when an error occurred during the loading session
* @param buffer defines the buffer to load the texture from in case the texture is loaded from a buffer representation
* @param deleteBuffer defines if the buffer we are loading the texture from should be deleted after load
* @param format defines the format of the texture we are trying to load (Engine.TEXTUREFORMAT_RGBA...)
* @param mimeType defines an optional mime type information
* @param loaderOptions options to be passed to the loader
* @param creationFlags specific flags to use when creating the texture (Constants.TEXTURE_CREATIONFLAG_STORAGE for storage textures, for eg)
* @param forcedExtension defines the extension to use to pick the right loader
*/
constructor(
url: Nullable<string>,
sceneOrEngine?: Nullable<Scene | ThinEngine>,
noMipmapOrOptions?: boolean | ITextureCreationOptions,
invertY?: boolean,
samplingMode: number = Texture.TRILINEAR_SAMPLINGMODE,
onLoad: Nullable<() => void> = null,
onError: Nullable<(message?: string, exception?: any) => void> = null,
buffer: Nullable<string | ArrayBuffer | ArrayBufferView | HTMLImageElement | Blob | ImageBitmap> = null,
deleteBuffer: boolean = false,
format?: number,
mimeType?: string,
loaderOptions?: any,
creationFlags?: number,
forcedExtension?: string
) {
super(sceneOrEngine);
this.name = url || "";
this.url = url;
let noMipmap: boolean;
let useSRGBBuffer: boolean = false;
let internalTexture: Nullable<InternalTexture> = null;
let gammaSpace = true;
if (typeof noMipmapOrOptions === "object" && noMipmapOrOptions !== null) {
noMipmap = noMipmapOrOptions.noMipmap ?? false;
invertY = noMipmapOrOptions.invertY ?? (CompatibilityOptions.UseOpenGLOrientationForUV ? false : true);
samplingMode = noMipmapOrOptions.samplingMode ?? Texture.TRILINEAR_SAMPLINGMODE;
onLoad = noMipmapOrOptions.onLoad ?? null;
onError = noMipmapOrOptions.onError ?? null;
buffer = noMipmapOrOptions.buffer ?? null;
deleteBuffer = noMipmapOrOptions.deleteBuffer ?? false;
format = noMipmapOrOptions.format;
mimeType = noMipmapOrOptions.mimeType;
loaderOptions = noMipmapOrOptions.loaderOptions;
creationFlags = noMipmapOrOptions.creationFlags;
useSRGBBuffer = noMipmapOrOptions.useSRGBBuffer ?? false;
internalTexture = noMipmapOrOptions.internalTexture ?? null;
gammaSpace = noMipmapOrOptions.gammaSpace ?? gammaSpace;
} else {
noMipmap = !!noMipmapOrOptions;
}
this._gammaSpace = gammaSpace;
this._noMipmap = noMipmap;
this._invertY = invertY === undefined ? (CompatibilityOptions.UseOpenGLOrientationForUV ? false : true) : invertY;
this._initialSamplingMode = samplingMode;
this._buffer = buffer;
this._deleteBuffer = deleteBuffer;
this._mimeType = mimeType;
this._loaderOptions = loaderOptions;
this._creationFlags = creationFlags;
this._useSRGBBuffer = useSRGBBuffer;
this._forcedExtension = forcedExtension;
if (format) {
this._format = format;
}
const scene = this.getScene();
const engine = this._getEngine();
if (!engine) {
return;
}
engine.onBeforeTextureInitObservable.notifyObservers(this);
const load = () => {
if (this._texture) {
if (this._texture._invertVScale) {
this.vScale *= -1;
this.vOffset += 1;
}
// Update texture to match internal texture's wrapping
if (this._texture._cachedWrapU !== null) {
this.wrapU = this._texture._cachedWrapU;
this._texture._cachedWrapU = null;
}
if (this._texture._cachedWrapV !== null) {
this.wrapV = this._texture._cachedWrapV;
this._texture._cachedWrapV = null;
}
if (this._texture._cachedWrapR !== null) {
this.wrapR = this._texture._cachedWrapR;
this._texture._cachedWrapR = null;
}
}
if (this.onLoadObservable.hasObservers()) {
this.onLoadObservable.notifyObservers(this);
}
if (onLoad) {
onLoad();
}
if (!this.isBlocking && scene) {
scene.resetCachedMaterial();
}
};
const errorHandler = (message?: string, exception?: any) => {
this._loadingError = true;
this._errorObject = { message, exception };
if (onError) {
onError(message, exception);
}
Texture.OnTextureLoadErrorObservable.notifyObservers(this);
};
if (!this.url && !internalTexture) {
this._delayedOnLoad = load;
this._delayedOnError = errorHandler;
return;
}
this._texture = internalTexture ?? this._getFromCache(this.url, noMipmap, samplingMode, this._invertY, useSRGBBuffer, this.isCube);
if (!this._texture) {
if (!scene || !scene.useDelayedTextureLoading) {
try {
this._texture = engine.createTexture(
this.url,
noMipmap,
this._invertY,
scene,
samplingMode,
load,
errorHandler,
this._buffer,
undefined,
this._format,
this._forcedExtension,
mimeType,
loaderOptions,
creationFlags,
useSRGBBuffer
);
} catch (e) {
errorHandler("error loading", e);
throw e;
}
if (deleteBuffer) {
this._buffer = null;
}
} else {
this.delayLoadState = Constants.DELAYLOADSTATE_NOTLOADED;
this._delayedOnLoad = load;
this._delayedOnError = errorHandler;
}
} else {
if (this._texture.isReady) {
TimingTools.SetImmediate(() => load());
} else {
const loadObserver = this._texture.onLoadedObservable.add(load);
this._texture.onErrorObservable.add((e) => {
errorHandler(e.message, e.exception);
this._texture?.onLoadedObservable.remove(loadObserver);
});
}
}
}
/**
* Update the url (and optional buffer) of this texture if url was null during construction.
* @param url the url of the texture
* @param buffer the buffer of the texture (defaults to null)
* @param onLoad callback called when the texture is loaded (defaults to null)
* @param forcedExtension defines the extension to use to pick the right loader
*/
public updateURL(
url: string,
buffer: Nullable<string | ArrayBuffer | ArrayBufferView | HTMLImageElement | Blob | ImageBitmap> = null,
onLoad?: () => void,
forcedExtension?: string
): void {
if (this.url) {
this.releaseInternalTexture();
this.getScene()!.markAllMaterialsAsDirty(Constants.MATERIAL_TextureDirtyFlag, (mat) => {
return mat.hasTexture(this);
});
}
if (!this.name || this.name.startsWith("data:")) {
this.name = url;
}
this.url = url;
this._buffer = buffer;
this._forcedExtension = forcedExtension;
this.delayLoadState = Constants.DELAYLOADSTATE_NOTLOADED;
if (onLoad) {
this._delayedOnLoad = onLoad;
}
this.delayLoad();
}
/**
* Finish the loading sequence of a texture flagged as delayed load.
* @internal
*/
public delayLoad(): void {
if (this.delayLoadState !== Constants.DELAYLOADSTATE_NOTLOADED) {
return;
}
const scene = this.getScene();
if (!scene) {
return;
}
this.delayLoadState = Constants.DELAYLOADSTATE_LOADED;
this._texture = this._getFromCache(this.url, this._noMipmap, this.samplingMode, this._invertY, this._useSRGBBuffer, this.isCube);
if (!this._texture) {
this._texture = scene
.getEngine()
.createTexture(
this.url,
this._noMipmap,
this._invertY,
scene,
this.samplingMode,
this._delayedOnLoad,
this._delayedOnError,
this._buffer,
null,
this._format,
this._forcedExtension,
this._mimeType,
this._loaderOptions,
this._creationFlags,
this._useSRGBBuffer
);
if (this._deleteBuffer) {
this._buffer = null;
}
} else {
if (this._delayedOnLoad) {
if (this._texture.isReady) {
TimingTools.SetImmediate(this._delayedOnLoad);
} else {
this._texture.onLoadedObservable.add(this._delayedOnLoad);
}
}
}
this._delayedOnLoad = null;
this._delayedOnError = null;
}
private _prepareRowForTextureGeneration(x: number, y: number, z: number, t: Vector3): void {
x *= this._cachedUScale;
y *= this._cachedVScale;
x -= this.uRotationCenter * this._cachedUScale;
y -= this.vRotationCenter * this._cachedVScale;
z -= this.wRotationCenter;
Vector3.TransformCoordinatesFromFloatsToRef(x, y, z, this._rowGenerationMatrix!, t);
t.x += this.uRotationCenter * this._cachedUScale + this._cachedUOffset;
t.y += this.vRotationCenter * this._cachedVScale + this._cachedVOffset;
t.z += this.wRotationCenter;
}
/**
* 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<Texture>): boolean {
return (
texture !== null &&
this.uOffset === texture.uOffset &&
this.vOffset === texture.vOffset &&
this.uScale === texture.uScale &&
this.vScale === texture.vScale &&
this.uAng === texture.uAng &&
this.vAng === texture.vAng &&
this.wAng === texture.wAng
);
}
/**
* Get the current texture matrix which includes the requested offsetting, tiling and rotation components.
* @param uBase
* @returns the transform matrix of the texture.
*/
public getTextureMatrix(uBase = 1): Matrix {
if (
this.uOffset === this._cachedUOffset &&
this.vOffset === this._cachedVOffset &&
this.uScale * uBase === this._cachedUScale &&
this.vScale === this._cachedVScale &&
this.uAng === this._cachedUAng &&
this.vAng === this._cachedVAng &&
this.wAng === this._cachedWAng &&
this.uRotationCenter === this._cachedURotationCenter &&
this.vRotationCenter === this._cachedVRotationCenter &&
this.wRotationCenter === this._cachedWRotationCenter &&
this.homogeneousRotationInUVTransform === this._cachedHomogeneousRotationInUVTransform
) {
return this._cachedTextureMatrix!;
}
this._cachedUOffset = this.uOffset;
this._cachedVOffset = this.vOffset;
this._cachedUScale = this.uScale * uBase;
this._cachedVScale = this.vScale;
this._cachedUAng = this.uAng;
this._cachedVAng = this.vAng;
this._cachedWAng = this.wAng;
this._cachedURotationCenter = this.uRotationCenter;
this._cachedVRotationCenter = this.vRotationCenter;
this._cachedWRotationCenter = this.wRotationCenter;
this._cachedHomogeneousRotationInUVTransform = this.homogeneousRotationInUVTransform;
if (!this._cachedTextureMatrix || !this._rowGenerationMatrix) {
this._cachedTextureMatrix = Matrix.Zero();
this._rowGenerationMatrix = new Matrix();
this._t0 = Vector3.Zero();
this._t1 = Vector3.Zero();
this._t2 = Vector3.Zero();
}
Matrix.RotationYawPitchRollToRef(this.vAng, this.uAng, this.wAng, this._rowGenerationMatrix!);
if (this.homogeneousRotationInUVTransform) {
Matrix.TranslationToRef(-this._cachedURotationCenter, -this._cachedVRotationCenter, -this._cachedWRotationCenter, TmpVectors.Matrix[0]);
Matrix.TranslationToRef(this._cachedURotationCenter, this._cachedVRotationCenter, this._cachedWRotationCenter, TmpVectors.Matrix[1]);
Matrix.ScalingToRef(this._cachedUScale, this._cachedVScale, 0, TmpVectors.Matrix[2]);
Matrix.TranslationToRef(this._cachedUOffset, this._cachedVOffset, 0, TmpVectors.Matrix[3]);
TmpVectors.Matrix[0].multiplyToRef(this._rowGenerationMatrix!, this._cachedTextureMatrix);
this._cachedTextureMatrix.multiplyToRef(TmpVectors.Matrix[1], this._cachedTextureMatrix);
this._cachedTextureMatrix.multiplyToRef(TmpVectors.Matrix[2], this._cachedTextureMatrix);
this._cachedTextureMatrix.multiplyToRef(TmpVectors.Matrix[3], this._cachedTextureMatrix);
// copy the translation row to the 3rd row of the matrix so that we don't need to update the shaders (which expects the translation to be on the 3rd row)
this._cachedTextureMatrix.setRowFromFloats(2, this._cachedTextureMatrix.m[12], this._cachedTextureMatrix.m[13], this._cachedTextureMatrix.m[14], 1);
} else {
this._prepareRowForTextureGeneration(0, 0, 0, this._t0!);
this._prepareRowForTextureGeneration(1.0, 0, 0, this._t1!);
this._prepareRowForTextureGeneration(0, 1.0, 0, this._t2!);
this._t1!.subtractInPlace(this._t0!);
this._t2!.subtractInPlace(this._t0!);
Matrix.FromValuesToRef(
this._t1!.x,
this._t1!.y,
this._t1!.z,
0.0,
this._t2!.x,
this._t2!.y,
this._t2!.z,
0.0,
this._t0!.x,
this._t0!.y,
this._t0!.z,
0.0,
0.0,
0.0,
0.0,
1.0,
this._cachedTextureMatrix
);
}
const scene = this.getScene();
if (!scene) {
return this._cachedTextureMatrix;
}
if (this.optimizeUVAllocation) {
// We flag the materials that are using this texture as "texture dirty" because depending on the fact that the matrix is the identity or not, some defines
// will get different values (see MaterialHelper.PrepareDefinesForMergedUV), meaning we should regenerate the effect accordingly
scene.markAllMaterialsAsDirty(Constants.MATERIAL_TextureDirtyFlag, (mat) => {
return mat.hasTexture(this);
});
}
return this._cachedTextureMatrix;
}
/**
* Get the current matrix used to apply reflection. This is useful to rotate an environment texture for instance.
* @returns The reflection texture transform
*/
public getReflectionTextureMatrix(): Matrix {
const scene = this.getScene();
if (!scene) {
return this._cachedReflectionTextureMatrix!;
}
if (
this.uOffset === this._cachedReflectionUOffset &&
this.vOffset === this._cachedReflectionVOffset &&
this.uScale === this._cachedReflectionUScale &&
this.vScale === this._cachedReflectionVScale &&
this.coordinatesMode === this._cachedReflectionCoordinatesMode
) {
if (this.coordinatesMode === Texture.PROJECTION_MODE) {
if (this._cachedReflectionProjectionMatrixId === scene.getProjectionMatrix().updateFlag) {
return this._cachedReflectionTextureMatrix!;
}
} else {
return this._cachedReflectionTextureMatrix!;
}
}
if (!this._cachedReflectionTextureMatrix) {
this._cachedReflectionTextureMatrix = Matrix.Zero();
}
if (!this._projectionModeMatrix) {
this._projectionModeMatrix = Matrix.Zero();
}
const flagMaterialsAsTextureDirty = this._cachedReflectionCoordinatesMode !== this.coordinatesMode;
this._cachedReflectionUOffset = this.uOffset;
this._cachedReflectionVOffset = this.vOffset;
this._cachedReflectionUScale = this.uScale;
this._cachedReflectionVScale = this.vScale;
this._cachedReflectionCoordinatesMode = this.coordinatesMode;
switch (this.coordinatesMode) {
case Texture.PLANAR_MODE: {
Matrix.IdentityToRef(this._cachedReflectionTextureMatrix);
(<any>this._cachedReflectionTextureMatrix)[0] = this.uScale;
(<any>this._cachedReflectionTextureMatrix)[5] = this.vScale;
(<any>this._cachedReflectionTextureMatrix)[12] = this.uOffset;
(<any>this._cachedReflectionTextureMatrix)[13] = this.vOffset;
break;
}
case Texture.PROJECTION_MODE: {
Matrix.FromValuesToRef(0.5, 0.0, 0.0, 0.0, 0.0, -0.5, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.5, 0.5, 1.0, 1.0, this._projectionModeMatrix);
const projectionMatrix = scene.getProjectionMatrix();
this._cachedReflectionProjectionMatrixId = projectionMatrix.updateFlag;
projectionMatrix.multiplyToRef(this._projectionModeMatrix, this._cachedReflectionTextureMatrix);
break;
}
default:
Matrix.IdentityToRef(this._cachedReflectionTextureMatrix);
break;
}
if (flagMaterialsAsTextureDirty) {
// We flag the materials that are using this texture as "texture dirty" if the coordinatesMode has changed.
// Indeed, this property is used to set the value of some defines used to generate the effect (in material.isReadyForSubMesh), so we must make sure this code will be re-executed and the effect recreated if necessary
scene.markAllMaterialsAsDirty(Constants.MATERIAL_TextureDirtyFlag, (mat) => {
return mat.hasTexture(this);
});
}
return this._cachedReflectionTextureMatrix;
}
/**
* Clones the texture.
* @returns the cloned texture
*/
public clone(): Texture {
const options: ITextureCreationOptions = {
noMipmap: this._noMipmap,
invertY: this._invertY,
samplingMode: this.samplingMode,
onLoad: undefined,
onError: undefined,
buffer: this._texture ? this._texture._buffer : undefined,
deleteBuffer: this._deleteBuffer,
format: this.textureFormat,
mimeType: this.mimeType,
loaderOptions: this._loaderOptions,
creationFlags: this._creationFlags,
useSRGBBuffer: this._useSRGBBuffer,
};
return SerializationHelper.Clone(() => {
return new Texture(this._texture ? this._texture.url : null, this.getScene(), options);
}, this);
}
/**
* Serialize the texture to a JSON representation we can easily use in the respective Parse function.
* @returns The JSON representation of the texture
*/
public serialize(): any {
const savedName = this.name;
if (!Texture.SerializeBuffers) {
if (this.name.startsWith("data:")) {
this.name = "";
}
}
if (this.name.startsWith("data:") && this.url === this.name) {
this.url = "";
}
const serializationObject = super.serialize(Texture._SerializeInternalTextureUniqueId);
if (!serializationObject) {
return null;
}
if (Texture.SerializeBuffers || Texture.ForceSerializeBuffers) {
if (typeof this._buffer === "string" && (this._buffer as string).substr(0, 5) === "data:") {
serializationObject.base64String = this._buffer;
serializationObject.name = serializationObject.name.replace("data:", "");
} else if (this.url && this.url.startsWith("data:") && this._buffer instanceof Uint8Array) {
serializationObject.base64String = "data:image/png;base64," + EncodeArrayBufferToBase64(this._buffer);
} else if (Texture.ForceSerializeBuffers || (this.url && this.url.startsWith("blob:")) || this._forceSerialize) {
serializationObject.base64String =
!this._engine || this._engine._features.supportSyncTextureRead ? GenerateBase64StringFromTexture(this) : GenerateBase64StringFromTextureAsync(this);
}
}
serializationObject.invertY = this._invertY;
serializationObject.samplingMode = this.samplingMode;
serializationObject._creationFlags = this._creationFlags;
serializationObject._useSRGBBuffer = this._useSRGBBuffer;
if (Texture._SerializeInternalTextureUniqueId) {
serializationObject.internalTextureUniqueId = this._texture?.uniqueId ?? undefined;
}
serializationObject.noMipmap = this._noMipmap;
this.name = savedName;
return serializationObject;
}
/**
* Get the current class name of the texture useful for serialization or dynamic coding.
* @returns "Texture"
*/
public getClassName(): string {
return "Texture";
}
/**
* Dispose the texture and release its associated resources.
*/
public dispose(): void {
super.dispose();
this.onLoadObservable.clear();
this._delayedOnLoad = null;
this._delayedOnError = null;
this._buffer = null;
}
/**
* Parse the JSON representation of a texture in order to recreate the texture in the given scene.
* @param parsedTexture Define the JSON representation of the texture
* @param scene Define the scene the parsed texture should be instantiated in
* @param rootUrl Define the root url of the parsing sequence in the case of relative dependencies
* @returns The parsed texture if successful
*/
public static Parse(parsedTexture: any, scene: Scene, rootUrl: string): Nullable<BaseTexture> {
if (parsedTexture.customType) {
const customTexture = InstantiationTools.Instantiate(parsedTexture.customType);
// Update Sampling Mode
const parsedCustomTexture: any = customTexture.Parse(parsedTexture, scene, rootUrl);
if (parsedTexture.samplingMode && parsedCustomTexture.updateSamplingMode && parsedCustomTexture._samplingMode) {
if (parsedCustomTexture._samplingMode !== parsedTexture.samplingMode) {
parsedCustomTexture.updateSamplingMode(parsedTexture.samplingMode);
}
}
return parsedCustomTexture;
}
if (parsedTexture.isCube && !parsedTexture.isRenderTarget) {
return Texture._CubeTextureParser(parsedTexture, scene, rootUrl);
}
const hasInternalTextureUniqueId = parsedTexture.internalTextureUniqueId !== undefined;
if (!parsedTexture.name && !parsedTexture.isRenderTarget && !hasInternalTextureUniqueId) {
return null;
}
let internalTexture: InternalTexture | undefined;
if (hasInternalTextureUniqueId) {
const cache = scene.getEngine().getLoadedTexturesCache();
for (const texture of cache) {
if (texture.uniqueId === parsedTexture.internalTextureUniqueId) {
internalTexture = texture;
break;
}
}
}
const onLoaded = (texture: Texture | null) => {
// Clear cache
if (texture && texture._texture) {
texture._texture._cachedWrapU = null;
texture._texture._cachedWrapV = null;
texture._texture._cachedWrapR = null;
}
// Update Sampling Mode
if (parsedTexture.samplingMode) {
const sampling: number = parsedTexture.samplingMode;
if (texture && texture.samplingMode !== sampling) {