Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
shaderz
--# Main
--Main
function setup()
ShaderList={Fog,Ghost,Flip,Hole,Transparent,Stencil}
parameter.integer("ShaderChoice",1,6,1,SetupShader)
parameter.integer("Slider",1,1000,500)
--parameter.boolean("Option",false)
end
function SetupShader()
S=ShaderList[ShaderChoice]
S:setup()
end
function draw()
S:draw()
end
--# Ghost
Ghost={}
function Ghost:setup()
Ghost.img=readImage("Planet Cute:Character Princess Girl")
m1=mesh()
m1.texture=Ghost.img
u=m1:addRect(0,0,Ghost.img.width,Ghost.img.height)
m1:setRectTex(u,0,0,1,1)
m1:setColors(color(255))
m1.shader = shader(GhostShader.vertexShader, GhostShader.fragmentShader) --$$$
output:clear()
print("Slide the slider to change the transparency")
end
function Ghost:draw()
background(220)
perspective()
camera(0,20,200,0,0,0)
pushMatrix()
translate(0,0,-300)
m1.shader.opaque=Slider/1000 --$$$ 0=visible 1=invisible
m1:draw()
popMatrix()
end
GhostShader = {
vertexShader = [[
//
// A basic vertex shader
//
//This is the current model * view * projection matrix
// Codea sets it automatically
uniform mat4 modelViewProjection;
//This is the current mesh vertex position, color and tex coord
// Set automatically
attribute vec4 position;
attribute vec4 color;
attribute vec2 texCoord;
//This is an output variable that will be passed to the fragment shader
varying lowp vec4 vColor;
varying highp vec2 vTexCoord;
void main()
{
//Pass the mesh color to the fragment shader
vColor = color;
vTexCoord = texCoord;
//Multiply the vertex position by our combined transform
gl_Position = modelViewProjection * position;
}
]],
fragmentShader = [[
//
// A basic fragment shader
//
//Default precision qualifier
precision highp float;
//This represents the current texture on the mesh
uniform lowp sampler2D texture;
uniform lowp float opaque; //$$$$$
//The interpolated vertex color for this fragment
varying lowp vec4 vColor;
//The interpolated texture coordinate for this fragment
varying highp vec2 vTexCoord;
void main()
{
lowp vec4 col = texture2D(texture, vTexCoord) ;
col=vec4(col.r*opaque,col.g*opaque,col.b*opaque,col.a*opaque); //$$$$$
gl_FragColor = col;
}
]]}
--# Fog
Fog={}
function Fog:setup()
Fog.img=readImage("Planet Cute:Character Princess Girl")
m1=mesh()
m1.texture=Fog.img
u=m1:addRect(0,0,Fog.img.width,Fog.img.height)
m1:setRectTex(u,0,0,1,1)
m1:setColors(color(255))
m1.shader = shader(FogShader.vertexShader, FogShader.fragmentShader)
m1.shader.texture2 = CreateFogImage() -- $$$
output:clear()
print("Slide the slider to move the image in and out of the fog")
end
function CreateFogImage() -- $$$
local img=image(100,100)
setContext(img)
pushStyle()
fill(220)
rect(0,0,img.width,img.height)
popStyle()
setContext()
return img
end
function Fog:draw()
background(220)
perspective()
camera(0,20,200,0,0,0)
pushMatrix()
local z=Slider --$$$
translate(0,0,-z/2) --$$$
--object becomes totally invisible at distance of 500
m1.shader.mixAmount = z/1000 --$$$$
m1:draw()
popMatrix()
end
FogShader = {
vertexShader = [[
//
// A basic vertex shader
//
//This is the current model * view * projection matrix
// Codea sets it automatically
uniform mat4 modelViewProjection;
//This is the current mesh vertex position, color and tex coord
// Set automatically
attribute vec4 position;
attribute vec4 color;
attribute vec2 texCoord;
//This is an output variable that will be passed to the fragment shader
varying lowp vec4 vColor;
varying highp vec2 vTexCoord;
void main()
{
//Pass the mesh color to the fragment shader
vColor = color;
vTexCoord = texCoord;
//Multiply the vertex position by our combined transform
gl_Position = modelViewProjection * position;
}
]],
fragmentShader = [[
//
// A basic fragment shader
//
//Default precision qualifier
precision highp float;
//This represents the current texture on the mesh
uniform lowp sampler2D texture;
uniform lowp sampler2D texture2; //$$$$
uniform float mixAmount; //$$$$
//The interpolated vertex color for this fragment
varying lowp vec4 vColor;
//The interpolated texture coordinate for this fragment
varying highp vec2 vTexCoord;
void main()
{
lowp vec4 col1 = texture2D(texture, vTexCoord) ;
lowp vec4 col2 = texture2D(texture2, vTexCoord) ; //$$$
if (col1.a > 0.) gl_FragColor = mix(col1, col2, mixAmount); //$$$
else gl_FragColor = col1;
}
]]}
--# Flip
Flip={}
function Flip:setup()
Flip.img=readImage("Planet Cute:Character Princess Girl")
m1=mesh()
m1.texture=Flip.img
u=m1:addRect(0,0,Flip.img.width,Flip.img.height)
m1:setRectTex(u,0,0,1,1)
m1:setColors(color(255))
m1.shader = shader(FlipShader.vertexShader, FlipShader.fragmentShader)
output:clear()
print("Slide the slider to flip the image on the x (slider<333) or y (slider>666) axis")
print("Actually, you don't need a shader to do this, but anyway..")
end
function Flip:draw()
background(220)
perspective()
camera(0,20,200,0,0,0)
pushMatrix()
translate(0,0,-200)
if Slider<333 then m1.shader.flip=1 --$$$$
elseif Slider<666 then m1.shader.flip=2 --$$$
else m1.shader.flip=3 end --$$$
m1:draw()
popMatrix()
end
FlipShader = {
vertexShader = [[
//
// A basic vertex shader
//
//This is the current model * view * projection matrix
// Codea sets it automatically
uniform mat4 modelViewProjection;
//This is the current mesh vertex position, color and tex coord
// Set automatically
attribute vec4 position;
attribute vec4 color;
attribute vec2 texCoord;
//This is an output variable that will be passed to the fragment shader
varying lowp vec4 vColor;
varying highp vec2 vTexCoord;
uniform float flip; //$$$$
void main()
{
//Pass the mesh color to the fragment shader
vColor = color;
if (flip==1.)
vTexCoord=vec2(1.-texCoord.x,texCoord.y); //$$$$$
else if (flip==3.)
vTexCoord=vec2(texCoord.x,1.-texCoord.y); //$$$$
else
vTexCoord = texCoord;
//Multiply the vertex position by our combined transform
gl_Position = modelViewProjection * position;
}
]],
fragmentShader = [[
//
// A basic fragment shader
//
//Default precision qualifier
precision highp float;
//This represents the current texture on the mesh
uniform lowp sampler2D texture;
//The interpolated vertex color for this fragment
varying lowp vec4 vColor;
//The interpolated texture coordinate for this fragment
varying highp vec2 vTexCoord;
void main()
{
lowp vec4 col = texture2D(texture, vTexCoord) ;
gl_FragColor = col;
}
]]}
--# Hole
Hole={}
function Hole:setup()
Hole.img=readImage("Planet Cute:Character Princess Girl")
m1=mesh()
m1.texture=Hole.img
u=m1:addRect(-Hole.img.width/2,-Hole.img.height/2,Hole.img.width,Hole.img.height)
m1:setRectTex(u,0,0,1,1)
m1:setColors(color(255))
--screen that we will cut a hole in --$$$$
m2=mesh()
Hole.img2=readImage("Cargo Bot:Starry Background") --CreateScreenImage()
m2.texture=Hole.img2
u=m2:addRect(0,0,Hole.img2.width,Hole.img2.height)
m2:setRectTex(u,0,0,1,1)
m2:setColors(color(255))
m2.shader = shader(HoleShader.vertexShader, HoleShader.fragmentShader)
m2.shader.hole=vec4(0.2,0.2,0.5,0.5)
output:clear()
print("We cut a hole in an image - this is NOT easy")
print("Slide the slider to move the image behind and in front of the screen")
end
function CreateScreenImage() -- $$$
local img=image(500,50)
setContext(img)
pushStyle()
fill(255,0,0,255)
strokeWidth(5)
stroke(255,255,0,255)
rect(0,0,img.width,img.height)
popStyle()
setContext()
return img
end
function Hole:draw()
background(220)
perspective()
camera(0,20,200,0,0,0)
pushMatrix()
translate(0,-50,-100)
m2:draw()
popMatrix()
pushMatrix()
translate(0,0,-Slider*2)
m1:draw()
popMatrix()
end
HoleShader = {
vertexShader = [[
//
// A basic vertex shader
//
//This is the current model * view * projection matrix
// Codea sets it automatically
uniform mat4 modelViewProjection;
//This is the current mesh vertex position, color and tex coord
// Set automatically
attribute vec4 position;
attribute vec4 color;
attribute vec2 texCoord;
//This is an output variable that will be passed to the fragment shader
varying lowp vec4 vColor;
varying highp vec2 vTexCoord;
void main()
{
//Pass the mesh color to the fragment shader
vColor = color;
vTexCoord = texCoord;
//Multiply the vertex position by our combined transform
gl_Position = modelViewProjection * position;
}
]],
fragmentShader = [[
//
// A basic fragment shader
//
//Default precision qualifier
precision highp float;
//This represents the current texture on the mesh
uniform lowp sampler2D texture;
uniform vec4 hole; //$$$$$
//The interpolated vertex color for this fragment
varying lowp vec4 vColor;
//The interpolated texture coordinate for this fragment
varying highp vec2 vTexCoord;
void main()
{
if (vTexCoord.x<=hole.x || vTexCoord.x>=hole.z || vTexCoord.y<=hole.y || vTexCoord.y>=hole.w)
gl_FragColor = texture2D(texture, vTexCoord);
else
discard; //$$$$$
}
]]}
--# Transparent
Transparent={}
function Transparent:setup()
m1=CreateMesh("Planet Cute:Character Princess Girl")
m2=CreateMesh("Planet Cute:Character Pink Girl")
m2.shader = shader(TransparentShader.vertexShader, TransparentShader.fragmentShader)
m3=CreateMesh("Cargo Bot:Starry Background")
output:clear()
print("If you draw an image with transparent pixels, OpenGL won't draw anything behind it. You have to discard those transparent pixels")
print("One of the front pictures has discarded its transparent pixels, the other hasn't. Slide the slider to see which it is.")
print("This shader discards transparent pixels.")
end
function CreateMesh(imgName)
local img=readImage(imgName)
local m=mesh()
m.texture=img
local u=m:addRect(-img.width/2,-img.height/2,img.width,img.height)
m:setRectTex(u,0,0,1,1)
m:setColors(color(255))
return m
end
function Transparent:draw()
background(220)
perspective()
camera(0,20,200,0,0,0)
pushMatrix()
translate(-50,0,-200)
m1:draw()
popMatrix()
pushMatrix()
translate(50,0,-200)
m2:draw()
popMatrix()
pushMatrix()
translate(Slider-500,0,-300)
m3:draw()
popMatrix()
end
TransparentShader = {
vertexShader = [[
//
// A basic vertex shader
//
//This is the current model * view * projection matrix
// Codea sets it automatically
uniform mat4 modelViewProjection;
//This is the current mesh vertex position, color and tex coord
// Set automatically
attribute vec4 position;
attribute vec4 color;
attribute vec2 texCoord;
//This is an output variable that will be passed to the fragment shader
varying lowp vec4 vColor;
varying highp vec2 vTexCoord;
void main()
{
//Pass the mesh color to the fragment shader
vColor = color;
vTexCoord = texCoord;
//Multiply the vertex position by our combined transform
gl_Position = modelViewProjection * position;
}
]],
fragmentShader = [[
//
// A basic fragment shader
//
//Default precision qualifier
precision highp float;
//This represents the current texture on the mesh
uniform lowp sampler2D texture;
//The interpolated vertex color for this fragment
varying lowp vec4 vColor;
//The interpolated texture coordinate for this fragment
varying highp vec2 vTexCoord;
void main()
{
lowp vec4 col=texture2D(texture, vTexCoord);
if (col.a<0.2) discard;
else gl_FragColor = col * vColor;
}
]]}
--# Stencil
-- StencilMask
Stencil={}
function Stencil:setup()
Stencil.img=readImage("Cargo Bot:Starry Background")
stencilImg=readImage("Planet Cute:Character Princess Girl")
m=mesh()
u=m:addRect(0,0,Stencil.img.width,Stencil.img.height)
m:setRectTex(u,0,0,1,1)
m.texture=Stencil.img
m.shader = shader(stencilShader.vertexShader, stencilShader.fragmentShader)
m.shader.texture2=stencilImg
m.shader.negative=Negative
output.clear()
print("One image is used as a stencil/mask for the other")
print("Slide the slider to invert the mask")
end
function Stencil:draw()
background(200)
pushMatrix()
translate(300,300)
if Slider<500 then m.shader.negative=true else m.shader.negative=false end
m.shader.offset=vec2(10/Stencil.img.width,10/Stencil.img.height)
m:draw()
popMatrix()
end
stencilShader = {
vertexShader = [[
//
// A basic vertex shader
//
//This is the current model * view * projection matrix
// Codea sets it automatically
uniform mat4 modelViewProjection;
//This is the current mesh vertex position, color and tex coord
// Set automatically
attribute vec4 position;
attribute vec4 color;
attribute vec2 texCoord;
//This is an output variable that will be passed to the fragment shader
varying lowp vec4 vColor;
varying highp vec2 vTexCoord;
void main()
{
//Pass the mesh color to the fragment shader
vColor = color;
vTexCoord = texCoord;
//Multiply the vertex position by our combined transform
gl_Position = modelViewProjection * position;
}
]],
fragmentShader = [[
//
// A basic fragment shader
//
//Default precision qualifier
precision highp float;
//This represents the current texture on the mesh
uniform lowp sampler2D texture;
uniform lowp sampler2D texture2;
uniform bool negative;
uniform vec2 offset;
//The interpolated vertex color for this fragment
varying lowp vec4 vColor;
//The interpolated texture coordinate for this fragment
varying highp vec2 vTexCoord;
void main()
{
//Sample the texture at the interpolated coordinate
lowp vec4 col1 = texture2D( texture, vTexCoord );
lowp vec4 col2 = texture2D( texture2, vec2(vTexCoord.x-offset.x,vTexCoord.y-offset.y));
if (negative)
{if (col2.a>0.) gl_FragColor = col1; else discard;}
else if (col2.a==0.) gl_FragColor = col1; else discard;
}
]]}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.