Not sure if this is a bug but when I try to use createShader()
instead of loadShader()
it doesn’t work on a p5.Graphics object.
Figured I’d post here before creating an issue in case I’m doing something wrong.
Below is a demo sketch and you can see it running on the web editor at: https://editor.p5js.org/duskvirkus/sketches/dWj7w-k3k
- Doesn’t have a problem with the exact same sketch when
loadShader()
is used. To demonstrate this, uselet useCreateShader = false;
. -
createShader()
works when not trying to draw on a p5.Graphics object. See this at https://editor.p5js.org/duskvirkus/sketches/R_bjIjzNG
Any advice would be helpful. I’ll file an issue unless someone points out something I’m missing.
sketch.js
let useCreateShader = true;
let sh;
function preload(){
if (!useCreateShader) {
sh = loadShader('shader.vert', 'shader.frag');
}
}
function setup() {
createCanvas(400, 400, WEBGL);
noStroke();
if (useCreateShader) {
sh = createShader(
`
// vert
#ifdef GL_ES
precision mediump float;
#endif
attribute vec3 aPosition;
void main() {
vec4 positionVec4 = vec4(aPosition, 1.0);
positionVec4.xy = positionVec4.xy * 2.0 - 1.0;
gl_Position = positionVec4;
}
`,
`
// frag
#ifdef GL_ES
precision mediump float;
#endif
uniform vec2 u_resolution;
void main() {
vec2 st = gl_FragCoord.xy/u_resolution.xy;
gl_FragColor = vec4(st.x,0.0,1.0,1.0); // R,G,B,A
}
`
);
}
}
function draw() {
sh.setUniform('u_resolution', [width, height]);
shader(sh);
rect(0,0,width, height);
}
shader.vert
// vert
#ifdef GL_ES
precision mediump float;
#endif
attribute vec3 aPosition;
void main() {
vec4 positionVec4 = vec4(aPosition, 1.0);
positionVec4.xy = positionVec4.xy * 2.0 - 1.0;
gl_Position = positionVec4;
}
shader.frag
// frag
#ifdef GL_ES
precision mediump float;
#endif
uniform vec2 u_resolution;
void main() {
vec2 st = gl_FragCoord.xy/u_resolution.xy;
gl_FragColor = vec4(st.x,0.0,1.0,1.0); // R,G,B,A
}