forked from pyalot/webgl-heatmap
-
Notifications
You must be signed in to change notification settings - Fork 6
/
heatmap-shader.js
93 lines (83 loc) · 2.41 KB
/
heatmap-shader.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
'use strict';
function HeatmapShader(gl, _arg) {
var fragment, vertex;
this.gl = gl;
vertex = _arg.vertex, fragment = _arg.fragment;
this.program = this.gl.createProgram();
this.vs = this.gl.createShader(this.gl.VERTEX_SHADER);
this.fs = this.gl.createShader(this.gl.FRAGMENT_SHADER);
this.gl.attachShader(this.program, this.vs);
this.gl.attachShader(this.program, this.fs);
this.compileShader(this.vs, vertex);
this.compileShader(this.fs, fragment);
this.link();
this.value_cache = {};
this.uniform_cache = {};
this.attribCache = {};
}
HeatmapShader.prototype.attribLocation = function(name) {
var location;
location = this.attribCache[name];
if (location === void 0) {
location = this.attribCache[name] = this.gl.getAttribLocation(this.program, name);
}
return location;
};
HeatmapShader.prototype.compileShader = function(shader, source) {
this.gl.shaderSource(shader, source);
this.gl.compileShader(shader);
if (!this.gl.getShaderParameter(shader, this.gl.COMPILE_STATUS)) {
throw "Shader Compile Error: " + (this.gl.getShaderInfoLog(shader));
}
};
HeatmapShader.prototype.link = function() {
this.gl.linkProgram(this.program);
if (!this.gl.getProgramParameter(this.program, this.gl.LINK_STATUS)) {
throw "Shader Link Error: " + (this.gl.getProgramInfoLog(this.program));
}
};
HeatmapShader.prototype.use = function() {
this.gl.useProgram(this.program);
return this;
};
HeatmapShader.prototype.uniformLoc = function(name) {
var location;
location = this.uniform_cache[name];
if (location === void 0) {
location = this.uniform_cache[name] = this.gl.getUniformLocation(this.program, name);
}
return location;
};
HeatmapShader.prototype.int = function(name, value) {
var cached, loc;
cached = this.value_cache[name];
if (cached !== value) {
this.value_cache[name] = value;
loc = this.uniformLoc(name);
if (loc) {
this.gl.uniform1i(loc, value);
}
}
return this;
};
HeatmapShader.prototype.vec2 = function(name, a, b) {
var loc;
loc = this.uniformLoc(name);
if (loc) {
this.gl.uniform2f(loc, a, b);
}
return this;
};
HeatmapShader.prototype.float = function(name, value) {
var cached, loc;
cached = this.value_cache[name];
if (cached !== value) {
this.value_cache[name] = value;
loc = this.uniformLoc(name);
if (loc) {
this.gl.uniform1f(loc, value);
}
}
return this;
};
module.exports = HeatmapShader;