1 | import {
|
2 | AnyMapping,
|
3 | AnyPixelFormat,
|
4 | MagnificationTextureFilter,
|
5 | Mapping,
|
6 | MinificationTextureFilter,
|
7 | PixelFormat,
|
8 | PixelFormatGPU,
|
9 | TextureDataType,
|
10 | Wrapping,
|
11 | } from "../constants.js";
|
12 | import { EventDispatcher } from "../core/EventDispatcher.js";
|
13 | import { Matrix3 } from "../math/Matrix3.js";
|
14 | import { Vector2 } from "../math/Vector2.js";
|
15 | import { CompressedTextureMipmap } from "./CompressedTexture.js";
|
16 | import { CubeTexture } from "./CubeTexture.js";
|
17 | import { Source } from "./Source.js";
|
18 |
|
19 | export interface TextureJSON {
|
20 | metadata: { version: number; type: string; generator: string };
|
21 |
|
22 | uuid: string;
|
23 | name: string;
|
24 |
|
25 | image: string;
|
26 |
|
27 | mapping: AnyMapping;
|
28 | channel: number;
|
29 |
|
30 | repeat: [x: number, y: number];
|
31 | offset: [x: number, y: number];
|
32 | center: [x: number, y: number];
|
33 | rotation: number;
|
34 |
|
35 | wrap: [wrapS: number, wrapT: number];
|
36 |
|
37 | format: AnyPixelFormat;
|
38 | internalFormat: PixelFormatGPU | null;
|
39 | type: TextureDataType;
|
40 | colorSpace: string;
|
41 |
|
42 | minFilter: MinificationTextureFilter;
|
43 | magFilter: MagnificationTextureFilter;
|
44 | anisotropy: number;
|
45 |
|
46 | flipY: boolean;
|
47 |
|
48 | generateMipmaps: boolean;
|
49 | premultiplyAlpha: boolean;
|
50 | unpackAlignment: number;
|
51 |
|
52 | userData?: Record<string, unknown>;
|
53 | }
|
54 |
|
55 | /** Shim for OffscreenCanvas. */
|
56 | // eslint-disable-next-line @typescript-eslint/no-empty-interface
|
57 | export interface OffscreenCanvas extends EventTarget {}
|
58 |
|
59 | /**
|
60 | * Create a {@link Texture} to apply to a surface or as a reflection or refraction map.
|
61 | * @remarks
|
62 | * After the initial use of a texture, its **dimensions**, {@link format}, and {@link type} cannot be changed
|
63 | * Instead, call {@link dispose | .dispose()} on the {@link Texture} and instantiate a new {@link Texture}.
|
64 | * @example
|
65 | * ```typescript
|
66 | * // load a texture, set wrap mode to repeat
|
67 | * const texture = new THREE.TextureLoader().load("textures/water.jpg");
|
68 | * texture.wrapS = THREE.RepeatWrapping;
|
69 | * texture.wrapT = THREE.RepeatWrapping;
|
70 | * texture.repeat.set(4, 4);
|
71 | * ```
|
72 | * @see Example: {@link https://threejs.org/examples/#webgl_materials_texture_filters | webgl materials texture filters}
|
73 | * @see {@link https://threejs.org/docs/index.html#api/en/constants/Textures | Texture Constants}
|
74 | * @see {@link https://threejs.org/docs/index.html#api/en/textures/Texture | Official Documentation}
|
75 | * @see {@link https://github.com/mrdoob/three.js/blob/master/src/Textures/Texture.js | Source}
|
76 | */
|
77 | export class Texture extends EventDispatcher<{ dispose: {} }> {
|
78 | /**
|
79 | * This creates a new {@link THREE.Texture | Texture} object.
|
80 | * @param image See {@link Texture.image | .image}. Default {@link THREE.Texture.DEFAULT_IMAGE}
|
81 | * @param mapping See {@link Texture.mapping | .mapping}. Default {@link THREE.Texture.DEFAULT_MAPPING}
|
82 | * @param wrapS See {@link Texture.wrapS | .wrapS}. Default {@link THREE.ClampToEdgeWrapping}
|
83 | * @param wrapT See {@link Texture.wrapT | .wrapT}. Default {@link THREE.ClampToEdgeWrapping}
|
84 | * @param magFilter See {@link Texture.magFilter | .magFilter}. Default {@link THREE.LinearFilter}
|
85 | * @param minFilter See {@link Texture.minFilter | .minFilter}. Default {@link THREE.LinearMipmapLinearFilter}
|
86 | * @param format See {@link Texture.format | .format}. Default {@link THREE.RGBAFormat}
|
87 | * @param type See {@link Texture.type | .type}. Default {@link THREE.UnsignedByteType}
|
88 | * @param anisotropy See {@link Texture.anisotropy | .anisotropy}. Default {@link THREE.Texture.DEFAULT_ANISOTROPY}
|
89 | * @param colorSpace See {@link Texture.colorSpace | .colorSpace}. Default {@link THREE.NoColorSpace}
|
90 | */
|
91 | constructor(
|
92 | image?: TexImageSource | OffscreenCanvas,
|
93 | mapping?: Mapping,
|
94 | wrapS?: Wrapping,
|
95 | wrapT?: Wrapping,
|
96 | magFilter?: MagnificationTextureFilter,
|
97 | minFilter?: MinificationTextureFilter,
|
98 | format?: PixelFormat,
|
99 | type?: TextureDataType,
|
100 | anisotropy?: number,
|
101 | colorSpace?: string,
|
102 | );
|
103 |
|
104 | /**
|
105 | * @deprecated
|
106 | */
|
107 | constructor(
|
108 | image: TexImageSource | OffscreenCanvas,
|
109 | mapping: Mapping,
|
110 | wrapS: Wrapping,
|
111 | wrapT: Wrapping,
|
112 | magFilter: MagnificationTextureFilter,
|
113 | minFilter: MinificationTextureFilter,
|
114 | format: PixelFormat,
|
115 | type: TextureDataType,
|
116 | anisotropy: number,
|
117 | );
|
118 |
|
119 | /**
|
120 | * Read-only flag to check if a given object is of type {@link Texture}.
|
121 | * @remarks This is a _constant_ value
|
122 | * @defaultValue `true`
|
123 | */
|
124 | readonly isTexture: true;
|
125 |
|
126 | /**
|
127 | * Unique number for this { Texture} instance.
|
128 | * in chronological order: 1, 2, 3, ..., incrementing by one for each new object.
Note that ids are assigned |
129 | * `Integer`
Expects a |
130 | */
|
131 | readonly id: number;
|
132 |
|
133 | /**
|
134 | * {@link http://en.wikipedia.org/wiki/Universally_unique_identifier | UUID} of this object instance.
|
135 | * @remarks This gets automatically assigned and shouldn't be edited.
|
136 | */
|
137 | uuid: string;
|
138 |
|
139 | /**
|
140 | * Optional name of the object
|
141 | * @remarks _(doesn't need to be unique)_.
|
142 | * @defaultValue `""`
|
143 | */
|
144 | name: string;
|
145 |
|
146 | /**
|
147 | * The data definition of a texture. A reference to the data source can be shared across textures.
|
148 | * This is often useful in context of spritesheets where multiple textures render the same data
|
149 | * but with different {@link Texture} transformations.
|
150 | */
|
151 | source: Source;
|
152 |
|
153 | /**
|
154 | * An image object, typically created using the {@link THREE.TextureLoader.load | TextureLoader.load()} method.
|
155 | * @remarks This can be any image (e.g., PNG, JPG, GIF, DDS) or video (e.g., MP4, OGG/OGV) type supported by three.js.
|
156 | * @remarks To use video as a {@link Texture} you need to have a playing HTML5 video element as a source
|
157 | * for your {@link Texture} image and continuously update this {@link Texture}
|
158 | * as long as video is playing - the {@link THREE.VideoTexture | VideoTexture} class handles this automatically.
|
159 | */
|
160 | get image(): any;
|
161 | set image(data: any);
|
162 |
|
163 | /**
|
164 | * Array of user-specified mipmaps
|
165 | * @defaultValue `[]`
|
166 | */
|
167 | mipmaps: CompressedTextureMipmap[] | CubeTexture[] | HTMLCanvasElement[] | undefined;
|
168 |
|
169 | /**
|
170 | * How the image is applied to the object.
|
171 | * @remarks All {@link Texture} types except {@link THREE.CubeTexture} expect the _values_ be {@link THREE.Mapping}
|
172 | * @remarks {@link CubeTexture} expect the _values_ be {@link THREE.CubeTextureMapping}
|
173 | * @see {@link https://threejs.org/docs/index.html#api/en/constants/Textures | Texture Constants}
|
174 | * @defaultValue _value of_ {@link THREE.Texture.DEFAULT_MAPPING}
|
175 | */
|
176 | mapping: AnyMapping;
|
177 |
|
178 | /**
|
179 | * Lets you select the uv attribute to map the texture to. `0` for `uv`, `1` for `uv1`, `2` for `uv2` and `3` for
|
180 | * `uv3`.
|
181 | */
|
182 | channel: number;
|
183 |
|
184 | /**
|
185 | * This defines how the {@link Texture} is wrapped *horizontally* and corresponds to **U** in UV mapping.
|
186 | * @remarks for **WEBGL1** - tiling of images in textures only functions if image dimensions are powers of two
|
187 | * (2, 4, 8, 16, 32, 64, 128, 256, 512, 1024, 2048, ...) in terms of pixels.
|
188 | * Individual dimensions need not be equal, but each must be a power of two. This is a limitation of WebGL1, not three.js.
|
189 | * **WEBGL2** does not have this limitation.
|
190 | * @see {@link https://threejs.org/docs/index.html#api/en/constants/Textures | Texture Constants}
|
191 | * @see {@link wrapT}
|
192 | * @see {@link repeat}
|
193 | * @defaultValue {@link THREE.ClampToEdgeWrapping}
|
194 | */
|
195 | wrapS: Wrapping;
|
196 |
|
197 | /**
|
198 | * This defines how the {@link Texture} is wrapped *vertically* and corresponds to **V** in UV mapping.
|
199 | * @remarks for **WEBGL1** - tiling of images in textures only functions if image dimensions are powers of two
|
200 | * (2, 4, 8, 16, 32, 64, 128, 256, 512, 1024, 2048, ...) in terms of pixels.
|
201 | * Individual dimensions need not be equal, but each must be a power of two. This is a limitation of WebGL1, not three.js.
|
202 | * **WEBGL2** does not have this limitation.
|
203 | * @see {@link https://threejs.org/docs/index.html#api/en/constants/Textures | Texture Constants}
|
204 | * @see {@link wrapS}
|
205 | * @see {@link repeat}
|
206 | * @defaultValue {@link THREE.ClampToEdgeWrapping}
|
207 | */
|
208 | wrapT: Wrapping;
|
209 |
|
210 | /**
|
211 | * How the {@link Texture} is sampled when a texel covers more than one pixel.
|
212 | * @see {@link https://threejs.org/docs/index.html#api/en/constants/Textures | Texture Constants}
|
213 | * @see {@link minFilter}
|
214 | * @see {@link THREE.MagnificationTextureFilter}
|
215 | * @defaultValue {@link THREE.LinearFilter}
|
216 | */
|
217 | magFilter: MagnificationTextureFilter;
|
218 |
|
219 | /**
|
220 | * How the {@link Texture} is sampled when a texel covers less than one pixel.
|
221 | * @see {@link https://threejs.org/docs/index.html#api/en/constants/Textures | Texture Constants}
|
222 | * @see {@link magFilter}
|
223 | * @see {@link THREE.MinificationTextureFilter}
|
224 | * @defaultValue {@link THREE.LinearMipmapLinearFilter}
|
225 | */
|
226 | minFilter: MinificationTextureFilter;
|
227 |
|
228 | /**
|
229 | * The number of samples taken along the axis through the pixel that has the highest density of texels.
|
230 | * @remarks A higher value gives a less blurry result than a basic mipmap, at the cost of more {@link Texture} samples being used.
|
231 | * @remarks Use {@link THREE.WebGLCapabilities.getMaxAnisotropy() | renderer.capabilities.getMaxAnisotropy()} to find the maximum valid anisotropy value for the GPU;
|
232 | * @remarks This value is usually a power of 2.
|
233 | * @default _value of_ {@link THREE.Texture.DEFAULT_ANISOTROPY}. That is normally `1`.
|
234 | */
|
235 | anisotropy: number;
|
236 |
|
237 | /**
|
238 | * These define how elements of a 2D texture, or texels, are read by shaders.
|
239 | * @remarks All {@link Texture} types except {@link THREE.DepthTexture} and {@link THREE.CompressedPixelFormat} expect the _values_ be {@link THREE.PixelFormat}
|
240 | * @remarks {@link DepthTexture} expect the _values_ be {@link THREE.CubeTextureMapping}
|
241 | * @remarks {@link CompressedPixelFormat} expect the _values_ be {@link THREE.CubeTextureMapping}
|
242 | * @see {@link https://threejs.org/docs/index.html#api/en/constants/Textures | Texture Constants}
|
243 | * @see {@link THREE.PixelFormat}
|
244 | * @defaultValue {@link THREE.RGBAFormat}.
|
245 | */
|
246 | format: AnyPixelFormat;
|
247 |
|
248 | /**
|
249 | * This must correspond to the {@link Texture.format | .format}.
|
250 | * @remarks {@link THREE.UnsignedByteType}, is the type most used by Texture formats.
|
251 | * @see {@link https://threejs.org/docs/index.html#api/en/constants/Textures | Texture Constants}
|
252 | * @see {@link THREE.TextureDataType}
|
253 | * @defaultValue {@link THREE.UnsignedByteType}
|
254 | */
|
255 | type: TextureDataType;
|
256 |
|
257 | /**
|
258 | * The GPU Pixel Format allows the developer to specify how the data is going to be stored on the GPU.
|
259 | * @remarks Compatible only with {@link WebGL2RenderingContext | WebGL 2 Rendering Context}.
|
260 | * @see {@link https://threejs.org/docs/index.html#api/en/constants/Textures | Texture Constants}
|
261 | * @defaultValue The default value is obtained using a combination of {@link Texture.format | .format} and {@link Texture.type | .type}.
|
262 | */
|
263 | internalFormat: PixelFormatGPU | null;
|
264 |
|
265 | /**
|
266 | * The uv-transform matrix for the texture.
|
267 | * @remarks
|
268 | * When {@link Texture.matrixAutoUpdate | .matrixAutoUpdate} property is `true`.
|
269 | * Will be updated by the renderer from the properties:
|
270 | * - {@link Texture.offset | .offset}
|
271 | * - {@link Texture.repeat | .repeat}
|
272 | * - {@link Texture.rotation | .rotation}
|
273 | * - {@link Texture.center | .center}
|
274 | * @remarks
|
275 | * When {@link Texture.matrixAutoUpdate | .matrixAutoUpdate} property is `false`.
|
276 | * This matrix may be set manually.
|
277 | * @see {@link matrixAutoUpdate | .matrixAutoUpdate}
|
278 | * @defaultValue `new THREE.Matrix3()`
|
279 | */
|
280 | matrix: Matrix3;
|
281 |
|
282 | /**
|
283 | * Whether is to update the texture's uv-transform {@link matrix | .matrix}.
|
284 | * @remarks Set this to `false` if you are specifying the uv-transform {@link matrix} directly.
|
285 | * @see {@link matrix | .matrix}
|
286 | * @defaultValue `true`
|
287 | */
|
288 | matrixAutoUpdate: boolean;
|
289 |
|
290 | /**
|
291 | * How much a single repetition of the texture is offset from the beginning, in each direction **U** and **V**.
|
292 | * @remarks Typical range is `0.0` to `1.0`.
|
293 | * @defaultValue `new THREE.Vector2(0, 0)`
|
294 | */
|
295 | offset: Vector2;
|
296 |
|
297 | /**
|
298 | * How many times the texture is repeated across the surface, in each direction **U** and **V**.
|
299 | * @remarks
|
300 | * If repeat is set greater than `1` in either direction, the corresponding *Wrap* parameter should
|
301 | * also be set to {@link THREE.RepeatWrapping} or {@link THREE.MirroredRepeatWrapping} to achieve the desired tiling effect.
|
302 | * @see {@link wrapS}
|
303 | * @see {@link wrapT}
|
304 | * @defaultValue `new THREE.Vector2( 1, 1 )`
|
305 | */
|
306 | repeat: Vector2;
|
307 |
|
308 | /**
|
309 | * The point around which rotation occurs.
|
310 | * @remarks A value of `(0.5, 0.5)` corresponds to the center of the texture.
|
311 | * @defaultValue `new THREE.Vector2( 0, 0 )`, _lower left._
|
312 | */
|
313 | center: Vector2;
|
314 |
|
315 | /**
|
316 | * How much the texture is rotated around the center point, in radians.
|
317 | * @remarks Positive values are counter-clockwise.
|
318 | * @defaultValue `0`
|
319 | */
|
320 | rotation: number;
|
321 |
|
322 | /**
|
323 | * Whether to generate mipmaps, _(if possible)_ for a texture.
|
324 | * @remarks Set this to false if you are creating mipmaps manually.
|
325 | * @defaultValue true
|
326 | */
|
327 | generateMipmaps: boolean;
|
328 |
|
329 | /**
|
330 | * If set to `true`, the alpha channel, if present, is multiplied into the color channels when the texture is uploaded to the GPU.
|
331 | * @remarks
|
332 | * Note that this property has no effect for {@link https://developer.mozilla.org/en-US/docs/Web/API/ImageBitmap | ImageBitmap}.
|
333 | * You need to configure on bitmap creation instead. See {@link THREE.ImageBitmapLoader | ImageBitmapLoader}.
|
334 | * @see {@link THREE.ImageBitmapLoader | ImageBitmapLoader}.
|
335 | * @defaultValue `false`
|
336 | */
|
337 | premultiplyAlpha: boolean;
|
338 |
|
339 | /**
|
340 | * If set to `true`, the texture is flipped along the vertical axis when uploaded to the GPU.
|
341 | * @remarks
|
342 | * Note that this property has no effect for {@link https://developer.mozilla.org/en-US/docs/Web/API/ImageBitmap | ImageBitmap}.
|
343 | * You need to configure on bitmap creation instead. See {@link THREE.ImageBitmapLoader | ImageBitmapLoader}.
|
344 | * @see {@link THREE.ImageBitmapLoader | ImageBitmapLoader}.
|
345 | * @defaultValue `true`
|
346 | */
|
347 | flipY: boolean;
|
348 |
|
349 | /**
|
350 | * Specifies the alignment requirements for the start of each pixel row in memory.
|
351 | * @remarks
|
352 | * The allowable values are:
|
353 | * - `1` (byte-alignment)
|
354 | * - `2` (rows aligned to even-numbered bytes)
|
355 | * - `4` (word-alignment)
|
356 | * - `8` (rows start on double-word boundaries).
|
357 | * @see {@link http://www.khronos.org/opengles/sdk/docs/man/xhtml/glPixelStorei.xml | glPixelStorei} for more information.
|
358 | * @defaultValue `4`
|
359 | */
|
360 | unpackAlignment: number; // TODO Fix typing to only allow the expected values.
|
361 |
|
362 | /**
|
363 | * The {@link Textures | {@link Texture} constants} page for details of other color spaces.
|
364 | * @remarks
|
365 | * Textures containing color data should be annotated with {@link SRGBColorSpace THREE.SRGBColorSpace} or
|
366 | * {@link LinearSRGBColorSpace THREE.LinearSRGBColorSpace}.
|
367 | * @see {@link https://threejs.org/docs/index.html#api/en/constants/Textures | Texture Constants}
|
368 | * @see {@link THREE.TextureDataType}
|
369 | * @defaultValue {@link THREE.NoColorSpace}
|
370 | */
|
371 | colorSpace: string;
|
372 |
|
373 | /**
|
374 | * Indicates whether a texture belongs to a render target or not
|
375 | * @defaultValue `false`
|
376 | */
|
377 | isRenderTargetTexture: boolean;
|
378 |
|
379 | /**
|
380 | * An object that can be used to store custom data about the texture.
|
381 | * @remarks It should not hold references to functions as these will not be cloned.
|
382 | * @defaultValue `{}`
|
383 | */
|
384 | userData: Record<string, any>;
|
385 |
|
386 | /**
|
387 | * This starts at `0` and counts how many times {@link needsUpdate | .needsUpdate} is set to `true`.
|
388 | * @remarks Expects a `Integer`
|
389 | * @defaultValue `0`
|
390 | */
|
391 | version: number;
|
392 |
|
393 | /**
|
394 | * Indicates whether this texture should be processed by PMREMGenerator or not (only relevant for render target
|
395 | * textures)
|
396 | */
|
397 | pmremVersion: number;
|
398 |
|
399 | /**
|
400 | * Set this to `true` to trigger an update next time the texture is used. Particularly important for setting the wrap mode.
|
401 | */
|
402 | set needsUpdate(value: boolean);
|
403 |
|
404 | /**
|
405 | * Indicates whether this texture should be processed by {@link THREE.PMREMGenerator} or not.
|
406 | * @remarks Only relevant for render target textures.
|
407 | * @defaultValue `false`
|
408 | */
|
409 | set needsPMREMUpdate(value: boolean);
|
410 |
|
411 | /**
|
412 | * The Global default value for {@link anisotropy | .anisotropy}.
|
413 | * @defaultValue `1`.
|
414 | */
|
415 | static DEFAULT_ANISOTROPY: number;
|
416 |
|
417 | /**
|
418 | * The Global default value for {@link Texture.image | .image}.
|
419 | * @defaultValue `null`.
|
420 | */
|
421 | static DEFAULT_IMAGE: any;
|
422 |
|
423 | /**
|
424 | * The Global default value for {@link mapping | .mapping}.
|
425 | * @defaultValue {@link THREE.UVMapping}
|
426 | */
|
427 | static DEFAULT_MAPPING: Mapping;
|
428 |
|
429 | /**
|
430 | * A callback function, called when the texture is updated _(e.g., when needsUpdate has been set to true and then the texture is used)_.
|
431 | */
|
432 | onUpdate: () => void;
|
433 |
|
434 | /**
|
435 | * Transform the **UV** based on the value of this texture's
|
436 | * {@link offset | .offset},
|
437 | * {@link repeat | .repeat},
|
438 | * {@link wrapS | .wrapS},
|
439 | * {@link wrapT | .wrapT} and
|
440 | * {@link flipY | .flipY} properties.
|
441 | * @param uv
|
442 | */
|
443 | transformUv(uv: Vector2): Vector2;
|
444 |
|
445 | /**
|
446 | * Update the texture's **UV-transform** {@link matrix | .matrix} from the texture properties
|
447 | * {@link offset | .offset},
|
448 | * {@link repeat | .repeat},
|
449 | * {@link rotation | .rotation} and
|
450 | * {@link center | .center}.
|
451 | */
|
452 | updateMatrix(): void;
|
453 |
|
454 | /**
|
455 | * Make copy of the texture
|
456 | * @remarks Note this is not a **"deep copy"**, the image is shared
|
457 | * @remarks
|
458 | * Besides, cloning a texture does not automatically mark it for a texture upload
|
459 | * You have to set {@link needsUpdate | .needsUpdate} to `true` as soon as it's image property (the data source) is fully loaded or ready.
|
460 | */
|
461 | clone(): this;
|
462 |
|
463 | copy(source: Texture): this;
|
464 |
|
465 | /**
|
466 | * Convert the texture to three.js {@link https://github.com/mrdoob/three.js/wiki/JSON-Object-Scene-format-4 | JSON Object/Scene format}.
|
467 | * @param meta Optional object containing metadata.
|
468 | */
|
469 | toJSON(meta?: string | {}): TextureJSON;
|
470 |
|
471 | /**
|
472 | * Frees the GPU-related resources allocated by this instance
|
473 | * @remarks Call this method whenever this instance is no longer used in your app.
|
474 | */
|
475 | dispose(): void;
|
476 | }
|