Advertisement
9551

Untitled

Aug 26th, 2021
55
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.03 KB | None | 0 0
  1. local engine
  2. local per = peripheral.wrap("back")
  3. local function getPlayerData()
  4. local meta = per.getMetaOwner()
  5. local x,y,z = gps.locate()
  6. local playerData = {
  7. rot = vector.new(meta.pitch, meta.yaw, 0),
  8. loc = vector.new(x,y,z)
  9. }
  10. return playerData
  11. end
  12. local function multiplyVerts(mat, vert)
  13. if #mat ~= 16 then
  14. error("Matrix must have length of 16 (4x4 matrix)")
  15. end
  16. local result = {}
  17. for i = 1, #vert, 4 do
  18. for k = 1, 4 do
  19. result[i+k-1] = ( mat[k*4-3] * vert[i] ) + ( mat[k*4-2] * vert[i+1] ) + ( mat[k*4-1] * vert[i+2] ) + ( mat[k*4] * vert[i+3] )
  20. end
  21. end
  22. return result
  23. end
  24.  
  25. local function getVecNeg(vec)
  26. return {
  27. x = -vec.x,
  28. y = -vec.y,
  29. z = -vec.z,
  30. }
  31. end
  32. local function makeView(position, rotationEulers)
  33. return multiply(makeRotation(rotationEulers), makeTranslation(position))
  34. end
  35.  
  36. local function makeIdentity()
  37. return
  38. {
  39. 1, 0, 0, 0,
  40. 0, 1, 0, 0,
  41. 0, 0, 1, 0,
  42. 0, 0, 0, 1, }
  43. end
  44.  
  45. local function makePerspective(width, height, n, f, fov) --n = near, f = far
  46. local aspectRatio = height / width
  47. fov = math.rad(fov)
  48. local tFov = math.tan(fov*0.5)
  49. return
  50. {
  51. 1/(aspectRatio*tFov), 0, 0, 0,
  52. 0, 1/tFov, 0, 0,
  53. 0, 0, -(f+n) / (f-n), -(2*f*n) / (f-n),
  54. 0, 0, -1, 0 }
  55. end
  56.  
  57. local function makeCameraMat(A,B,C,P)
  58. return {
  59. A.x
  60. }
  61. end
  62.  
  63. local function makeRotation(eulers)
  64. local x = math.rad(eulers.x)
  65. local y = math.rad(eulers.y)
  66. local z = math.rad(eulers.z)
  67.  
  68. local sx = math.sin(x)
  69. local sy = math.sin(y)
  70. local sz = math.sin(z)
  71.  
  72. local cx = math.cos(x)
  73. local cy = math.cos(y)
  74. local cz = math.cos(z)
  75.  
  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. end
  83.  
  84. local function makeTranslation(translation)
  85. return
  86. {
  87. 1, 0, 0, translation.x,
  88. 0, 1, 0, translation.y,
  89. 0, 0, 1, translation.z,
  90. 0, 0, 0, 1, }
  91. end
  92.  
  93. local function makeScale(scale)
  94. return
  95. {
  96. scale.x, 0, 0, 0,
  97. 0, scale.y, 0, 0,
  98. 0, 0, scale.z, 0,
  99. 0, 0, 0, 1 }
  100. end
  101. local function makeCamera()
  102. return {
  103. loc = vector.new(0,0,0),
  104. rot = vector.new(0,0,0),
  105. }
  106. end
  107. local function getTex(fileName)
  108. local f, err = fs.open(fileName, "r")
  109. assert(f, err)
  110. local fileData = textutils.unserialize(f.readAll())
  111. f.close()
  112. return fileData
  113. end
  114.  
  115. local function newCube()
  116. local objData = {
  117. --Matrix values
  118. scale = vector.new(1,1,1),
  119. loc = vector.new(0,0,0),
  120. rot = vector.new(0,0,0),
  121. colorList = {
  122. {186, 31, 51}, {205, 93, 103},
  123. {242, 166, 90}, {238, 193, 112},
  124.  
  125. {70, 177, 201}, {132, 192, 198},
  126. {191, 172, 181}, {229, 208, 204},
  127.  
  128. {245, 100, 169}, {250, 164, 189},
  129. {140, 215, 144}, {170, 252, 184},
  130. },
  131. uvList = {
  132. 0,0,
  133. 1,0,
  134. 0,1,
  135. 1,1,
  136.  
  137. 1,0,
  138. 0,0,
  139. 1,1,
  140. 0,1,
  141. },
  142. texData = nil,
  143. uvIL = {
  144.  
  145. },
  146. indexList = {
  147. 1,3,2, 3,4,2,
  148. 2,4,6, 4,8,6,
  149.  
  150. 3,7,4, 4,7,8,
  151. 5,6,8, 5,8,7,
  152.  
  153. 1,5,3, 3,5,7,
  154. 1,2,5, 2,6,5, },
  155. vertices = {
  156. -0.5, -0.5, -0.5, 1,
  157. 0.5, -0.5, -0.5, 1,
  158. -0.5, 0.5, -0.5, 1,
  159. 0.5, 0.5, -0.5, 1,
  160.  
  161. -0.5, -0.5, 0.5, 1,
  162. 0.5, -0.5, 0.5, 1,
  163. -0.5, 0.5, 0.5, 1,
  164. 0.5, 0.5, 0.5, 1, }
  165. }
  166. for i,val in ipairs(objData.indexList) do
  167. objData.indexList[i] = val*4-3
  168. objData.uvIL[i] = val*2-1
  169. end
  170. return objData
  171. end
  172. local function newSqr()
  173. local objData = {
  174. scale = vector.new(1,1,1),
  175. loc = vector.new(0,0,0),
  176. rot = vector.new(0,0,0),
  177. colorList = { 0xE56399, 0x7F96FF, },
  178. indexList = { 1,3,2, 3,4,2, },
  179. vertices = {
  180. -0.5, -0.5, -0.5, 1,
  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. for i,val in ipairs(objData.indexList) do
  185. objData.indexList[i] = val*4-3
  186. end
  187. return objData
  188. end
  189. local function screenTransform(objectData,display,camera)
  190. local iL = objectData.indexList
  191. local scale = makeScale(objectData.scale)
  192. local rotMat = makeRotation(objectData.rot)
  193. local transMat = makeTranslation(objectData.loc)
  194. camera.loc.z = -camera.loc.z
  195. camera.loc.y = -camera.loc.y
  196. local camRot = makeRotation( getVecNeg(camera.rot) )
  197. local camLoc = makeTranslation( getVecNeg(camera.loc) )
  198. local result = multiplyVerts(transMat, multiplyVerts(rotMat, multiplyVerts(scale, objectData.vertices)))
  199. result = multiplyVerts(camRot, multiplyVerts(camLoc, result))
  200. result.cullFlags = {}
  201. for i = 1, #iL, 3 do
  202. local i1,i2 = i+1, i+2
  203. local vec1 = vector.new(result[iL[i]], result[iL[i]+1], result[iL[i]+2])
  204. local vec2 = vector.new(result[iL[i1]], result[iL[i1]+1], result[iL[i1]+2])
  205. local vec3 = vector.new(result[iL[i2]], result[iL[i2]+1], result[iL[i2]+2])
  206. result.cullFlags[i] = (vec3:cross(vec2)):dot(vec1)
  207. end
  208. for i = 1,#result, 4 do
  209. local zInv = 1/result[i+2]
  210. result[i] = ((result[i] *zInv +1) * display.x) * (display.y/display.x) + display.x*0.5
  211. result[i+1] = (-result[i+1]*zInv +1) * display.y
  212. end
  213. return result
  214. end
  215. return
  216. {
  217. screenTransform = screenTransform,
  218. newCube = newCube,
  219. newSqr = newSqr,
  220. makeCamera = makeCamera,
  221. getTex = getTex,
  222. getPlayerData = getPlayerData,
  223. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement