Created
November 6, 2014 05:21
-
-
Save dermotbalson/6774ced4dbb6945a4a5b to your computer and use it in GitHub Desktop.
3D text
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
-- ThreeDChars | |
displayMode(FULLSCREEN) | |
function setup() | |
ThreeD=Create3DImage(180) --180 is size of image | |
Settings() | |
end | |
function Settings() | |
--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(0,0,0) | |
--time until we randomly change the rotation | |
nextRotChange=5 | |
--change in z value at each frame | |
dZ=1 | |
--number of layers of text and separation between them | |
LayerCount=25 | |
LayerThickness=0.4 | |
end | |
function Create3DImage(size) | |
local m=mesh() | |
m:addRect(0,0,size,size) | |
local img=image(size,size) | |
setContext(img) | |
fill(255) | |
fontSize(144) | |
text("3D",size/2,size/2) | |
setContext() | |
m.texture=img | |
return m | |
end | |
function draw() | |
background(255) | |
SetupCamera() | |
ChangeDistance() | |
pushMatrix() | |
TranslateRotate() | |
ReverseRotationIfSideOn() | |
DrawLayers() | |
popMatrix() | |
ChangeRotation() | |
end | |
function SetupCamera() | |
perspective() | |
camera(0,0,0,0,0,-500) | |
end | |
--move image forward (or backward) | |
function ChangeDistance() | |
pos.z=pos.z+dZ | |
--change direction if we get too close or far away | |
if pos.z<-500 or pos.z>-210 then dZ=-dZ end | |
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 ReverseRotationIfSideOn() | |
local v1,v2= modelMatrix()*vec3(0,0,-1),modelMatrix()*vec3(0,0,1) | |
if math.abs(v1.z-v2.z)<0.1 then dRot=-dRot end | |
end | |
function DrawLayers() | |
--set colour for outside layer | |
--draw inside layers | |
ThreeD:setColors(173, 42, 42) | |
for i=1,LayerCount do | |
translate(0,0,LayerThickness) | |
ThreeD:draw() | |
end | |
--draw other outside layer | |
translate(0,0,LayerThickness) | |
ThreeD:setColors(255,0,0) | |
ThreeD:draw() | |
end | |
function ChangeRotation() | |
rot=rot+dRot | |
--change rotation randomly every 15 seconds | |
if ElapsedTime>nextRotChange then | |
dRot=vec3(math.random()-0.5,math.random()-0.5,math.random()-0.5)*2 | |
nextRotChange=ElapsedTime+15 | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment