Skip to content

Instantly share code, notes, and snippets.

@dermotbalson
Created November 8, 2014 13:54
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/15be926f7961b479b223 to your computer and use it in GitHub Desktop.
Save dermotbalson/15be926f7961b479b223 to your computer and use it in GitHub Desktop.
3D Sbs
--# Main
--Main
--You can ignore this code tab. All it does is manage your selection of different options.
--This code manages which Code tab is run
--it remembers your last choice, and if you select a different one, it runs that instead
local tabs = {}
local fnames = {"setup","draw","touched","collide","orientationChanged","close","restart","keyboard","cleanup"}
local fns = {}
local tabDesc={}
function setup()
for k,v in ipairs(fnames) do --store addresses of key event functions
fns[v] = _G[v]
end
LastCode=readProjectData("Code") or 1 --load stored tab number
parameter.integer("Choose_a_tab",1,#tabs,LastCode,ShowList) --tab selector
parameter.action("Run selected tab", RunCode)
RunCode()
end
function ShowList()
output.clear()
for i=1,#tabs do
print(i,tabDesc[i])
end
end
--these two functions do all the tab switching magic (thanks to Andrew_Stacey)
function localise(n,d)
if d then tabDesc[n]=d end
local t= {}
setmetatable(t,{__index = _G})
setfenv(2,t)
tabs[n] = t
end
--change tabs
function RunCode()
output.clear()
saveProjectData("Code",Choose_a_tab)
cleanup()
local t = tabs[Choose_a_tab]
for k,v in ipairs(fnames) do
if t[v] then _G[v] = t[v] else _G[v] = fns[v] end -- overwrite with the new code
end
setup()
end
--default empty function to avoid errors for tabs that don't have it
function cleanup()
end
--# ThreeD
--- Three D Text class ---------
--needed for all the 3D examples
ThreeD=class()
function ThreeD:init(arg)
self.textColor=arg.textColor or color(255)
self.shadeColor=color(self.textColor.r-50,self.textColor.g-50,self.textColor.b-50,255)
if arg.image then self.img=arg.image
else self.img=self:CreateImage(arg) end
self:CreateMesh(arg)
self.width=arg.width or 5
--number of layers of text and separation between them
self.LayerThickness=0.25
self.LayerCount=arg.width/self.LayerThickness
self.LayerAdjustForSideOn=self.width/2 --multipies number of layers by this when sideon
self.currLayers,self.currThickness=self.LayerCount,self.LayerThickness
end
function ThreeD:CreateMesh(arg)
--create the mesh
self.m=mesh()
self.m:addRect(0,0,self.img.width,self.img.height)
self.m.texture=self.img
end
function ThreeD:CreateImage(arg)
pushStyle()
if arg.font then font(arg.font) end
if arg.size then fontSize(arg.size) end
fill(255)
--size the image
local w,h=textSize(arg.text)
--create the texture image
local img=image(w,h)
setContext(img)
text(arg.text,w/2,h/2)
setContext()
popStyle()
return img
end
function ThreeD:draw()
local reverse=self:AdjustForRotation()
self:DrawLayers(reverse)
--self.m:draw()
--sprite(self.img,0,0)
end
function ThreeD:AdjustForRotation()
local v=self:RotationValue()
if math.abs(v)<0.1 then
self.currLayers=self.LayerCount*self.LayerAdjustForSideOn
self.currThickness=self.LayerThickness/self.LayerAdjustForSideOn
else
self.currLayers,self.currThickness=self.LayerCount,self.LayerThickness
end
if v<0 then return false else return true end
end
function ThreeD:DrawLayers(reverse)
pushMatrix()
--set colour for outside layer
local t=self.currThickness
if reverse then
translate(0,0,(self.currThickness*self.currLayers))
t=-t
end
self.m:setColors(self.textColor)
self.m:draw()
--draw inside layers
self.m:setColors(self.shadeColor)
for i=1,self.currLayers do
translate(0,0,t)
self.m:draw()
end
--draw other outside layer
translate(0,0,t)
self.m:setColors(self.textColor)
self.m:draw()
popMatrix()
end
function ThreeD:IsReversing(t)
t=t or 0
if self:RotationValue()>-t then return true else return false end
end
function ThreeD:RotationValue()
local v1,v2= modelMatrix()*vec3(0,0,-1),modelMatrix()*vec3(0,0,1)
return v1.z-v2.z
end
--# TwoD
if localise then localise(1,"2D") end
--writing 3D text on a 2D screen
function setup()
txt="Codea"
foreColor=color(78, 158, 120, 255)
--make a darker colour for the 3D part
backColor=color(foreColor.r-50,foreColor.g-50,foreColor.b-50)
depth=10 --pixel depth of 3D part
size=144
end
function draw()
background(220)
pushStyle()
fontSize(size)
fill(backColor)
position=vec2(WIDTH/2+depth,HEIGHT/2+depth) --start at back
for i=1,depth*2-2 do --come forward in half pixel steps
text(txt,position.x,position.y)
position=position-vec2(0.5,0.5)
end
--change to forecolour for front image
fill(foreColor)
text(txt,position.x,position.y)
popStyle()
end
--# ThreeD_Basic
if localise then localise(2,"Basic 3D") end
-- ThreeDChars
--displayMode(FULLSCREEN)
function setup()
txt=ThreeD{text="Codea",size=72,width=10,textColor=color(55, 148, 117)}
--starting position of image
pos=vec3(0,0,-500)
end
function draw()
background(158, 204, 184, 255)
perspective()
camera(0,0,0,0,0,-500)
pushMatrix()
translate(pos.x,pos.y,pos.z)
rotate(25,1,0,0)
rotate(30,0,1,0)
txt:draw()
popMatrix()
end
--# ThreeD_Rotate
if localise then localise(3,"Rotating 3D") end
-- ThreeDChars
--displayMode(FULLSCREEN)
function setup()
txt=ThreeD{text="Codea\nis cool",font="Noteworthy-Bold",size=72,width=5,textColor=color(55, 148, 117)}
--starting position of image
pos=vec3(0,0,-500)
--starting rotation in x,y,z axes
rot=vec3(0,0,0)
--change in rotation at each frame
dRot=vec3(2,-1,0)
end
function draw()
background(158, 204, 184, 255)
perspective()
camera(0,0,0,0,0,-500)
pushMatrix()
translate(pos.x,pos.y,pos.z)
rotate(rot.x,1,0,0)
rotate(rot.y,0,1,0)
rotate(rot.z,0,0,1)
txt:draw()
rot=rot+dRot
popMatrix()
end
--# ThreeD_All
if localise then localise(4,"Multiple 3D objects") end
-- ThreeDChars
--displayMode(FULLSCREEN)
function setup()
txt=ThreeD{text="Codea\nis cool",font="Noteworthy-Bold",size=72,width=5,textColor=color(55, 148, 117)}
txt2=ThreeD{text="really\n\n ^",font="Noteworthy-Bold",size=36,width=5,textColor=color(255, 255, 0, 255)}
ship=ThreeD{image=readImage("Space Art:Red Ship"),width=5}
--starting position of image
pos=vec3(0,0,-500)
--starting rotation in x,y,z axes
rot=vec3(0,0,0)
--change in rotation at each frame
dRot=vec3(2,-1,0)
end
function draw()
background(158, 204, 184, 255)
SetupCamera()
pushMatrix()
TranslateRotate()
DrawText()
popMatrix()
rot=rot+dRot
end
function SetupCamera()
perspective()
camera(0,0,0,0,0,-500)
end
function TranslateRotate()
translate(pos.x,pos.y,pos.z)
rotate(rot.x,1,0,0)
rotate(rot.y,0,1,0)
rotate(rot.z,0,0,1)
end
function DrawText()
if txt:IsReversing() then
pushMatrix()
translate(-25,-70,10)
txt2:draw()
translate(35,200,0)
ship:draw()
popMatrix()
txt:draw()
else
txt:draw()
translate(-25,-70,10)
txt2:draw()
translate(35,200,0)
ship:draw()
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment