Advertisement
9551

3d testing V4

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