I've been playing around with shaders on sprites for a day now and here's two examples I came up with for those who are interested.
main.agc file
// AGK v2 Shader Test
// AGK Version: AGK2 alpha 1
// Created January 2014 by Ranietz
// display setup
setVirtualResolution(1024,768)
setSyncRate(60,0)
setPrintSize(16)
// create a test sprite
testSprite = createSprite( loadImage( "sprite.png" ) )
setSpriteOffset( testSprite, getSpriteWidth( testSprite )/2, getSpriteHeight( testSprite )/2)
setSpritePositionByOffset( testSprite,1024/2,768/2)
setSpriteTransparency( testSprite, 1 )
// load sprite shaders
loadShader( 1, "Sprite.vs", "pixelate.ps" )
loadShader( 2, "Sprite.vs", "grayscale.ps" )
// apply a shader to the test sprite
setSpriteShader( testSprite, 1 )
// setup varaibles
modSpeed# = 0.25 // speed of the modulation (the time it takes to go from 0.0 to 1.0 (in seconds))
modDirection = 0 // the direction of the modulation (0 = increase, 1 = decrease)
modulation# = 0.0 // modulation variable
shaderNr = 1 // what shader to use
// main loop
do
// store the time of the last frame (in seconds)
ft# = getFrameTime()
// make the variable "modulation" modulate between 0.0 and 1.0
if modDirection = 0
modulation# = modulation# + modSpeed# * ft#
if modulation# > 1.0
modulation# = 1.0
modDirection = 1
endif
else
modulation# = modulation# - modSpeed# * ft#
if modulation# < 0.0
modulation# = 0.0
modDirection = 0
endif
endif
// change shader if spacekey is pressed
if getRawKeyPressed(32)=1
if shaderNr = 1
shaderNr = 2
setSpriteShader( testSprite, 2 )
modulation# = 0.0
else
shaderNr = 1
setSpriteShader( testSprite, 1 )
modulation# = 0.0
endif
endif
// send the variable "modulation#" to the shader
select shaderNr
case 1
// the sprite will be blank if modulation = 0.0, so make sure it's not 0.0
if modulation# = 0.0 then modulation# = 0.00001
// send the modulation variable to the shader
setShaderConstantByName( 1, "agk_input", modulation#, 0.0, 0.0, 0.0 )
endcase
case 2
// no modulation for the grayscale shader yet...
// setShaderConstantByName( 2, "agk_input", modulation#, 0.0, 0.0, 0.0 )
endcase
endselect
// print stuff
printc("FPS: ")
print(screenFPS())
print("Press Spacekey To Change Shader")
if shaderNr = 1 then print("Current Shader - Pixelate")
if shaderNr = 2 then print("Current Shader - Grayscale")
// update screen
sync()
loop
sprite.vs (the same as Paul wrote earlier in this thread)
attribute vec4 position;
attribute vec4 color;
attribute vec2 uv;
varying vec2 uvVarying;
varying vec4 colorVarying;
varying vec2 posVarying;
uniform mat4 agk_Ortho;
void main() {
gl_Position = agk_Ortho * position;
posVarying = position.xy;
uvVarying = uv;
colorVarying = color;
}
pixelate.ps
uniform sampler2D texture0;
varying vec2 uvVarying;
uniform vec2 agk_spritesize;
uniform float agk_input;
vec2 max_pixelation = vec2(agk_spritesize.x/10.0,agk_spritesize.y/10.0);
void main()
{
float dx = max_pixelation.x*agk_input *(1.0/agk_spritesize.x);
float dy = max_pixelation.y*agk_input *(1.0/agk_spritesize.y);
vec2 coord = vec2(dx*floor(uvVarying.x/dx),dy*floor(uvVarying.y/dy));
gl_FragColor = texture2D(texture0, coord);
}
grayscale.ps
uniform sampler2D texture0;
varying vec2 uvVarying;
void main()
{
vec4 color = texture2D(texture0, uvVarying.st);
float gray = dot(color.rgb, vec3(0.299, 0.587, 0.114));
gl_FragColor = vec4( gray, gray, gray, color.a);
}