Skip to content

Instantly share code, notes, and snippets.

@dermotbalson
Last active December 17, 2015 11:59
Show Gist options
  • Save dermotbalson/5606877 to your computer and use it in GitHub Desktop.
Save dermotbalson/5606877 to your computer and use it in GitHub Desktop.
3D text
function setup()
parameter.integer("Viewpoint",-2000,2000,20)
local i=CreateImage()
local f=readImage("SpaceCute:Background") --NEW
speed,angle=0,0
ds,da=0,0
posX,posY,posZ=-20,0,10 --starting position
rad=math.pi/180
meshTable={}
AddFourWalls(0,0,400,150,200,40,i)
AddLevel(-100,0,0,400,1000,f)
FPS=60
end
function draw()
background(220)
perspective(45,WIDTH/HEIGHT)
if Viewpoint<10 then Viewpoint=10 end
angle=angle+da*DeltaTime
posX,posZ=posX+ds*DeltaTime*math.sin(angle*rad),posZ+ds*DeltaTime*math.cos(angle*rad)
--look in the same direction as we are facing
lookX,lookY,lookZ=posX+1000*math.sin(angle*rad),20,posZ+1000*math.cos(angle*rad)
camera(posX,Viewpoint,-posZ,lookX,lookY,-lookZ, 0,1,0)
fill(255)
for i,m in pairs(meshTable) do
if m.ang==nil then m:draw()
else
pushMatrix()
translate(m.pos.x,m.pos.y,-m.pos.z)
rotate(-m.ang,0,1,0)
m:draw()
popMatrix()
end
end
ortho() --this is needed to get text written on the screen
viewMatrix(matrix()) --and this
FPS=FPS*.9+.1/DeltaTime
pushStyle()
fill(0)
fontSize(18)
strokeWidth(3)
textMode(CORNER)
text("FPS: "..string.format("%d",FPS),50,HEIGHT-50)
text("x="..string.format("% d",posX),50,HEIGHT-70)
text("y="..string.format("% d",Viewpoint),50,HEIGHT-90)
text("z="..string.format("% d",posZ),50,HEIGHT-110)
popStyle()
end
function CreateImage()
local i=image(200,110)
pushStyle()
setContext(i)
fill(255,255,0,220)
rectMode(CORNER)
rect(0,0,i.width,i.height)
fill(255,0,100,255)
ellipse(55,i.height/2,100)
setContext()
popStyle()
return i
end
function AddFourWalls(x,y,z,w,d,h,img)
local m=mesh()
m.texture=img
local v,t={},{}
v,t=AddImage(x,y,-z,w,h,0,v,t)
v,t=AddImage(x,y,-z-d,0,h,-d,v,t)
v,t=AddImage(x+w,y,-z-d,-w,h,0,v,t)
v,t=AddImage(x+w,y,-z,0,h,d,v,t)
m.vertices=v
m:setColors(color(255))
m.texCoords=t
table.insert(meshTable,m)
end
function AddImage(x,y,z,w,h,d,v,t)
v[#v+1]=vec3(x,y,z) t[#t+1]=vec2(0,0)
v[#v+1]=vec3(x+w,y,z-d) t[#t+1]=vec2(1,0)
v[#v+1]=vec3(x+w,y+h,z-d) t[#t+1]=vec2(1,1)
v[#v+1]=vec3(x+w,y+h,z-d) t[#t+1]=vec2(1,1)
v[#v+1]=vec3(x,y+h,z) t[#t+1]=vec2(0,1)
v[#v+1]=vec3(x,y,z) t[#t+1]=vec2(0,0)
return v,t
end
function AddLevel(x,y,z,w,d,i)
local m=mesh()
m.texture=i
local v,t={},{}
v[#v+1]=vec3(x,y,z) t[#t+1]=vec2(0,0)
v[#v+1]=vec3(x+w,y,z) t[#t+1]=vec2(1,0)
v[#v+1]=vec3(x+w,y,z-d) t[#t+1]=vec2(1,1)
v[#v+1]=vec3(x+w,y,z-d) t[#t+1]=vec2(1,1)
v[#v+1]=vec3(x,y,z-d) t[#t+1]=vec2(0,1)
v[#v+1]=vec3(x,y,z) t[#t+1]=vec2(0,0)
m.vertices=v
m:setColors(color(255))
m.texCoords=t
m.pos=vec3(x,y,z)
table.insert(meshTable,m)
end
function touched(touch)
local center=true
if touch.x<WIDTH/4 then
da=da-2
center=false
elseif touch.x>WIDTH*3/4 then
da=da+2
center=false
end
if touch.y<HEIGHT/4 then
ds=ds-3
center=false
elseif touch.y>HEIGHT*3/4 then
ds=ds+3
center=false
end
if center then ds=0 da=0 end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment