Created
September 23, 2014 09:34
-
-
Save dermotbalson/7dbe101a856a50cba2b9 to your computer and use it in GitHub Desktop.
Tile Shader
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() | |
m=mesh() | |
img=readImage("Cargo Bot:Starry Background") | |
m.texture=img | |
local v,t={},{} | |
meshWidth,meshDepth=4000,15000 --huge mesh | |
imgScale=0.5 --use the image at this fraction of its normal size, ie reduce it | |
--now calculate how many times the image is used along the x and z axes | |
--use these as the maximum texture settings | |
--the shader will just use the fractional part of the texture mapping | |
--(the shader only requires one line to change, to do this) | |
local tilesWide,tilesDeep=meshWidth/img.width/imgScale,meshDepth/img.height/imgScale | |
local x1,x2,y,z1,z2=-meshWidth/2,meshWidth/2,0,0,-meshDepth | |
local tx1,tx2,tz1,tz2=0,tilesWide,0,tilesDeep | |
v[1]=vec3(x1,y,z1) t[1]=vec2(tx1,tz1) | |
v[2]=vec3(x2,y,z1) t[2]=vec2(tx2,tz1) | |
v[3]=vec3(x2,y,z2) t[3]=vec2(tx2,tz2) | |
v[4]=vec3(x1,y,z2) t[4]=vec2(tx1,tz2) | |
v[5]=vec3(x1,y,z1) t[5]=vec2(tx1,tz1) | |
v[6]=vec3(x2,y,z2) t[6]=vec2(tx2,tz2) | |
m.vertices=v | |
m.texCoords=t | |
m.shader=shader(TileShader.vertexShader,TileShader.fragmentShader) | |
m:setColors(color(255)) | |
print("Tile a giant area seamlessly with one image") | |
end | |
function draw() | |
background(220) | |
perspective() | |
camera(0,1000,100,0,0,-meshDepth) | |
m:draw() | |
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; | |
uniform float flip; //$$$$ | |
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, vec2(mod(vTexCoord.x,1.0), mod(vTexCoord.y,1.0))); | |
gl_FragColor = col; | |
} | |
]]} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment