-
Notifications
You must be signed in to change notification settings - Fork 34
/
sampler.js
36 lines (36 loc) · 1.04 KB
/
sampler.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
import { getTextureFiltering } from './utils';
let _UID = 0;
class Sampler {
constructor(gl) {
this._uid = _UID++;
this.gl = gl;
this.id = gl.createSampler();
this.setFilter(true);
}
bind(unit) {
this.gl.bindSampler(unit, this.id);
}
dispose() {
this.gl.deleteSampler(this.id);
}
setFilter(smooth = false, mipmap = false, miplinear = false) {
const gl = this.gl;
gl.samplerParameteri(this.id, gl.TEXTURE_MAG_FILTER, getTextureFiltering(!!smooth, false, false));
gl.samplerParameteri(this.id, gl.TEXTURE_MIN_FILTER, getTextureFiltering(!!smooth, !!mipmap, !!miplinear));
}
repeat() {
this.wrap(this.gl.REPEAT);
}
clamp() {
this.wrap(this.gl.CLAMP_TO_EDGE);
}
mirror() {
this.wrap(this.gl.MIRRORED_REPEAT);
}
wrap(wrap) {
const gl = this.gl;
gl.samplerParameteri(this.id, gl.TEXTURE_WRAP_S, wrap);
gl.samplerParameteri(this.id, gl.TEXTURE_WRAP_T, wrap);
}
}
export default Sampler;