Skip to content

Instantly share code, notes, and snippets.

@dermotbalson
Created June 2, 2013 08:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dermotbalson/5693079 to your computer and use it in GitHub Desktop.
Save dermotbalson/5693079 to your computer and use it in GitHub Desktop.
tilershader
function setup()
img=readImage("Platformer Art:Block Brick")
--note that when parameter values change, CreateMesh will be called
--and this will rebuild the mesh at the new size
parameter.integer("MeshWidth",20,700,100,CreateMesh)
parameter.integer("MeshHeight",20,700,100,CreateMesh)
print("Slide the sliders to change the size of the mesh")
end
function CreateMesh()
--CreateMesh will be called for the first time when the parameters are defined above, which creates
--a problem with the first one, because MeshHeight hasn't been defined yet (it's the second one!). So
--the first line below just returns if we're not ready to build it yet. So the MeshHeight parameter
--will also call CreateMesh, and this time it will be ok to continue. After this, this function will
--only be called if the user changes size.
if MeshHeight==nil then return end
--clear any existing mesh
if m~=nil then m:clear() else m=mesh() end
m.texture=img
--calculate how many images will fit into our mesh
nx,ny=MeshWidth/img.width,MeshHeight/img.height
--create the mesh, one big rectangle with 6 vertices
v,t={},{}
v[1]=vec3(0,0,0) t[1]=vec2(0,0)
v[2]=vec3(MeshWidth,0,0) t[2]=vec2(nx,0)
v[3]=vec3(MeshWidth,MeshHeight,0) t[3]=vec2(nx,ny)
v[4]=vec3(MeshWidth,MeshHeight,0) t[4]=vec2(nx,ny)
v[5]=vec3(0,MeshHeight,0) t[5]=vec2(0,ny)
v[6]=vec3(0,0,0) t[6]=vec2(0,0)
m.vertices=v
m.texCoords=t
m:setColors(color(255))
m.shader = shader(TilerShader.vertexShader, TilerShader.fragmentShader)
end
function draw()
background(200)
pushMatrix()
translate(10,10)
m:draw()
popMatrix()
end
TilerShader = {
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()
{
//Sample the texture at the interpolated coordinate
lowp vec4 col = texture2D( texture, vec2(mod(vTexCoord.x,1.0), mod(vTexCoord.y,1.0))) * vColor;
//Set the output color to the texture color
gl_FragColor = col;
}
]]}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment