Advertisement
9551

3d testing V3

Oct 7th, 2021
62
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 8.51 KB | None | 0 0
  1. local pretty = require("cc.pretty")
  2. local plethoraMode = false
  3. local c
  4. local width,height
  5. if not plethoraMode then
  6.     local ot = term
  7.     term = peripheral.find("monitor")
  8.     term.setTextScale(0.5)
  9.     term.setCursorPos(10,10)
  10.     width,height = term.getSize()
  11. else
  12.     c = peripheral.wrap("back").canvas()
  13.     width,height = c.getSize()
  14. end
  15. local objList = {}
  16. local function makeProjection(w,h,n,f)
  17.     --w = width
  18.     --h = height
  19.     --n = near plane
  20.     --f = far plane
  21.     return {
  22.         {2*n/w,0,0,0},
  23.         {0,2*n/h, 0,0},
  24.         {0,0,f/(f-n),-n*f/(f-n)},
  25.         {0,0,-1,0}
  26.     }
  27. end
  28. local makeIdentity = function()
  29.     return {
  30.         {1,0,0,0},
  31.         {0,1,0,0},
  32.         {0,0,1,0},
  33.         {0,0,0,1}
  34.     }
  35. end
  36. local makeTranslation = function(cords)
  37.     return {
  38.         {1,0,0,cords.x},
  39.         {0,1,0,cords.y},
  40.         {0,0,1,cords.z},
  41.         {0,0,0,1}
  42.     }
  43. end
  44. local function makeScale(scale)
  45.     return
  46.     {
  47.         {scale.x, 0, 0, 0},
  48.         {0, scale.y, 0, 0},
  49.         {0, 0, scale.z, 0},
  50.         {0, 0, 0, 1}
  51.     }        
  52. end
  53. local function makeRotation(eulers)
  54.     local x = math.rad(eulers.x)
  55.     local y = math.rad(eulers.y)
  56.     local z = math.rad(eulers.z)
  57.     local sx = math.sin(x)
  58.     local sy = math.sin(y)
  59.     local sz = math.sin(z)
  60.    
  61.     local cx = math.cos(x)
  62.     local cy = math.cos(y)
  63.     local cz = math.cos(z)
  64.     return
  65.     {
  66.         {cy * cz, -cy * sz, sy, 0},
  67.         {(sx * sy * cz) + (cx * sz), (-sx * sy * sz) + (cx * cz), -sx * cy, 0},
  68.         {(-cx * sy * cz) + (sx * sz), (cx * sy * sz) + (sx * cz), cx * cy, 0},
  69.         {0, 0, 0, 1,}
  70.     }
  71. end
  72. --[[local function makePerspective(f,n,fov)
  73.     local tanFov = math.tan(
  74.         fov/2*math.pi/180
  75.     )
  76.     local S = 1/tanFov
  77.     return {
  78.         {S,0,0,0},
  79.         {0,S,0,0},
  80.         {0,0,-(f/(f-n)),-(f*n/(f-n))},
  81.         {0,0,-1,0}
  82.     }
  83. end]]
  84. local function makePerspective(width, height, n, f, fov)
  85.     local aspectRatio = height / width
  86.     fov = math.rad(fov)
  87.     return
  88.     {
  89.         {aspectRatio/math.tan(fov*0.5),0,0,0},
  90.         {0,1/(math.tan(fov*0.5)),0,0},
  91.         {0,0,-f/(f-n),-f*n/(f-n)},
  92.         {0,0,-1,0}
  93.     }
  94. end
  95. local function matmul(m1, m2)
  96.     if #m1[1] ~= #m2 then
  97.         error("Columns m1 must match rows m2",2)
  98.     end
  99.     local result = {}
  100.     for i = 1, #m1 do
  101.         result[i] = {}
  102.         for j = 1, #m2[1] do
  103.             local sum = 0
  104.             for k = 1, #m2 do sum = sum + (m1[i][k] * m2[k][j]) end
  105.             result[i][j] = sum
  106.         end
  107.     end
  108.     return result
  109. end
  110. local function drawLine(startX, startY, endX, endY, color)
  111.     local sc = term.getBackgroundColor()
  112.     term.setBackgroundColor(color)
  113.     local drawPixelInternal = function(x,y) term.setCursorPos(x,y) term.write(" ") end
  114.     local startX,startY,endX,endY = math.floor(startX),math.floor(startY),math.floor(endX),math.floor(endY)
  115.     if startX == endX and startY == endY then drawPixelInternal(startX, startY) term.setBackgroundColor(sc) return end
  116.     local minX = math.min(startX, endX)
  117.     local maxX, minY, maxY
  118.     if minX == startX then minY = startY maxX = endX maxY = endY
  119.     else minY = endY maxX = startX maxY = startY end
  120.     local xDiff,yDiff = maxX - minX,maxY - minY
  121.     if xDiff > math.abs(yDiff) then
  122.         local y,dy = minY,yDiff / xDiff
  123.         for x = minX, maxX do drawPixelInternal(x, math.floor(y + 0.5)) y = y + dy end
  124.     else
  125.         local x,dx = minX,xDiff / yDiff
  126.         if maxY >= minY then for y = minY, maxY do drawPixelInternal(math.floor(x + 0.5), y) x = x + dx end
  127.         else for y = minY, maxY, -1 do drawPixelInternal(math.floor(x + 0.5), y) x = x - dx end end
  128.     end
  129.     term.setBackgroundColor(sc)
  130. end
  131. local function newFrame()
  132.     local objData = {
  133.         scale = vector.new(1,1,1),
  134.         loc = vector.new(0,0,0),
  135.         rot = vector.new(0,0,0),
  136.         colorList = {colors.red},
  137.         indexList = {1},
  138.         vertices = {
  139.             {{-0.5}, {-0.5}, {-0.5}, {1}},
  140.             {{0.5}, {-0.5}, {-0.5}, {1}},
  141.             {{-0.5},  {0.5}, {-0.5}, {1}},
  142.             {{0.5},  {0.5}, {-0.5}, {1}}
  143.         },
  144.         connections = {
  145.             {1,2},
  146.             {2,4},
  147.             {3,4},
  148.             {1,3}
  149.         }
  150.     }
  151.     for i,val in ipairs(objData.indexList) do
  152.         objData.indexList[i] = val*4-3
  153.     end
  154.     return objData
  155. end
  156. local function newBox()
  157.     local objData = {
  158.         scale = vector.new(1,1,1),
  159.         loc = vector.new(0,0,0),
  160.         rot = vector.new(0,0,0),
  161.         colorList = {colors.red},
  162.         indexList = {1},
  163.         vertices = {
  164.             {{-0.5}, {-0.5}, {-0.5}, {1}},
  165.             {{0.5}, {-0.5}, {-0.5}, {1}},
  166.             {{-0.5},  {0.5}, {-0.5}, {1}},
  167.             {{0.5},  {0.5}, {-0.5}, {1}},
  168.            
  169.             {{-0.5}, {-0.5}, {0.5}, {1}},
  170.             {{0.5}, {-0.5}, {0.5}, {1}},
  171.             {{-0.5},  {0.5}, {0.5}, {1}},
  172.             {{0.5},  {0.5}, {0.5}, {1}}
  173.         },
  174.         connections = {{1,2},{2,4},{3,4},{1,3},{1,5},{2,6},{3,7},{4,8},{5,7},{6,8},{5,6},{8,7}}
  175.     }
  176.     for i,val in ipairs(objData.indexList) do
  177.         objData.indexList[i] = val*4-3
  178.     end
  179.     return objData
  180. end
  181. local function newPyramid()
  182.     local objData = {
  183.         scale = vector.new(1,1,1),
  184.         loc = vector.new(0,0,0),
  185.         rot = vector.new(0,0,0),
  186.         colorList = {colors.red},
  187.         indexList = {1},
  188.         vertices = {
  189.             {{-0.5}, {-0.5}, {-0.5}, {1}},
  190.             {{0.5}, {-0.5}, {-0.5}, {1}},
  191.             {{-0.5}, {-0.5}, {0.5}, {1}},
  192.             {{0.5}, {-0.5}, {0.5}, {1}},
  193.             {{0}, {0.5}, {0}, {1}}
  194.         },
  195.         connections = {{1,2},{1,3},{3,4},{4,2},{1,5},{2,5},{3,5},{4,5},}
  196.     }
  197.     for i,val in ipairs(objData.indexList) do
  198.         objData.indexList[i] = val*4-3
  199.     end
  200.     return objData
  201. end
  202. local proj = {}
  203. local rot = 0
  204. local xFactor = height*0.5
  205. local yFactor = width*0.5
  206. local objList = {
  207.     frame=newPyramid()
  208. }
  209. objList.frame.loc.x = 0
  210. objList.frame.loc.y = 0
  211. objList.frame.loc.z = -20
  212. objList.frame.scale.x = 7
  213. objList.frame.scale.y = 7
  214. objList.frame.scale.z = 7
  215. local frames = 1000
  216. local st = os.epoch("utc")/1000
  217. for i=1,frames do
  218.     --objList.frame.rot.x = rot
  219.     objList.frame.rot.y = rot
  220.     if not plethoraMode then
  221.         term.clear()
  222.     else
  223.         c.clear()
  224.     end
  225.     --[[local cam = matmul(
  226.         transMat, matmul(
  227.             rotMat, matmul(
  228.                 scale, objectData.vertices
  229.             )
  230.         )
  231.     )--]]
  232.     local objectsInt = {}
  233.     local objectsInt = setmetatable({},
  234.         {
  235.             __index=function(t,k)
  236.                 local new = {}
  237.                 t[k]=new
  238.                 return new
  239.             end
  240.         }
  241.     )
  242.     local cameraRot = makeRotation(-vector.new(0,0,0))
  243.     local cameraLoc = makeTranslation(-vector.new(0,0,-0.5))
  244.     --local projMat = makeProjection(width,height,10,50)
  245.     --local projMat = makePerspective(10,100,100)
  246.     local projMat = makePerspective(width,height,100,10,40)
  247.     for k,v in pairs(objList) do
  248.         objectsInt[k] = {main=v,vectors={}}
  249.         local scale = makeScale(v.scale)
  250.         local rot = makeRotation(v.rot)
  251.         local loc = makeTranslation(v.loc)
  252.         for k1,v1 in pairs(v.vertices) do
  253.             local model = matmul(loc, matmul(rot, matmul(scale, v1)))
  254.             local cam = matmul(cameraRot, matmul(cameraLoc, model))
  255.             local projected = matmul(projMat ,cam)
  256.             local w = 1/projected[4][1]
  257.             projected[1][1] = (projected[1][1] * w  +1) * (width * 0.5 )
  258.             projected[2][1] = (-projected[2][1] * w +1) * (height * 0.5 )
  259.             table.insert(objectsInt[k].vectors,projected)
  260.         end
  261.     end
  262.     for k,vm in pairs(objectsInt) do
  263.         for k,v in pairs(vm.main.connections) do
  264.             if not plethoraMode then
  265.                 drawLine(vm.vectors[v[1]][1][1],vm.vectors[v[1]][2][1],vm.vectors[v[2]][1][1],vm.vectors[v[2]][2][1],colors.red)
  266.             else
  267.                 c.addLine({vm.vectors[v[1]][1][1],vm.vectors[v[1]][2][1]},{vm.vectors[v[2]][1][1],vm.vectors[v[2]][2][1]},0xFF0000,3)
  268.             end
  269.         end
  270.         term.setBackgroundColor(colors.white)
  271.         for _,vector in pairs(vm.vectors) do
  272.             if not plethoraMode then
  273.                 term.setCursorPos(vector[1][1],vector[2][1])
  274.                 term.write(" ")
  275.             else
  276.                 c.addDot({vector[1][1],vector[2][1]},5)
  277.             end
  278.         end
  279.         term.setBackgroundColor(colors.black)
  280.     end
  281.     rot = rot + 5
  282.     sleep(0.05)
  283. end
  284. local et = os.epoch("utc")/1000
  285. local fps = frames/(et-st)
  286. print(fps)
  287.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement