Hi I would appreciate help implementing this feature for an art project I’m working on. Functionally it would be similar to a p5.Image filter() but using my own fragment shader instead of the built in filters.
So what I have is a “sprite sheet” image, which is multiple images packed into a larger image.
I want to draw a subsection of the sprite sheet image and run it through a fragment shader so I can modify the colors on the fly. That means I need to set the texture uv components.
shader.vert:
attribute vec3 aPosition;
attribute vec2 aTexCoord;
varying vec2 vTexCoord;
void main() {
vTexCoord = aTexCoord;
vec4 positionVec4 = vec4(aPosition, 1.0);
gl_Position = positionVec4;
}
shader.frag
precision mediump float;
// ranges from 0..1
varying vec2 vTexCoord;
uniform sampler2D texture;
void main() {
vec2 uv = vTexCoord;
uv = 1.0 - uv;
vec4 tex = texture2D(texture, uv);
// just invert the texture color to demonstrate the fragement shader is running
gl_FragColor = vec4(1.0-tex.rgb, tex.a);
}
implementation 1-- the shader is ignored
p5.shader(shader);
p5.image(image, x+ss.x, y+ss.y, frame.w, frame.h, xOffset, yOffset, frame.w, frame.h);
ss and frame and xOffset(s) are values that come from the image location on the larger sprite sheet image.
implementation 2-- shader is ignored.
p5.shader(shader);
p5.beginShape();
p5.texture(image);
p5.vertex(x+ss.x, y+ss.y, xOffset, yOffset);
p5.vertex(x+ss.x + frame.w, y+ss.y, xOffset+frame.w, yOffset);
p5.vertex(x+ss.x + frame.w, y+ss.y+frame.h, xOffset+frame.w, yOffset+frame.h);
p5.vertex(x+ss.x, y+ss.y+frame.h, xOffset, yOffset+frame.h);
p5.endShape();
I have tried other more in depth implementations but none work very well. Any help would be appreciated!