Advertisement
9551

3d testing V5

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