@thi.ng/webgl 5.0.14 → 6.0.4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +112 -213
- package/README.md +15 -5
- package/api/buffers.d.ts +1 -1
- package/api/canvas.d.ts +1 -1
- package/api/material.d.ts +1 -1
- package/api/model.d.ts +3 -3
- package/api/multipass.d.ts +4 -4
- package/api/shader.d.ts +12 -6
- package/api/texture.d.ts +1 -1
- package/buffer.d.ts +3 -3
- package/buffer.js +4 -4
- package/canvas.d.ts +2 -2
- package/canvas.js +6 -3
- package/checks.d.ts +1 -1
- package/checks.js +1 -1
- package/draw.d.ts +1 -1
- package/draw.js +4 -4
- package/error.d.ts +11 -3
- package/error.js +2 -5
- package/fbo.d.ts +1 -1
- package/fbo.js +5 -5
- package/geo/cube.d.ts +1 -1
- package/geo/cube.js +2 -2
- package/geo/quad.d.ts +1 -1
- package/geo/quad.js +14 -11
- package/index.d.ts +32 -32
- package/index.js +32 -32
- package/{api/logger.d.ts → logger.d.ts} +1 -1
- package/{api/logger.js → logger.js} +1 -1
- package/material.d.ts +2 -2
- package/matrices.d.ts +2 -2
- package/matrices.js +5 -2
- package/multipass.d.ts +1 -1
- package/multipass.js +33 -15
- package/package.json +151 -40
- package/rbo.d.ts +1 -1
- package/rbo.js +1 -1
- package/readpixels.d.ts +1 -1
- package/readpixels.js +1 -1
- package/shader.d.ts +6 -6
- package/shader.js +33 -23
- package/shaders/lambert.d.ts +3 -3
- package/shaders/lambert.js +43 -11
- package/shaders/phong.d.ts +3 -3
- package/shaders/phong.js +38 -10
- package/shaders/pipeline.d.ts +1 -1
- package/shaders/pipeline.js +5 -1
- package/syntax.d.ts +2 -2
- package/syntax.js +2 -2
- package/texture.d.ts +1 -1
- package/texture.js +10 -6
- package/textures/checkerboard.js +9 -2
- package/textures/stripes.js +8 -2
- package/uniforms.d.ts +2 -2
- package/uniforms.js +2 -2
- package/utils.d.ts +4 -4
- package/utils.js +1 -1
- package/lib/index.js +0 -1820
- package/lib/index.js.map +0 -1
- package/lib/index.umd.js +0 -1
- package/lib/index.umd.js.map +0 -1
package/lib/index.js
DELETED
|
@@ -1,1820 +0,0 @@
|
|
|
1
|
-
'use strict';
|
|
2
|
-
|
|
3
|
-
Object.defineProperty(exports, '__esModule', { value: true });
|
|
4
|
-
|
|
5
|
-
var api = require('@thi.ng/api');
|
|
6
|
-
var adaptDpi = require('@thi.ng/adapt-dpi');
|
|
7
|
-
var checks = require('@thi.ng/checks');
|
|
8
|
-
var associative = require('@thi.ng/associative');
|
|
9
|
-
var matrices = require('@thi.ng/matrices');
|
|
10
|
-
var shaderAst = require('@thi.ng/shader-ast');
|
|
11
|
-
var transducers = require('@thi.ng/transducers');
|
|
12
|
-
var errors = require('@thi.ng/errors');
|
|
13
|
-
var memoize = require('@thi.ng/memoize');
|
|
14
|
-
var shaderAstGlsl = require('@thi.ng/shader-ast-glsl');
|
|
15
|
-
var equiv = require('@thi.ng/equiv');
|
|
16
|
-
var vectors = require('@thi.ng/vectors');
|
|
17
|
-
var shaderAstStdlib = require('@thi.ng/shader-ast-stdlib');
|
|
18
|
-
var pixel = require('@thi.ng/pixel');
|
|
19
|
-
|
|
20
|
-
exports.Blend = void 0;
|
|
21
|
-
(function (Blend) {
|
|
22
|
-
Blend[Blend["ZERO"] = 0] = "ZERO";
|
|
23
|
-
Blend[Blend["ONE"] = 1] = "ONE";
|
|
24
|
-
Blend[Blend["SRC_COLOR"] = 768] = "SRC_COLOR";
|
|
25
|
-
Blend[Blend["ONE_MINUS_SRC_COLOR"] = 769] = "ONE_MINUS_SRC_COLOR";
|
|
26
|
-
Blend[Blend["DST_COLOR"] = 774] = "DST_COLOR";
|
|
27
|
-
Blend[Blend["ONE_MINUS_DST_COLOR"] = 775] = "ONE_MINUS_DST_COLOR";
|
|
28
|
-
Blend[Blend["SRC_ALPHA"] = 770] = "SRC_ALPHA";
|
|
29
|
-
Blend[Blend["ONE_MINUS_SRC_ALPHA"] = 771] = "ONE_MINUS_SRC_ALPHA";
|
|
30
|
-
Blend[Blend["DST_ALPHA"] = 772] = "DST_ALPHA";
|
|
31
|
-
Blend[Blend["ONE_MINUS_DST_ALPHA"] = 773] = "ONE_MINUS_DST_ALPHA";
|
|
32
|
-
Blend[Blend["CONSTANT_COLOR"] = 32769] = "CONSTANT_COLOR";
|
|
33
|
-
Blend[Blend["ONE_MINUS_CONSTANT_COLOR"] = 32770] = "ONE_MINUS_CONSTANT_COLOR";
|
|
34
|
-
Blend[Blend["CONSTANT_ALPHA"] = 32771] = "CONSTANT_ALPHA";
|
|
35
|
-
Blend[Blend["ONE_MINUS_CONSTANT_ALPHA"] = 32772] = "ONE_MINUS_CONSTANT_ALPHA";
|
|
36
|
-
Blend[Blend["SRC_ALPHA_SATURATE"] = 776] = "SRC_ALPHA_SATURATE";
|
|
37
|
-
})(exports.Blend || (exports.Blend = {}));
|
|
38
|
-
exports.BlendEquation = void 0;
|
|
39
|
-
(function (BlendEquation) {
|
|
40
|
-
BlendEquation[BlendEquation["FUNC_ADD"] = 32774] = "FUNC_ADD";
|
|
41
|
-
BlendEquation[BlendEquation["FUNC_REVERSE_SUBTRACT"] = 32779] = "FUNC_REVERSE_SUBTRACT";
|
|
42
|
-
BlendEquation[BlendEquation["FUNC_SUBTRACT"] = 32778] = "FUNC_SUBTRACT";
|
|
43
|
-
BlendEquation[BlendEquation["MAX"] = 32776] = "MAX";
|
|
44
|
-
BlendEquation[BlendEquation["MIN"] = 32775] = "MIN";
|
|
45
|
-
})(exports.BlendEquation || (exports.BlendEquation = {}));
|
|
46
|
-
const BLEND_NORMAL = [
|
|
47
|
-
exports.Blend.SRC_ALPHA,
|
|
48
|
-
exports.Blend.ONE_MINUS_SRC_ALPHA,
|
|
49
|
-
];
|
|
50
|
-
const BLEND_ADD = [exports.Blend.SRC_ALPHA, exports.Blend.DST_ALPHA];
|
|
51
|
-
|
|
52
|
-
const GL_EXT_INFO = {
|
|
53
|
-
WEBGL_draw_buffers: {
|
|
54
|
-
gl: true,
|
|
55
|
-
alias: "GL_EXT_draw_buffers",
|
|
56
|
-
},
|
|
57
|
-
OES_standard_derivatives: {
|
|
58
|
-
gl: true,
|
|
59
|
-
alias: "GL_OES_standard_derivatives",
|
|
60
|
-
},
|
|
61
|
-
};
|
|
62
|
-
|
|
63
|
-
exports.LOGGER = api.NULL_LOGGER;
|
|
64
|
-
const setLogger = (logger) => (exports.LOGGER = logger);
|
|
65
|
-
|
|
66
|
-
exports.DrawMode = void 0;
|
|
67
|
-
(function (DrawMode) {
|
|
68
|
-
DrawMode[DrawMode["POINTS"] = 0] = "POINTS";
|
|
69
|
-
DrawMode[DrawMode["LINES"] = 1] = "LINES";
|
|
70
|
-
DrawMode[DrawMode["LINE_LOOP"] = 2] = "LINE_LOOP";
|
|
71
|
-
DrawMode[DrawMode["LINE_STRIP"] = 3] = "LINE_STRIP";
|
|
72
|
-
DrawMode[DrawMode["TRIANGLES"] = 4] = "TRIANGLES";
|
|
73
|
-
DrawMode[DrawMode["TRIANGLE_STRIP"] = 5] = "TRIANGLE_STRIP";
|
|
74
|
-
DrawMode[DrawMode["TRIANGLE_FAN"] = 6] = "TRIANGLE_FAN";
|
|
75
|
-
})(exports.DrawMode || (exports.DrawMode = {}));
|
|
76
|
-
|
|
77
|
-
const DEFAULT_OUTPUT = { fragColor: ["vec4", 0] };
|
|
78
|
-
|
|
79
|
-
exports.TextureFormat = void 0;
|
|
80
|
-
(function (TextureFormat) {
|
|
81
|
-
TextureFormat[TextureFormat["ALPHA"] = 6406] = "ALPHA";
|
|
82
|
-
TextureFormat[TextureFormat["DEPTH_COMPONENT"] = 6402] = "DEPTH_COMPONENT";
|
|
83
|
-
TextureFormat[TextureFormat["DEPTH_COMPONENT16"] = 33189] = "DEPTH_COMPONENT16";
|
|
84
|
-
TextureFormat[TextureFormat["DEPTH_COMPONENT24"] = 33190] = "DEPTH_COMPONENT24";
|
|
85
|
-
TextureFormat[TextureFormat["DEPTH_COMPONENT32F"] = 36012] = "DEPTH_COMPONENT32F";
|
|
86
|
-
TextureFormat[TextureFormat["DEPTH_STENCIL"] = 34041] = "DEPTH_STENCIL";
|
|
87
|
-
TextureFormat[TextureFormat["DEPTH24_STENCIL8"] = 35056] = "DEPTH24_STENCIL8";
|
|
88
|
-
TextureFormat[TextureFormat["DEPTH32F_STENCIL8"] = 36013] = "DEPTH32F_STENCIL8";
|
|
89
|
-
TextureFormat[TextureFormat["LUMINANCE"] = 6409] = "LUMINANCE";
|
|
90
|
-
TextureFormat[TextureFormat["LUMINANCE_ALPHA"] = 6410] = "LUMINANCE_ALPHA";
|
|
91
|
-
TextureFormat[TextureFormat["R11F_G11F_B10F"] = 35898] = "R11F_G11F_B10F";
|
|
92
|
-
TextureFormat[TextureFormat["R16F"] = 33325] = "R16F";
|
|
93
|
-
TextureFormat[TextureFormat["R16I"] = 33331] = "R16I";
|
|
94
|
-
TextureFormat[TextureFormat["R16UI"] = 33332] = "R16UI";
|
|
95
|
-
TextureFormat[TextureFormat["R32F"] = 33326] = "R32F";
|
|
96
|
-
TextureFormat[TextureFormat["R32I"] = 33333] = "R32I";
|
|
97
|
-
TextureFormat[TextureFormat["R32UI"] = 33334] = "R32UI";
|
|
98
|
-
TextureFormat[TextureFormat["R8"] = 33321] = "R8";
|
|
99
|
-
TextureFormat[TextureFormat["R8_SNORM"] = 36756] = "R8_SNORM";
|
|
100
|
-
TextureFormat[TextureFormat["R8I"] = 33329] = "R8I";
|
|
101
|
-
TextureFormat[TextureFormat["R8UI"] = 33330] = "R8UI";
|
|
102
|
-
TextureFormat[TextureFormat["RED"] = 6403] = "RED";
|
|
103
|
-
TextureFormat[TextureFormat["RED_INTEGER"] = 36244] = "RED_INTEGER";
|
|
104
|
-
TextureFormat[TextureFormat["RG"] = 33319] = "RG";
|
|
105
|
-
TextureFormat[TextureFormat["RG_INTEGER"] = 33320] = "RG_INTEGER";
|
|
106
|
-
TextureFormat[TextureFormat["RG16F"] = 33327] = "RG16F";
|
|
107
|
-
TextureFormat[TextureFormat["RG16I"] = 33337] = "RG16I";
|
|
108
|
-
TextureFormat[TextureFormat["RG16UI"] = 33338] = "RG16UI";
|
|
109
|
-
TextureFormat[TextureFormat["RG32F"] = 33328] = "RG32F";
|
|
110
|
-
TextureFormat[TextureFormat["RG32I"] = 33339] = "RG32I";
|
|
111
|
-
TextureFormat[TextureFormat["RG32UI"] = 33340] = "RG32UI";
|
|
112
|
-
TextureFormat[TextureFormat["RG8"] = 33323] = "RG8";
|
|
113
|
-
TextureFormat[TextureFormat["RG8_SNORM"] = 36757] = "RG8_SNORM";
|
|
114
|
-
TextureFormat[TextureFormat["RG8I"] = 33335] = "RG8I";
|
|
115
|
-
TextureFormat[TextureFormat["RG8UI"] = 33336] = "RG8UI";
|
|
116
|
-
TextureFormat[TextureFormat["RGB"] = 6407] = "RGB";
|
|
117
|
-
TextureFormat[TextureFormat["RGB_INTEGER"] = 36248] = "RGB_INTEGER";
|
|
118
|
-
TextureFormat[TextureFormat["RGB10_A2"] = 32857] = "RGB10_A2";
|
|
119
|
-
TextureFormat[TextureFormat["RGB10_A2UI"] = 36975] = "RGB10_A2UI";
|
|
120
|
-
TextureFormat[TextureFormat["RGB16F"] = 34843] = "RGB16F";
|
|
121
|
-
TextureFormat[TextureFormat["RGB16I"] = 36233] = "RGB16I";
|
|
122
|
-
TextureFormat[TextureFormat["RGB16UI"] = 36215] = "RGB16UI";
|
|
123
|
-
TextureFormat[TextureFormat["RGB32F"] = 34837] = "RGB32F";
|
|
124
|
-
TextureFormat[TextureFormat["RGB32I"] = 36227] = "RGB32I";
|
|
125
|
-
TextureFormat[TextureFormat["RGB32UI"] = 36209] = "RGB32UI";
|
|
126
|
-
TextureFormat[TextureFormat["RGB5_A1"] = 32855] = "RGB5_A1";
|
|
127
|
-
TextureFormat[TextureFormat["RGB565"] = 36194] = "RGB565";
|
|
128
|
-
TextureFormat[TextureFormat["RGB8"] = 32849] = "RGB8";
|
|
129
|
-
TextureFormat[TextureFormat["RGB8_SNORM"] = 36758] = "RGB8_SNORM";
|
|
130
|
-
TextureFormat[TextureFormat["RGB8I"] = 36239] = "RGB8I";
|
|
131
|
-
TextureFormat[TextureFormat["RGB8UI"] = 36221] = "RGB8UI";
|
|
132
|
-
TextureFormat[TextureFormat["RGB9_E5"] = 35901] = "RGB9_E5";
|
|
133
|
-
TextureFormat[TextureFormat["RGBA"] = 6408] = "RGBA";
|
|
134
|
-
TextureFormat[TextureFormat["RGBA_INTEGER"] = 36249] = "RGBA_INTEGER";
|
|
135
|
-
TextureFormat[TextureFormat["RGBA16F"] = 34842] = "RGBA16F";
|
|
136
|
-
TextureFormat[TextureFormat["RGBA16I"] = 36232] = "RGBA16I";
|
|
137
|
-
TextureFormat[TextureFormat["RGBA16UI"] = 36214] = "RGBA16UI";
|
|
138
|
-
TextureFormat[TextureFormat["RGBA32F"] = 34836] = "RGBA32F";
|
|
139
|
-
TextureFormat[TextureFormat["RGBA32I"] = 36226] = "RGBA32I";
|
|
140
|
-
TextureFormat[TextureFormat["RGBA32UI"] = 36208] = "RGBA32UI";
|
|
141
|
-
TextureFormat[TextureFormat["RGBA4"] = 32854] = "RGBA4";
|
|
142
|
-
TextureFormat[TextureFormat["RGBA8"] = 32856] = "RGBA8";
|
|
143
|
-
TextureFormat[TextureFormat["RGBA8_SNORM"] = 36759] = "RGBA8_SNORM";
|
|
144
|
-
TextureFormat[TextureFormat["RGBA8I"] = 36238] = "RGBA8I";
|
|
145
|
-
TextureFormat[TextureFormat["RGBA8UI"] = 36220] = "RGBA8UI";
|
|
146
|
-
TextureFormat[TextureFormat["SRGB8"] = 35905] = "SRGB8";
|
|
147
|
-
TextureFormat[TextureFormat["SRGB8_ALPHA8"] = 35907] = "SRGB8_ALPHA8";
|
|
148
|
-
})(exports.TextureFormat || (exports.TextureFormat = {}));
|
|
149
|
-
exports.TextureType = void 0;
|
|
150
|
-
(function (TextureType) {
|
|
151
|
-
TextureType[TextureType["BYTE"] = 5120] = "BYTE";
|
|
152
|
-
TextureType[TextureType["UNSIGNED_BYTE"] = 5121] = "UNSIGNED_BYTE";
|
|
153
|
-
TextureType[TextureType["SHORT"] = 5122] = "SHORT";
|
|
154
|
-
TextureType[TextureType["UNSIGNED_SHORT"] = 5123] = "UNSIGNED_SHORT";
|
|
155
|
-
TextureType[TextureType["INT"] = 5124] = "INT";
|
|
156
|
-
TextureType[TextureType["UNSIGNED_INT"] = 5125] = "UNSIGNED_INT";
|
|
157
|
-
TextureType[TextureType["FLOAT"] = 5126] = "FLOAT";
|
|
158
|
-
TextureType[TextureType["HALF_FLOAT"] = 5131] = "HALF_FLOAT";
|
|
159
|
-
TextureType[TextureType["UNSIGNED_SHORT_4_4_4_4"] = 32819] = "UNSIGNED_SHORT_4_4_4_4";
|
|
160
|
-
TextureType[TextureType["UNSIGNED_SHORT_5_5_5_1"] = 32820] = "UNSIGNED_SHORT_5_5_5_1";
|
|
161
|
-
TextureType[TextureType["UNSIGNED_SHORT_5_6_5"] = 33635] = "UNSIGNED_SHORT_5_6_5";
|
|
162
|
-
TextureType[TextureType["UNSIGNED_INT_2_10_10_10_REV"] = 33640] = "UNSIGNED_INT_2_10_10_10_REV";
|
|
163
|
-
TextureType[TextureType["UNSIGNED_INT_24_8"] = 34042] = "UNSIGNED_INT_24_8";
|
|
164
|
-
TextureType[TextureType["UNSIGNED_INT_10F_11F_11F_REV"] = 35899] = "UNSIGNED_INT_10F_11F_11F_REV";
|
|
165
|
-
TextureType[TextureType["UNSIGNED_INT_5_9_9_9_REV"] = 35902] = "UNSIGNED_INT_5_9_9_9_REV";
|
|
166
|
-
TextureType[TextureType["HALF_FLOAT_OES"] = 36193] = "HALF_FLOAT_OES";
|
|
167
|
-
TextureType[TextureType["FLOAT_32_UNSIGNED_INT_24_8_REV"] = 36269] = "FLOAT_32_UNSIGNED_INT_24_8_REV";
|
|
168
|
-
})(exports.TextureType || (exports.TextureType = {}));
|
|
169
|
-
exports.TextureTarget = void 0;
|
|
170
|
-
(function (TextureTarget) {
|
|
171
|
-
TextureTarget[TextureTarget["TEXTURE_2D"] = 3553] = "TEXTURE_2D";
|
|
172
|
-
TextureTarget[TextureTarget["TEXTURE_3D"] = 32879] = "TEXTURE_3D";
|
|
173
|
-
TextureTarget[TextureTarget["TEXTURE_CUBE_MAP"] = 34067] = "TEXTURE_CUBE_MAP";
|
|
174
|
-
TextureTarget[TextureTarget["TEXTURE_2D_ARRAY"] = 35866] = "TEXTURE_2D_ARRAY";
|
|
175
|
-
})(exports.TextureTarget || (exports.TextureTarget = {}));
|
|
176
|
-
exports.TextureFilter = void 0;
|
|
177
|
-
(function (TextureFilter) {
|
|
178
|
-
TextureFilter[TextureFilter["LINEAR"] = 9729] = "LINEAR";
|
|
179
|
-
TextureFilter[TextureFilter["NEAREST"] = 9728] = "NEAREST";
|
|
180
|
-
TextureFilter[TextureFilter["NEAREST_MIPMAP_NEAREST"] = 9984] = "NEAREST_MIPMAP_NEAREST";
|
|
181
|
-
TextureFilter[TextureFilter["LINEAR_MIPMAP_NEAREST"] = 9985] = "LINEAR_MIPMAP_NEAREST";
|
|
182
|
-
TextureFilter[TextureFilter["NEAREST_MIPMAP_LINEAR"] = 9986] = "NEAREST_MIPMAP_LINEAR";
|
|
183
|
-
TextureFilter[TextureFilter["LINEAR_MIPMAP_LINEAR"] = 9987] = "LINEAR_MIPMAP_LINEAR";
|
|
184
|
-
})(exports.TextureFilter || (exports.TextureFilter = {}));
|
|
185
|
-
exports.TextureRepeat = void 0;
|
|
186
|
-
(function (TextureRepeat) {
|
|
187
|
-
TextureRepeat[TextureRepeat["REPEAT"] = 10497] = "REPEAT";
|
|
188
|
-
TextureRepeat[TextureRepeat["CLAMP"] = 33071] = "CLAMP";
|
|
189
|
-
TextureRepeat[TextureRepeat["REPEAT_MIRROR"] = 33648] = "REPEAT_MIRROR";
|
|
190
|
-
})(exports.TextureRepeat || (exports.TextureRepeat = {}));
|
|
191
|
-
const $$1 = (format, types, num, render = false, filter = false, renderExt = render) => ({
|
|
192
|
-
format,
|
|
193
|
-
types,
|
|
194
|
-
render,
|
|
195
|
-
renderExt,
|
|
196
|
-
filter,
|
|
197
|
-
num,
|
|
198
|
-
});
|
|
199
|
-
const TEX_FORMATS = {
|
|
200
|
-
[exports.TextureFormat.ALPHA]: $$1(exports.TextureFormat.ALPHA, [
|
|
201
|
-
exports.TextureType.UNSIGNED_BYTE,
|
|
202
|
-
1,
|
|
203
|
-
exports.TextureType.HALF_FLOAT,
|
|
204
|
-
2,
|
|
205
|
-
exports.TextureType.HALF_FLOAT_OES,
|
|
206
|
-
2,
|
|
207
|
-
exports.TextureType.FLOAT,
|
|
208
|
-
4,
|
|
209
|
-
], 1, true, true),
|
|
210
|
-
[exports.TextureFormat.DEPTH_COMPONENT]: $$1(exports.TextureFormat.DEPTH_COMPONENT, [exports.TextureType.UNSIGNED_SHORT, 2, exports.TextureType.UNSIGNED_INT, 4], 1, true),
|
|
211
|
-
[exports.TextureFormat.DEPTH_COMPONENT16]: $$1(exports.TextureFormat.DEPTH_COMPONENT, [exports.TextureType.UNSIGNED_SHORT, 2, exports.TextureType.UNSIGNED_INT, 4], 1, true),
|
|
212
|
-
[exports.TextureFormat.DEPTH_COMPONENT24]: $$1(exports.TextureFormat.DEPTH_COMPONENT, [exports.TextureType.UNSIGNED_INT, 4], 1, true),
|
|
213
|
-
[exports.TextureFormat.DEPTH_COMPONENT32F]: $$1(exports.TextureFormat.DEPTH_COMPONENT, [exports.TextureType.FLOAT, 4], 1, true),
|
|
214
|
-
[exports.TextureFormat.DEPTH_STENCIL]: $$1(exports.TextureFormat.DEPTH_STENCIL, [exports.TextureType.UNSIGNED_INT_24_8, 4], 1, true),
|
|
215
|
-
[exports.TextureFormat.DEPTH24_STENCIL8]: $$1(exports.TextureFormat.DEPTH_STENCIL, [exports.TextureType.UNSIGNED_INT_24_8, 4], 1, true),
|
|
216
|
-
[exports.TextureFormat.DEPTH32F_STENCIL8]: $$1(exports.TextureFormat.DEPTH_STENCIL, [exports.TextureType.FLOAT_32_UNSIGNED_INT_24_8_REV, 4], 1, true),
|
|
217
|
-
[exports.TextureFormat.LUMINANCE_ALPHA]: $$1(exports.TextureFormat.LUMINANCE_ALPHA, [
|
|
218
|
-
exports.TextureType.UNSIGNED_BYTE,
|
|
219
|
-
2,
|
|
220
|
-
exports.TextureType.HALF_FLOAT,
|
|
221
|
-
4,
|
|
222
|
-
exports.TextureType.HALF_FLOAT_OES,
|
|
223
|
-
4,
|
|
224
|
-
exports.TextureType.FLOAT,
|
|
225
|
-
8,
|
|
226
|
-
], 2, true, true),
|
|
227
|
-
[exports.TextureFormat.LUMINANCE]: $$1(exports.TextureFormat.LUMINANCE, [
|
|
228
|
-
exports.TextureType.UNSIGNED_BYTE,
|
|
229
|
-
1,
|
|
230
|
-
exports.TextureType.HALF_FLOAT,
|
|
231
|
-
2,
|
|
232
|
-
exports.TextureType.HALF_FLOAT_OES,
|
|
233
|
-
2,
|
|
234
|
-
exports.TextureType.FLOAT,
|
|
235
|
-
4,
|
|
236
|
-
], 1, true, true),
|
|
237
|
-
[exports.TextureFormat.R11F_G11F_B10F]: $$1(exports.TextureFormat.RGB, [
|
|
238
|
-
exports.TextureType.FLOAT,
|
|
239
|
-
12,
|
|
240
|
-
exports.TextureType.HALF_FLOAT,
|
|
241
|
-
6,
|
|
242
|
-
exports.TextureType.UNSIGNED_INT_10F_11F_11F_REV,
|
|
243
|
-
4,
|
|
244
|
-
], 3, false, true, true),
|
|
245
|
-
[exports.TextureFormat.R16F]: $$1(exports.TextureFormat.RED, [exports.TextureType.FLOAT, 4, exports.TextureType.HALF_FLOAT, 2], 1, false, true, true),
|
|
246
|
-
[exports.TextureFormat.R16I]: $$1(exports.TextureFormat.RED_INTEGER, [exports.TextureType.SHORT, 2], 1, true),
|
|
247
|
-
[exports.TextureFormat.R16UI]: $$1(exports.TextureFormat.RED_INTEGER, [exports.TextureType.UNSIGNED_SHORT, 2], 1, true),
|
|
248
|
-
[exports.TextureFormat.R32F]: $$1(exports.TextureFormat.RED, [exports.TextureType.FLOAT, 4], 1, false, false, true),
|
|
249
|
-
[exports.TextureFormat.R32I]: $$1(exports.TextureFormat.RED_INTEGER, [exports.TextureType.INT, 4], 1, true),
|
|
250
|
-
[exports.TextureFormat.R32UI]: $$1(exports.TextureFormat.RED_INTEGER, [exports.TextureType.UNSIGNED_INT, 4], 1, true),
|
|
251
|
-
[exports.TextureFormat.R8_SNORM]: $$1(exports.TextureFormat.RED, [exports.TextureType.BYTE, 1], 1, false, true),
|
|
252
|
-
[exports.TextureFormat.R8]: $$1(exports.TextureFormat.RED, [exports.TextureType.UNSIGNED_BYTE, 1], 1, true, true),
|
|
253
|
-
[exports.TextureFormat.R8I]: $$1(exports.TextureFormat.RED_INTEGER, [exports.TextureType.BYTE, 1], 1, true),
|
|
254
|
-
[exports.TextureFormat.R8UI]: $$1(exports.TextureFormat.RED_INTEGER, [exports.TextureType.UNSIGNED_BYTE, 1], 1, true),
|
|
255
|
-
[exports.TextureFormat.RG16F]: $$1(exports.TextureFormat.RG, [exports.TextureType.FLOAT, 8, exports.TextureType.HALF_FLOAT, 4], 2, false, true, true),
|
|
256
|
-
[exports.TextureFormat.RG16I]: $$1(exports.TextureFormat.RG_INTEGER, [exports.TextureType.SHORT, 4], 2, true),
|
|
257
|
-
[exports.TextureFormat.RG16UI]: $$1(exports.TextureFormat.RG_INTEGER, [exports.TextureType.UNSIGNED_SHORT, 4], 2, true),
|
|
258
|
-
[exports.TextureFormat.RG32F]: $$1(exports.TextureFormat.RG, [exports.TextureType.FLOAT, 8], 2, false, false, true),
|
|
259
|
-
[exports.TextureFormat.RG32I]: $$1(exports.TextureFormat.RG_INTEGER, [exports.TextureType.INT, 8], 2, true),
|
|
260
|
-
[exports.TextureFormat.RG32UI]: $$1(exports.TextureFormat.RG_INTEGER, [exports.TextureType.UNSIGNED_INT, 8], 2, true),
|
|
261
|
-
[exports.TextureFormat.RG8_SNORM]: $$1(exports.TextureFormat.RG, [exports.TextureType.BYTE, 2], 2, false, true),
|
|
262
|
-
[exports.TextureFormat.RG8]: $$1(exports.TextureFormat.RG, [exports.TextureType.UNSIGNED_BYTE, 2], 2, true, true),
|
|
263
|
-
[exports.TextureFormat.RG8I]: $$1(exports.TextureFormat.RG_INTEGER, [exports.TextureType.BYTE, 2], 2, true),
|
|
264
|
-
[exports.TextureFormat.RG8UI]: $$1(exports.TextureFormat.RG_INTEGER, [exports.TextureType.UNSIGNED_BYTE, 2], 2, true),
|
|
265
|
-
[exports.TextureFormat.RGB]: $$1(exports.TextureFormat.RGB, [
|
|
266
|
-
exports.TextureType.UNSIGNED_BYTE,
|
|
267
|
-
3,
|
|
268
|
-
exports.TextureType.HALF_FLOAT,
|
|
269
|
-
6,
|
|
270
|
-
exports.TextureType.HALF_FLOAT_OES,
|
|
271
|
-
6,
|
|
272
|
-
exports.TextureType.FLOAT,
|
|
273
|
-
12,
|
|
274
|
-
exports.TextureType.UNSIGNED_SHORT_5_6_5,
|
|
275
|
-
2,
|
|
276
|
-
], 3, true, true),
|
|
277
|
-
[exports.TextureFormat.RGB10_A2]: $$1(exports.TextureFormat.RGBA, [exports.TextureType.UNSIGNED_INT_2_10_10_10_REV, 4], 4, true, true),
|
|
278
|
-
[exports.TextureFormat.RGB10_A2UI]: $$1(exports.TextureFormat.RGBA_INTEGER, [exports.TextureType.UNSIGNED_INT_2_10_10_10_REV, 4], 4, true),
|
|
279
|
-
[exports.TextureFormat.RGB16F]: $$1(exports.TextureFormat.RGB, [exports.TextureType.FLOAT, 12, exports.TextureType.HALF_FLOAT, 6], 3, false, true),
|
|
280
|
-
[exports.TextureFormat.RGB16I]: $$1(exports.TextureFormat.RGB_INTEGER, [exports.TextureType.SHORT, 6], 3),
|
|
281
|
-
[exports.TextureFormat.RGB16UI]: $$1(exports.TextureFormat.RGB_INTEGER, [exports.TextureType.UNSIGNED_SHORT, 6], 3),
|
|
282
|
-
[exports.TextureFormat.RGB32F]: $$1(exports.TextureFormat.RGB, [exports.TextureType.FLOAT, 12], 3),
|
|
283
|
-
[exports.TextureFormat.RGB32I]: $$1(exports.TextureFormat.RGB_INTEGER, [exports.TextureType.INT, 12], 3),
|
|
284
|
-
[exports.TextureFormat.RGB32UI]: $$1(exports.TextureFormat.RGB_INTEGER, [exports.TextureType.UNSIGNED_INT, 12], 3),
|
|
285
|
-
[exports.TextureFormat.RGB5_A1]: $$1(exports.TextureFormat.RGBA, [
|
|
286
|
-
exports.TextureType.UNSIGNED_BYTE,
|
|
287
|
-
4,
|
|
288
|
-
exports.TextureType.UNSIGNED_SHORT_5_5_5_1,
|
|
289
|
-
2,
|
|
290
|
-
exports.TextureType.UNSIGNED_INT_2_10_10_10_REV,
|
|
291
|
-
4,
|
|
292
|
-
], 4, true, true),
|
|
293
|
-
[exports.TextureFormat.RGB565]: $$1(exports.TextureFormat.RGB, [exports.TextureType.UNSIGNED_BYTE, 3, exports.TextureType.UNSIGNED_SHORT_5_6_5, 2], 3, true, true),
|
|
294
|
-
[exports.TextureFormat.RGB8_SNORM]: $$1(exports.TextureFormat.RGB, [exports.TextureType.BYTE, 3], 3, false, true),
|
|
295
|
-
[exports.TextureFormat.RGB8]: $$1(exports.TextureFormat.RGB, [exports.TextureType.UNSIGNED_BYTE, 3], 3, true, true),
|
|
296
|
-
[exports.TextureFormat.RGB8I]: $$1(exports.TextureFormat.RGB_INTEGER, [exports.TextureType.BYTE, 3], 3),
|
|
297
|
-
[exports.TextureFormat.RGB8UI]: $$1(exports.TextureFormat.RGB_INTEGER, [exports.TextureType.UNSIGNED_BYTE, 3], 3),
|
|
298
|
-
[exports.TextureFormat.RGB9_E5]: $$1(exports.TextureFormat.RGB, [
|
|
299
|
-
exports.TextureType.FLOAT,
|
|
300
|
-
12,
|
|
301
|
-
exports.TextureType.HALF_FLOAT,
|
|
302
|
-
6,
|
|
303
|
-
exports.TextureType.UNSIGNED_INT_5_9_9_9_REV,
|
|
304
|
-
4,
|
|
305
|
-
], 3, false, true),
|
|
306
|
-
[exports.TextureFormat.RGBA]: $$1(exports.TextureFormat.RGBA, [
|
|
307
|
-
exports.TextureType.UNSIGNED_BYTE,
|
|
308
|
-
4,
|
|
309
|
-
exports.TextureType.HALF_FLOAT,
|
|
310
|
-
8,
|
|
311
|
-
exports.TextureType.HALF_FLOAT_OES,
|
|
312
|
-
8,
|
|
313
|
-
exports.TextureType.FLOAT,
|
|
314
|
-
16,
|
|
315
|
-
exports.TextureType.UNSIGNED_SHORT_4_4_4_4,
|
|
316
|
-
2,
|
|
317
|
-
exports.TextureType.UNSIGNED_SHORT_5_5_5_1,
|
|
318
|
-
2,
|
|
319
|
-
], 4, true, true),
|
|
320
|
-
[exports.TextureFormat.RGBA16F]: $$1(exports.TextureFormat.RGBA, [exports.TextureType.FLOAT, 16, exports.TextureType.HALF_FLOAT, 8], 4, false, true, true),
|
|
321
|
-
[exports.TextureFormat.RGBA16I]: $$1(exports.TextureFormat.RGBA_INTEGER, [exports.TextureType.SHORT, 8], 4, true),
|
|
322
|
-
[exports.TextureFormat.RGBA16UI]: $$1(exports.TextureFormat.RGBA_INTEGER, [exports.TextureType.UNSIGNED_SHORT, 8], 4, true),
|
|
323
|
-
[exports.TextureFormat.RGBA32F]: $$1(exports.TextureFormat.RGBA, [exports.TextureType.FLOAT, 16], 4, false, false, true),
|
|
324
|
-
[exports.TextureFormat.RGBA32I]: $$1(exports.TextureFormat.RGBA_INTEGER, [exports.TextureType.INT, 16], 4, true),
|
|
325
|
-
[exports.TextureFormat.RGBA32UI]: $$1(exports.TextureFormat.RGBA_INTEGER, [exports.TextureType.UNSIGNED_INT, 16], 4, true),
|
|
326
|
-
[exports.TextureFormat.RGBA4]: $$1(exports.TextureFormat.RGBA, [exports.TextureType.UNSIGNED_BYTE, 4, exports.TextureType.UNSIGNED_SHORT_4_4_4_4, 2], 4, true, true),
|
|
327
|
-
[exports.TextureFormat.RGBA8_SNORM]: $$1(exports.TextureFormat.RGBA, [exports.TextureType.BYTE, 4], 4, false, true),
|
|
328
|
-
[exports.TextureFormat.RGBA8]: $$1(exports.TextureFormat.RGBA, [exports.TextureType.UNSIGNED_BYTE, 4], 4, true, true),
|
|
329
|
-
[exports.TextureFormat.RGBA8I]: $$1(exports.TextureFormat.RGBA_INTEGER, [exports.TextureType.BYTE, 4], 4, true),
|
|
330
|
-
[exports.TextureFormat.RGBA8UI]: $$1(exports.TextureFormat.RGBA_INTEGER, [exports.TextureType.UNSIGNED_BYTE, 4], 4, true),
|
|
331
|
-
[exports.TextureFormat.SRGB8_ALPHA8]: $$1(exports.TextureFormat.RGBA, [exports.TextureType.UNSIGNED_BYTE, 4], 4, true, true),
|
|
332
|
-
[exports.TextureFormat.SRGB8]: $$1(exports.TextureFormat.RGB, [exports.TextureType.UNSIGNED_BYTE, 3], 3, false, true),
|
|
333
|
-
};
|
|
334
|
-
|
|
335
|
-
const isGL2Context = (gl) => typeof WebGL2RenderingContext !== "undefined" &&
|
|
336
|
-
gl instanceof WebGL2RenderingContext;
|
|
337
|
-
const isFloatTexture = (tex) => tex.type === exports.TextureType.FLOAT ||
|
|
338
|
-
tex.type === exports.TextureType.HALF_FLOAT ||
|
|
339
|
-
tex.type === exports.TextureType.HALF_FLOAT_OES ||
|
|
340
|
-
tex.type === exports.TextureType.FLOAT_32_UNSIGNED_INT_24_8_REV;
|
|
341
|
-
|
|
342
|
-
class WebGLError extends Error {
|
|
343
|
-
constructor(msg) {
|
|
344
|
-
super(`WebGL error ${msg ? ": " + msg : ""}`);
|
|
345
|
-
}
|
|
346
|
-
}
|
|
347
|
-
const error = (msg) => {
|
|
348
|
-
throw new WebGLError(msg);
|
|
349
|
-
};
|
|
350
|
-
|
|
351
|
-
class WebGLArrayBuffer {
|
|
352
|
-
constructor(gl, data, target = gl.ARRAY_BUFFER, mode = gl.STATIC_DRAW) {
|
|
353
|
-
this.gl = gl;
|
|
354
|
-
this.buffer = gl.createBuffer() || error("error creating WebGL buffer");
|
|
355
|
-
this.target = target;
|
|
356
|
-
this.mode = mode;
|
|
357
|
-
data && this.set(data);
|
|
358
|
-
}
|
|
359
|
-
bind() {
|
|
360
|
-
this.gl.bindBuffer(this.target, this.buffer);
|
|
361
|
-
return true;
|
|
362
|
-
}
|
|
363
|
-
unbind() {
|
|
364
|
-
this.gl.bindBuffer(this.target, null);
|
|
365
|
-
return true;
|
|
366
|
-
}
|
|
367
|
-
release() {
|
|
368
|
-
if (this.buffer) {
|
|
369
|
-
this.gl.deleteBuffer(this.buffer);
|
|
370
|
-
delete this.buffer;
|
|
371
|
-
}
|
|
372
|
-
return true;
|
|
373
|
-
}
|
|
374
|
-
set(data, mode = this.mode) {
|
|
375
|
-
this.bind();
|
|
376
|
-
this.gl.bufferData(this.target, data, mode);
|
|
377
|
-
}
|
|
378
|
-
setChunk(data, offset = 0) {
|
|
379
|
-
this.bind();
|
|
380
|
-
this.gl.bufferSubData(this.target, offset, data);
|
|
381
|
-
}
|
|
382
|
-
}
|
|
383
|
-
const defBuffer = (gl, data, target = gl.ARRAY_BUFFER, mode = gl.STATIC_DRAW) => new WebGLArrayBuffer(gl, data, target, mode);
|
|
384
|
-
const compileModel = (gl, spec, mode = gl.STATIC_DRAW) => {
|
|
385
|
-
if (spec.attribPool) {
|
|
386
|
-
spec.attribs = compileAttribPool(gl, spec.attribPool, undefined, gl.ARRAY_BUFFER, mode);
|
|
387
|
-
}
|
|
388
|
-
else {
|
|
389
|
-
compileAttribs(gl, spec.attribs, mode);
|
|
390
|
-
}
|
|
391
|
-
spec.instances && compileAttribs(gl, spec.instances.attribs, mode);
|
|
392
|
-
compileIndices(gl, spec.indices, mode);
|
|
393
|
-
spec.mode == null && (spec.mode = exports.DrawMode.TRIANGLES);
|
|
394
|
-
return spec;
|
|
395
|
-
};
|
|
396
|
-
const initBuffer = (gl, src, type, mode) => {
|
|
397
|
-
if (src.buffer) {
|
|
398
|
-
src.data && src.buffer.set(src.data);
|
|
399
|
-
}
|
|
400
|
-
else {
|
|
401
|
-
src.buffer = new WebGLArrayBuffer(gl, src.data, type, mode);
|
|
402
|
-
}
|
|
403
|
-
};
|
|
404
|
-
const compileAttribs = (gl, attribs, mode) => {
|
|
405
|
-
if (attribs) {
|
|
406
|
-
for (let id in attribs) {
|
|
407
|
-
initBuffer(gl, attribs[id], gl.ARRAY_BUFFER, mode);
|
|
408
|
-
}
|
|
409
|
-
}
|
|
410
|
-
return attribs;
|
|
411
|
-
};
|
|
412
|
-
const compileIndices = (gl, index, mode = gl.STATIC_DRAW) => {
|
|
413
|
-
if (index) {
|
|
414
|
-
initBuffer(gl, index, gl.ELEMENT_ARRAY_BUFFER, mode);
|
|
415
|
-
}
|
|
416
|
-
return index;
|
|
417
|
-
};
|
|
418
|
-
const compileVAO = (gl, spec) => {
|
|
419
|
-
if (spec.shader) {
|
|
420
|
-
const isGL2 = isGL2Context(gl);
|
|
421
|
-
const ext = !isGL2 ? gl.getExtension("OES_vertex_array_object") : null;
|
|
422
|
-
if (isGL2 || ext) {
|
|
423
|
-
let vao;
|
|
424
|
-
if (isGL2) {
|
|
425
|
-
vao = gl.createVertexArray();
|
|
426
|
-
gl.bindVertexArray(vao);
|
|
427
|
-
}
|
|
428
|
-
else {
|
|
429
|
-
vao = ext.createVertexArrayOES();
|
|
430
|
-
ext.bindVertexArrayOES(vao);
|
|
431
|
-
}
|
|
432
|
-
!!vao && error("error creating VAO");
|
|
433
|
-
spec.shader.bindAttribs(spec.attribs);
|
|
434
|
-
if (spec.indices) {
|
|
435
|
-
spec.indices.buffer.bind();
|
|
436
|
-
}
|
|
437
|
-
spec.shader.unbind(null);
|
|
438
|
-
if (isGL2) {
|
|
439
|
-
gl.bindVertexArray(null);
|
|
440
|
-
}
|
|
441
|
-
else {
|
|
442
|
-
ext.bindVertexArrayOES(null);
|
|
443
|
-
}
|
|
444
|
-
return vao;
|
|
445
|
-
}
|
|
446
|
-
}
|
|
447
|
-
};
|
|
448
|
-
const compileAttribPool = (gl, pool, ids, target = gl.ARRAY_BUFFER, mode = gl.STATIC_DRAW) => {
|
|
449
|
-
const buf = defBuffer(gl, pool.bytes(), target, mode);
|
|
450
|
-
const spec = {};
|
|
451
|
-
for (let id of ids || Object.keys(pool.specs)) {
|
|
452
|
-
const attr = pool.specs[id];
|
|
453
|
-
spec[id] = {
|
|
454
|
-
buffer: buf,
|
|
455
|
-
size: attr.size,
|
|
456
|
-
type: api.asGLType(attr.type),
|
|
457
|
-
stride: pool.byteStride,
|
|
458
|
-
offset: attr.byteOffset,
|
|
459
|
-
};
|
|
460
|
-
}
|
|
461
|
-
return spec;
|
|
462
|
-
};
|
|
463
|
-
|
|
464
|
-
const defaultOpts = {
|
|
465
|
-
alpha: true,
|
|
466
|
-
antialias: true,
|
|
467
|
-
depth: true,
|
|
468
|
-
premultipliedAlpha: true,
|
|
469
|
-
preserveDrawingBuffer: false,
|
|
470
|
-
stencil: false,
|
|
471
|
-
};
|
|
472
|
-
const glCanvas = (opts = {}) => {
|
|
473
|
-
const canvas = opts.canvas
|
|
474
|
-
? checks.isString(opts.canvas)
|
|
475
|
-
? document.getElementById(opts.canvas)
|
|
476
|
-
: opts.canvas
|
|
477
|
-
: document.createElement("canvas");
|
|
478
|
-
opts.width && (canvas.width = opts.width);
|
|
479
|
-
opts.height && (canvas.height = opts.height);
|
|
480
|
-
opts.autoScale !== false && adaptDpi.adaptDPI(canvas, canvas.width, canvas.height);
|
|
481
|
-
opts.parent && opts.parent.appendChild(canvas);
|
|
482
|
-
const gl = canvas.getContext(opts.version === 2 ? "webgl2" : "webgl", Object.assign(Object.assign({}, defaultOpts), opts.opts));
|
|
483
|
-
if (!gl) {
|
|
484
|
-
error("WebGL unavailable");
|
|
485
|
-
}
|
|
486
|
-
opts.onContextLost &&
|
|
487
|
-
canvas.addEventListener("webglcontextlost", opts.onContextLost);
|
|
488
|
-
return {
|
|
489
|
-
canvas,
|
|
490
|
-
gl,
|
|
491
|
-
ext: getExtensions(gl, opts.ext),
|
|
492
|
-
};
|
|
493
|
-
};
|
|
494
|
-
const getExtensions = (gl, ids, required = true) => {
|
|
495
|
-
const ext = {};
|
|
496
|
-
if (ids) {
|
|
497
|
-
for (let id of ids) {
|
|
498
|
-
ext[id] = gl.getExtension(id);
|
|
499
|
-
required && !ext[id] && error(`extension ${id} not available`);
|
|
500
|
-
}
|
|
501
|
-
}
|
|
502
|
-
return ext;
|
|
503
|
-
};
|
|
504
|
-
|
|
505
|
-
const $bind = (op) => (textures) => {
|
|
506
|
-
if (!textures)
|
|
507
|
-
return;
|
|
508
|
-
for (let i = textures.length, tex; --i >= 0;) {
|
|
509
|
-
(tex = textures[i]) && tex[op](i);
|
|
510
|
-
}
|
|
511
|
-
};
|
|
512
|
-
const bindTextures = $bind("bind");
|
|
513
|
-
const unbindTextures = $bind("unbind");
|
|
514
|
-
class Texture {
|
|
515
|
-
constructor(gl, opts = {}) {
|
|
516
|
-
this.gl = gl;
|
|
517
|
-
this.tex = gl.createTexture() || error("error creating WebGL texture");
|
|
518
|
-
this.configure(Object.assign({ filter: exports.TextureFilter.NEAREST, wrap: exports.TextureRepeat.CLAMP }, opts));
|
|
519
|
-
}
|
|
520
|
-
configure(opts = {}, unbind = true) {
|
|
521
|
-
const gl = this.gl;
|
|
522
|
-
const target = opts.target || this.target || exports.TextureTarget.TEXTURE_2D;
|
|
523
|
-
const format = opts.format || this.format || exports.TextureFormat.RGBA;
|
|
524
|
-
const decl = TEX_FORMATS[format];
|
|
525
|
-
const type = opts.type || this.type || decl.types[0];
|
|
526
|
-
!this.target && (this.target = target);
|
|
527
|
-
this.format = format;
|
|
528
|
-
this.type = type;
|
|
529
|
-
gl.bindTexture(this.target, this.tex);
|
|
530
|
-
opts.flip !== undefined &&
|
|
531
|
-
gl.pixelStorei(gl.UNPACK_FLIP_Y_WEBGL, opts.flip ? 1 : 0);
|
|
532
|
-
opts.premultiply !== undefined &&
|
|
533
|
-
gl.pixelStorei(gl.UNPACK_PREMULTIPLY_ALPHA_WEBGL, opts.premultiply ? 1 : 0);
|
|
534
|
-
this.configureImage(target, opts);
|
|
535
|
-
opts.mipmap && gl.generateMipmap(target);
|
|
536
|
-
this.configureFilter(target, opts);
|
|
537
|
-
this.configureWrap(target, opts);
|
|
538
|
-
this.configureLOD(target, opts);
|
|
539
|
-
this.configureLevels(target, opts);
|
|
540
|
-
unbind && gl.bindTexture(this.target, null);
|
|
541
|
-
return true;
|
|
542
|
-
}
|
|
543
|
-
configureImage(target, opts) {
|
|
544
|
-
if (opts.image === undefined)
|
|
545
|
-
return;
|
|
546
|
-
target === exports.TextureTarget.TEXTURE_3D
|
|
547
|
-
? this.configureImage3d(target, opts)
|
|
548
|
-
: this.configureImage2d(target, opts);
|
|
549
|
-
}
|
|
550
|
-
configureImage2d(target, opts) {
|
|
551
|
-
const level = opts.level || 0;
|
|
552
|
-
const pos = opts.pos || [0, 0, 0];
|
|
553
|
-
const { image, width, height } = opts;
|
|
554
|
-
const decl = TEX_FORMATS[this.format];
|
|
555
|
-
const baseFormat = decl.format;
|
|
556
|
-
const { gl, type, format } = this;
|
|
557
|
-
if (width && height) {
|
|
558
|
-
if (opts.sub) {
|
|
559
|
-
gl.texSubImage2D(target, level, pos[0], pos[1], width, height, baseFormat, type, image);
|
|
560
|
-
}
|
|
561
|
-
else {
|
|
562
|
-
if (level === 0) {
|
|
563
|
-
this.size = [width, height];
|
|
564
|
-
}
|
|
565
|
-
gl.texImage2D(target, level, format, width, height, 0, baseFormat, type, image);
|
|
566
|
-
}
|
|
567
|
-
}
|
|
568
|
-
else {
|
|
569
|
-
if (opts.sub) {
|
|
570
|
-
gl.texSubImage2D(target, level, pos[0], pos[1], baseFormat, type, image);
|
|
571
|
-
}
|
|
572
|
-
else {
|
|
573
|
-
if (image != null && level === 0) {
|
|
574
|
-
this.size = [image.width, image.height];
|
|
575
|
-
}
|
|
576
|
-
gl.texImage2D(target, level, format, baseFormat, type, image);
|
|
577
|
-
}
|
|
578
|
-
}
|
|
579
|
-
}
|
|
580
|
-
configureImage3d(target, opts) {
|
|
581
|
-
const { image, width, height, depth } = opts;
|
|
582
|
-
if (!(width && height && depth))
|
|
583
|
-
return;
|
|
584
|
-
const level = opts.level || 0;
|
|
585
|
-
const pos = opts.pos || [0, 0, 0];
|
|
586
|
-
const decl = TEX_FORMATS[this.format];
|
|
587
|
-
const baseFormat = decl.format;
|
|
588
|
-
const { gl, type, format } = this;
|
|
589
|
-
if (opts.sub) {
|
|
590
|
-
gl.texSubImage3D(target, level, pos[0], pos[1], pos[2], width, height, depth, baseFormat, type, image);
|
|
591
|
-
}
|
|
592
|
-
else {
|
|
593
|
-
if (level === 0) {
|
|
594
|
-
this.size = [width, height, depth];
|
|
595
|
-
}
|
|
596
|
-
gl.texImage3D(target, level, format, width, height, depth, 0, baseFormat, type, image);
|
|
597
|
-
}
|
|
598
|
-
}
|
|
599
|
-
configureFilter(target, opts) {
|
|
600
|
-
const gl = this.gl;
|
|
601
|
-
const flt = opts.filter || this.filter || exports.TextureFilter.NEAREST;
|
|
602
|
-
let t1, t2;
|
|
603
|
-
if (checks.isArray(flt)) {
|
|
604
|
-
t1 = flt[0];
|
|
605
|
-
t2 = flt[1] || t1;
|
|
606
|
-
this.filter = [t1, t2];
|
|
607
|
-
}
|
|
608
|
-
else {
|
|
609
|
-
this.filter = [flt, flt, flt];
|
|
610
|
-
t1 = t2 = flt;
|
|
611
|
-
}
|
|
612
|
-
gl.texParameteri(target, gl.TEXTURE_MIN_FILTER, t1);
|
|
613
|
-
gl.texParameteri(target, gl.TEXTURE_MAG_FILTER, t2);
|
|
614
|
-
}
|
|
615
|
-
configureWrap(target, opts) {
|
|
616
|
-
const gl = this.gl;
|
|
617
|
-
const wrap = opts.wrap || this.wrap || exports.TextureRepeat.CLAMP;
|
|
618
|
-
let t1, t2, t3;
|
|
619
|
-
if (checks.isArray(wrap)) {
|
|
620
|
-
t1 = wrap[0];
|
|
621
|
-
t2 = wrap[1] || t1;
|
|
622
|
-
t3 = wrap[2] || t1;
|
|
623
|
-
this.wrap = [t1, t2, t3];
|
|
624
|
-
}
|
|
625
|
-
else {
|
|
626
|
-
t1 = t2 = t3 = wrap;
|
|
627
|
-
this.wrap = [wrap, wrap, wrap];
|
|
628
|
-
}
|
|
629
|
-
gl.texParameteri(target, gl.TEXTURE_WRAP_S, t1);
|
|
630
|
-
gl.texParameteri(target, gl.TEXTURE_WRAP_T, t2);
|
|
631
|
-
isGL2Context(gl) &&
|
|
632
|
-
target === gl.TEXTURE_3D &&
|
|
633
|
-
gl.texParameteri(target, gl.TEXTURE_WRAP_R, t3);
|
|
634
|
-
}
|
|
635
|
-
configureLOD(target, opts) {
|
|
636
|
-
const gl = this.gl;
|
|
637
|
-
if (opts.lod) {
|
|
638
|
-
const [t1, t2] = opts.lod;
|
|
639
|
-
t1 &&
|
|
640
|
-
gl.texParameterf(target, gl.TEXTURE_MIN_LOD, t1);
|
|
641
|
-
t2 &&
|
|
642
|
-
gl.texParameterf(target, gl.TEXTURE_MAX_LOD, t2);
|
|
643
|
-
}
|
|
644
|
-
}
|
|
645
|
-
configureLevels(target, opts) {
|
|
646
|
-
const gl = this.gl;
|
|
647
|
-
if (opts.minMaxLevel) {
|
|
648
|
-
const [t1, t2] = opts.minMaxLevel;
|
|
649
|
-
gl.texParameteri(target, gl.TEXTURE_BASE_LEVEL, t1);
|
|
650
|
-
gl.texParameteri(target, gl.TEXTURE_MAX_LEVEL, t2);
|
|
651
|
-
}
|
|
652
|
-
}
|
|
653
|
-
bind(id = 0) {
|
|
654
|
-
const gl = this.gl;
|
|
655
|
-
gl.activeTexture(gl.TEXTURE0 + id);
|
|
656
|
-
gl.bindTexture(this.target, this.tex);
|
|
657
|
-
return true;
|
|
658
|
-
}
|
|
659
|
-
unbind(id = 0) {
|
|
660
|
-
const gl = this.gl;
|
|
661
|
-
gl.activeTexture(gl.TEXTURE0 + id);
|
|
662
|
-
gl.bindTexture(this.target, null);
|
|
663
|
-
return true;
|
|
664
|
-
}
|
|
665
|
-
release() {
|
|
666
|
-
if (this.tex) {
|
|
667
|
-
this.gl.deleteTexture(this.tex);
|
|
668
|
-
delete this.tex;
|
|
669
|
-
delete this.gl;
|
|
670
|
-
return true;
|
|
671
|
-
}
|
|
672
|
-
return false;
|
|
673
|
-
}
|
|
674
|
-
}
|
|
675
|
-
const defTexture = (gl, opts) => new Texture(gl, opts);
|
|
676
|
-
const defTextureCubeMap = (gl, faces, opts = {}) => {
|
|
677
|
-
const tex = new Texture(gl, { target: gl.TEXTURE_CUBE_MAP });
|
|
678
|
-
const faceOpts = associative.withoutKeysObj(opts, [
|
|
679
|
-
"target",
|
|
680
|
-
"image",
|
|
681
|
-
"filter",
|
|
682
|
-
"mipmap",
|
|
683
|
-
]);
|
|
684
|
-
for (let i = 0; i < 6; i++) {
|
|
685
|
-
faceOpts.target = gl.TEXTURE_CUBE_MAP_POSITIVE_X + i;
|
|
686
|
-
faceOpts.image = faces[i];
|
|
687
|
-
tex.configure(faceOpts);
|
|
688
|
-
}
|
|
689
|
-
tex.configure({ filter: opts.filter, mipmap: opts.mipmap });
|
|
690
|
-
return tex;
|
|
691
|
-
};
|
|
692
|
-
const defTextureFloat = (gl, data, width, height, format, type) => new Texture(gl, {
|
|
693
|
-
filter: gl.NEAREST,
|
|
694
|
-
wrap: gl.CLAMP_TO_EDGE,
|
|
695
|
-
format: format ||
|
|
696
|
-
(isGL2Context(gl) ? exports.TextureFormat.RGBA32F : exports.TextureFormat.RGBA),
|
|
697
|
-
type: type || gl.FLOAT,
|
|
698
|
-
image: data,
|
|
699
|
-
width,
|
|
700
|
-
height,
|
|
701
|
-
});
|
|
702
|
-
|
|
703
|
-
const draw = (specs, opts = {}) => {
|
|
704
|
-
const _specs = checks.isArray(specs) ? specs : [specs];
|
|
705
|
-
for (let i = 0, n = _specs.length; i < n; i++) {
|
|
706
|
-
const spec = _specs[i];
|
|
707
|
-
const indices = spec.indices;
|
|
708
|
-
const gl = spec.shader.gl;
|
|
709
|
-
opts.bindTex !== false && bindTextures(spec.textures);
|
|
710
|
-
opts.shaderState !== false && spec.shader.prepareState();
|
|
711
|
-
opts.bindShader !== false && spec.shader.bind(spec);
|
|
712
|
-
if (indices && indices.buffer) {
|
|
713
|
-
indices.buffer.bind();
|
|
714
|
-
if (spec.instances) {
|
|
715
|
-
drawInstanced(gl, spec);
|
|
716
|
-
}
|
|
717
|
-
else {
|
|
718
|
-
gl.drawElements(spec.mode, spec.num, indices.data instanceof Uint32Array
|
|
719
|
-
? gl.UNSIGNED_INT
|
|
720
|
-
: gl.UNSIGNED_SHORT, 0);
|
|
721
|
-
}
|
|
722
|
-
}
|
|
723
|
-
else {
|
|
724
|
-
if (spec.instances) {
|
|
725
|
-
drawInstanced(gl, spec);
|
|
726
|
-
}
|
|
727
|
-
else {
|
|
728
|
-
gl.drawArrays(spec.mode, 0, spec.num);
|
|
729
|
-
}
|
|
730
|
-
}
|
|
731
|
-
opts.unbindShader !== false && spec.shader.unbind(null);
|
|
732
|
-
opts.unbindTex && unbindTextures(spec.textures);
|
|
733
|
-
}
|
|
734
|
-
};
|
|
735
|
-
const drawInstanced = (gl, spec) => {
|
|
736
|
-
const isGL2 = isGL2Context(gl);
|
|
737
|
-
const ext = !isGL2 ? gl.getExtension("ANGLE_instanced_arrays") : undefined;
|
|
738
|
-
if (!(isGL2 || ext)) {
|
|
739
|
-
error("instancing not supported");
|
|
740
|
-
}
|
|
741
|
-
const sattribs = spec.shader.attribs;
|
|
742
|
-
const iattribs = spec.instances.attribs;
|
|
743
|
-
spec.shader.bindAttribs(iattribs);
|
|
744
|
-
for (let id in iattribs) {
|
|
745
|
-
const attr = sattribs[id];
|
|
746
|
-
if (attr) {
|
|
747
|
-
let div = iattribs[id].divisor;
|
|
748
|
-
div = div !== undefined ? div : 1;
|
|
749
|
-
isGL2
|
|
750
|
-
? gl.vertexAttribDivisor(attr.loc, div)
|
|
751
|
-
: ext.vertexAttribDivisorANGLE(attr.loc, div);
|
|
752
|
-
}
|
|
753
|
-
}
|
|
754
|
-
if (spec.indices) {
|
|
755
|
-
const type = spec.indices.data instanceof Uint32Array
|
|
756
|
-
? gl.UNSIGNED_INT
|
|
757
|
-
: gl.UNSIGNED_SHORT;
|
|
758
|
-
isGL2
|
|
759
|
-
? gl.drawElementsInstanced(spec.mode, spec.num, type, 0, spec.instances.num)
|
|
760
|
-
: ext.drawElementsInstancedANGLE(spec.mode, spec.num, type, 0, spec.instances.num);
|
|
761
|
-
}
|
|
762
|
-
else {
|
|
763
|
-
isGL2
|
|
764
|
-
? gl.drawArraysInstanced(spec.mode, 0, spec.num, spec.instances.num)
|
|
765
|
-
: ext.drawArraysInstancedANGLE(spec.mode, 0, spec.num, spec.instances.num);
|
|
766
|
-
}
|
|
767
|
-
for (let id in iattribs) {
|
|
768
|
-
const attr = sattribs[id];
|
|
769
|
-
attr &&
|
|
770
|
-
(isGL2
|
|
771
|
-
? gl.vertexAttribDivisor(attr.loc, 0)
|
|
772
|
-
: ext.vertexAttribDivisorANGLE(attr.loc, 0));
|
|
773
|
-
}
|
|
774
|
-
spec.shader.unbind(null);
|
|
775
|
-
};
|
|
776
|
-
|
|
777
|
-
class RBO {
|
|
778
|
-
constructor(gl, opts) {
|
|
779
|
-
this.gl = gl;
|
|
780
|
-
this.buffer = gl.createRenderbuffer() || error("error creating RBO");
|
|
781
|
-
this.configure(opts);
|
|
782
|
-
}
|
|
783
|
-
bind() {
|
|
784
|
-
this.gl.bindRenderbuffer(this.gl.RENDERBUFFER, this.buffer);
|
|
785
|
-
return true;
|
|
786
|
-
}
|
|
787
|
-
unbind() {
|
|
788
|
-
this.gl.bindRenderbuffer(this.gl.RENDERBUFFER, null);
|
|
789
|
-
return true;
|
|
790
|
-
}
|
|
791
|
-
release() {
|
|
792
|
-
this.gl.deleteRenderbuffer(this.buffer);
|
|
793
|
-
delete this.buffer;
|
|
794
|
-
return true;
|
|
795
|
-
}
|
|
796
|
-
configure(opts, unbind = true) {
|
|
797
|
-
const gl = this.gl;
|
|
798
|
-
this.bind();
|
|
799
|
-
this.format = opts.format || gl.DEPTH_COMPONENT16;
|
|
800
|
-
this.width = opts.width;
|
|
801
|
-
this.height = opts.height;
|
|
802
|
-
gl.renderbufferStorage(gl.RENDERBUFFER, opts.format || gl.DEPTH_COMPONENT16, opts.width, opts.height);
|
|
803
|
-
return unbind ? this.unbind() : true;
|
|
804
|
-
}
|
|
805
|
-
}
|
|
806
|
-
const defRBO = (gl, opts) => new RBO(gl, opts);
|
|
807
|
-
|
|
808
|
-
const GL_COLOR_ATTACHMENT0_WEBGL = 0x8ce0;
|
|
809
|
-
const GL_MAX_COLOR_ATTACHMENTS_WEBGL = 0x8cdf;
|
|
810
|
-
class FBO {
|
|
811
|
-
constructor(gl, opts) {
|
|
812
|
-
this.gl = gl;
|
|
813
|
-
this.fbo = gl.createFramebuffer() || error("error creating FBO");
|
|
814
|
-
this.ext =
|
|
815
|
-
!isGL2Context(gl) && opts && opts.tex && opts.tex.length > 1
|
|
816
|
-
? gl.getExtension("WEBGL_draw_buffers") ||
|
|
817
|
-
error("missing WEBGL_draw_buffers ext")
|
|
818
|
-
: undefined;
|
|
819
|
-
this.maxAttachments = gl.getParameter(GL_MAX_COLOR_ATTACHMENTS_WEBGL);
|
|
820
|
-
opts && this.configure(opts);
|
|
821
|
-
}
|
|
822
|
-
bind() {
|
|
823
|
-
this.gl.bindFramebuffer(this.gl.FRAMEBUFFER, this.fbo);
|
|
824
|
-
return true;
|
|
825
|
-
}
|
|
826
|
-
unbind() {
|
|
827
|
-
this.gl.bindFramebuffer(this.gl.FRAMEBUFFER, null);
|
|
828
|
-
return true;
|
|
829
|
-
}
|
|
830
|
-
release() {
|
|
831
|
-
this.gl.deleteFramebuffer(this.fbo);
|
|
832
|
-
delete this.fbo;
|
|
833
|
-
delete this.ext;
|
|
834
|
-
return true;
|
|
835
|
-
}
|
|
836
|
-
configure(opts, unbind = true) {
|
|
837
|
-
const gl = this.gl;
|
|
838
|
-
this.bind();
|
|
839
|
-
if (opts.tex) {
|
|
840
|
-
api.assert(opts.tex.length < this.maxAttachments, `too many attachments (max. ${this.maxAttachments})`);
|
|
841
|
-
const attachments = [];
|
|
842
|
-
for (let i = 0; i < opts.tex.length; i++) {
|
|
843
|
-
const tex = opts.tex[i];
|
|
844
|
-
api.assert(!!(TEX_FORMATS[tex.format].render ||
|
|
845
|
-
TEX_FORMATS[tex.format].renderExt), `texture #${i} has non-renderable format`);
|
|
846
|
-
const attach = GL_COLOR_ATTACHMENT0_WEBGL + i;
|
|
847
|
-
gl.framebufferTexture2D(gl.FRAMEBUFFER, attach, gl.TEXTURE_2D, tex.tex, 0);
|
|
848
|
-
attachments[i] = attach;
|
|
849
|
-
}
|
|
850
|
-
if (this.ext) {
|
|
851
|
-
this.ext.drawBuffersWEBGL(attachments);
|
|
852
|
-
}
|
|
853
|
-
else if (isGL2Context(gl)) {
|
|
854
|
-
gl.drawBuffers(attachments);
|
|
855
|
-
}
|
|
856
|
-
}
|
|
857
|
-
if (opts.depth) {
|
|
858
|
-
opts.depth instanceof RBO
|
|
859
|
-
? gl.framebufferRenderbuffer(gl.FRAMEBUFFER, gl.DEPTH_ATTACHMENT, gl.RENDERBUFFER, opts.depth.buffer)
|
|
860
|
-
: gl.framebufferTexture2D(gl.FRAMEBUFFER, gl.DEPTH_ATTACHMENT, gl.TEXTURE_2D, opts.depth.tex, 0);
|
|
861
|
-
}
|
|
862
|
-
this.validate();
|
|
863
|
-
return unbind ? this.unbind() : true;
|
|
864
|
-
}
|
|
865
|
-
validate() {
|
|
866
|
-
const gl = this.gl;
|
|
867
|
-
const err = gl.checkFramebufferStatus(gl.FRAMEBUFFER);
|
|
868
|
-
switch (err) {
|
|
869
|
-
case gl.FRAMEBUFFER_COMPLETE:
|
|
870
|
-
return true;
|
|
871
|
-
case gl.FRAMEBUFFER_UNSUPPORTED:
|
|
872
|
-
error("FBO unsupported");
|
|
873
|
-
case gl.FRAMEBUFFER_INCOMPLETE_ATTACHMENT:
|
|
874
|
-
error("FBO incomplete attachment");
|
|
875
|
-
case gl.FRAMEBUFFER_INCOMPLETE_DIMENSIONS:
|
|
876
|
-
error("FBO incomplete dimensions");
|
|
877
|
-
case gl.FRAMEBUFFER_INCOMPLETE_MISSING_ATTACHMENT:
|
|
878
|
-
error("FBO incomplete missing attachment");
|
|
879
|
-
default:
|
|
880
|
-
return error(`FBO error: ${err}`);
|
|
881
|
-
}
|
|
882
|
-
}
|
|
883
|
-
}
|
|
884
|
-
const defFBO = (gl, opts) => new FBO(gl, opts);
|
|
885
|
-
|
|
886
|
-
const DEFAULT_MATERIAL = {
|
|
887
|
-
ambientCol: [0.1, 0.1, 0.1],
|
|
888
|
-
diffuseCol: [0.8, 0.8, 0.8],
|
|
889
|
-
specularCol: [1, 1, 1],
|
|
890
|
-
};
|
|
891
|
-
const TYPES = {
|
|
892
|
-
ambientCol: "vec3",
|
|
893
|
-
diffuseCol: "vec3",
|
|
894
|
-
specularCol: "vec3",
|
|
895
|
-
};
|
|
896
|
-
const defMaterial = (mat = {}, flags = {}, base = DEFAULT_MATERIAL) => Object.keys(base).reduce((acc, id) => {
|
|
897
|
-
flags[id] !== false &&
|
|
898
|
-
(acc[id] = [TYPES[id], mat[id] || base[id]]);
|
|
899
|
-
return acc;
|
|
900
|
-
}, {});
|
|
901
|
-
|
|
902
|
-
const $ = (a, b, id) => a[id] || b[id].defaultVal || matrices.IDENT44;
|
|
903
|
-
const autoNormalMatrix1 = (model = "model") => (shaderU, specU) => matrices.normal44([], $(specU, shaderU, model));
|
|
904
|
-
const autoNormalMatrix2 = (model = "model", view = "view") => (shaderU, specU) => (matrices.normal44(null, matrices.mulM44([], $(specU, shaderU, view), $(specU, shaderU, model))));
|
|
905
|
-
function screen2d(a, b) {
|
|
906
|
-
return checks.isNumber(a)
|
|
907
|
-
? matrices.ortho([], 0, a, b, 0, -1, 1)
|
|
908
|
-
: matrices.ortho([], 0, a.drawingBufferWidth, a.drawingBufferHeight, 0, -1, 1);
|
|
909
|
-
}
|
|
910
|
-
|
|
911
|
-
const defQuadModel = (opts) => {
|
|
912
|
-
let { size, uv, center } = Object.assign({ size: 2, uv: true, center: true }, opts);
|
|
913
|
-
size *= 0.5;
|
|
914
|
-
const o = center ? 0 : size;
|
|
915
|
-
return {
|
|
916
|
-
attribs: Object.assign({ position: {
|
|
917
|
-
data: new Float32Array([
|
|
918
|
-
o - size,
|
|
919
|
-
o - size,
|
|
920
|
-
o + size,
|
|
921
|
-
o - size,
|
|
922
|
-
o - size,
|
|
923
|
-
o + size,
|
|
924
|
-
o + size,
|
|
925
|
-
o + size,
|
|
926
|
-
]),
|
|
927
|
-
size: 2,
|
|
928
|
-
} }, (uv
|
|
929
|
-
? {
|
|
930
|
-
uv: {
|
|
931
|
-
data: new Float32Array([0, 0, 1, 0, 0, 1, 1, 1]),
|
|
932
|
-
size: 2,
|
|
933
|
-
},
|
|
934
|
-
}
|
|
935
|
-
: null)),
|
|
936
|
-
uniforms: {},
|
|
937
|
-
shader: null,
|
|
938
|
-
mode: exports.DrawMode.TRIANGLE_STRIP,
|
|
939
|
-
num: 4,
|
|
940
|
-
};
|
|
941
|
-
};
|
|
942
|
-
|
|
943
|
-
const PREFIXES = {
|
|
944
|
-
a: "a_",
|
|
945
|
-
v: "v_",
|
|
946
|
-
u: "u_",
|
|
947
|
-
o: "o_",
|
|
948
|
-
};
|
|
949
|
-
const NO_PREFIXES = {
|
|
950
|
-
a: "",
|
|
951
|
-
v: "",
|
|
952
|
-
u: "",
|
|
953
|
-
o: "",
|
|
954
|
-
};
|
|
955
|
-
const SYNTAX = {
|
|
956
|
-
[shaderAstGlsl.GLSLVersion.GLES_100]: {
|
|
957
|
-
number: 100,
|
|
958
|
-
attrib: (id, type, pre) => `attribute ${checks.isArray(type) ? type[0] : type} ${pre.a}${id};`,
|
|
959
|
-
varying: {
|
|
960
|
-
vs: (id, type, pre) => arrayDecl("varying", type, pre.v + id),
|
|
961
|
-
fs: (id, type, pre) => arrayDecl("varying", type, pre.v + id),
|
|
962
|
-
},
|
|
963
|
-
uniform: (id, u, pre) => arrayDecl("uniform", u, pre.u + id),
|
|
964
|
-
output: (id, type, pre) => checks.isArray(type)
|
|
965
|
-
? `#define ${pre.o}${id} gl_FragData[${type[1]}]`
|
|
966
|
-
: "",
|
|
967
|
-
},
|
|
968
|
-
[shaderAstGlsl.GLSLVersion.GLES_300]: {
|
|
969
|
-
number: 300,
|
|
970
|
-
attrib: (id, type, pre) => checks.isArray(type)
|
|
971
|
-
? `layout(location=${type[1]}) in ${type[0]} ${pre.a}${id};`
|
|
972
|
-
: `in ${type} ${pre.a}${id};`,
|
|
973
|
-
varying: {
|
|
974
|
-
vs: (id, type, pre) => arrayDecl("out", type, pre.v + id),
|
|
975
|
-
fs: (id, type, pre) => arrayDecl("in", type, pre.v + id),
|
|
976
|
-
},
|
|
977
|
-
uniform: (id, u, pre) => arrayDecl("uniform", u, pre.u + id),
|
|
978
|
-
output: (id, type, pre) => checks.isArray(type)
|
|
979
|
-
? `layout(location=${type[1]}) out ${type[0]} ${pre.o}${id};`
|
|
980
|
-
: `out ${type} ${pre.o}${id};`,
|
|
981
|
-
},
|
|
982
|
-
};
|
|
983
|
-
const arrayDecl = (qualifier, decl, id) => {
|
|
984
|
-
const type = checks.isArray(decl) ? decl[0] : decl;
|
|
985
|
-
return type.indexOf("[]") > 0
|
|
986
|
-
? `${qualifier} ${type.replace("[]", "")} ${id}[${decl[1]}];`
|
|
987
|
-
: `${qualifier} ${type} ${id};`;
|
|
988
|
-
};
|
|
989
|
-
const VERSION_CHECK = (ver, ok, fail = "") => {
|
|
990
|
-
let cmp = ">=";
|
|
991
|
-
if (!ok) {
|
|
992
|
-
ok = fail;
|
|
993
|
-
fail = null;
|
|
994
|
-
cmp = "<";
|
|
995
|
-
}
|
|
996
|
-
return `#if __VERSION__ ${cmp} ${ver}
|
|
997
|
-
${ok}${fail ? `\n#else\n${fail}` : ""}
|
|
998
|
-
#endif`;
|
|
999
|
-
};
|
|
1000
|
-
const ALIAS_TEXTURE = VERSION_CHECK(300, "", "#define texture texture2D");
|
|
1001
|
-
const EXPORT_FRAGCOL = (body = "col", out = "o_fragColor") => VERSION_CHECK(300, `${out}=${body};`, `gl_FragColor=${body};`);
|
|
1002
|
-
const GLSL_HEADER = `#ifdef GL_FRAGMENT_PRECISION_HIGH
|
|
1003
|
-
precision highp int;
|
|
1004
|
-
precision highp float;
|
|
1005
|
-
#else
|
|
1006
|
-
precision mediump int;
|
|
1007
|
-
precision mediump float;
|
|
1008
|
-
#endif
|
|
1009
|
-
#ifndef PI
|
|
1010
|
-
#define PI 3.141592653589793
|
|
1011
|
-
#endif
|
|
1012
|
-
#ifndef TAU
|
|
1013
|
-
#define TAU 6.283185307179586
|
|
1014
|
-
#endif
|
|
1015
|
-
#ifndef HALF_PI
|
|
1016
|
-
#define HALF_PI 1.570796326794896
|
|
1017
|
-
#endif
|
|
1018
|
-
`;
|
|
1019
|
-
|
|
1020
|
-
const uniformS = (fn) => (gl, loc, defaultVal = 0) => {
|
|
1021
|
-
let prev;
|
|
1022
|
-
return (x) => {
|
|
1023
|
-
x = x === undefined ? defaultVal : x;
|
|
1024
|
-
if (x !== prev) {
|
|
1025
|
-
gl["uniform1" + fn](loc, x);
|
|
1026
|
-
prev = x;
|
|
1027
|
-
}
|
|
1028
|
-
};
|
|
1029
|
-
};
|
|
1030
|
-
const uniformV = (fn, sysDefault) => (gl, loc, defaultVal = sysDefault) => {
|
|
1031
|
-
let prev = [];
|
|
1032
|
-
return (x) => {
|
|
1033
|
-
x = x === undefined ? defaultVal : x;
|
|
1034
|
-
if (!equiv.equivArrayLike(prev, x)) {
|
|
1035
|
-
gl["uniform" + fn](loc, x);
|
|
1036
|
-
prev = [...x];
|
|
1037
|
-
}
|
|
1038
|
-
};
|
|
1039
|
-
};
|
|
1040
|
-
const uniformM = (fn, sysDefault) => (gl, loc, defaultVal = sysDefault) => {
|
|
1041
|
-
let prev = [];
|
|
1042
|
-
return (x) => {
|
|
1043
|
-
x = x === undefined ? defaultVal : x;
|
|
1044
|
-
if (!equiv.equivArrayLike(prev, x)) {
|
|
1045
|
-
gl["uniformMatrix" + fn](loc, false, x);
|
|
1046
|
-
prev = [...x];
|
|
1047
|
-
}
|
|
1048
|
-
};
|
|
1049
|
-
};
|
|
1050
|
-
const Z1 = [0];
|
|
1051
|
-
const UNIFORM_SETTERS = {
|
|
1052
|
-
bool: uniformS("i"),
|
|
1053
|
-
float: uniformS("f"),
|
|
1054
|
-
int: uniformS("i"),
|
|
1055
|
-
uint: uniformS("ui"),
|
|
1056
|
-
bvec2: uniformV("2iv", vectors.ZERO2),
|
|
1057
|
-
bvec3: uniformV("3iv", vectors.ZERO3),
|
|
1058
|
-
bvec4: uniformV("4iv", vectors.ZERO4),
|
|
1059
|
-
ivec2: uniformV("2iv", vectors.ZERO2),
|
|
1060
|
-
ivec3: uniformV("3iv", vectors.ZERO3),
|
|
1061
|
-
ivec4: uniformV("4iv", vectors.ZERO4),
|
|
1062
|
-
vec2: uniformV("2fv", vectors.ZERO2),
|
|
1063
|
-
vec3: uniformV("3fv", vectors.ZERO3),
|
|
1064
|
-
vec4: uniformV("4fv", vectors.ZERO4),
|
|
1065
|
-
mat2: uniformM("2fv", matrices.IDENT22),
|
|
1066
|
-
mat3: uniformM("3fv", matrices.IDENT33),
|
|
1067
|
-
mat4: uniformM("4fv", matrices.IDENT44),
|
|
1068
|
-
sampler2D: uniformS("i"),
|
|
1069
|
-
sampler2DShadow: uniformS("i"),
|
|
1070
|
-
sampler3D: uniformS("i"),
|
|
1071
|
-
samplerCube: uniformS("i"),
|
|
1072
|
-
samplerCubeShadow: uniformS("i"),
|
|
1073
|
-
"bool[]": uniformV("1iv", Z1),
|
|
1074
|
-
"float[]": uniformV("1fv", Z1),
|
|
1075
|
-
"int[]": uniformV("1iv", Z1),
|
|
1076
|
-
"uint[]": uniformV("1uiv", Z1),
|
|
1077
|
-
"bvec2[]": uniformV("2iv", vectors.ZERO2),
|
|
1078
|
-
"bvec3[]": uniformV("3iv", vectors.ZERO3),
|
|
1079
|
-
"bvec4[]": uniformV("4iv", vectors.ZERO4),
|
|
1080
|
-
"ivec2[]": uniformV("2iv", vectors.ZERO2),
|
|
1081
|
-
"ivec3[]": uniformV("3iv", vectors.ZERO3),
|
|
1082
|
-
"ivec4[]": uniformV("4iv", vectors.ZERO4),
|
|
1083
|
-
"vec2[]": uniformV("2fv", vectors.ZERO2),
|
|
1084
|
-
"vec3[]": uniformV("3fv", vectors.ZERO3),
|
|
1085
|
-
"vec4[]": uniformV("4fv", vectors.ZERO4),
|
|
1086
|
-
"mat2[]": uniformM("2fv", vectors.ZERO2),
|
|
1087
|
-
"mat3[]": uniformM("3fv", vectors.ZERO3),
|
|
1088
|
-
"mat4[]": uniformM("4fv", vectors.ZERO4),
|
|
1089
|
-
"sampler2D[]": uniformV("1iv", Z1),
|
|
1090
|
-
"sampler2DShadow[]": uniformV("1iv", Z1),
|
|
1091
|
-
"sampler3D[]": uniformV("1iv", Z1),
|
|
1092
|
-
"samplerCube[]": uniformV("1iv", Z1),
|
|
1093
|
-
"samplerCubeShadow[]": uniformV("1iv", Z1),
|
|
1094
|
-
};
|
|
1095
|
-
|
|
1096
|
-
const ERROR_REGEXP = /ERROR: \d+:(\d+): (.*)/;
|
|
1097
|
-
class Shader {
|
|
1098
|
-
constructor(gl, program, attribs, uniforms, state) {
|
|
1099
|
-
this.warnAttrib = memoize.doOnce((id) => exports.LOGGER.warn(`unknown attrib: ${id}`));
|
|
1100
|
-
this.warnUni = memoize.doOnce((id) => exports.LOGGER.warn(`unknown uniform: ${id}`));
|
|
1101
|
-
this.gl = gl;
|
|
1102
|
-
this.program = program;
|
|
1103
|
-
this.attribs = attribs;
|
|
1104
|
-
this.uniforms = uniforms;
|
|
1105
|
-
this.state = state || {};
|
|
1106
|
-
}
|
|
1107
|
-
bind(spec) {
|
|
1108
|
-
if (this.program) {
|
|
1109
|
-
this.gl.useProgram(this.program);
|
|
1110
|
-
this.bindAttribs(spec.attribs);
|
|
1111
|
-
this.bindUniforms(spec.uniforms);
|
|
1112
|
-
return true;
|
|
1113
|
-
}
|
|
1114
|
-
return false;
|
|
1115
|
-
}
|
|
1116
|
-
unbind() {
|
|
1117
|
-
let shaderAttrib;
|
|
1118
|
-
for (let id in this.attribs) {
|
|
1119
|
-
if ((shaderAttrib = this.attribs[id])) {
|
|
1120
|
-
this.gl.disableVertexAttribArray(shaderAttrib.loc);
|
|
1121
|
-
}
|
|
1122
|
-
}
|
|
1123
|
-
this.gl.useProgram(null);
|
|
1124
|
-
return true;
|
|
1125
|
-
}
|
|
1126
|
-
release() {
|
|
1127
|
-
if (this.program) {
|
|
1128
|
-
this.gl.deleteProgram(this.program);
|
|
1129
|
-
delete this.program;
|
|
1130
|
-
return true;
|
|
1131
|
-
}
|
|
1132
|
-
return false;
|
|
1133
|
-
}
|
|
1134
|
-
bindAttribs(specAttribs) {
|
|
1135
|
-
const gl = this.gl;
|
|
1136
|
-
let shaderAttrib;
|
|
1137
|
-
for (let id in specAttribs) {
|
|
1138
|
-
if ((shaderAttrib = this.attribs[id])) {
|
|
1139
|
-
const attr = specAttribs[id];
|
|
1140
|
-
attr.buffer.bind();
|
|
1141
|
-
gl.enableVertexAttribArray(shaderAttrib.loc);
|
|
1142
|
-
gl.vertexAttribPointer(shaderAttrib.loc, attr.size || 3, api.asGLType(attr.type || gl.FLOAT), attr.normalized || false, attr.stride || 0, attr.offset || 0);
|
|
1143
|
-
}
|
|
1144
|
-
else {
|
|
1145
|
-
this.warnAttrib(id);
|
|
1146
|
-
}
|
|
1147
|
-
}
|
|
1148
|
-
}
|
|
1149
|
-
bindUniforms(specUnis = {}) {
|
|
1150
|
-
const shaderUnis = this.uniforms;
|
|
1151
|
-
for (let id in specUnis) {
|
|
1152
|
-
const u = shaderUnis[id];
|
|
1153
|
-
if (u) {
|
|
1154
|
-
let val = specUnis[id];
|
|
1155
|
-
val = checks.isFunction(val) ? val(shaderUnis, specUnis) : api.deref(val);
|
|
1156
|
-
u.setter(val);
|
|
1157
|
-
}
|
|
1158
|
-
else {
|
|
1159
|
-
this.warnUni(id);
|
|
1160
|
-
}
|
|
1161
|
-
}
|
|
1162
|
-
for (let id in shaderUnis) {
|
|
1163
|
-
if (shaderUnis.hasOwnProperty(id) &&
|
|
1164
|
-
(!specUnis || !checks.existsAndNotNull(specUnis[id]))) {
|
|
1165
|
-
const u = shaderUnis[id];
|
|
1166
|
-
const val = u.defaultFn
|
|
1167
|
-
? u.defaultFn(shaderUnis, specUnis)
|
|
1168
|
-
: u.defaultVal;
|
|
1169
|
-
u.setter(val);
|
|
1170
|
-
}
|
|
1171
|
-
}
|
|
1172
|
-
}
|
|
1173
|
-
prepareState(state = this.state) {
|
|
1174
|
-
const gl = this.gl;
|
|
1175
|
-
state.depth !== undefined && this.setState(gl.DEPTH_TEST, state.depth);
|
|
1176
|
-
if (state.cull !== undefined) {
|
|
1177
|
-
this.setState(gl.CULL_FACE, state.cull);
|
|
1178
|
-
state.cullMode && gl.cullFace(state.cullMode);
|
|
1179
|
-
}
|
|
1180
|
-
if (state.blend !== undefined) {
|
|
1181
|
-
this.setState(gl.BLEND, state.blend);
|
|
1182
|
-
state.blendFn && gl.blendFunc(state.blendFn[0], state.blendFn[1]);
|
|
1183
|
-
state.blendEq !== undefined && gl.blendEquation(state.blendEq);
|
|
1184
|
-
}
|
|
1185
|
-
if (state.stencil !== undefined) {
|
|
1186
|
-
this.setState(gl.STENCIL_TEST, state.stencil);
|
|
1187
|
-
state.stencilFn &&
|
|
1188
|
-
gl.stencilFunc(state.stencilFn[0], state.stencilFn[1], state.stencilFn[2]);
|
|
1189
|
-
state.stencilOp &&
|
|
1190
|
-
gl.stencilOp(state.stencilOp[0], state.stencilOp[1], state.stencilOp[2]);
|
|
1191
|
-
state.stencilMask !== undefined &&
|
|
1192
|
-
gl.stencilMask(state.stencilMask);
|
|
1193
|
-
}
|
|
1194
|
-
}
|
|
1195
|
-
setState(id, val) {
|
|
1196
|
-
if (val) {
|
|
1197
|
-
this.gl.enable(id);
|
|
1198
|
-
}
|
|
1199
|
-
else {
|
|
1200
|
-
this.gl.disable(id);
|
|
1201
|
-
}
|
|
1202
|
-
}
|
|
1203
|
-
}
|
|
1204
|
-
const defShader = (gl, spec) => {
|
|
1205
|
-
const version = isGL2Context(gl)
|
|
1206
|
-
? shaderAstGlsl.GLSLVersion.GLES_300
|
|
1207
|
-
: shaderAstGlsl.GLSLVersion.GLES_100;
|
|
1208
|
-
const srcVS = checks.isFunction(spec.vs)
|
|
1209
|
-
? shaderSourceFromAST(spec, "vs", version)
|
|
1210
|
-
: prepareShaderSource(spec, "vs", version);
|
|
1211
|
-
const srcFS = checks.isFunction(spec.fs)
|
|
1212
|
-
? shaderSourceFromAST(spec, "fs", version)
|
|
1213
|
-
: prepareShaderSource(spec, "fs", version);
|
|
1214
|
-
exports.LOGGER.debug(srcVS);
|
|
1215
|
-
exports.LOGGER.debug(srcFS);
|
|
1216
|
-
initShaderExtensions(gl, spec.ext);
|
|
1217
|
-
const vs = compileShader(gl, gl.VERTEX_SHADER, srcVS);
|
|
1218
|
-
const fs = compileShader(gl, gl.FRAGMENT_SHADER, srcFS);
|
|
1219
|
-
const program = gl.createProgram() || error("error creating shader program");
|
|
1220
|
-
gl.attachShader(program, vs);
|
|
1221
|
-
gl.attachShader(program, fs);
|
|
1222
|
-
gl.linkProgram(program);
|
|
1223
|
-
if (gl.getProgramParameter(program, gl.LINK_STATUS)) {
|
|
1224
|
-
const attribs = initAttributes(gl, program, spec.attribs);
|
|
1225
|
-
const uniforms = initUniforms(gl, program, spec.uniforms);
|
|
1226
|
-
gl.deleteShader(vs);
|
|
1227
|
-
gl.deleteShader(fs);
|
|
1228
|
-
return new Shader(gl, program, attribs, uniforms, spec.state);
|
|
1229
|
-
}
|
|
1230
|
-
throw new Error(`Error linking shader: ${gl.getProgramInfoLog(program)}`);
|
|
1231
|
-
};
|
|
1232
|
-
const compileVars = (attribs, syntax, prefixes) => {
|
|
1233
|
-
let decls = [];
|
|
1234
|
-
for (let id in attribs) {
|
|
1235
|
-
if (attribs.hasOwnProperty(id)) {
|
|
1236
|
-
decls.push(syntax(id, attribs[id], prefixes));
|
|
1237
|
-
}
|
|
1238
|
-
}
|
|
1239
|
-
decls.push("");
|
|
1240
|
-
return decls.join("\n");
|
|
1241
|
-
};
|
|
1242
|
-
const compileExtensionPragma = (id, behavior, version) => {
|
|
1243
|
-
const ext = GL_EXT_INFO[id];
|
|
1244
|
-
const gl2 = version === shaderAstGlsl.GLSLVersion.GLES_300;
|
|
1245
|
-
return ext && ((!gl2 && ext.gl) || (gl2 && ext.gl2))
|
|
1246
|
-
? `#extension ${(ext && ext.alias) || id} : ${checks.isBoolean(behavior) ? (behavior ? "enable" : "disable") : behavior}\n`
|
|
1247
|
-
: "";
|
|
1248
|
-
};
|
|
1249
|
-
const initShaderExtensions = (gl, exts) => {
|
|
1250
|
-
if (exts) {
|
|
1251
|
-
for (let id in exts) {
|
|
1252
|
-
const state = exts[id];
|
|
1253
|
-
if (state === true || state === "require") {
|
|
1254
|
-
getExtensions(gl, [id], state === "require");
|
|
1255
|
-
}
|
|
1256
|
-
}
|
|
1257
|
-
}
|
|
1258
|
-
};
|
|
1259
|
-
const compilePrelude = (spec, version) => {
|
|
1260
|
-
let prelude = spec.pre
|
|
1261
|
-
? spec.replacePrelude
|
|
1262
|
-
? spec.pre
|
|
1263
|
-
: spec.pre + "\n" + GLSL_HEADER
|
|
1264
|
-
: GLSL_HEADER;
|
|
1265
|
-
if (spec.ext) {
|
|
1266
|
-
for (let id in spec.ext) {
|
|
1267
|
-
prelude += compileExtensionPragma(id, spec.ext[id], version);
|
|
1268
|
-
}
|
|
1269
|
-
}
|
|
1270
|
-
return prelude;
|
|
1271
|
-
};
|
|
1272
|
-
const compileIODecls = (decl, src, dest) => {
|
|
1273
|
-
for (let id in src) {
|
|
1274
|
-
const a = src[id];
|
|
1275
|
-
dest[id] = checks.isArray(a)
|
|
1276
|
-
? decl(a[0], id, { loc: a[1] })
|
|
1277
|
-
: decl(a, id);
|
|
1278
|
-
}
|
|
1279
|
-
};
|
|
1280
|
-
const varyingOpts = (v) => {
|
|
1281
|
-
const [vtype, opts] = checks.isArray(v)
|
|
1282
|
-
? [v[0], { num: v[1] }]
|
|
1283
|
-
: [v, {}];
|
|
1284
|
-
/(u?int|[ui]vec[234])/.test(vtype) && (opts.smooth = "flat");
|
|
1285
|
-
return [vtype, opts];
|
|
1286
|
-
};
|
|
1287
|
-
const compileVaryingDecls = (spec, decl, acc) => {
|
|
1288
|
-
for (let id in spec.varying) {
|
|
1289
|
-
const [vtype, opts] = varyingOpts(spec.varying[id]);
|
|
1290
|
-
acc[id] = decl(vtype, id, opts);
|
|
1291
|
-
}
|
|
1292
|
-
};
|
|
1293
|
-
const compileUniformDecls = (spec, acc) => {
|
|
1294
|
-
for (let id in spec.uniforms) {
|
|
1295
|
-
const u = spec.uniforms[id];
|
|
1296
|
-
acc[id] = checks.isArray(u)
|
|
1297
|
-
? shaderAst.uniform(u[0], id, u[0].indexOf("[]") > 0 ? { num: u[1] } : undefined)
|
|
1298
|
-
: shaderAst.uniform(u, id);
|
|
1299
|
-
}
|
|
1300
|
-
};
|
|
1301
|
-
const shaderSourceFromAST = (spec, type, version) => {
|
|
1302
|
-
let prelude = compilePrelude(spec, version);
|
|
1303
|
-
const inputs = {};
|
|
1304
|
-
const outputs = {};
|
|
1305
|
-
const outputAliases = {};
|
|
1306
|
-
const unis = {};
|
|
1307
|
-
spec.uniforms && compileUniformDecls(spec, unis);
|
|
1308
|
-
if (type === "vs") {
|
|
1309
|
-
compileIODecls(shaderAst.input, spec.attribs, inputs);
|
|
1310
|
-
spec.varying && compileVaryingDecls(spec, shaderAst.output, outputs);
|
|
1311
|
-
}
|
|
1312
|
-
else {
|
|
1313
|
-
spec.varying && compileVaryingDecls(spec, shaderAst.input, inputs);
|
|
1314
|
-
const outs = spec.outputs || DEFAULT_OUTPUT;
|
|
1315
|
-
if (version >= shaderAstGlsl.GLSLVersion.GLES_300) {
|
|
1316
|
-
compileIODecls(shaderAst.output, outs, outputs);
|
|
1317
|
-
}
|
|
1318
|
-
else {
|
|
1319
|
-
for (let id in outs) {
|
|
1320
|
-
const o = outs[id];
|
|
1321
|
-
if (checks.isArray(o) && o[0] === "vec4") {
|
|
1322
|
-
prelude += `#define ${id} gl_FragData[${o[1]}]\n`;
|
|
1323
|
-
outputAliases[id] = shaderAst.sym("vec4", id);
|
|
1324
|
-
}
|
|
1325
|
-
else {
|
|
1326
|
-
errors.unsupported(`GLSL ${version} doesn't support output vars`);
|
|
1327
|
-
}
|
|
1328
|
-
}
|
|
1329
|
-
}
|
|
1330
|
-
}
|
|
1331
|
-
const target = shaderAstGlsl.targetGLSL({
|
|
1332
|
-
type,
|
|
1333
|
-
version,
|
|
1334
|
-
prelude,
|
|
1335
|
-
});
|
|
1336
|
-
return (target(shaderAst.program([
|
|
1337
|
-
...transducers.vals(unis),
|
|
1338
|
-
...transducers.vals(inputs),
|
|
1339
|
-
...transducers.vals(outputs),
|
|
1340
|
-
...spec[type](target, unis, inputs, Object.assign(Object.assign({}, outputs), outputAliases)),
|
|
1341
|
-
])) + (spec.post ? "\n" + spec.post : ""));
|
|
1342
|
-
};
|
|
1343
|
-
const prepareShaderSource = (spec, type, version) => {
|
|
1344
|
-
const syntax = SYNTAX[version];
|
|
1345
|
-
const prefixes = Object.assign(Object.assign({}, NO_PREFIXES), spec.declPrefixes);
|
|
1346
|
-
const isVS = type === "vs";
|
|
1347
|
-
let src = "";
|
|
1348
|
-
src += `#version ${version}\n`;
|
|
1349
|
-
src += compilePrelude(spec, version);
|
|
1350
|
-
if (spec.generateDecls !== false) {
|
|
1351
|
-
src += isVS
|
|
1352
|
-
? compileVars(spec.attribs, syntax.attrib, prefixes)
|
|
1353
|
-
: compileVars(spec.outputs || DEFAULT_OUTPUT, syntax.output, prefixes);
|
|
1354
|
-
src += compileVars(spec.varying, syntax.varying[type], prefixes);
|
|
1355
|
-
src += compileVars(spec.uniforms, syntax.uniform, prefixes);
|
|
1356
|
-
}
|
|
1357
|
-
src += spec[type];
|
|
1358
|
-
spec.post && (src += "\n" + spec.post);
|
|
1359
|
-
return src;
|
|
1360
|
-
};
|
|
1361
|
-
const compileShader = (gl, type, src) => {
|
|
1362
|
-
const shader = gl.createShader(type) || error("error creating shader");
|
|
1363
|
-
gl.shaderSource(shader, src);
|
|
1364
|
-
gl.compileShader(shader);
|
|
1365
|
-
if (gl.getShaderParameter(shader, gl.COMPILE_STATUS)) {
|
|
1366
|
-
return shader;
|
|
1367
|
-
}
|
|
1368
|
-
return parseAndThrowShaderError(gl, shader, src);
|
|
1369
|
-
};
|
|
1370
|
-
const parseAndThrowShaderError = (gl, shader, src) => {
|
|
1371
|
-
const lines = src.split("\n");
|
|
1372
|
-
const log = gl.getShaderInfoLog(shader).split("\n");
|
|
1373
|
-
const errors = log
|
|
1374
|
-
.map((line) => {
|
|
1375
|
-
const matches = ERROR_REGEXP.exec(line);
|
|
1376
|
-
const ln = matches ? matches[1] : null;
|
|
1377
|
-
if (ln) {
|
|
1378
|
-
return `line ${ln}: ${matches[2]}\n${lines[parseInt(ln) - 1]}`;
|
|
1379
|
-
}
|
|
1380
|
-
})
|
|
1381
|
-
.filter(checks.existsAndNotNull)
|
|
1382
|
-
.join("\n");
|
|
1383
|
-
return error(`Error compiling shader:\n${errors}`);
|
|
1384
|
-
};
|
|
1385
|
-
const initAttributes = (gl, prog, attribs) => {
|
|
1386
|
-
const res = {};
|
|
1387
|
-
for (let id in attribs) {
|
|
1388
|
-
const val = attribs[id];
|
|
1389
|
-
const [type, loc] = checks.isArray(val) ? val : [val, null];
|
|
1390
|
-
const aid = id;
|
|
1391
|
-
if (loc != null) {
|
|
1392
|
-
gl.bindAttribLocation(prog, loc, aid);
|
|
1393
|
-
res[id] = { type, loc };
|
|
1394
|
-
}
|
|
1395
|
-
else {
|
|
1396
|
-
res[id] = {
|
|
1397
|
-
type,
|
|
1398
|
-
loc: gl.getAttribLocation(prog, aid),
|
|
1399
|
-
};
|
|
1400
|
-
}
|
|
1401
|
-
}
|
|
1402
|
-
return res;
|
|
1403
|
-
};
|
|
1404
|
-
const initUniforms = (gl, prog, uniforms = {}) => {
|
|
1405
|
-
const res = {};
|
|
1406
|
-
for (let id in uniforms) {
|
|
1407
|
-
const val = uniforms[id];
|
|
1408
|
-
let type;
|
|
1409
|
-
let t1, t2, defaultVal, defaultFn;
|
|
1410
|
-
if (checks.isArray(val)) {
|
|
1411
|
-
[type, t1, t2] = val;
|
|
1412
|
-
defaultVal = type.indexOf("[]") < 0 ? t1 : t2;
|
|
1413
|
-
if (checks.isFunction(defaultVal)) {
|
|
1414
|
-
defaultFn = defaultVal;
|
|
1415
|
-
defaultVal = undefined;
|
|
1416
|
-
}
|
|
1417
|
-
}
|
|
1418
|
-
else {
|
|
1419
|
-
type = val;
|
|
1420
|
-
}
|
|
1421
|
-
const loc = gl.getUniformLocation(prog, id);
|
|
1422
|
-
if (loc != null) {
|
|
1423
|
-
const setter = UNIFORM_SETTERS[type];
|
|
1424
|
-
if (setter) {
|
|
1425
|
-
res[id] = {
|
|
1426
|
-
loc,
|
|
1427
|
-
setter: setter(gl, loc, defaultVal),
|
|
1428
|
-
defaultFn,
|
|
1429
|
-
defaultVal,
|
|
1430
|
-
type,
|
|
1431
|
-
};
|
|
1432
|
-
}
|
|
1433
|
-
else {
|
|
1434
|
-
error(`invalid uniform type: ${type}`);
|
|
1435
|
-
}
|
|
1436
|
-
}
|
|
1437
|
-
else {
|
|
1438
|
-
exports.LOGGER.warn(`unknown uniform: ${id}`);
|
|
1439
|
-
}
|
|
1440
|
-
}
|
|
1441
|
-
return res;
|
|
1442
|
-
};
|
|
1443
|
-
|
|
1444
|
-
const PASSTHROUGH_VS = (gl, _, ins) => [
|
|
1445
|
-
shaderAst.defMain(() => [shaderAst.assign(gl.gl_Position, shaderAst.vec4(ins.position, shaderAst.FLOAT0, shaderAst.FLOAT1))]),
|
|
1446
|
-
];
|
|
1447
|
-
const PASSTHROUGH_VS_UV = (gl, _, ins, outs) => [
|
|
1448
|
-
shaderAst.defMain(() => [
|
|
1449
|
-
shaderAst.assign(outs.v_uv, ins.uv),
|
|
1450
|
-
shaderAst.assign(gl.gl_Position, shaderAst.vec4(ins.position, shaderAst.FLOAT0, shaderAst.FLOAT1)),
|
|
1451
|
-
]),
|
|
1452
|
-
];
|
|
1453
|
-
const PASSTHROUGH_FS = (gl, _, __, outs) => [
|
|
1454
|
-
shaderAst.defMain(() => [shaderAst.assign(outs.fragColor, shaderAst.$xy(gl.gl_FragCoord))]),
|
|
1455
|
-
];
|
|
1456
|
-
const PASSTHROUGH_FS_UV = (_, unis, ins, outs) => [
|
|
1457
|
-
shaderAst.defMain(() => [shaderAst.assign(outs.fragColor, shaderAst.texture(unis.tex, ins.v_uv))]),
|
|
1458
|
-
];
|
|
1459
|
-
const FX_SHADER_SPEC = {
|
|
1460
|
-
vs: PASSTHROUGH_VS,
|
|
1461
|
-
fs: PASSTHROUGH_FS,
|
|
1462
|
-
attribs: { position: "vec2" },
|
|
1463
|
-
varying: {},
|
|
1464
|
-
uniforms: {},
|
|
1465
|
-
state: { depth: false },
|
|
1466
|
-
ext: {},
|
|
1467
|
-
};
|
|
1468
|
-
const FX_SHADER_SPEC_UV = {
|
|
1469
|
-
vs: PASSTHROUGH_VS_UV,
|
|
1470
|
-
fs: PASSTHROUGH_FS_UV,
|
|
1471
|
-
attribs: { position: "vec2", uv: "vec2" },
|
|
1472
|
-
varying: { v_uv: "vec2" },
|
|
1473
|
-
uniforms: { tex: "sampler2D" },
|
|
1474
|
-
state: { depth: false },
|
|
1475
|
-
ext: {},
|
|
1476
|
-
};
|
|
1477
|
-
|
|
1478
|
-
const defMultiPass = (opts) => {
|
|
1479
|
-
const gl = opts.gl;
|
|
1480
|
-
const numPasses = opts.passes.length;
|
|
1481
|
-
api.assert(numPasses > 0, "require at least one shader pass");
|
|
1482
|
-
const useMainBuffer = !opts.passes[numPasses - 1].outputs.length;
|
|
1483
|
-
const textures = initTextures(opts);
|
|
1484
|
-
const passes = initPasses(opts, textures);
|
|
1485
|
-
const fbos = initBuffers(opts, textures, useMainBuffer);
|
|
1486
|
-
const drawPass = (i, time) => {
|
|
1487
|
-
const spec = opts.passes[i];
|
|
1488
|
-
const pass = passes[i];
|
|
1489
|
-
const shader = pass.shader;
|
|
1490
|
-
const size = spec.outputs.length
|
|
1491
|
-
? textures[spec.outputs[0]].size
|
|
1492
|
-
: [gl.drawingBufferWidth, gl.drawingBufferHeight];
|
|
1493
|
-
shader.uniforms.resolution && (pass.uniforms.resolution = size);
|
|
1494
|
-
shader.uniforms.time && (pass.uniforms.time = time);
|
|
1495
|
-
gl.viewport(0, 0, size[0], size[1]);
|
|
1496
|
-
draw(pass);
|
|
1497
|
-
};
|
|
1498
|
-
const update = (time) => {
|
|
1499
|
-
for (let i = 0; i < fbos.length; i++) {
|
|
1500
|
-
fbos[i].bind();
|
|
1501
|
-
drawPass(i, time);
|
|
1502
|
-
fbos[i].unbind();
|
|
1503
|
-
}
|
|
1504
|
-
useMainBuffer && drawPass(numPasses - 1, time);
|
|
1505
|
-
};
|
|
1506
|
-
const updateRAF = () => {
|
|
1507
|
-
update((Date.now() - t0) * 1e-3);
|
|
1508
|
-
active && (rafID = requestAnimationFrame(updateRAF));
|
|
1509
|
-
};
|
|
1510
|
-
let active;
|
|
1511
|
-
let t0 = Date.now();
|
|
1512
|
-
let rafID;
|
|
1513
|
-
const instance = {
|
|
1514
|
-
start() {
|
|
1515
|
-
t0 = Date.now();
|
|
1516
|
-
active = true;
|
|
1517
|
-
rafID = requestAnimationFrame(updateRAF);
|
|
1518
|
-
},
|
|
1519
|
-
stop() {
|
|
1520
|
-
if (active) {
|
|
1521
|
-
active = false;
|
|
1522
|
-
cancelAnimationFrame(rafID);
|
|
1523
|
-
}
|
|
1524
|
-
},
|
|
1525
|
-
update(time) {
|
|
1526
|
-
update(time);
|
|
1527
|
-
},
|
|
1528
|
-
passes: opts.passes,
|
|
1529
|
-
fbos,
|
|
1530
|
-
models: passes,
|
|
1531
|
-
textures,
|
|
1532
|
-
};
|
|
1533
|
-
return instance;
|
|
1534
|
-
};
|
|
1535
|
-
const initPasses = (opts, textures) => {
|
|
1536
|
-
const gl = opts.gl;
|
|
1537
|
-
const model = compileModel(gl, defQuadModel({ uv: false }));
|
|
1538
|
-
return opts.passes.map((pass) => {
|
|
1539
|
-
const m = pass.model ? compileModel(gl, pass.model) : Object.assign({}, model);
|
|
1540
|
-
m.shader = initShader(gl, pass, textures);
|
|
1541
|
-
m.uniforms = Object.assign({}, pass.uniformVals);
|
|
1542
|
-
pass.inputs.length > 0 &&
|
|
1543
|
-
(m.textures = pass.inputs.map((id) => textures[id]));
|
|
1544
|
-
return m;
|
|
1545
|
-
});
|
|
1546
|
-
};
|
|
1547
|
-
const initShader = (gl, pass, textures) => {
|
|
1548
|
-
const isGL2 = isGL2Context(gl);
|
|
1549
|
-
const numIns = pass.inputs.length;
|
|
1550
|
-
const numOuts = pass.outputs.length;
|
|
1551
|
-
const ext = {};
|
|
1552
|
-
const spec = {
|
|
1553
|
-
vs: pass.vs || PASSTHROUGH_VS,
|
|
1554
|
-
fs: pass.fs,
|
|
1555
|
-
attribs: pass.attribs || {
|
|
1556
|
-
position: "vec2",
|
|
1557
|
-
},
|
|
1558
|
-
varying: pass.varying,
|
|
1559
|
-
uniforms: Object.assign(Object.assign({}, pass.uniforms), transducers.transduce(transducers.map((i) => [`input${i}`, ["sampler2D", i]]), transducers.assocObj(), transducers.range(numIns))),
|
|
1560
|
-
outputs: numOuts
|
|
1561
|
-
? transducers.transduce(transducers.map((i) => [
|
|
1562
|
-
`output${i}`,
|
|
1563
|
-
["vec4", i],
|
|
1564
|
-
]), transducers.assocObj(), transducers.range(numOuts))
|
|
1565
|
-
: undefined,
|
|
1566
|
-
state: pass.state,
|
|
1567
|
-
pre: pass.pre,
|
|
1568
|
-
post: pass.post,
|
|
1569
|
-
replacePrelude: pass.replacePrelude,
|
|
1570
|
-
generateDecls: pass.generateDecls,
|
|
1571
|
-
ext,
|
|
1572
|
-
};
|
|
1573
|
-
const floatIn = transducers.some((id) => isFloatTexture(textures[id]), pass.inputs);
|
|
1574
|
-
const floatOut = transducers.some((id) => isFloatTexture(textures[id]), pass.outputs);
|
|
1575
|
-
if (!isGL2) {
|
|
1576
|
-
floatIn && (ext.OES_texture_float = "require");
|
|
1577
|
-
numOuts > 1 && (ext.WEBGL_draw_buffers = "require");
|
|
1578
|
-
}
|
|
1579
|
-
if (floatOut) {
|
|
1580
|
-
ext[isGL2 ? "EXT_color_buffer_float" : "WEBGL_color_buffer_float"] =
|
|
1581
|
-
"require";
|
|
1582
|
-
}
|
|
1583
|
-
return defShader(gl, spec);
|
|
1584
|
-
};
|
|
1585
|
-
const initTextures = (opts) => Object.keys(opts.textures).reduce((acc, id) => {
|
|
1586
|
-
acc[id] = defTexture(opts.gl, Object.assign({ width: opts.width, height: opts.height, filter: opts.gl.NEAREST, wrap: opts.gl.CLAMP_TO_EDGE, image: null }, opts.textures[id]));
|
|
1587
|
-
return acc;
|
|
1588
|
-
}, {});
|
|
1589
|
-
const initBuffers = (opts, textures, useMainBuffer) => (useMainBuffer
|
|
1590
|
-
? opts.passes.slice(0, opts.passes.length - 1)
|
|
1591
|
-
: opts.passes).map((pass) => defFBO(opts.gl, { tex: pass.outputs.map((id) => textures[id]) }));
|
|
1592
|
-
const passCopy = (src, dest) => {
|
|
1593
|
-
api.assert(src.length === dest.length, `require same number of in/out textures`);
|
|
1594
|
-
return {
|
|
1595
|
-
fs: (gl, unis, _, outs) => [
|
|
1596
|
-
shaderAst.defMain(() => [
|
|
1597
|
-
...transducers.map((i) => shaderAst.assign(outs[`output${i}`], shaderAst.texelFetch(unis[`input${i}`], shaderAst.ivec2(shaderAst.$xy(gl.gl_FragCoord)), shaderAst.INT0)), transducers.range(src.length)),
|
|
1598
|
-
]),
|
|
1599
|
-
],
|
|
1600
|
-
inputs: src,
|
|
1601
|
-
outputs: dest,
|
|
1602
|
-
};
|
|
1603
|
-
};
|
|
1604
|
-
|
|
1605
|
-
const readPixels = (gl, x, y, w, h, format, type, out) => {
|
|
1606
|
-
gl.readPixels(x, y, w, h, format, type, out);
|
|
1607
|
-
return out;
|
|
1608
|
-
};
|
|
1609
|
-
const readTexture = (gl, tex, format, type, out) => {
|
|
1610
|
-
const fbo = new FBO(gl, { tex: [tex] });
|
|
1611
|
-
fbo.bind();
|
|
1612
|
-
gl.readPixels(0, 0, tex.size[0], tex.size[1], format, type, out);
|
|
1613
|
-
fbo.unbind();
|
|
1614
|
-
fbo.release();
|
|
1615
|
-
return out;
|
|
1616
|
-
};
|
|
1617
|
-
|
|
1618
|
-
const positionAttrib = (opts, attribs, pos = "position") => opts.instancePos
|
|
1619
|
-
? shaderAst.add(attribs[pos], attribs[opts.instancePos])
|
|
1620
|
-
: attribs[pos];
|
|
1621
|
-
const colorAttrib = (opts, attribs, fallback) => opts.instanceColor
|
|
1622
|
-
? shaderAst.mul(attribs[opts.instanceColor], fallback)
|
|
1623
|
-
: opts.color
|
|
1624
|
-
? shaderAst.mul(attribs[opts.color], fallback)
|
|
1625
|
-
: fallback;
|
|
1626
|
-
|
|
1627
|
-
const LAMBERT = (opts = {}) => ({
|
|
1628
|
-
vs: (gl, unis, ins, outs) => [
|
|
1629
|
-
shaderAst.defMain(() => [
|
|
1630
|
-
opts.uv ? shaderAst.assign(outs.vuv, ins[opts.uv]) : null,
|
|
1631
|
-
shaderAst.assign(outs.vcolor, colorAttrib(opts, ins, unis.diffuseCol)),
|
|
1632
|
-
shaderAst.assign(outs.vnormal, shaderAstStdlib.surfaceNormal(ins.normal, unis.normalMat)),
|
|
1633
|
-
shaderAst.assign(gl.gl_Position, shaderAstStdlib.transformMVP(positionAttrib(opts, ins), unis.model, unis.view, unis.proj)),
|
|
1634
|
-
]),
|
|
1635
|
-
],
|
|
1636
|
-
fs: (_, unis, ins, outs) => [
|
|
1637
|
-
shaderAst.defMain(() => [
|
|
1638
|
-
shaderAst.assign(outs.fragColor, shaderAst.vec4(shaderAstStdlib.diffuseLighting((opts.bidir !== false ? shaderAstStdlib.halfLambert : shaderAstStdlib.lambert)(shaderAst.normalize(ins.vnormal), unis.lightDir), opts.uv
|
|
1639
|
-
? shaderAst.mul(shaderAst.$(shaderAst.texture(unis.tex, ins.vuv), "xyz"), ins.vcolor)
|
|
1640
|
-
: ins.vcolor, unis.lightCol, unis.ambientCol), 1)),
|
|
1641
|
-
]),
|
|
1642
|
-
],
|
|
1643
|
-
attribs: Object.assign(Object.assign(Object.assign(Object.assign({ position: "vec3", normal: "vec3" }, (opts.uv ? { [opts.uv]: "vec2" } : null)), (opts.color && !opts.instanceColor
|
|
1644
|
-
? { [opts.color]: "vec3" }
|
|
1645
|
-
: null)), (opts.instancePos ? { [opts.instancePos]: "vec3" } : null)), (opts.instanceColor ? { [opts.instanceColor]: "vec3" } : null)),
|
|
1646
|
-
varying: Object.assign({ vcolor: "vec3", vnormal: "vec3" }, (opts.uv ? { vuv: "vec2" } : null)),
|
|
1647
|
-
uniforms: Object.assign(Object.assign({ model: "mat4", view: "mat4", proj: "mat4", normalMat: ["mat4", autoNormalMatrix2()], lightDir: ["vec3", [0, 1, 0]], lightCol: ["vec3", [1, 1, 1]] }, defMaterial(Object.assign({ diffuseCol: [1, 1, 1] }, opts.material), { specularCol: false })), (opts.uv ? { tex: "sampler2D" } : null)),
|
|
1648
|
-
state: Object.assign({ depth: true, cull: true }, opts.state),
|
|
1649
|
-
});
|
|
1650
|
-
|
|
1651
|
-
const PHONG = (opts = {}) => ({
|
|
1652
|
-
vs: (gl, unis, ins, outs) => [
|
|
1653
|
-
shaderAst.defMain(() => {
|
|
1654
|
-
let worldPos;
|
|
1655
|
-
return [
|
|
1656
|
-
(worldPos = shaderAst.sym(shaderAst.mul(unis.model, shaderAst.vec4(positionAttrib(opts, ins), 1)))),
|
|
1657
|
-
shaderAst.assign(outs.vnormal, shaderAstStdlib.surfaceNormal(ins.normal, unis.normalMat)),
|
|
1658
|
-
shaderAst.assign(outs.vlight, shaderAst.sub(unis.lightPos, shaderAst.$(worldPos, "xyz"))),
|
|
1659
|
-
shaderAst.assign(outs.veye, shaderAst.sub(unis.eyePos, shaderAst.$(worldPos, "xyz"))),
|
|
1660
|
-
shaderAst.assign(outs.vcolor, colorAttrib(opts, ins, unis.diffuseCol)),
|
|
1661
|
-
shaderAst.assign(gl.gl_Position, shaderAst.mul(shaderAst.mul(unis.proj, unis.view), worldPos)),
|
|
1662
|
-
];
|
|
1663
|
-
}),
|
|
1664
|
-
],
|
|
1665
|
-
fs: (_, unis, ins, outs) => [
|
|
1666
|
-
shaderAst.defMain(() => {
|
|
1667
|
-
let normal;
|
|
1668
|
-
let light;
|
|
1669
|
-
let directional;
|
|
1670
|
-
let specular;
|
|
1671
|
-
return [
|
|
1672
|
-
(normal = shaderAst.sym(shaderAst.normalize(ins.vnormal))),
|
|
1673
|
-
(light = shaderAst.sym(shaderAst.normalize(ins.vlight))),
|
|
1674
|
-
(directional = shaderAst.sym(shaderAst.max(shaderAst.dot(normal, light), shaderAst.FLOAT0))),
|
|
1675
|
-
(specular = shaderAst.sym(shaderAst.ternary(shaderAst.gt(directional, shaderAst.FLOAT0), shaderAst.pow(shaderAst.dot(normal, shaderAst.normalize(shaderAst.add(light, shaderAst.normalize(ins.veye)))), unis.shininess), shaderAst.FLOAT0))),
|
|
1676
|
-
shaderAst.assign(outs.fragColor, shaderAst.vec4(shaderAst.add(shaderAstStdlib.diffuseLighting(directional, ins.vcolor, unis.lightCol, unis.ambientCol), shaderAst.mul(unis.specularCol, specular)), 1)),
|
|
1677
|
-
];
|
|
1678
|
-
}),
|
|
1679
|
-
],
|
|
1680
|
-
attribs: Object.assign(Object.assign(Object.assign({ position: "vec3", normal: "vec3" }, (opts.color && !opts.instanceColor
|
|
1681
|
-
? { [opts.color]: "vec3" }
|
|
1682
|
-
: null)), (opts.instancePos ? { [opts.instancePos]: "vec3" } : null)), (opts.instanceColor ? { [opts.instanceColor]: "vec3" } : null)),
|
|
1683
|
-
varying: {
|
|
1684
|
-
vnormal: "vec3",
|
|
1685
|
-
veye: "vec3",
|
|
1686
|
-
vlight: "vec3",
|
|
1687
|
-
vcolor: "vec3",
|
|
1688
|
-
},
|
|
1689
|
-
uniforms: Object.assign({ model: "mat4", normalMat: ["mat4", autoNormalMatrix1()], view: "mat4", proj: "mat4", shininess: ["float", 32], eyePos: "vec3", lightPos: ["vec3", [0, 0, 2]], lightCol: ["vec3", [1, 1, 1]] }, defMaterial(opts.material)),
|
|
1690
|
-
state: Object.assign({ depth: true, cull: true }, opts.state),
|
|
1691
|
-
});
|
|
1692
|
-
|
|
1693
|
-
const checkerboard = (opts) => {
|
|
1694
|
-
opts = Object.assign({ size: 16, col1: 0xffffffff, col2: 0xff000000, cornerCols: [0xffff0000, 0xff00ff00, 0xff0000ff, 0xffffff00] }, opts);
|
|
1695
|
-
const size = opts.size;
|
|
1696
|
-
const col1 = pixel.ARGB8888.toABGR(opts.col1);
|
|
1697
|
-
const col2 = pixel.ARGB8888.toABGR(opts.col2);
|
|
1698
|
-
const { canvas, ctx, img, pixels } = pixel.canvasPixels(size);
|
|
1699
|
-
for (let y = 0, i = 0; y < size; y++) {
|
|
1700
|
-
for (let x = 0; x < size; x++, i++) {
|
|
1701
|
-
pixels[i] = (y & 1) ^ (x & 1) ? col1 : col2;
|
|
1702
|
-
}
|
|
1703
|
-
}
|
|
1704
|
-
if (opts.corners) {
|
|
1705
|
-
const corners = opts.cornerCols.map(pixel.ARGB8888.toABGR);
|
|
1706
|
-
pixels[0] = corners[0];
|
|
1707
|
-
pixels[size - 1] = corners[1];
|
|
1708
|
-
pixels[pixels.length - size] = corners[2];
|
|
1709
|
-
pixels[pixels.length - 1] = corners[3];
|
|
1710
|
-
}
|
|
1711
|
-
ctx.putImageData(img, 0, 0);
|
|
1712
|
-
return canvas;
|
|
1713
|
-
};
|
|
1714
|
-
|
|
1715
|
-
const stripes = (opts) => {
|
|
1716
|
-
opts = Object.assign({ size: 16, col1: 0xffffffff, col2: 0xff000000 }, opts);
|
|
1717
|
-
const size = opts.size;
|
|
1718
|
-
const col1 = pixel.ARGB8888.toABGR(opts.col1);
|
|
1719
|
-
const col2 = pixel.ARGB8888.toABGR(opts.col2);
|
|
1720
|
-
const { canvas, ctx, img, pixels } = opts.horizontal
|
|
1721
|
-
? pixel.canvasPixels(1, size)
|
|
1722
|
-
: pixel.canvasPixels(size, 1);
|
|
1723
|
-
for (let x = size; --x >= 0;) {
|
|
1724
|
-
pixels[x] = x & 1 ? col1 : col2;
|
|
1725
|
-
}
|
|
1726
|
-
ctx.putImageData(img, 0, 0);
|
|
1727
|
-
return canvas;
|
|
1728
|
-
};
|
|
1729
|
-
|
|
1730
|
-
const defCubeModel = (opts) => {
|
|
1731
|
-
opts = Object.assign({ size: 1, normal: true, uv: true }, opts);
|
|
1732
|
-
const s = opts.size;
|
|
1733
|
-
const spec = {
|
|
1734
|
-
attribs: {
|
|
1735
|
-
position: {
|
|
1736
|
-
data: new Float32Array([s, s, -s, s, s, s, s, -s, s, s, -s, -s, -s, s, s, -s, s, -s, -s, -s, -s, -s, -s, s, -s, s, s, s, s, s, s, s, -s, -s, s, -s, -s, -s, -s, s, -s, -s, s, -s, s, -s, -s, s, s, s, s, -s, s, s, -s, -s, s, s, -s, s, -s, s, -s, s, s, -s, s, -s, -s, -s, -s, -s])
|
|
1737
|
-
},
|
|
1738
|
-
},
|
|
1739
|
-
indices: {
|
|
1740
|
-
data: new Uint16Array([0, 1, 2, 0, 2, 3, 4, 5, 6, 4, 6, 7, 8, 9, 10, 8, 10, 11, 12, 13, 14, 12, 14, 15, 16, 17, 18, 16, 18, 19, 20, 21, 22, 20, 22, 23])
|
|
1741
|
-
},
|
|
1742
|
-
uniforms: {},
|
|
1743
|
-
shader: null,
|
|
1744
|
-
mode: exports.DrawMode.TRIANGLES,
|
|
1745
|
-
num: 36
|
|
1746
|
-
};
|
|
1747
|
-
opts.normal && (spec.attribs.normal = {
|
|
1748
|
-
data: new Float32Array([1, 0, 0, 1, 0, 0, 1, 0, 0, 1, 0, 0, -1, 0, 0, -1, 0, 0, -1, 0, 0, -1, 0, 0, 0, 1, 0, 0, 1, 0, 0, 1, 0, 0, 1, 0, 0, -1, 0, 0, -1, 0, 0, -1, 0, 0, -1, 0, 0, 0, 1, 0, 0, 1, 0, 0, 1, 0, 0, 1, 0, 0, -1, 0, 0, -1, 0, 0, -1, 0, 0, -1])
|
|
1749
|
-
});
|
|
1750
|
-
opts.uv && (spec.attribs.uv = {
|
|
1751
|
-
data: new Float32Array([1, 0, 0, 0, 0, 1, 1, 1, 1, 0, 0, 0, 0, 1, 1, 1, 1, 0, 0, 0, 0, 1, 1, 1, 1, 0, 0, 0, 0, 1, 1, 1, 1, 0, 0, 0, 0, 1, 1, 1, 1, 0, 0, 0, 0, 1, 1, 1]),
|
|
1752
|
-
size: 2
|
|
1753
|
-
});
|
|
1754
|
-
return spec;
|
|
1755
|
-
};
|
|
1756
|
-
|
|
1757
|
-
exports.ALIAS_TEXTURE = ALIAS_TEXTURE;
|
|
1758
|
-
exports.BLEND_ADD = BLEND_ADD;
|
|
1759
|
-
exports.BLEND_NORMAL = BLEND_NORMAL;
|
|
1760
|
-
exports.DEFAULT_MATERIAL = DEFAULT_MATERIAL;
|
|
1761
|
-
exports.DEFAULT_OUTPUT = DEFAULT_OUTPUT;
|
|
1762
|
-
exports.EXPORT_FRAGCOL = EXPORT_FRAGCOL;
|
|
1763
|
-
exports.FBO = FBO;
|
|
1764
|
-
exports.FX_SHADER_SPEC = FX_SHADER_SPEC;
|
|
1765
|
-
exports.FX_SHADER_SPEC_UV = FX_SHADER_SPEC_UV;
|
|
1766
|
-
exports.GLSL_HEADER = GLSL_HEADER;
|
|
1767
|
-
exports.GL_EXT_INFO = GL_EXT_INFO;
|
|
1768
|
-
exports.LAMBERT = LAMBERT;
|
|
1769
|
-
exports.NO_PREFIXES = NO_PREFIXES;
|
|
1770
|
-
exports.PASSTHROUGH_FS = PASSTHROUGH_FS;
|
|
1771
|
-
exports.PASSTHROUGH_FS_UV = PASSTHROUGH_FS_UV;
|
|
1772
|
-
exports.PASSTHROUGH_VS = PASSTHROUGH_VS;
|
|
1773
|
-
exports.PASSTHROUGH_VS_UV = PASSTHROUGH_VS_UV;
|
|
1774
|
-
exports.PHONG = PHONG;
|
|
1775
|
-
exports.PREFIXES = PREFIXES;
|
|
1776
|
-
exports.RBO = RBO;
|
|
1777
|
-
exports.SYNTAX = SYNTAX;
|
|
1778
|
-
exports.Shader = Shader;
|
|
1779
|
-
exports.TEX_FORMATS = TEX_FORMATS;
|
|
1780
|
-
exports.Texture = Texture;
|
|
1781
|
-
exports.VERSION_CHECK = VERSION_CHECK;
|
|
1782
|
-
exports.WebGLArrayBuffer = WebGLArrayBuffer;
|
|
1783
|
-
exports.WebGLError = WebGLError;
|
|
1784
|
-
exports.autoNormalMatrix1 = autoNormalMatrix1;
|
|
1785
|
-
exports.autoNormalMatrix2 = autoNormalMatrix2;
|
|
1786
|
-
exports.bindTextures = bindTextures;
|
|
1787
|
-
exports.checkerboard = checkerboard;
|
|
1788
|
-
exports.colorAttrib = colorAttrib;
|
|
1789
|
-
exports.compileAttribPool = compileAttribPool;
|
|
1790
|
-
exports.compileIndices = compileIndices;
|
|
1791
|
-
exports.compileModel = compileModel;
|
|
1792
|
-
exports.compileShader = compileShader;
|
|
1793
|
-
exports.compileVAO = compileVAO;
|
|
1794
|
-
exports.defBuffer = defBuffer;
|
|
1795
|
-
exports.defCubeModel = defCubeModel;
|
|
1796
|
-
exports.defFBO = defFBO;
|
|
1797
|
-
exports.defMaterial = defMaterial;
|
|
1798
|
-
exports.defMultiPass = defMultiPass;
|
|
1799
|
-
exports.defQuadModel = defQuadModel;
|
|
1800
|
-
exports.defRBO = defRBO;
|
|
1801
|
-
exports.defShader = defShader;
|
|
1802
|
-
exports.defTexture = defTexture;
|
|
1803
|
-
exports.defTextureCubeMap = defTextureCubeMap;
|
|
1804
|
-
exports.defTextureFloat = defTextureFloat;
|
|
1805
|
-
exports.draw = draw;
|
|
1806
|
-
exports.error = error;
|
|
1807
|
-
exports.getExtensions = getExtensions;
|
|
1808
|
-
exports.glCanvas = glCanvas;
|
|
1809
|
-
exports.isFloatTexture = isFloatTexture;
|
|
1810
|
-
exports.isGL2Context = isGL2Context;
|
|
1811
|
-
exports.passCopy = passCopy;
|
|
1812
|
-
exports.positionAttrib = positionAttrib;
|
|
1813
|
-
exports.prepareShaderSource = prepareShaderSource;
|
|
1814
|
-
exports.readPixels = readPixels;
|
|
1815
|
-
exports.readTexture = readTexture;
|
|
1816
|
-
exports.screen2d = screen2d;
|
|
1817
|
-
exports.setLogger = setLogger;
|
|
1818
|
-
exports.shaderSourceFromAST = shaderSourceFromAST;
|
|
1819
|
-
exports.stripes = stripes;
|
|
1820
|
-
exports.unbindTextures = unbindTextures;
|