While trying to get the shockwave shader to go fullscreen, I used the scanline shader (community shader thread page 2) as an example. Then I figured it could be useful for me as a sprite shader so I tried to port the other way too from fullscreen->sprite shader.
This is what I got:
main.agc:
// Project: scanline
// Created: 2016-10-04
// show all errors
SetErrorMode(2)
// set display properties
SetWindowTitle( "scanline" )
SetWindowSize( 1024, 768, 0 )
SetVirtualResolution( 1024, 768 )
SetSyncRate( 60, 0 )
SetPrintSize(16)
// load media
img = LoadImage("spr_shield_preview.png")
//img = LoadImage("noscanline.png")
scanline_offset = 0
sid = CreateSprite(img)
//406x558 is the size of the "noscanline.png" image from baxslash's post
//SetSpriteSize(sid, 406, 558)
sid2 = CreateSprite(img)
//SetSpriteSize(sid2, 406, 558)
SetSpriteX(sid2, 406)
shaderid = LoadSpriteShader("scanline.ps" )
SetSpriteShader(sid,shaderid)
SetShaderConstantByName(shaderid, "screenSize", GetSpriteWidth(sid), GetSpriteHeight(sid), 0, 0)
//SetShaderConstantByName(shaderid, "screenSize", 406, 558, 0, 0)
// set 1 to 0 to remove noise
SetShaderConstantByName(shaderid, "noise", 1, 0, 0, 0)
#constant SHADERDELAY = 2
delay = SHADERDELAY
do
if delay = 0
scanline_offset = scanline_offset + 1
delay = SHADERDELAY
else
delay = delay -1
endif
if scanline_offset > 400
scanline_offset = 0
endif
SetShaderConstantByName(shaderid, "offset", scanline_offset, 0, 0, 0)
sync()
loop
scanline.ps:
#ifdef GL_ES
#ifdef GL_FRAGMENT_PRECISION_HIGH
precision highp float;
#else
precision mediump float;
#endif
#endif
uniform sampler2D texture0;
varying vec2 uvVarying;
uniform vec2 screenSize;
uniform float noise;
uniform float offset;
float rand(vec2 n)
{
return 0.5 + 0.5 *
fract(sin(dot(n.xy, vec2(12.9898, 78.233)))* 43758.5453);
}
void main()
{
float y = floor(uvVarying.y * screenSize.y);
float ran = (rand(uvVarying.xy) - 1.0) * 0.5;
if (noise < 0.1)
{
ran = 0.0;
}
float delta = mod(y+offset, 4.0) * 0.5;
gl_FragColor = texture2D(texture0, uvVarying.xy) * (delta + ran);
}
So it seems to work fine as a sprite shader. But a few questions.
Notice how I tried to change the scanline to move across the image in the y-direction. This is the "offset" variable I added into the scanline pixel shader. I am shader n00b forever, so probably all wrong. Anyway, I feel like I got a bit of an odd result.
If you use the "noscanline.png" image (just download it from baxslash's shader post), it seems to "flicker". That is, the size or lines seem to go up and down. Different flicker than what I got with multiple ripples in shockwave. This flicker here is actually quite nice, and I would like to have a version that does this reliably as another version.
But if I use the "spr_shield_preview.png" image from https://opengameart.org/content/shield-effect, the scanlines seem to be moving up as I was trying to do. Is it just some property of the images or what is the cause?
Also, I am not entirely clear as to what the last line of the scanline shader does:
gl_FragColor = texture2D(texture0, uvVarying.xy) * (delta + ran);
When a line is changed to "black" (creates the scanline) is it also changing the alpha to 0 so that it is actually transparent? I tried to put the shield with the shader applied on it on top of something else and it seems to be transparent, but it would not be the first time for me to try it wrong