-
-
Notifications
You must be signed in to change notification settings - Fork 3k
/
PaletteTexture.js
60 lines (55 loc) · 1.34 KB
/
PaletteTexture.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
/**
* @module ol/webgl/PaletteTexture
*/
class PaletteTexture {
/**
* @param {string} name The name of the texture.
* @param {Uint8Array} data The texture data.
*/
constructor(name, data) {
this.name = name;
this.data = data;
/**
* @type {WebGLTexture|null}
* @private
*/
this.texture_ = null;
}
/**
* @param {WebGLRenderingContext} gl Rendering context.
* @return {WebGLTexture} The texture.
*/
getTexture(gl) {
if (!this.texture_) {
const texture = gl.createTexture();
gl.bindTexture(gl.TEXTURE_2D, texture);
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_S, gl.CLAMP_TO_EDGE);
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_T, gl.CLAMP_TO_EDGE);
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, gl.NEAREST);
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, gl.NEAREST);
gl.texImage2D(
gl.TEXTURE_2D,
0,
gl.RGBA,
this.data.length / 4,
1,
0,
gl.RGBA,
gl.UNSIGNED_BYTE,
this.data,
);
this.texture_ = texture;
}
return this.texture_;
}
/**
* @param {WebGLRenderingContext} gl Rendering context.
*/
delete(gl) {
if (this.texture_) {
gl.deleteTexture(this.texture_);
}
this.texture_ = null;
}
}
export default PaletteTexture;