UNPKG

3.82 kBTypeScriptView Raw
1import {
2 MagnificationTextureFilter,
3 Mapping,
4 MinificationTextureFilter,
5 PixelFormat,
6 TextureDataType,
7 Wrapping,
8} from "../constants.js";
9import { Texture } from "./Texture.js";
10
11/**
12 * Creates a texture directly from raw data, width and height.
13 * @example
14 * ```typescript
15 * // create a buffer with color data
16 * const width = 512;
17 * const height = 512;
18 * const size = width * height;
19 * const data = new Uint8Array(4 * size);
20 * const color = new THREE.Color(0xffffff);
21 * const r = Math.floor(color.r * 255);
22 * const g = Math.floor(color.g * 255);
23 * const b = Math.floor(color.b * 255);
24 * for (let i = 0; i & lt; size; i++) {
25 * const stride = i * 4;
26 * data[stride] = r;
27 * data[stride + 1] = g;
28 * data[stride + 2] = b;
29 * data[stride + 3] = 255;
30 * }
31 * // used the buffer to create a [name]
32 * const texture = new THREE.DataTexture(data, width, height);
33 * texture.needsUpdate = true;
34 * ```
35 * @see {@link https://threejs.org/docs/index.html#api/en/textures/DataTexture | Official Documentation}
36 * @see {@link https://github.com/mrdoob/three.js/blob/master/src/textures/DataTexture.js | Source}
37 */
38export class DataTexture extends Texture {
39 /**
40 * @param data {@link https://developer.mozilla.org/en-US/docs/Web/API/ArrayBufferView | ArrayBufferView} of the texture. Default `null`.
41 * @param width Width of the texture. Default `1`.
42 * @param height Height of the texture. Default `1`.
43 * @param format See {@link Texture.format | .format}. Default {@link THREE.RGBAFormat}
44 * @param type See {@link Texture.type | .type}. Default {@link THREE.UnsignedByteType}
45 * @param mapping See {@link Texture.mapping | .mapping}. Default {@link THREE.Texture.DEFAULT_MAPPING}
46 * @param wrapS See {@link Texture.wrapS | .wrapS}. Default {@link THREE.ClampToEdgeWrapping}
47 * @param wrapT See {@link Texture.wrapT | .wrapT}. Default {@link THREE.ClampToEdgeWrapping}
48 * @param magFilter See {@link Texture.magFilter | .magFilter}. Default {@link THREE.NearestFilter}
49 * @param minFilter See {@link Texture.minFilter | .minFilter}. Default {@link THREE.NearestFilter}
50 * @param anisotropy See {@link Texture.anisotropy | .anisotropy}. Default {@link THREE.Texture.DEFAULT_ANISOTROPY}
51 * @param colorSpace See {@link Texture.colorSpace | .colorSpace}. Default {@link NoColorSpace}
52 */
53 constructor(
54 data?: BufferSource | null,
55 width?: number,
56 height?: number,
57 format?: PixelFormat,
58 type?: TextureDataType,
59 mapping?: Mapping,
60 wrapS?: Wrapping,
61 wrapT?: Wrapping,
62 magFilter?: MagnificationTextureFilter,
63 minFilter?: MinificationTextureFilter,
64 anisotropy?: number,
65 colorSpace?: string,
66 );
67
68 /**
69 * Read-only flag to check if a given object is of type {@link DataTexture}.
70 * @remarks This is a _constant_ value
71 * @defaultValue `true`
72 */
73 readonly isDataTexture: true;
74
75 /**
76 * Overridden with a record type holding data, width and height and depth.
77 * @override
78 */
79 get image(): TextureImageData;
80 set image(value: TextureImageData);
81
82 /**
83 * @override
84 * @defaultValue {@link THREE.NearestFilter}
85 */
86 magFilter: MagnificationTextureFilter;
87
88 /**
89 * @override
90 * @defaultValue {@link THREE.NearestFilter}
91 */
92 minFilter: MinificationTextureFilter;
93
94 /**
95 * @override
96 * @defaultValue `false`
97 */
98 flipY: boolean;
99
100 /**
101 * @override
102 * @defaultValue `false`
103 */
104 generateMipmaps: boolean;
105
106 /**
107 * @override
108 * @defaultValue `1`
109 */
110 unpackAlignment: number;
111}
112
113export interface TextureImageData {
114 data: Uint8Array | Uint8ClampedArray;
115 height: number;
116 width: number;
117}