-
Notifications
You must be signed in to change notification settings - Fork 68
/
texture.js
353 lines (303 loc) · 12.8 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
///////////////////////////////////////////////////////////////////////////////////
// The MIT License (MIT)
//
// Copyright (c) 2017 Tarek Sherif
//
// Permission is hereby granted, free of charge, to any person obtaining a copy of
// this software and associated documentation files (the "Software"), to deal in
// the Software without restriction, including without limitation the rights to
// use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
// the Software, and to permit persons to whom the Software is furnished to do so,
// subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
// FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
// COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
// IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
// CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
///////////////////////////////////////////////////////////////////////////////////
import {
GL,
WEBGL_INFO,
TEXTURE_FORMATS,
COMPRESSED_TEXTURE_TYPES,
DUMMY_OBJECT,
DUMMY_UNIT_ARRAY,
// DEPRECATED
TEXTURE_FORMAT_DEFAULTS
} from "./constants.js";
/**
General-purpose texture.
@class Texture
@prop {WebGLRenderingContext} gl The WebGL context.
@prop {WebGLTexture} texture Handle to the texture.
@prop {number} width Texture width.
@prop {number} height Texture height.
@prop {number} depth Texture depth.
@prop {GLenum} binding Binding point for the texture.
@prop {GLenum} type Type of data stored in the texture.
@prop {GLenum} format Layout of texture data.
@prop {GLenum} internalFormat Internal arrangement of the texture data.
@prop {number} currentUnit The current texture unit this texture is bound to.
@prop {boolean} is3D Whether this texture contains 3D data.
@prop {boolean} flipY Whether the y-axis is flipped for this texture.
@prop {boolean} premultiplyAlpha Whether alpha should be pre-multiplied when loading this texture.
@prop {boolean} mipmaps Whether this texture is using mipmap filtering
(and thus should have a complete mipmap chain).
@prop {Object} appState Tracked GL state.
*/
export class Texture {
constructor(gl, appState, binding, image, width = image.width, height = image.height, depth, is3D, options = DUMMY_OBJECT) {
this.gl = gl;
this.binding = binding;
this.texture = null;
this.width = width || 0;
this.height = height || 0;
this.depth = depth || 0;
this.is3D = is3D;
this.appState = appState;
this.compressed = Boolean(COMPRESSED_TEXTURE_TYPES[options.internalFormat]);
if (options.format !== undefined) {
console.warn("Texture option 'format' is deprecated and will be removed. Use 'internalFormat' with a sized format instead.");
this.compressed = Boolean(COMPRESSED_TEXTURE_TYPES[options.format]);
if (options.type === undefined) {
options.type = options.format === GL.DEPTH_COMPONENT ? GL.UNSIGNED_SHORT : GL.UNSIGNED_BYTE;
}
if (options.internalFormat === undefined) {
if (this.compressed) {
options.internalFormat = options.format;
} else {
options.internalFormat = TEXTURE_FORMAT_DEFAULTS[options.type][options.format];
}
}
}
if (this.compressed) {
// For compressed textures, just need to provide one of format, internalFormat.
// The other will be the same.
this.internalFormat = options.internalFormat;
this.format = this.internalFormat;
this.type = GL.UNSIGNED_BYTE;
} else {
this.internalFormat = options.internalFormat !== undefined ? options.internalFormat : GL.RGBA8;
let formatInfo = TEXTURE_FORMATS[this.internalFormat];
this.format = formatInfo[0];
this.type = options.type !== undefined ? options.type : formatInfo[1];
}
// -1 indicates unbound
this.currentUnit = -1;
// Sampling parameters
let {
minFilter = image ? GL.LINEAR_MIPMAP_NEAREST : GL.NEAREST,
magFilter = image ? GL.LINEAR : GL.NEAREST,
wrapS = GL.REPEAT,
wrapT = GL.REPEAT,
wrapR = GL.REPEAT,
compareMode = GL.NONE,
compareFunc = GL.LEQUAL,
minLOD = null,
maxLOD = null,
baseLevel = null,
maxLevel = null,
maxAnisotropy = 1,
flipY = false,
premultiplyAlpha = false
} = options;
this.minFilter = minFilter;
this.magFilter = magFilter;
this.wrapS = wrapS;
this.wrapT = wrapT;
this.wrapR = wrapR;
this.compareMode = compareMode;
this.compareFunc = compareFunc;
this.minLOD = minLOD;
this.maxLOD = maxLOD;
this.baseLevel = baseLevel;
this.maxLevel = maxLevel;
this.maxAnisotropy = Math.min(maxAnisotropy, WEBGL_INFO.MAX_TEXTURE_ANISOTROPY);
this.flipY = flipY;
this.premultiplyAlpha = premultiplyAlpha;
this.mipmaps = (minFilter === GL.LINEAR_MIPMAP_NEAREST || minFilter === GL.LINEAR_MIPMAP_LINEAR);
this.restore(image);
}
/**
Restore texture after context loss.
@method
@param {HTMLElement|ArrayBufferView|Array} [image] Image data. An array can be passed to manually set all levels
of the mipmap chain. If a single level is passed and mipmap filtering is being used,
generateMipmap() will be called to produce the remaining levels.
@return {Texture} The Texture object.
*/
restore(image) {
this.texture = null;
this.resize(this.width, this.height, this.depth);
if (image) {
this.data(image);
}
return this;
}
/**
Re-allocate texture storage.
@method
@param {number} width Image width.
@param {number} height Image height.
@param {number} [depth] Image depth or number of images. Required when passing 3D or texture array data.
@return {Texture} The Texture object.
*/
resize(width, height, depth) {
depth = depth || 0;
if (this.texture && width === this.width && height === this.height && depth === this.depth) {
return this;
}
this.gl.deleteTexture(this.texture);
if (this.currentUnit !== -1) {
this.appState.textures[this.currentUnit] = null;
}
this.texture = this.gl.createTexture();
this.bind(Math.max(this.currentUnit, 0));
this.width = width;
this.height = height;
this.depth = depth;
this.gl.texParameteri(this.binding, GL.TEXTURE_MIN_FILTER, this.minFilter);
this.gl.texParameteri(this.binding, GL.TEXTURE_MAG_FILTER, this.magFilter);
this.gl.texParameteri(this.binding, GL.TEXTURE_WRAP_S, this.wrapS);
this.gl.texParameteri(this.binding, GL.TEXTURE_WRAP_T, this.wrapT);
this.gl.texParameteri(this.binding, GL.TEXTURE_WRAP_R, this.wrapR);
this.gl.texParameteri(this.binding, GL.TEXTURE_COMPARE_FUNC, this.compareFunc);
this.gl.texParameteri(this.binding, GL.TEXTURE_COMPARE_MODE, this.compareMode);
if (this.minLOD !== null) {
this.gl.texParameterf(this.binding, GL.TEXTURE_MIN_LOD, this.minLOD);
}
if (this.maxLOD !== null) {
this.gl.texParameterf(this.binding, GL.TEXTURE_MAX_LOD, this.maxLOD);
}
if (this.baseLevel !== null) {
this.gl.texParameteri(this.binding, GL.TEXTURE_BASE_LEVEL, this.baseLevel);
}
if (this.maxLevel !== null) {
this.gl.texParameteri(this.binding, GL.TEXTURE_MAX_LEVEL, this.maxLevel);
}
if (this.maxAnisotropy > 1) {
this.gl.texParameteri(this.binding, GL.TEXTURE_MAX_ANISOTROPY_EXT, this.maxAnisotropy);
}
let levels;
if (this.is3D) {
if (this.mipmaps) {
levels = Math.floor(Math.log2(Math.max(Math.max(this.width, this.height), this.depth))) + 1;
} else {
levels = 1;
}
this.gl.texStorage3D(this.binding, levels, this.internalFormat, this.width, this.height, this.depth);
} else {
if (this.mipmaps) {
levels = Math.floor(Math.log2(Math.max(this.width, this.height))) + 1;
} else {
levels = 1;
}
this.gl.texStorage2D(this.binding, levels, this.internalFormat, this.width, this.height);
}
return this;
}
/**
Set the image data for the texture. An array can be passed to manually set all levels
of the mipmap chain. If a single level is passed and mipmap filtering is being used,
generateMipmap() will be called to produce the remaining levels.
NOTE: the data must fit the currently-allocated storage!
@method
@param {HTMLImageElement|ArrayBufferView|Array} data Image data. If an array is passed, it will be
used to set mip map levels.
@return {Texture} The Texture object.
*/
data(data) {
if (!Array.isArray(data)) {
DUMMY_UNIT_ARRAY[0] = data;
data = DUMMY_UNIT_ARRAY;
}
let numLevels = this.mipmaps ? data.length : 1;
let width = this.width;
let height = this.height;
let depth = this.depth;
let generateMipmaps = this.mipmaps && data.length === 1;
let i;
this.bind(Math.max(this.currentUnit, 0));
this.gl.pixelStorei(GL.UNPACK_FLIP_Y_WEBGL, this.flipY);
this.gl.pixelStorei(GL.UNPACK_PREMULTIPLY_ALPHA_WEBGL, this.premultiplyAlpha);
if (this.compressed) {
if (this.is3D) {
for (i = 0; i < numLevels; ++i) {
this.gl.compressedTexSubImage3D(this.binding, i, 0, 0, 0, width, height, depth, this.format, data[i]);
width = Math.max(width >> 1, 1);
height = Math.max(height >> 1, 1);
depth = Math.max(depth >> 1, 1);
}
} else {
for (i = 0; i < numLevels; ++i) {
this.gl.compressedTexSubImage2D(this.binding, i, 0, 0, width, height, this.format, data[i]);
width = Math.max(width >> 1, 1);
height = Math.max(height >> 1, 1);
}
}
} else if (this.is3D) {
for (i = 0; i < numLevels; ++i) {
this.gl.texSubImage3D(this.binding, i, 0, 0, 0, width, height, depth, this.format, this.type, data[i]);
width = Math.max(width >> 1, 1);
height = Math.max(height >> 1, 1);
depth = Math.max(depth >> 1, 1);
}
} else {
for (i = 0; i < numLevels; ++i) {
this.gl.texSubImage2D(this.binding, i, 0, 0, width, height, this.format, this.type, data[i]);
width = Math.max(width >> 1, 1);
height = Math.max(height >> 1, 1);
}
}
if (generateMipmaps) {
this.gl.generateMipmap(this.binding);
}
return this;
}
/**
Delete this texture.
@method
@return {Texture} The Texture object.
*/
delete() {
if (this.texture) {
this.gl.deleteTexture(this.texture);
this.texture = null;
if (this.currentUnit !== -1 && this.appState.textures[this.currentUnit] === this) {
this.appState.textures[this.currentUnit] = null;
this.currentUnit = -1;
}
}
return this;
}
/**
Bind this texture to a texture unit.
@method
@ignore
@return {Texture} The Texture object.
*/
bind(unit) {
let currentTexture = this.appState.textures[unit];
if (this.appState.activeTexture !== unit) {
this.gl.activeTexture(GL.TEXTURE0 + unit);
this.appState.activeTexture = unit;
}
if (currentTexture !== this) {
if (currentTexture) {
currentTexture.currentUnit = -1;
}
if (this.currentUnit !== -1) {
this.appState.textures[this.currentUnit] = null;
}
this.gl.bindTexture(this.binding, this.texture);
this.appState.textures[unit] = this;
this.currentUnit = unit;
}
return this;
}
}