@
CJB how did you go with your cave flying shooter?
As your project was circuit I thought perhaps you may be interested in using a circuit shader
currently Three 2D shaders but is easy enough to make either one a 3D shader
SetErrorMode(2)
#constant KEY_SPACE = 32 //the space bar
// set window properties
SetWindowTitle( "circuit" )
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
createShaderFile()
CreateSprite(1,0)
CreateSprite(2,0)
CreateSprite(3,0)
NoiseInit()
SetSpriteImage (3,createRandomBgImage())
SetSpriteAdditionalImage (3,generateperlinimage(),1)
SetSpriteSize(1,1024,768)
SetSpriteSize(2,1024,768)
SetSpriteSize(3,1024,768)
LoadSpriteShader(1,"circuit.ps")
LoadSpriteShader(2,"circuit2.ps")
LoadSpriteShader(3,"circuit3.ps")
SetSpriteShader( 1,1 )
SetSpriteShader( 2,2 )
SetSpriteShader( 3,3 )
SetShaderConstantByName(1,"resolution",1024,768,0,0)
SetShaderConstantByName(2,"resolution",1024,768,0,0)
SetShaderConstantByName(3,"iResolution",1024,768,0,0)
num=1
SetSpriteDepth(num,0)
do
print("press space to switch shaders")
If GetRawKeyPressed(KEY_SPACE)
if num = 1
num=2:SetSpriteDepth(1,10):SetSpriteDepth(2,0):SetSpriteDepth(3,10)
elseif num =2
num=3:SetSpriteDepth(1,10):SetSpriteDepth(2,10):SetSpriteDepth(3,0)
else
num=1:SetSpriteDepth(1,0):SetSpriteDepth(2,10):SetSpriteDepth(3,10)
endif
endif
SetShaderConstantByName(num,"time",timer(),0,0,0)
print(num)
sync()
loop
function createShaderFile()
fw=OpenToWrite("circuit.ps")
WriteLine(fw,"#ifdef GL_ES")
WriteLine(fw,"precision mediump float;")
WriteLine(fw,"#endif")
WriteLine(fw,"uniform float time;")
WriteLine(fw,"uniform vec2 resolution;")
WriteLine(fw,"float iTime = 0.0;")
WriteLine(fw,"vec3 iResolution = vec3(0.0);")
WriteLine(fw,"#define texture(s, uv) vec4(0.0)")
WriteLine(fw,"#define textureLod(s, uv, lod) vec4(0.0)")
WriteLine(fw,"#define time iTime*.02")
WriteLine(fw,"#define width .005")
WriteLine(fw,"float zoom = .18;")
WriteLine(fw,"float shape=0.;")
WriteLine(fw,"vec3 color=vec3(0.),randcol;")
WriteLine(fw,"void formula(vec2 z, float c) {")
WriteLine(fw," float minit=0.;")
WriteLine(fw," float o,ot2,ot=ot2=1000.;")
WriteLine(fw," for (int i=0; i<9; i++) {")
WriteLine(fw," z=abs(z)/clamp(dot(z,z),.1,.5)-c;")
WriteLine(fw," float l=length(z);")
WriteLine(fw," o=min(max(abs(min(z.x,z.y)),-l+.25),abs(l-.25));")
WriteLine(fw," ot=min(ot,o);")
WriteLine(fw," ot2=min(l*.1,ot2);")
WriteLine(fw," minit=max(minit,float(i)*(1.-abs(sign(ot-o))));")
WriteLine(fw," }")
WriteLine(fw," minit+=1.;")
WriteLine(fw," float w=width*minit*2.;")
WriteLine(fw," float circ=pow(max(0.,w-ot2)/w,6.);")
WriteLine(fw," shape+=max(pow(max(0.,w-ot)/w,.25),circ);")
WriteLine(fw," vec3 col=normalize(.1+texture(iChannel1,vec2(minit*.1)).rgb);")
WriteLine(fw," color+=col*(.4+mod(minit/9.-time*10.+ot2*2.,1.)*1.6);")
WriteLine(fw," color+=vec3(1.,.7,.3)*circ*(10.-minit)*3.*smoothstep(0.,.5,.15+texture(iChannel0,vec2(.0,1.)).x-.5);")
WriteLine(fw,"}")
WriteLine(fw,"void mainImage( out vec4 fragColor, in vec2 fragCoord )")
WriteLine(fw,"{")
WriteLine(fw," vec2 pos = fragCoord.xy / iResolution.xy - .5;")
WriteLine(fw," pos.x*=iResolution.x/iResolution.y;")
WriteLine(fw," vec2 uv=pos;")
WriteLine(fw," float sph = length(uv); sph = sqrt(1. - sph*sph); // curve for spheric distortion")
WriteLine(fw," uv=normalize(vec3(uv,sph)).xy;")
WriteLine(fw," float a=.5+mod(.5,1.)*.5;")
WriteLine(fw," vec2 luv=uv;")
WriteLine(fw," float b=a*5.48535;")
WriteLine(fw,"// zoom*=1.+sin(time*3.758123)*.8;")
WriteLine(fw," uv*=mat2(cos(b),sin(b),-sin(b),cos(b));")
WriteLine(fw," uv+=vec2(sin(a),cos(a*.5))*8.;")
WriteLine(fw," uv*=zoom;")
WriteLine(fw," float pix=.5/iResolution.x*zoom/sph;")
WriteLine(fw," float dof=max(1.,(10.-mod(time,1.)/.01));")
WriteLine(fw," float c=1.5+mod(floor(time),6.)*.125;")
WriteLine(fw," for (int aa=0; aa<36; aa++) {")
WriteLine(fw," vec2 aauv=floor(vec2(float(aa)/6.,mod(float(aa),6.)));")
WriteLine(fw," formula(uv+aauv*pix*dof,c);")
WriteLine(fw," }")
WriteLine(fw," shape/=36.; color/=36.;")
WriteLine(fw," vec3 colo=mix(vec3(.15),color,shape)*(1.-length(pos))*min(1.,abs(.5-mod(time+.5,1.))*10.); ")
WriteLine(fw," colo*=vec3(1.2,1.1,1.0);")
WriteLine(fw," fragColor = vec4(colo,1.0);")
WriteLine(fw,"}")
WriteLine(fw,"#undef time")
WriteLine(fw,"void main(void)")
WriteLine(fw,"{")
WriteLine(fw," iTime = time;")
WriteLine(fw," iResolution = vec3(resolution, 0.0);")
WriteLine(fw," mainImage(gl_FragColor, gl_FragCoord.xy);")
WriteLine(fw,"}")
CloseFile(fw)
fw=OpenToWrite("circuit2.ps")
WriteLine(fw,"#ifdef GL_ES")
WriteLine(fw,"precision mediump float;")
WriteLine(fw,"#endif")
WriteLine(fw,"uniform float time;")
WriteLine(fw,"uniform vec2 resolution;")
WriteLine(fw,"float iTime = 0.0;")
WriteLine(fw,"vec3 iResolution = vec3(0.0);")
WriteLine(fw,"#define texture(s, uv) vec4(0.0)")
WriteLine(fw,"#define textureLod(s, uv, lod) vec4(0.0)")
WriteLine(fw,"#define time iTime*.009")
WriteLine(fw,"#define width .005")
WriteLine(fw,"float zoom = .18;")
WriteLine(fw,"float shape=0.;")
WriteLine(fw,"vec3 color=vec3(0.),randcol;")
WriteLine(fw,"void formula(vec2 z, float c) {")
WriteLine(fw," float minit=0.;")
WriteLine(fw," float o,ot2,ot=ot2=1000.;")
WriteLine(fw," for (int i=0; i<9; i++) {")
WriteLine(fw," z=abs(z)/clamp(dot(z,z),.1,.5)-c;")
WriteLine(fw," float l=length(z);")
WriteLine(fw," o=min(max(abs(min(z.x,z.y)),-l+.25),abs(l-.25));")
WriteLine(fw," ot=min(ot,o);")
WriteLine(fw," ot2=min(l*.1,ot2);")
WriteLine(fw," minit=max(minit,float(i)*(1.-abs(sign(ot-o))));")
WriteLine(fw," }")
WriteLine(fw," minit+=1.;")
WriteLine(fw," float w=width*minit*2.;")
WriteLine(fw," float circ=pow(max(0.,w-ot2)/w,6.);")
WriteLine(fw," shape+=max(pow(max(0.,w-ot)/w,.25),circ);")
WriteLine(fw," vec3 col=normalize(.1+texture(iChannel1,vec2(minit*.1)).rgb);")
WriteLine(fw," color+=col*(.4+mod(minit/9.-time*10.+ot2*2.,1.)*1.6);")
WriteLine(fw," color+=vec3(1.,.7,.3)*circ*(10.-minit)*3.*smoothstep(0.,.5,.15+texture(iChannel0,vec2(.0,1.)).x-.5);")
WriteLine(fw,"}")
WriteLine(fw,"void mainImage( out vec4 fragColor, in vec2 fragCoord )")
WriteLine(fw,"{")
WriteLine(fw," vec2 pos = fragCoord.xy / iResolution.xy - .5;")
WriteLine(fw," pos.x*=iResolution.x/iResolution.y;")
WriteLine(fw," vec2 uv=pos;")
WriteLine(fw," float sph = length(uv); sph = sqrt(1. - sph*sph)*1.5; // curve for spheric distortion")
WriteLine(fw," uv=normalize(vec3(uv,sph)).xy;")
WriteLine(fw," float a=time+mod(time,1.)*.5;")
WriteLine(fw," vec2 luv=uv;")
WriteLine(fw," float b=a*5.48535;")
WriteLine(fw,"// zoom*=1.+sin(time*3.758123)*.8;")
WriteLine(fw," uv*=mat2(cos(b),sin(b),-sin(b),cos(b));")
WriteLine(fw," uv+=vec2(sin(a),cos(a*.5))*8.;")
WriteLine(fw," uv*=zoom;")
WriteLine(fw," float pix=.5/iResolution.x*zoom/sph;")
WriteLine(fw," float dof=max(1.,(10.-mod(time,1.)/.01));")
WriteLine(fw," float c=1.5+mod(floor(time),6.)*.125;")
WriteLine(fw," for (int aa=0; aa<36; aa++) {")
WriteLine(fw," vec2 aauv=floor(vec2(float(aa)/6.,mod(float(aa),6.)));")
WriteLine(fw," formula(uv+aauv*pix*dof,c);")
WriteLine(fw," }")
WriteLine(fw," shape/=36.; color/=36.;")
WriteLine(fw," vec3 colo=mix(vec3(.15),color,shape)*(1.-length(pos))*min(1.,abs(.5-mod(time+.5,1.))*10.); ")
WriteLine(fw," colo*=vec3(1.2,1.1,1.0);")
WriteLine(fw," fragColor = vec4(colo,1.0);")
WriteLine(fw,"}")
WriteLine(fw,"#undef time")
WriteLine(fw,"void main(void)")
WriteLine(fw,"{")
WriteLine(fw," iTime = time;")
WriteLine(fw," iResolution = vec3(resolution, 0.0);")
WriteLine(fw," mainImage(gl_FragColor, gl_FragCoord.xy);")
WriteLine(fw,"}")
CloseFile(fw)
fw=OpenToWrite("circuit3.ps")
WriteLine(fw,"uniform vec3 iResolution; // viewport resolution (in pixels)")
WriteLine(fw,"uniform float time; // shader playback time (in seconds)")
WriteLine(fw,"uniform vec4 iMouse; // mouse pixel coords. xy: current (if MLB down), zw: click")
WriteLine(fw,"uniform sampler2D texture0;")
WriteLine(fw,"uniform sampler2D texture1;")
WriteLine(fw,"#define iTime time*.02")
WriteLine(fw,"#define width .005")
WriteLine(fw,"float zoom = .18;")
WriteLine(fw,"float shape=0.;")
WriteLine(fw,"vec3 color=vec3(0.),randcol;")
WriteLine(fw,"void formula(vec2 z, float c) {")
WriteLine(fw," float minit=0.;")
WriteLine(fw," float o,ot2,ot=ot2=1000.;")
WriteLine(fw," for (int i=0; i<9; i++) {")
WriteLine(fw," z=abs(z)/clamp(dot(z,z),.1,.5)-c;")
WriteLine(fw," float l=length(z);")
WriteLine(fw," o=min(max(abs(min(z.x,z.y)),-l+.25),abs(l-.25));")
WriteLine(fw," ot=min(ot,o);")
WriteLine(fw," ot2=min(l*.1,ot2);")
WriteLine(fw," minit=max(minit,float(i)*(1.-abs(sign(ot-o))));")
WriteLine(fw," }")
WriteLine(fw," minit+=1.;")
WriteLine(fw," float w=width*minit*2.;")
WriteLine(fw," float circ=pow(max(0.,w-ot2)/w,6.);")
WriteLine(fw," shape+=max(pow(max(0.,w-ot)/w,.25),circ);")
WriteLine(fw," vec3 col=normalize(.1+texture2D(texture1,vec2(minit*.1)).rgb);")
WriteLine(fw," color+=col*(.4+mod(minit/9.-iTime*10.+ot2*2.,1.)*1.6);")
WriteLine(fw," color+=vec3(1.,.7,.3)*circ*(10.-minit)*3.*smoothstep(0.,.5,.15+texture2D(texture0,vec2(.0,1.)).x-.5);")
WriteLine(fw,"}")
WriteLine(fw,"void main(void)")
WriteLine(fw,"{")
WriteLine(fw," vec2 pos = gl_FragCoord.xy / iResolution.xy - .5;")
WriteLine(fw," pos.x*=iResolution.x/iResolution.y;")
WriteLine(fw," vec2 uv=pos;")
WriteLine(fw," float sph = length(uv); sph = sqrt(1. - sph*sph)*1.5; // curve for spheric distortion")
WriteLine(fw," uv=normalize(vec3(uv,sph)).xy;")
WriteLine(fw," float a=iTime+mod(iTime,1.)*.5;")
WriteLine(fw," vec2 luv=uv;")
WriteLine(fw," float b=a*5.48535;")
WriteLine(fw," // zoom*=1.+sin(iTime*3.758123)*.8;")
WriteLine(fw," uv*=mat2(cos(b),sin(b),-sin(b),cos(b));")
WriteLine(fw," uv+=vec2(sin(a),cos(a*.5))*8.;")
WriteLine(fw," uv*=zoom;")
WriteLine(fw," float pix=.5/iResolution.x*zoom/sph;")
WriteLine(fw," float dof=max(1.,(10.-mod(iTime,1.)/.01));")
WriteLine(fw," float c=1.5+mod(floor(iTime),6.)*.125;")
WriteLine(fw," for (int aa=0; aa<36; aa++) {")
WriteLine(fw," vec2 aauv=floor(vec2(float(aa)/6.,mod(float(aa),6.)));")
WriteLine(fw," formula(uv+aauv*pix*dof,c);")
WriteLine(fw," }")
WriteLine(fw," shape/=36.; color/=36.;")
WriteLine(fw," vec3 colo=mix(vec3(.15),color,shape)*(1.-length(pos))*min(1.,abs(.5-mod(iTime+.5,1.))*10.); ")
WriteLine(fw," colo*=vec3(1.2,1.1,1.0);")
WriteLine(fw," gl_FragColor = vec4(colo,1.0);")
WriteLine(fw,"}")
CloseFile(fw)
endfunction
//creates texture 0 for sprite 3
function createRandomBgImage()
SetClearColor(0,0,0)
ClearScreen()
Render()
For num = 1 to 500
DrawEllipse(Random(1,1023),Random(1,787),random(10,30),random(10,30),makecolor(random(1,255),random(1,255),random(1,255)),makecolor(random(1,255),random(1,255),random(1,255)),1)
next num
swap()
img = getimage(0,0,1024,768)
//SetImageTransparentColor(img,0,0,0)
endfunction img
//the rest just creates a noise image for texture 1 for sprite x
function generateperlinimage()
// Generate image from memblock
w as integer = 100
h as integer =100
size = w * h * 4 + 12
mem = CreateMemblock(size)
SetMemblockInt(mem,0,w)
SetMemblockInt(mem,4,h)
SetMemblockInt(mem,8,32)
offset as integer = 12
a as float, b as float
a = 5.0
b = 2.0
for y = 0 to h - 1
for x = 0 to w - 1
a = a + 0.0001
b = b + 0.002
// Try out these two noise methods
//noise = 255.0*Noise2D(x/10.0,y/10.0)
noise = 255.0*Noise2D(x/100.0,y/100.0)
noise = abs(noise)
//clouds
if noise>200
SetMemblockByte(mem, offset, noise)
SetMemblockByte(mem, offset+1, noise)
SetMemblockByte(mem, offset+2, noise)
SetMemblockByte(mem, offset+3, 255)
endif
//greenary
if noise>150 and noise<=200
SetMemblockByte(mem, offset, 0)
SetMemblockByte(mem, offset+1, noise)
SetMemblockByte(mem, offset+2, 0)
SetMemblockByte(mem, offset+3, 255)
endif
//sand
if noise>100 and noise<=150
SetMemblockByte(mem, offset, noise)
SetMemblockByte(mem, offset+1, noise)
SetMemblockByte(mem, offset+2, 0)
SetMemblockByte(mem, offset+3, 255)
endif
// water
if noise<=100
SetMemblockByte(mem, offset, 0)
SetMemblockByte(mem, offset+1, 0)
SetMemblockByte(mem, offset+2, noise)
SetMemblockByte(mem, offset+3, 255)
endif
offset = offset + 4
next
next
map=CreateImageFromMemblock(mem)
endfunction map
// ***************************************************************************************************
// Ken Perlin's Simplex Noise 2D. AGK Version.
// Ported from Stefan Gustavson's Java implementation
// (http://staffwww.itn.liu.se/~stegu/simplexnoise/simplexnoise.pdf)
// 2015.02.03
// AGK reference https://forum.thegamecreators.com/thread/213532
// Thank you Thank you
#constant PN3DF2 = 0.5*(sqrt(3.0)-1.0)
#constant PN3DG2 = (3.0-sqrt(3.0))/6.0
Type sPNVECTOR
x as float
y as float
z as float
endtype
Global PNHash as integer[512]
Global PNGrad3 as sPNVECTOR[256]
Function NoiseInit()
Local n as integer, rn as integer
For n=0 To 255
PNHash[n] = n
Next n
For n=0 To 255
rn=Random(0, 255)
PNHash.swap(n,rn)
Next n
For n=0 To 255
PNHash[256 + n] = PNHash[n]
Next n
PNHash[511] = PNHash[0]
For n=0 To 15
PNGrad3[n * 16 + 0].x = 1 : PNGrad3[n * 16 + 0].y = 1 : PNGrad3[n * 16 + 0].z = 0
PNGrad3[n * 16 + 1].x = -1 : PNGrad3[n * 16 + 1].y = 1 : PNGrad3[n * 16 + 1].z = 0
PNGrad3[n * 16 + 2].x = 1 : PNGrad3[n * 16 + 2].y = -1 : PNGrad3[n * 16 + 2].z = 0
PNGrad3[n * 16 + 3].x = -1 : PNGrad3[n * 16 + 3].y = -1 : PNGrad3[n * 16 + 3].z = 0
PNGrad3[n * 16 + 4].x = 1 : PNGrad3[n * 16 + 4].y = 0 : PNGrad3[n * 16 + 4].z = 1
PNGrad3[n * 16 + 5].x = -1 : PNGrad3[n * 16 + 5].y = 0 : PNGrad3[n * 16 + 5].z = 1
PNGrad3[n * 16 + 6].x = 1 : PNGrad3[n * 16 + 6].y = 0 : PNGrad3[n * 16 + 6].z = -1
PNGrad3[n * 16 + 7].x = -1 : PNGrad3[n * 16 + 7].y = 0 : PNGrad3[n * 16 + 7].z = -1
PNGrad3[n * 16 + 8].x = 0 : PNGrad3[n * 16 + 8].y = 1 : PNGrad3[n * 16 + 8].z = 1
PNGrad3[n * 16 + 9].x = 0 : PNGrad3[n * 16 + 9].y = -1 : PNGrad3[n * 16 + 9].z = 1
PNGrad3[n * 16 + 10].x = 0 : PNGrad3[n * 16 + 10].y = 1 : PNGrad3[n * 16 + 10].z = -1
PNGrad3[n * 16 + 11].x = 0 : PNGrad3[n * 16 + 11].y = -1 : PNGrad3[n * 16 + 11].z = -1
PNGrad3[n * 16 + 12].x = 1 : PNGrad3[n * 16 + 12].y = 1 : PNGrad3[n * 16 + 12].z = 0
PNGrad3[n * 16 + 13].x = -1 : PNGrad3[n * 16 + 13].y = 1 : PNGrad3[n * 16 + 13].z = 0
PNGrad3[n * 16 + 14].x = 0 : PNGrad3[n * 16 + 14].y = -1 : PNGrad3[n * 16 + 14].z = 1
PNGrad3[n * 16 + 15].x = 0 : PNGrad3[n * 16 + 15].y = -1 : PNGrad3[n * 16 + 15].z = -1
Next n
endfunction
function Noise2D(xin as float, yin as float)
local n0 as float, n1 as float, n2 as float, s as float, t as float, x0 as float, y0 as float, xs as float, ys as float
local i as integer, j as integer, i1 as integer, j1 as integer, i2 as integer, j2 as integer, gi0 as integer, gi1 as integer, gi2 as integer
local x1 as float, y1 as float, x2 as float, y2 as float, x3 as float, y3 as float, t0 as float, t1 as float, t2 as float
s = (xin + yin) * PN3DF2
xs = xin + s
i = floor(xs)
ys = yin + s
j = floor(ys)
t = (i + j) * PN3DG2
x0 = xin - (i - t)
y0 = yin - (j - t)
if x0>y0
i1=1
j1=0
else
i1=0
j1=1
endif
x1 = x0 - i1 + PN3DG2
y1 = y0 - j1 + PN3DG2
x2 = x0 - 1.0 + 2.0 * PN3DG2
y2 = y0 - 1.0 + 2.0 * PN3DG2
i = i && 255
j = j && 255
gi0 = PNHash[i + PNHash[j]] && 15
gi1 = PNHash[i + i1 + PNHash[j + j1]] && 15
gi2 = PNHash[i + 1 + PNHash[j+ 1]] && 15
t0 = 0.5 - x0*x0-y0*y0
if t0<0
n0 = 0.0
else
t0 = t0 * t0
n0 = t0 * t0 * (PNGrad3[gi0].x * x0 + PNGrad3[gi0].y * y0)
endif
t1 = 0.5 - x1*x1-y1*y1
if t1<0
n1 = 0.0
else
t1 = t1 * t1
n1 = t1 * t1 * (PNGrad3[gi1].x * x1 + PNGrad3[gi1].y * y1)
endif
t2 = 0.5 - x2*x2-y2*y2
if t2<0
n2 = 0.0
else
t2 = t2 * t2
n2 = t2 * t2 * (PNGrad3[gi2].x * x2 + PNGrad3[gi2].y * y2)
endif
endfunction 70.0 * (n0 + n1 + n2)
fubar