Scanlines Shader
SetErrorMode(2)
// set window properties
SetWindowTitle( "Scanlines" )
SetWindowSize( 1024, 768, 0 )
SetWindowAllowResize( 1 ) // allow the user to resize the window
// set display properties
SetVirtualResolution( 1024, 768 ) // doesn't have to match the window
SetOrientationAllowed( 1, 1, 1, 1 ) // allow both portrait and landscape on mobile devices
SetSyncRate( 30, 0 ) // 30fps instead of 60 to save battery
SetScissor( 0,0,0,0 ) // use the maximum available screen space, no black borders
UseNewDefaultFonts( 1 ) // since version 2.0.22 we can use nicer default fonts
createVSShaderFile()
createPSshaderFile()
chunk = CreateObjectPlane(1000,1000)
SetObjectColor(chunk,255,255,255,255)
Shader=LoadShader("scanlines.vs","scanlines.ps")
SetObjectShader(chunk,Shader)
SetShaderConstantByName( Shader,"iResolution",1024,768,0,0 )
do
//SetShaderConstantByName( Shader,"iResolution",Timer()*100,Timer()*100,0,0 )
SetShaderConstantByName( Shader,"time",Timer()*.5,0,0,0 )
Print( ScreenFPS() )
Sync()
loop
// This is the function which creates the two files VS and PS
// These just simply output lines of code to the two files
function createVSShaderFile()
fw = OpenToWrite("scanlines.vs")
WriteLine(fw,"attribute highp vec3 position;")
WriteLine(fw,"attribute mediump vec3 normal;")
WriteLine(fw,"varying highp vec3 posVarying;")
WriteLine(fw,"varying mediump vec3 normalVarying;")
WriteLine(fw,"varying mediump vec3 lightVarying;")
WriteLine(fw,"mediump vec3 GetVSLighting( mediump vec3 normal, highp vec3 pos );")
WriteLine(fw,"uniform highp mat3 agk_WorldNormal;")
WriteLine(fw,"uniform highp mat4 agk_World;")
WriteLine(fw,"uniform highp mat4 agk_ViewProj;")
WriteLine(fw,"attribute highp vec2 uv;")
WriteLine(fw,"varying highp vec2 uvVarying;")
WriteLine(fw,"uniform highp vec4 uvBounds0;")
WriteLine(fw,"void main()")
WriteLine(fw,"{ ")
WriteLine(fw,"uvVarying = uv * uvBounds0.xy + uvBounds0.zw;")
WriteLine(fw,"highp vec4 pos = agk_World * vec4(position,1.0);")
WriteLine(fw,"mediump vec3 norm = normalize(agk_WorldNormal * normal);")
WriteLine(fw,"gl_Position = agk_ViewProj * pos;")
WriteLine(fw,"posVarying = pos.xyz;")
WriteLine(fw,"normalVarying = norm;")
WriteLine(fw,"lightVarying = GetVSLighting( norm, posVarying );")
WriteLine(fw,"}")
CloseFile(fw)
endfunction
function createPSshaderFile()
fw=OpenToWrite("scanlines.ps")
WriteLine(fw,"#ifdef GL_ES")
WriteLine(fw,"precision mediump float;")
WriteLine(fw,"#endif")
WriteLine(fw,"uniform vec2 iResolution;")
WriteLine(fw,"uniform float time;")
WriteLine(fw,"#define num 60.0")
WriteLine(fw,"float random(vec2 p){")
WriteLine(fw," return fract(sin(dot(p, vec2(12.9898,78.233))) * 43758.5453+time*4.0); ")
WriteLine(fw,"}")
WriteLine(fw,"void main() {")
WriteLine(fw," vec2 p = ( gl_FragCoord.xy / (iResolution.xy) );")
WriteLine(fw," //- vec2(0.5, 0.5);")
WriteLine(fw," //vec2 p = (gl_FragCoord.xy * 2.0 - iResolution.xy) / min(iResolution.x, iResolution.y) - vec2(.5 , .5);")
WriteLine(fw," float top, bottom, linewidth;")
WriteLine(fw," float offSet = 0.5;")
WriteLine(fw," float span = 0.01;")
WriteLine(fw," linewidth = 0.01;")
WriteLine(fw," top = fract(time);")
WriteLine(fw," bottom = fract(time) + linewidth;")
WriteLine(fw," vec4 border = vec4(0.0, 0.0, 0.0, 1.0);")
WriteLine(fw," for(float i = 1.0; i < num; i++){")
WriteLine(fw," top += i * span; ")
WriteLine(fw," top = fract(top);")
WriteLine(fw," bottom += i *span;")
WriteLine(fw," bottom = fract(bottom);")
WriteLine(fw," if(p.y > top && p.y < bottom){")
WriteLine(fw," border = vec4(abs(1.0 * sin(time)), 0.0, 0.0, 1.0);")
WriteLine(fw," }")
WriteLine(fw," }")
WriteLine(fw," float c = random(p);")
WriteLine(fw," vec3 col = mix(vec3(c, c, c), border.xyz, 0.6);")
WriteLine(fw," //float r = mix(c, border.x, 0.6);")
WriteLine(fw," //vec4 col= vec4(mix(vec3(p, p, p), border.xyz), 1.0);")
WriteLine(fw," gl_FragColor = vec4(col, 1.0);")
WriteLine(fw," /*")
WriteLine(fw," if(p.y < -0.5){")
WriteLine(fw," gl_FragColor = vec4(1.0, 0.0, 0.0, 1.0);")
WriteLine(fw," */")
WriteLine(fw," }")
CloseFile(fw)
endfunction
That's an example of a scanline shader placed on a plane you could just use the ps shader if you only want to use on 2D
Edit here is an example applying to a textured 3d object