9551

3d testing V2

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