-
Notifications
You must be signed in to change notification settings - Fork 65
/
shader.js
206 lines (185 loc) · 5.78 KB
/
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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
var createRegl = require('regl');
var createQuad = require('primitive-quad');
var parseColor = require('parse-color');
var defined = require('defined');
module.exports = createShader;
function createShader (opt) {
opt = opt || {};
if (!opt.gl) {
throw new Error('Must specify { context: "webgl" } in sketch settings, or a WebGL-enabled canvas');
}
var gl = opt.gl;
var reglOpts = { gl: gl };
// regl is strict on what options you pass in
if (typeof opt.extensions !== 'undefined') reglOpts.extensions = opt.extensions;
if (typeof opt.optionalExtensions !== 'undefined') reglOpts.optionalExtensions = opt.optionalExtensions;
if (typeof opt.profile !== 'undefined') reglOpts.profile = opt.profile;
if (typeof opt.onDone !== 'undefined') reglOpts.onDone = opt.onDone;
// Create regl for handling GL stuff
var regl = createRegl(reglOpts);
// A mesh for a flat plane
var quad = createQuad();
var textureMap = new Map();
// Wire up user uniforms nicely
var uniformsMap = opt.uniforms || {};
var uniforms = Object.assign({}, uniformsMap);
Object.keys(uniformsMap).forEach(function (key) {
var value = uniformsMap[key];
if (typeof value === 'function') {
uniforms[key] = function (state, props, batchID) {
var result = value.call(uniformsMap, props, batchID);
// If user is using a function to wrap an image,
// then we need to make sure we re-upload to same GL texture
if (isTextureLike(result)) {
if (textureMap.has(value)) {
// Texture is already created, re-upload
var prevTex = textureMap.get(value);
prevTex(result);
// Return the texture
result = prevTex;
} else {
// Creating the texture for the first time
var texture = regl.texture(result);
textureMap.set(value, texture);
// Return the texture, not the image
result = texture;
}
}
return result;
};
} else if (isTextureLike(value)) {
uniforms[key] = regl.texture(value);
} else {
uniforms[key] = value;
}
});
// Get the drawing command
var drawQuadCommand;
try {
drawQuadCommand = createDrawQuad();
} catch (err) {
handleError(err);
}
// Nicely get a clear color for the canvas
var clearColor = defined(opt.clearColor, 'black');
if (typeof clearColor === 'string') {
var parsed = parseColor(clearColor);
if (!parsed.rgb) {
throw new Error('Error parsing { clearColor } color string "' + clearColor + '"');
}
clearColor = parsed.rgb.slice(0, 3).map(function (n) {
return n / 255;
});
} else if (clearColor && (!Array.isArray(clearColor) || clearColor.length < 3)) {
throw new Error('Error with { clearColor } option, must be a string or [ r, g, b ] float array');
}
var clearAlpha = defined(opt.clearAlpha, 1);
var clear = clearColor ? clearColor.concat([ clearAlpha || 0 ]) : false;
// Return a renderer object
return {
render: function (props) {
// On each tick, update regl timers and sizes
regl.poll();
// Clear backbuffer with color
if (clear) {
regl.clear({
color: clear,
depth: 1,
stencil: 0
});
}
// Submit draw command
drawQuad(props);
// Flush pending GL calls for this frame
gl.flush();
},
regl: regl,
drawQuad: drawQuad,
unload: function () {
// Remove GL texture mappings
textureMap.clear();
// Unload the current regl instance
// TODO: We should probably also destroy textures created from this module!
regl.destroy();
}
};
// A user-friendly draw command that spits out errors
function drawQuad (props) {
props = props || {};
// Draw generative / shader art
if (drawQuadCommand) {
try {
drawQuadCommand(props);
} catch (err) {
if (handleError(err)) {
if (props == null) {
console.warn('Warning: shader.render() is not called with any "props" parameter');
}
}
}
}
}
// Draw command
function createDrawQuad () {
return regl({
scissor: opt.scissor ? {
enable: true,
box: {
x: regl.prop('scissorX'),
y: regl.prop('scissorY'),
width: regl.prop('scissorWidth'),
height: regl.prop('scissorHeight')
}
} : false,
// Pass down props from javascript
uniforms: uniforms,
// Fall back to a simple fragment shader
frag: opt.frag || [
'precision highp float;',
'',
'void main () {',
' gl_FragColor = vec4(0.0, 0.0, 0.0, 1.0);',
'}'
].join('\n'),
// Fall back to a simple vertex shader
vert: opt.vert || [
'precision highp float;',
'attribute vec3 position;',
'varying vec2 vUv;',
'',
'void main () {',
' gl_Position = vec4(position.xyz, 1.0);',
' vUv = gl_Position.xy * 0.5 + 0.5;',
'}'
].join('\n'),
// Setup transparency blending
blend: opt.blend !== false ? {
enable: true,
func: {
srcRGB: 'src alpha',
srcAlpha: 1,
dstRGB: 'one minus src alpha',
dstAlpha: 1
}
} : undefined,
// Send mesh vertex attributes to shader
attributes: {
position: quad.positions
},
// The indices for the quad mesh
elements: quad.cells
});
}
function handleError (err) {
if (/^\(regl\)/.test(err.message)) {
// Regl already logs a message to the console :\
// so let's just avoid re-printing the same thing
return true;
} else {
throw err;
}
}
}
function isTextureLike (data) {
return data && !Array.isArray(data) && typeof data === 'object';
}