-
Notifications
You must be signed in to change notification settings - Fork 12
/
texture.js
363 lines (327 loc) · 10.9 KB
/
texture.js
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
import { checkProps } from "./utils.js";
/**
* @typedef {HTMLImageElement | HTMLVideoElement | HTMLCanvasElement} TextureOptionsData
* @property {Array|import("./types.js").TypedArray} data
* @property {number} width
* @property {number} height
*/
/**
* @typedef {WebGLRenderingContext.TEXTURE_2D | WebGLRenderingContext.TEXTURE_CUBE_MAP} TextureTarget
*/
/**
* @typedef {import("./types.js").PexResource} TextureOptions
* @property {HTMLImageElement | HTMLVideoElement | HTMLCanvasElement | Array | import("./types.js").TypedArray |TextureOptionsData | HTMLImageElement[] | TextureOptionsData[]} [data]
* @property {number} [width]
* @property {number} [height]
* @property {ctx.PixelFormat} [pixelFormat=ctx.PixelFormat.RGB8]
* @property {ctx.TextureFormat} [internalFormat=ctx.TextureFormat.RGBA]
* @property {ctx.DataType} [type=ctx.TextureFormat[opts.pixelFormat]]
* @property {ctx.Encoding} [encoding=ctx.Encoding.Linear]
* @property {ctx.Wrap} [wrapS=ctx.Wrap.ClampToEdge]
* @property {ctx.Wrap} [wrapT=ctx.Wrap.ClampToEdge]
* @property {ctx.Wrap} [wrap=ctx.Wrap.ClampToEdge]
* @property {ctx.Filter} [min=ctx.Filter.Nearest]
* @property {ctx.Filter} [mag=ctx.Filter.Nearest]
* @property {number} [aniso=0] requires [EXT_texture_filter_anisotropic](https://www.khronos.org/registry/webgl/extensions/EXT_texture_filter_anisotropic/)
* @property {boolean} [mipmap=true] requires `min` to be set to `ctx.Filter.LinearMipmapLinear` or similar
* @property {boolean} [premultiplyAlpha=false]
* @property {boolean} [flipY=false]
* @property {boolean} [compressed=false]
* @property {TextureTarget} [target]
*/
/**
* @typedef {import("./types.js").PexResource} TextureCubeOptions
* @augments TextureOptions
* @property {HTMLImageElement[]|import("./types.js").TypedArray[]} [data] 6 images, one for each face +X, -X, +Y, -Y, +Z, -Z
*/
const allowedProps = [
"name",
"data",
"width",
"height",
"pixelFormat",
"internalFormat",
"type",
"encoding",
"flipY",
"mipmap",
"target",
"min",
"mag",
"wrap",
"wrapS",
"wrapT",
"aniso",
"premultiplyAlpha",
"compressed",
];
function createTexture(ctx, opts) {
checkProps(allowedProps, opts);
const gl = ctx.gl;
const texture = {
class: "texture",
handle: gl.createTexture(),
target: opts.target,
width: 0,
height: 0,
_update: updateTexture2D,
_dispose() {
gl.deleteTexture(this.handle);
this.handle = null;
},
};
updateTexture2D(ctx, texture, opts);
return texture;
}
function orValue(a, b) {
return a !== undefined ? a : b;
}
function updateTexture2D(ctx, texture, opts) {
// checkProps(allowedProps, opts)
const gl = ctx.gl;
let compressed = opts.compressed || texture.compressed;
let data = null;
let width = opts.width;
let height = opts.height;
let flipY = orValue(opts.flipY, orValue(texture.flipY, false));
let target = opts.target || texture.target;
let pixelFormat =
opts.pixelFormat || texture.pixelFormat || ctx.PixelFormat.RGBA8;
let encoding = opts.encoding || texture.encoding || ctx.Encoding.Linear;
let min = opts.min || texture.min || gl.NEAREST;
let mag = opts.mag || texture.mag || gl.NEAREST;
let wrapS =
opts.wrapS ||
opts.wrap ||
texture.wrapS ||
texture.wrap ||
gl.CLAMP_TO_EDGE;
let wrapT =
opts.wrapT ||
opts.wrap ||
texture.wrapT ||
texture.wrap ||
gl.CLAMP_TO_EDGE;
let aniso = opts.aniso || texture.aniso || 0;
let premultiplyAlpha = orValue(
opts.premultiplyAlpha,
orValue(texture.premultiplyAlpha, false),
);
let internalFormat = opts.internalFormat || texture.internalFormat;
let type;
let format;
const textureUnit = 0;
gl.activeTexture(gl.TEXTURE0 + textureUnit);
gl.bindTexture(texture.target, texture.handle);
ctx.state.activeTextures[textureUnit] = texture;
gl.pixelStorei(gl.UNPACK_FLIP_Y_WEBGL, flipY);
gl.pixelStorei(gl.UNPACK_PREMULTIPLY_ALPHA_WEBGL, premultiplyAlpha);
gl.texParameteri(target, gl.TEXTURE_MAG_FILTER, mag);
gl.texParameteri(target, gl.TEXTURE_MIN_FILTER, min);
gl.texParameteri(target, gl.TEXTURE_WRAP_S, wrapS);
gl.texParameteri(target, gl.TEXTURE_WRAP_T, wrapT);
if (ctx.capabilities.textureFilterAnisotropic && aniso > 0) {
const anisoExt = gl.getExtension("EXT_texture_filter_anisotropic");
gl.texParameterf(target, anisoExt.TEXTURE_MAX_ANISOTROPY_EXT, aniso);
}
const img = opts.data ? opts.data : opts;
if (
(img && img.nodeName) ||
(!ctx.capabilities.isWebGL2 && img instanceof ImageBitmap)
) {
console.assert(
img instanceof HTMLImageElement ||
img instanceof HTMLVideoElement ||
img instanceof HTMLCanvasElement ||
img instanceof ImageBitmap,
"Texture2D.update opts has to be HTMLImageElement, HTMLVideoElement, HTMLCanvasElement or ImageBitmap",
);
width = img.width || img.videoHeight;
height = img.height || img.videoHeight;
internalFormat = gl.RGBA;
format = gl.RGBA;
type = gl.UNSIGNED_BYTE;
pixelFormat = ctx.PixelFormat.RGBA;
gl.texImage2D(target, 0, internalFormat, format, type, img);
texture.width = width;
texture.height = height;
} else if (typeof opts === "object") {
// Check data type
console.assert(
!data ||
Array.isArray(opts.data) ||
Object.values(ctx.DataTypeConstructor).some(
(TypedArray) => opts.data instanceof TypedArray,
),
"Texture2D.update opts.data has to be null, an Array or a TypedArray",
);
// Handle pixel data with flags
data = opts.data ? opts.data.data || opts.data : null;
if (!opts.width && data && data.width) width = data.width;
if (!opts.height && data && data.height) height = data.height;
console.assert(
!data || (width !== undefined && height !== undefined),
"Texture2D.update opts.width and opts.height are required when providing opts.data",
);
// Get internalFormat (format the GPU use internally) from opts.internalFormat (mainly for compressed texture) or pixelFormat
if (!internalFormat || opts.internalFormat) {
internalFormat = opts.internalFormat || gl[pixelFormat];
// WebGL1
if (ctx.gl instanceof WebGLRenderingContext) {
// WEBGL_depth_texture (WebGL1 only) just adds DEPTH_COMPONENT and DEPTH_STENCIL
if (
ctx.capabilities.depthTexture &&
["DEPTH_COMPONENT16", "DEPTH_COMPONENT24"].includes(pixelFormat)
) {
internalFormat = gl["DEPTH_COMPONENT"];
}
// Handle legacy types
if (!internalFormat) {
if (pixelFormat === ctx.PixelFormat.R16F) {
pixelFormat = "R16FLegacy";
internalFormat = gl.ALPHA;
} else if (pixelFormat === ctx.PixelFormat.R32F) {
pixelFormat = "R32FLegacy";
internalFormat = gl.ALPHA;
} else if (pixelFormat === ctx.PixelFormat.RGBA8) {
pixelFormat = ctx.PixelFormat.RGBA;
internalFormat = gl.RGBA;
} else if (
pixelFormat === ctx.PixelFormat.RGBA16F ||
pixelFormat === ctx.PixelFormat.RGBA32F
) {
internalFormat = gl.RGBA;
}
}
}
console.assert(
internalFormat,
`Texture2D.update Unknown internalFormat "${internalFormat}" for pixelFormat "${pixelFormat}".`,
);
}
// Get actual format and type (data supplied), allowing type override
[format, type] = ctx.TextureFormat[pixelFormat];
type = opts.type || type;
console.assert(type, `Texture2D.update Unknown type ${type}.`);
if (target === gl.TEXTURE_2D) {
// Prepare data for mipmaps
data =
Array.isArray(data) && data[0].data ? data : [{ data, width, height }];
for (let level = 0; level < data.length; level++) {
let { data: levelData, width, height } = data[level];
// Convert array of numbers to typed array
if (Array.isArray(levelData)) {
const TypedArray = ctx.DataTypeConstructor[type];
console.assert(TypedArray, `Unknown texture data type: ${type}`);
levelData = new TypedArray(levelData);
}
if (compressed) {
gl.compressedTexImage2D(
target,
level,
internalFormat,
width,
height,
0,
levelData,
);
} else if (width && height) {
gl.texImage2D(
target,
level,
internalFormat,
width,
height,
0,
format,
type,
levelData,
);
}
}
if (data[0].width) texture.width = data[0].width;
if (data[0].height) texture.height = data[0].height;
} else if (target === gl.TEXTURE_CUBE_MAP) {
console.assert(
!data || (Array.isArray(data) && data.length === 6),
"TextureCube requires data for 6 faces",
);
// TODO: gl.compressedTexImage2D, manual mimaps
let lod = 0;
for (let i = 0; i < 6; i++) {
let faceData = data ? data[i].data || data[i] : null;
const faceTarget = gl.TEXTURE_CUBE_MAP_POSITIVE_X + i;
if (Array.isArray(faceData)) {
// Convert array of numbers to typed array
const TypedArray = ctx.DataTypeConstructor[type];
console.assert(TypedArray, `Unknown texture data type: ${type}`);
faceData = new TypedArray(faceData);
gl.texImage2D(
faceTarget,
lod,
internalFormat,
width,
height,
0,
format,
type,
faceData,
);
} else if (faceData && faceData.nodeName) {
gl.texImage2D(
faceTarget,
lod,
internalFormat,
format,
type,
faceData,
);
} else {
gl.texImage2D(
faceTarget,
lod,
internalFormat,
width,
height,
0,
format,
type,
faceData,
);
}
texture.width = width;
texture.height = height;
}
}
} else {
// TODO: should i assert of throw new Error(msg)?
throw new Error(
"Texture2D.update opts has to be a HTMLElement, ImageBitmap or Object",
);
}
if (opts.mipmap) {
gl.generateMipmap(texture.target);
}
texture.compressed = compressed;
texture.target = target;
texture.pixelFormat = pixelFormat;
texture.encoding = encoding;
texture.min = min;
texture.mag = mag;
texture.wrapS = wrapS;
texture.wrapT = wrapT;
texture.format = format;
texture.flipY = flipY;
texture.internalFormat = internalFormat;
texture.type = type;
texture.info = "";
texture.info += Object.keys(ctx.PixelFormat).find(
(key) => ctx.PixelFormat[key] === pixelFormat,
);
texture.info += "_";
texture.info += Object.keys(ctx.Encoding).find(
(key) => ctx.Encoding[key] === encoding,
);
return texture;
}
export default createTexture;