Advertisement
9551

temp

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