Excellent Shaders
Jack
And I believe
baxslash 2d for the additive code
I modified the Lensflare and snow shaders by
jack and added an additive blend of the background image rather than just use the noise this created
a background that could have a sun scroll down in the background adding a colour blend to the background sprite. Great for what I wanted a sunset
in 2d without the use of a quad image. I didn't change all that much to baxslashes awesome lensflare but now it does what I wanted and I have
commented the changes.
The summer.png that I use can be any 2d background image
The following allows you to switch between sun(lensflare) or snow
// Project: AGK_Lensflare_Shader
// Created: 05.04.2017
// show all errors
SetErrorMode(2)
#constant xMax=1024
#constant yMax=600
//#constant snowImg=1002:rem The snow image used with the snowShader sprite
//#constant snowSpr=1002:rem The sprite used for the snow snowShader image
#constant seasonBg=999:rem the season background sprite
#constant seasonAlpha=200:rem Sets the alpha level for the season image
#constant spring=991:rem Spring image
#constant snowShader=1:rem The sprite snowShader used for snow
#constant sunLense=2:rem The sprite snowShader used for sun
// set window properties
SetWindowTitle( "AGK_Lensflare_Shader" )
SetWindowSize( xMax, yMax, 0 )
// set display properties
SetVirtualResolution( xMax, yMax )
SetOrientationAllowed( 1, 1, 1, 1 )
UseNewDefaultFonts( 1 ) // since version 2.0.22 we can use nicer default fonts
SetVSync(1)
//the snow to be used for winter
CreateSnowShader()
LoadSpriteShader(snowShader,"snow.ps")
noise = LoadImage("Summer.png"):rem changed the noise to my background image
SetShaderConstantByName(snowShader,"Settings",50.0,0.1,0.8,.5) // LAYERS, DEPTH, WIDTH, SPEED
//CreateImageColor(snowImg,255,255,255,255)
CreateSprite(seasonBg,noise)
SetSpriteShader(seasonBg,snowShader)
setspritesize(seasonBg,xMax,yMax)
//summer
CreateLenseFlare()
LoadSpriteShader(sunLense,"lensflare.ps")
//SetSpriteShader(seasonBg,sunLense)
SetShaderConstantByName(sunLense,"Position",1024.0,600.0,0.0,0.0)
ResetTimer()
do
print(" Season shader Demo press 1 for sun 2 for snow")
X# = sin(timer())*xMax
Y# = sin(timer())*yMax
if GetRawKeyPressed(49)=1 then SetSpriteShader(seasonBg,sunLense)
if GetRawKeyPressed(50)=1 then SetSpriteShader(seasonBg,snowShader)
SetShaderConstantByName(sunLense,"Position",X#,Y#,0.0,0.0)
Print( "FPS: "+str(ScreenFPS())+" Lensflare Position X: "+str(X#)+" Y: "+str(Y#) )
Sync()
loop
function CreateSnowShader()
file = OpenToWrite("snow.ps")
WriteLine(file,"varying mediump vec2 uvVarying;")
WriteLine(file,"uniform float agk_time;")
WriteLine(file,"uniform vec4 Settings;")
WriteLine(file,"uniform sampler2D texture0;") //Added this for colouring
WriteLine(file,"varying vec4 colorVarying;//") //Added this colour varying variable so as it will blend with background
WriteLine(file,"void main( )")
WriteLine(file,"{")
WriteLine(file,"const mat3 p = mat3(13.323122,23.5112,21.71123,21.1212,28.7312,11.9312,21.8112,14.7212,61.3934);")
WriteLine(file,"vec2 uv = uvVarying;")
WriteLine(file,"vec3 acc = vec3(0.0);")
WriteLine(file,"float dof = 5.*sin(agk_time*.1);")
WriteLine(file,"for (int i=0;i<int(Settings.r);i++) {")
WriteLine(file,"float fi = float(i);")
WriteLine(file,"vec2 q = uv*(1.+fi*Settings.g);")
WriteLine(file,"q += vec2(q.y*(Settings.b*mod(fi*7.238917,1.)-Settings.b*.5),Settings.a*-agk_time/(1.+fi*Settings.g*.03));")
WriteLine(file,"vec3 n = vec3(floor(q),31.189+fi);")
WriteLine(file,"vec3 m = floor(n)*.00001 + fract(n);")
WriteLine(file,"vec3 mp = (31415.9+m)/fract(p*m);")
WriteLine(file,"vec3 r = fract(mp);")
WriteLine(file,"vec2 s = abs(mod(q,1.)-.5+.9*r.xy-.45);")
WriteLine(file,"s += .01*abs(2.*fract(10.*q.yx)-1.);")
WriteLine(file,"float d = .6*max(s.x-s.y,s.x+s.y)+max(s.x,s.y)-.01;")
WriteLine(file,"float edge = .005+.05*min(.5*abs(fi-5.-dof),1.);")
WriteLine(file,"acc += vec3(smoothstep(edge,-edge,d)*(r.x/(.25+.02*fi*Settings.g)));")
WriteLine(file,"}")
WriteLine(file,"gl_FragColor = vec4(vec3(acc),1.0);")
//added an additive colour blend
WriteLine(file,"gl_FragColor = texture2D(texture0, uvVarying) + gl_FragColor;")
//change for a subtractive colour blend
//WriteLine(file,"gl_FragColor = texture2D(texture0, uvVarying) - gl_FragColor;")
//change for a multiply color blend
//WriteLine(file,"gl_FragColor = texture2D(texture0, uvVarying) * gl_FragColor;")
WriteLine(file,"}")
CloseFile(file)
endfunction
function CreateLenseFlare()
file = OpenToWrite("lensflare.ps")
WriteLine(file,"uniform float agk_time;")
WriteLine(file,"uniform sampler2D texture0;") //added this for colouring
WriteLine(file,"varying mediump vec2 uvVarying;")
WriteLine(file,"uniform vec2 agk_resolution;")
WriteLine(file,"uniform mediump vec2 Position;")
WriteLine(file,"varying vec4 colorVarying;//") //Added this colour varying variable so as it will blend with background
WriteLine(file,"float noise(float t)")
WriteLine(file,"{")
WriteLine(file,"return texture2D(texture0,vec2(t, 0.0) / agk_resolution.xy).x;")
WriteLine(file,"}")
WriteLine(file,"float noise(vec2 t)")
WriteLine(file,"{")
WriteLine(file,"return texture2D(texture0,(t + vec2(agk_time)) / agk_resolution.xy).x;")
WriteLine(file,"}")
WriteLine(file,"vec3 lensflare(vec2 uv,vec2 pos)")
WriteLine(file,"{")
WriteLine(file,"vec2 main = uv-pos;")
WriteLine(file,"vec2 uvd = uv*(length(uv));")
WriteLine(file,"float ang = atan(main.y, main.x);")
WriteLine(file,"float dist=length(main); dist = pow(dist,.1);")
WriteLine(file,"float n = noise(vec2((ang-agk_time/9.0)*16.0,dist*32.0));")
WriteLine(file,"float f0 = 1.0/(length(uv-pos)*16.0+1.0);")
WriteLine(file,"f0 = f0+f0*(sin((ang+agk_time/18.0 + noise(abs(ang)+n/2.0)*2.0)*12.0)*.1+dist*.1+.8);")
WriteLine(file,"float f2 = max(1.0/(1.0+32.0*pow(length(uvd+0.8*pos),2.0)),.0)*00.25;")
WriteLine(file,"float f22 = max(1.0/(1.0+32.0*pow(length(uvd+0.85*pos),2.0)),.0)*00.23;")
WriteLine(file,"float f23 = max(1.0/(1.0+32.0*pow(length(uvd+0.9*pos),2.0)),.0)*00.21;")
WriteLine(file,"vec2 uvx = mix(uv,uvd,-0.5);")
WriteLine(file,"float f4 = max(0.01-pow(length(uvx+0.4*pos),2.4),.0)*6.0;")
WriteLine(file,"float f42 = max(0.01-pow(length(uvx+0.45*pos),2.4),.0)*5.0;")
WriteLine(file,"float f43 = max(0.01-pow(length(uvx+0.5*pos),2.4),.0)*3.0;")
WriteLine(file,"uvx = mix(uv,uvd,-.2);")
WriteLine(file,"float f5 = max(0.01-pow(length(uvx+0.2*pos),5.5),.0)*2.0;")
WriteLine(file,"float f52 = max(0.01-pow(length(uvx+0.4*pos),5.5),.0)*2.0;")
WriteLine(file,"float f53 = max(0.01-pow(length(uvx+0.6*pos),5.5),.0)*2.0;")
WriteLine(file,"uvx = mix(uv,uvd,-0.5);")
WriteLine(file,"float f6 = max(0.01-pow(length(uvx-0.3*pos),1.6),.0)*6.0;")
WriteLine(file,"float f62 = max(0.01-pow(length(uvx-0.325*pos),1.6),.0)*3.0;")
WriteLine(file,"float f63 = max(0.01-pow(length(uvx-0.35*pos),1.6),.0)*5.0;")
WriteLine(file,"vec3 c = vec3(.0);")
WriteLine(file,"c.r+=f2+f4+f5+f6; c.g+=f22+f42+f52+f62; c.b+=f23+f43+f53+f63;")
WriteLine(file,"c+=vec3(f0);")
WriteLine(file,"return c;")
WriteLine(file,"}")
WriteLine(file,"vec3 cc(vec3 color, float factor,float factor2) // color modifier")
WriteLine(file,"{")
WriteLine(file,"float w = color.x+color.y+color.z;")
WriteLine(file,"return mix(color,vec3(w)*factor,w*factor2);")
WriteLine(file,"}")
WriteLine(file,"void main( )")
WriteLine(file,"{")
WriteLine(file,"vec2 uv = uvVarying*2.0;")
WriteLine(file,"uv.x = uv.x - 1.0;")
WriteLine(file,"uv.y = uv.y - 1.0;")
WriteLine(file,"vec3 color = vec3(1.4,1.2,1.0)*lensflare(uv,(vec2((Position.xy/agk_resolution)*2.0)-1.0));")
WriteLine(file,"color = cc(color,.5,.1);")
WriteLine(file,"gl_FragColor = vec4(color,2.0);")
//added an additive colour blend
WriteLine(file,"gl_FragColor = texture2D(texture0, uvVarying) + gl_FragColor;")
//change for a subtractive colour blend
//WriteLine(file,"gl_FragColor = texture2D(texture0, uvVarying) - gl_FragColor;")
//change for a multiply color blend
//WriteLine(file,"gl_FragColor = texture2D(texture0, uvVarying) * gl_FragColor;")
WriteLine(file,"}")
CloseFile(file)
endfunction
fubar