ÎÌÌÌ̈ˆˆ¬ªªª¬ˆˆˆ¬ÌÌ̬ÌÀ̬ÌÀ̬ÌÀÌÌÌìÌÀÀî̬ÀÌÀ¬ÀÀÀ¬ÀÀÀ¬ÀÀÎÌÌÌ̈ˆˆ¬ªªª¬ˆˆˆ¬ÌÌ̬ÌÌ̬ÌÀ̬ÌÀÌÌÌìÌÀÀî̬ÀÌ̬ÀÀÀ¬ÀÀÀ¬ÀÀ¬ÌÌ̬ªªª¬ª¬ª¬ªÊ̬ªªªŒˆˆˆÌÀÌÎÌÌÎ̬̪ªÀ쬪ÀÀÀÌîÀìîÌÌîî¬ÌÌ̬ªªª¬ª¬ª¬ªÊ̬ªªªŒˆˆˆÌÀÌÎÌÌÎ̬̪ªÀ쬪ÀÀÀÌîÀìîÌÌîî >~>fffff`fffffffffff`ffffffffffffffffffffffffffffffffff&""affffffffffff"""""fffffff&"""""""aff""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""í-- title: 3D cube -- author: Phoebe Harris -- desc: 3D graphics from scratch -- script: lua -- version = 1.0 -- license: CC0 -- viewport depth d = 90 -- color palette memory address PALETTE = 0x3FC0 local sin, cos, rad = math.sin, math.cos, math.rad -- converts a position in the viewport to a pixel coordinate function ViewPortToCanvas(x, y) return x * (240/136) + 120, y * (240/136) + 68 end -- projects a 3D vertex onto the 2D plane function ProjectVertex(v) return ViewPortToCanvas(v.x * d / v.z, v.y * d / v.z) end -- draw a line through 3D function DrawLine(v1, v2, color) x1, y1 = ProjectVertex(v1) x2, y2 = ProjectVertex(v2) line(x1, y1, x2, y2, color) end -- dot product function dot(v1, v2) return v1.x * v2.x + v1.y * v2.y + v1.z * v2.z end -- subtract 2 vectors function minus(v1, v2) return {x = v1.x - v2.x, y = v1.y - v2.y, z = v1.z - v2.z} end -- cross product function cross(v1, v2) x = (v1.y * v2.z) - (v1.z * v2.y) y = (v1.z * v2.x) - (v1.x * v2.z) z = (v1.x * v2.y) - (v1.y * v2.x) return {x=x,y=y,z=z} end -- vector magnitude function mag(v) return math.sqrt((v.x * v.x) + (v.y * v.y) + (v.z * v.z)) end -- 1 if a poly is dead-on, 0 if parallel with camera, negative if facing away function DegreeTowardsCamera(v1, v2, v3) a = cross(minus(v2, v1), minus(v3, v1)) b = v1 return dot(a, b)/(mag(b) * mag(a)) end -- sets the color at c+6 as c * proportion function ReplaceColor(color, proportion) if proportion > 1 then proportion = 1 end r = peek(PALETTE+(color*3)) g = peek(PALETTE+(color*3)+1) b = peek(PALETTE+(color*3)+2) color = color + 6 poke(PALETTE+(color*3), r*proportion) poke(PALETTE+(color*3)+1, g*proportion) poke(PALETTE+(color*3)+2, b*proportion) end -- draws a triangle in 3D space function DrawTri(v1, v2, v3, color, replace) replace = replace or false angle = (DegreeTowardsCamera(v1, v2, v3)) -- back-face culling if angle < 0 then return end x1, y1 = ProjectVertex(v1) x2, y2 = ProjectVertex(v2) x3, y3 = ProjectVertex(v3) if replace then -- set shading. 0.5 is for ambient lighting ReplaceColor(color, angle+0.5) end -- the +6 uses the shaded colour later in the palette tri(x1, y1, x2, y2, x3, y3, color+(6*shade)) end -- rotate the cube count degrees from initial posiion function Rotate(count) -- vAf, vBf x1 = -1.5-(0.7*sin(rad(135+count))) z1 = 5.5-(0.7*cos(rad(135+count))) -- vCf, vDf x2 = -1.5-(0.7*sin(rad(225+count))) z2 = 5.5-(0.7*cos(rad(225+count))) -- vAb, vBb x3 = -1.5-(0.7*sin(rad(45+count))) z3 = 5.5-(0.7*cos(rad(45+count))) -- vCb, vDb x4 = -1.5-(0.7*sin(rad(315+count))) z4 = 5.5-(0.7*cos(rad(315+count))) vAf = {x=x1, y=vAf.y, z=z1} vBf = {x=x1, y=vBf.y, z=z1} vCf = {x=x2, y=vCf.y, z=z2} vDf = {x=x2, y=vDf.y, z=z2} vAb = {x=x3, y=vAb.y, z=z3} vBb = {x=x3, y=vBb.y, z=z3} vCb = {x=x4, y=vCb.y, z=z4} vDb = {x=x4, y=vDb.y, z=z4} end --The four "front" vertices vAf = {x=-0.5, y=1, z=5} vBf = {x=-0.5, y=2, z=5} vCf = {x=0.5, y=2, z=5} vDf = {x=0.5, y=1, z=5} --The four "back" vertices vAb = {x=-0.5, y=1, z=6} vBb = {x=-0.5, y=2, z=6} vCb = {x=0.5, y=2, z=6} vDb = {x=0.5, y=1, z=6} -- vCb, vBb, vAb ABC -- vCb, vAb, vDb ACD -- vCf, vCb, vDb EAD -- vCf, vDb, vDf EDH -- vBf, vCf, vDf FEH -- vBf, vDf, vAf FHG -- vBb, vBf, vAf BFG -- vBb, vAf, vAb BGC -- vCf, vBf, vBb EFB -- vCf, vBb, bCb EBA -- vAb, vAf, vDf CGH -- vAb, vDf, vDb CHD count = 0 shade = 1 function TIC() cls() if keyp(48) then shade = 1 - shade end Rotate(count) count = count + 1 -- The front face DrawTri(vCb, vBb, vAb, 1, true) DrawTri(vCb, vAb, vDb, 1) DrawTri(vCf, vCb, vDb, 2, true) DrawTri(vCf, vDb, vDf, 2) DrawTri(vBf, vCf, vDf, 3, true) DrawTri(vBf, vDf, vAf, 3) DrawTri(vBb, vBf, vAf, 4, true) DrawTri(vBb, vAf, vAb, 4) DrawTri(vCf, vBf, vBb, 5, true) DrawTri(vCf, vBb, vCb, 5) DrawTri(vAb, vAf, vDf, 6, true) DrawTri(vAb, vDf, vDb, 6) -- The front face --DrawLine(vAf, vBf, 12) --DrawLine(vBf, vCf, 12) --DrawLine(vCf, vDf, 12) --DrawLine(vDf, vAf, 12) -- The back face --DrawLine(vAb, vBb, 2) --DrawLine(vBb, vCb, 2) --DrawLine(vCb, vDb, 2) --DrawLine(vDb, vAb, 2) -- The front-to-back edges --DrawLine(vAf, vAb, 6) --DrawLine(vBf, vBb, 6) --DrawLine(vCf, vCb, 6) --DrawLine(vDf, vDb, 6) end