New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
createShader() assumes _renderer causing bug when used with p5.Graphics #5099
Comments
Might have made this issue prematurely. Got some input from Naoto over on the processing forum and have to see if my assessment of the problem was correct. Thread on discourse: https://discourse.processing.org/t/createshader-not-working-on-p5-graphics/28617 |
Hi @duskvirkus ! Thanks for the detailed writeup. If I'm understanding the problem correctly, I think you just need to call createShader as a member function of your graphics object. In your example that would be tex = createGraphics(width, height, WEBGL);
sh = tex.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
}
`
);
|
@aferriss great solution! I didn't think of doing that. Might be worth adding an example to Closing this issue as it seems like my methodology was wrong and I think I better understand how things are working behind the scenes. |
Just jumping on in this closed issue to mention that I also stumbled on this and find this a little inconsistent. See also related -- As a relative p5js beginner, it's not immediately clear what the difference between createCanvas and createGraphics is, or how you can get a "graphics" object from a canvas allocated to webgl -- my intuition is that if I have a shader working with createCanvas(...., WEBGL) and loadShader -- it should also just work with createShader. Or if not, there should be a clear / easy way to understand how to get it to work. As is, it fails silently and is a little hard to debug. In my case, I was trying to switch in the same code from preloading a shader to loading from a string (for example, in case you wanted to process the shader, etc). Some more concrete examples about how you might switch these up and/or making it more unified I think will help. |
Most appropriate sub-area of p5.js?
Details about the bug:
Sketches That Demonstrate the Problem
bug when using p5.Graphics with
createShader()
: https://editor.p5js.org/duskvirkus/sketches/dJlFCUjEicomparison with
loadShader()
: https://editor.p5js.org/duskvirkus/sketches/LiqLNendrwork around: https://editor.p5js.org/duskvirkus/sketches/6EsuQi93w
(see below for full sketch code)
Explanation of the Problem
createShader()
creates a new shader withthis._renderer
see:p5.js/src/webgl/material.js
Line 176 in 9d12b8c
Compared to
loadShader()
which doesn't specify a_renderer
for the shader see:p5.js/src/webgl/material.js
Line 71 in 9d12b8c
shader()
function is called it will usethis._renderer
ifs._renderer
is undefined see:p5.js/src/webgl/material.js
Line 255 in 9d12b8c
The work around I came up with was including
<shader-variable>._renderer = undefined
when using a shader that was created withcreateShader()
.Suggested Solution
I'd like to suggest removing the
_renderer
property because it's not defined when someone loads a shader. And it causes problems when working with p5.Graphics objects. This problem would probably also occur if you were trying to use a shader on two (or more) p5.Graphics objects in the same sketch. So I think it would make more sense to just use whateverthis._renderer
is whenshader()
is called by default. Does someone have a better suggestion? Would this break other things? I could work on a pull request if not.It also looks like it might worth reworking shader creation so it's more consistent and has more error message. (Something I'd also be interested in working on.)
Full Sketch Code
Be sure to run with 1.3.0 because there's another bug in 1.2.0 that will cause them to break.
bug when using p5.Graphics with
createShader()
:comparison with
loadShader()
:work around:
The text was updated successfully, but these errors were encountered: