Skip to content

Instantly share code, notes, and snippets.

@dermotbalson
Last active December 19, 2015 05:59
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/5907888 to your computer and use it in GitHub Desktop.
Save dermotbalson/5907888 to your computer and use it in GitHub Desktop.
tuch
--# Main
--Main
function setup()
counter=0 --for showing text on screen when we have a hit
CreateImages() --create images
parameter.integer("FPS",0,60,60) --show running speed on screen
end
function draw()
FPS=FPS*.9+.1/DeltaTime
ThreeDT.preDraw() --****ThreeDT - put this just before background
background(80)
perspective()
camera(0,0,-200,0,0,-1000,0,1,0)
--draw all circle meshes
for i,p in pairs(b) do
pushMatrix()
translate(p.x,p.y,p.z)
ThreeDT.drawMesh(p.m,p.id) --****ThreeDT - draw mesh using ThreeDT, include id number (or nil)
popMatrix()
end
id=ThreeDT.CheckHit() --****ThreeDT - check for hit, return ID number if a hit
if id~=nil then lastID=id counter=120 end --do what you want with the hit id
if counter>0 then ShowResult() end
--this code just draws an annoying little girl that bounces up and down to show there is no lag
ortho()
viewMatrix(matrix())
if img0 == nil then img0=readImage("Planet Cute:Character Princess Girl") end
translate(HEIGHT/2,WIDTH*(1+math.cos(ElapsedTime*2*3.14))/2)
sprite(img0,0,0)
end
function touched(touch)
--if we have a touch, store the x,y details
ThreeDT.touched(touch)
end
function ShowResult()
ortho() --this and next statement required to allow us to draw text on a 3D screen
viewMatrix(matrix())
pushStyle()
fill(255)
textMode(CORNER)
fontSize(36)
text("You touched "..lastID,50,HEIGHT-50) --message
popStyle()
counter=counter-1 --decrement counter
end
--this just creates images for testing
function CreateImages()
size=50 --size of circles
n=30 --number of circle
b={} --table of circles
for i=1,n do --create circles
p={} --table to hold details for each circle
p.id=i --id number of circle
p.m=mesh() --create mesh for circle, one per circle
local img=image(size,size) --create drawing on an image
setContext(img)
fill(math.random(0,255),math.random(0,255),math.random(0,255))
ellipse(size/2,size/2,size)
fill(0)
fontSize(24)
text(i,size/2,size/2) --draw id number on circle
setContext() --done drawing our image
p.m:addRect(0,0,size,size) --add circle to mesh
p.m:setRectTex(1,0,0,1,1)
p.m.texture=img
--random position
p.x,p.y,p.z=math.random(-100,100),math.random(-200,200),-math.random(300,1000)
table.insert(b,p) --add to table of circles
end
--sort images by distance so they are drawn correctly
table.sort(b,function(i,j) return i.z<j.z end)
end
--# ThreeDT
--ThreeDT
ThreeDT={}
function ThreeDT.setShader(m)
m.shader=shader(idShader.vertexShader,idShader.fragmentShader)
m.shader.id=0
end
function ThreeDT.touched(touch)
if touch.state==ENDED then
ThreeDT.t=vec2(touch.x,touch.y)
end
end
function ThreeDT.preDraw()
if ThreeDT.t~=nil then --draw on hidden image if we've had a hit
setContext(ThreeDT.img)
clip(ThreeDT.t.x-2,ThreeDT.t.y-2,4,4)
end
--set up hidden image if not done
if ThreeDT.img==nil then
ThreeDT.img=image(WIDTH,HEIGHT)
ThreeDT.t=nil
end
end
function ThreeDT.drawMesh(m,id)
--if shader not set yet, do it now
if ThreeDT.shadersSet==nil then
m.shader=shader(idShader.vertexShader,idShader.fragmentShader)
m.shader.id=0
end
--if we have a touch, set pixels with id number
if ThreeDT.t~=nil then
m.shader.id=id/255
m:draw()
m.shader.id=0
else
m:draw()
end
end
function ThreeDT.CheckHit()
ThreeDT.shadersSet=ThreeDT.shadersSet or true --if we hadn't previously, we've set shaders for all meshes now
if ThreeDT.t==nil then return end
setContext()
clip()
local r,g,b=ThreeDT.img:get(math.floor(ThreeDT.t.x+0.5),math.floor(ThreeDT.t.y+0.5))
ThreeDT.t=nil
---qqq=true
if g+b==0 and r>0 then return r else return nil end
end
idShader = {
vertexShader = [[
uniform mat4 modelViewProjection;
attribute vec4 position;
attribute vec4 color;
attribute vec2 texCoord;
varying lowp vec4 vColor;
varying highp vec2 vTexCoord;
void main()
{
vColor = color;
vTexCoord = texCoord;
gl_Position = modelViewProjection * position;
}
]],
fragmentShader = [[
precision highp float;
uniform lowp sampler2D texture;
uniform float id;
varying lowp vec4 vColor;
varying highp vec2 vTexCoord;
void main()
{
lowp vec4 col=texture2D(texture, vTexCoord);
if (id==0. || col.a<0.2) gl_FragColor=col;
else gl_FragColor = vec4(id,0.,0.,1.);
}
]]}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment