xxxxxxxxxx
55
// Trying to make a grayscale pass on a p5.Graphics.
// It appears to render from and to the main canvas, just not gray
function setup() {
createCanvas(400, 400, WEBGL)
pg = new p5.Graphics(width, height, WEBGL, _renderer._pInst)
pg.noStroke()
noStroke()
background('RED')
circle(50,50,50)
s = createShader(vert, frag)
pg.shader(s)
// shader(s) // for testing on only main canvas
s.setUniform('tex0', _renderer)
pg.rect(0,0,width,height)
texture(pg)
plane(width, height)
// rect(0,0,width,height) // for testing on only main canvas
}
vert = `attribute vec3 aPosition;
attribute vec2 aTexCoord;
varying vec2 vTexCoord;
void main() {
vTexCoord = aTexCoord;
vec4 positionVec4 = vec4(aPosition, 1.0);
positionVec4.xy = positionVec4.xy * 2.0 - 1.0;
gl_Position = positionVec4;
}`
frag = `precision mediump float;
varying mediump vec2 vTexCoord;
// passed manually from sketch
uniform sampler2D tex0;
float luma(vec3 color) {
return dot(color, vec3(0.299, 0.587, 0.114));
}
void main() {
vec2 uv = vTexCoord;
// gets flipped vertically
uv.y = 1.0 - uv.y;
vec4 sampledColor = texture2D(tex0, uv);
float gray = luma(sampledColor.rgb);
gl_FragColor = vec4(gray, gray, gray, 1);
}`