-
Notifications
You must be signed in to change notification settings - Fork 1
/
util.js
116 lines (97 loc) · 2.03 KB
/
util.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
// Global gl pls
function getShader(id) {
var shaderScript = document.getElementById(id);
if (!shaderScript) {
return null;
}
var str = "";
var k = shaderScript.firstChild;
while (k) {
if (k.nodeType == 3) {
str += k.textContent;
}
k = k.nextSibling;
}
var shader;
if (shaderScript.type == "x-shader/x-fragment") {
shader = gl.createShader(gl.FRAGMENT_SHADER);
} else if (shaderScript.type == "x-shader/x-vertex") {
shader = gl.createShader(gl.VERTEX_SHADER);
} else {
return null;
}
gl.shaderSource(shader, str);
gl.compileShader(shader);
if (!gl.getShaderParameter(shader, gl.COMPILE_STATUS)) {
alert(gl.getShaderInfoLog(shader));
return null;
}
return shader;
}
var _texturesLoaded = 0;
function getTexture(src) {
_texturesLoaded += 1;
var tex = gl.createTexture();
tex.image = new Image();
tex.image.onload = function () {
gl.bindTexture(gl.TEXTURE_2D, tex);
gl.pixelStorei(gl.UNPACK_FLIP_Y_WEBGL, true);
gl.texImage2D(gl.TEXTURE_2D, 0, gl.RGBA, gl.RGBA, gl.UNSIGNED_BYTE, tex.image);
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, gl.NEAREST);
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, gl.NEAREST);
gl.bindTexture(gl.TEXTURE_2D, null);
// Tick the game after everything's loaded
_texturesLoaded -= 1;
if (_texturesLoaded == 0)
game.tick();
};
tex.image.src = src;
return tex;
}
function genScaledUV(x, y, z) {
var textureCoords = [
// Front face
0.0, 0.0,
x, 0.0,
x, y,
0.0, 0.0,
x, y,
0.0, y,
// Back face
x, 0.0,
x, y,
0.0, y,
x, 0.0,
0.0, y,
0.0, 0.0,
// Top face
0.0, z,
0.0, 0.0,
x, 0.0,
0.0, z,
x, 0.0,
x, z,
// Bottom face
x, z,
0.0, z,
0.0, 0.0,
x, z,
0.0, 0.0,
x, 0.0,
// Right face
z, 0.0,
z, y,
0.0, y,
z, 0.0,
0.0, y,
0.0, 0.0,
// Left face
0.0, 0.0,
z, 0.0,
z, y,
0.0, 0.0,
z, y,
0.0, y,
];
return textureCoords;
}