Created
July 17, 2013 04:05
-
-
Save dermotbalson/6017617 to your computer and use it in GitHub Desktop.
tileshader
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function setup() | |
parameter.integer("ChooseImage",1,3,1) | |
parameter.integer("Fader",0,255,100) | |
parameter.boolean("Mist",true) | |
tileWidth,tileHeight=101,171 | |
img=CreateImage() | |
m=mesh() | |
m:addRect(0,0,img.width,img.height) | |
m.texture=img | |
m.shader=shader(tileShader.vertexShader,tileShader.fragmentShader) | |
end | |
function CreateImage() | |
local img=image(tileWidth*3,tileHeight) | |
setContext(img) | |
spriteMode(CORNER) | |
sprite("Planet Cute:Character Horn Girl",0,0) | |
sprite("Planet Cute:Character Pink Girl",tileWidth,0) | |
sprite("Planet Cute:Character Princess Girl",tileWidth*2,0) | |
setContext() | |
return img | |
end | |
function draw() | |
background(220) | |
--1 for mist, 0 for light/dark | |
if Mist then m.shader.mist=1 else m.shader.mist=0 end | |
m.shader.fade=Fader/255 -- low=more mist/dark, high=less mist/more light | |
--tell shader which part of the image to fade, as fraction of width and height | |
--set all to 0 if not needed | |
local x1,y1=(ChooseImage-1)*tileWidth/img.width,0 | |
local x2,y2=x1+tileWidth/img.width,tileHeight/img.height | |
m.shader.tile=vec4(x1,y1,x2,y2) | |
--draw the mesh somewhere random | |
pushMatrix() | |
translate(200,300) | |
m:draw() | |
popMatrix() | |
end | |
tileShader = { | |
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; | |
varying vec4 vPos; | |
void main() | |
{ | |
//Pass the mesh color to the fragment shader | |
vColor=color; | |
vTexCoord = texCoord; | |
vPos=position; | |
//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 float fade; | |
uniform float mist; | |
uniform vec4 tile; | |
//The interpolated vertex color for this fragment | |
varying lowp vec4 vColor; | |
//varying vec4 vPos; | |
//The interpolated texture coordinate for this fragment | |
varying highp vec2 vTexCoord; | |
void main() | |
{ | |
//Sample the texture at the interpolated coordinate | |
lowp vec4 col =texture2D( texture, vTexCoord) * vColor; | |
if (col.a>0. && vTexCoord.x>=tile.x && vTexCoord.x<=tile.z && vTexCoord.y>=tile.y && vTexCoord.y<=tile.w) | |
{ | |
if (mist==0.) col.rgb = col.rgb*fade; | |
else col=col*fade; | |
} | |
gl_FragColor =col; | |
} | |
]]} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment