Advertisement
9551

3d testing V6

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