Advertisement
Xelostar

ThreeD_API_v0.5.1_minified

Jul 22nd, 2018
297
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 12.77 KB | None | 0 0
  1.  
  2. -- Made by Xelostar: https://www.youtube.com/channel/UCDE2STpSWJrIUyKtiYGeWxw
  3.  
  4. local function e(p)return math.floor(p+0.5)end
  5. local function t(p,v,b,g,k,q,j)local x={}
  6. for z=1,#p do local _=p[z]local E=
  7. (math.min(
  8. _[1]+v,_[4]+v,_[7]+v)+math.max(_[1]+v,_[4]+v,_[7]+v))*0.5;local T=
  9. (math.min(
  10. _[2]+b,_[5]+b,_[8]+b)+math.max(_[2]+b,_[5]+b,_[8]+b))*0.5;local A=
  11. (math.min(
  12. _[3]+g,_[6]+g,_[9]+g)+math.max(_[3]+g,_[6]+g,_[9]+g))*0.5;local O=k-
  13. E;local I=q-T;local N=j-A
  14. local S=math.pow(O*O+I*I+N*N,0.5)_[14]=S;x[#x+1]=_ end
  15. table.sort(x,function(z,_)return z[14]>_[14]end)return x end
  16. local function a(p,v)local b=fs.open(v.."/"..p,"r")content=b.readAll()
  17. b.close()local g=textutils.unserialise(content)return g end
  18. local function o(p,v,b,g)local p=p+0.0000001;local v=v+0.0000001;local b=b+0.0000001
  19. local k=math.deg(math.atan(p/b))
  20. if(b<0)then if(p<0)then k=k-180 else k=k+180 end else k=k end;local q=k+g;if(q>180)then while(q>180)do q=q-360 end elseif(q<-180)then while(q<-180)do
  21. q=q+360 end end;local j=p/
  22. math.sin(math.rad(k))
  23. local p=j*math.sin(math.rad(q))local b=j*math.cos(math.rad(q))return p,v,b end
  24. local function i(p,v)
  25. if(v~=0)then local b={}
  26. for g,k in pairs(p)do local q,j,x=o(k[1],k[2],k[3],v)
  27. local z,_,E=o(k[4],k[5],k[6],v)local T,A,O=o(k[7],k[8],k[9],v)b[#b+1]={q,j,x,z,_,E,T,A,O}
  28. b[#b][10]=k[10]b[#b][11]=k[11]b[#b][12]=k[12]b[#b][13]=k[13]end;return b else return p end end
  29. local function n(p,v,b,g,k)local q={}
  30. for j=1,#p do local x=p[j]local z=v-x.x;local _=b-x.y;local E=g-x.z
  31. local T=math.pow(z*z+_*_+E*E,0.5)if(T<=k)then x.distance=T;q[#q+1]=x end end
  32. table.sort(q,function(j,x)return j.distance>x.distance end)return q end;local function s(p)local v={x=-p.y,y=p.x}return v end;local function h(p,v)return
  33. (p.x*v.x+p.y*v.y)end
  34. local function r(p,v,b)local g={}g.x=v.x-p.x;g.y=v.y-p.y
  35. local k={}k.x=b.x-v.x;k.y=b.y-v.y
  36. if(h(s(g),k)<0)then return true end;return false end;local function d(p,v,b,g)local k=g-v;local q=b-p;local j=math.pow(10,99)if(q~=0)then j=k/q end
  37. local x=v-j*p;return j,x end
  38. local function l(p,v,b,g,k,q,j,x,z,_)
  39. local E,T=d(b,g,k,q)
  40. if(k>=b)then local A=b>1 and b or 1
  41. for O=A,k do if(O==p)then local I=E*O+T
  42. if(I==v)then return true end end;if(O>z-j+6)then break end end else local A=k>1 and k or 1
  43. for O=A,b do
  44. if(O==p)then local I=E*O+T;if(I==v)then return true end end;if(O>z-j+6)then break end end end
  45. if(q>=g)then local A=g>1 and g or 1
  46. for O=A,q do if(O==v)then local I=(O-T)/E
  47. if(I==p)then return true end end;if(O>_-x+6)then break end end else local A=q>1 and q or 1
  48. for O=A,g do if(O==v)then local I=(O-T)/E
  49. if(I==p)then return true end end;if(O>_-x+6)then break end end end;return false end
  50. local function u(p,v,b,g,k,q,j,x,z,_,E,T)if(j<0)then j=0 elseif(j>T-_+2)then j=T-_+2 end;if(x<0)then x=0 elseif
  51. (x>T-_+2)then x=T-_+2 end
  52. for A=j,x do
  53. if(A==v)then local O=A
  54. if(A~=j and A~=x)then O=e(A)end;local I=e((e(O-0.5)-g)/b)
  55. local N=e((e(O-0.5)-q)/k)if(I<0)then I=0 elseif(I>E-z+2)then I=E-z+2 end;if(N<0)then N=0 elseif
  56. (N>E-z+2)then N=E-z+2 end
  57. if(I<N)then
  58. for S=I,N do if(S==p)then return true end end else for S=N,I do if(S==p)then return true end end end end end;return false end;local function c(p,v,b)return
  59. (p.x-b.x)* (v.y-b.y)- (v.x-b.x)* (p.y-b.y)end
  60. local function m(p,v,b,g,k,q,j,x,z,_,E,T,A)
  61. if(not A)then
  62. local O,I=d(b,g,k,q)local N,S=d(k,q,j,x)local H,R=d(b,g,j,x)
  63. if(g<=q and g<=x)then
  64. if(q<=x)then if(O~=0)then if
  65. (u(p,v,O,I,H,R,g,q,z,_,E,T))then return true end end;if(N~=0)then if
  66. (u(p,v,N,S,H,R,q,x,z,_,E,T))then return true end end else if(H~=0)then if
  67. (u(p,v,O,I,H,R,g,x,z,_,E,T))then return true end end;if(N~=0)then if
  68. (u(p,v,O,I,N,S,x,q,z,_,E,T))then return true end end end elseif(q<=g and q<=x)then
  69. if(g<=x)then if(O~=0)then
  70. if(u(p,v,O,I,N,S,q,g,z,_,E,T))then return true end end;if(H~=0)then if(u(p,v,N,S,H,R,g,x,z,_,E,T))then
  71. return true end end else if(N~=0)then if
  72. (u(p,v,O,I,N,S,q,x,z,_,E,T))then return true end end;if(H~=0)then if
  73. (u(p,v,O,I,H,R,x,g,z,_,E,T))then return true end end end else
  74. if(g<=q)then if(H~=0)then
  75. if(u(p,v,N,S,H,R,x,g,z,_,E,T))then return true end end;if(O~=0)then if(u(p,v,O,I,N,S,g,q,z,_,E,T))then
  76. return true end end else if(N~=0)then if
  77. (u(p,v,N,S,H,R,x,q,z,_,E,T))then return true end end;if(O~=0)then if
  78. (u(p,v,O,I,H,R,q,g,z,_,E,T))then return true end end end end
  79. if(l(p,v,b,g,k,q,z,_,E,T))then return true elseif(l(p,v,k,q,j,x,z,_,E,T))then return true elseif
  80. (l(p,v,j,x,b,g,z,_,E,T))then return true end;return false else
  81. local O=c({x=p,y=v},{x=b,y=g},{x=k,y=q})<0;local I=c({x=p,y=v},{x=k,y=q},{x=j,y=x})<0;local N=
  82. c({x=p,y=v},{x=j,y=x},{x=b,y=g})<0
  83. return((O==I)and(I==N))end end;local f=math.pi;local w=2*math.pi;local y=0.5*math.pi
  84. function newFrame(p,v,b,g,k,q,j,x,z,_,E,T)if(
  85. p==nil or v==nil or b==nil or g==nil)then
  86. error("When creating a new frame, the size must be specified!")end;local A={}A.modelsDir=T or E or
  87. "/models"A.models={}A.modelsSorted={}
  88. A.saveSortedModelsBool=false;A.frameX1=p;A.frameY1=v;A.frameX2=b;A.frameY2=g
  89. A.buffer=bufferAPI.newBuffer(p,v,b,g)A.buffer:clear(colors.white)A.blittleOn=false
  90. A.pixelratio=1.5;A.renderDistance=math.huge;A.FoV=k or 90;A.d=0.2;A.t=
  91. math.tan(math.rad(A.FoV/2))*2*A.d;A.cameraDirZ=0;if(z)then
  92. A.cameraDirZ=math.rad(z)end;A.cameraDirY=0
  93. if(_)then A.cameraDirY=math.rad(_)end;A.cameraX=q or 0;A.cameraY=j or 0;A.cameraZ=x or 0
  94. A.loadedBackgroundColor=false
  95. function A:setSize(p,v,b,g)self.frameX1=p;self.frameY1=v;self.frameX2=b;self.frameY2=g
  96. if(not
  97. self.blittleOn)then self.buffer:setBufferSize(p,v,b,g)self.width=
  98. b-p+1;self.height=g-v+1;self.pixelratio=1.5 else self.buffer:setBufferSize(
  99. p*2,v*3,b*2,g*3)
  100. self.width=(b-p+1)*2;self.height=(g-v+1)*3;self.pixelratio=1 end end
  101. function A:useBLittle(O)self.blittleOn=O
  102. if(O)then
  103. self.buffer:setBufferSize(
  104. (self.frameX1-1)*2+1,(self.frameY1-1)*3+1,(self.frameX2)*2,(
  105. self.frameY2+1)*3)
  106. self.width=(self.frameX2-self.frameX1+1)*2
  107. self.height=(self.frameY2-self.frameY1+1)*3;self.pixelratio=1 else
  108. self.buffer:setBufferSize(self.frameX1,self.frameY1,self.frameX2,self.frameY2)self.width=self.frameX2-self.frameX1+1;self.height=
  109. self.frameY2-self.frameY1+1
  110. self.pixelratio=1.5 end end
  111. function A:setCamera(q,j,x,_,z,k)self.cameraX=q+0.001;self.cameraY=j+0.001
  112. self.cameraZ=x+0.001
  113. if(_~=nil)then self.cameraDirY=math.rad(_)+0.001 end
  114. if(z~=nil)then self.cameraDirZ=math.rad(z)+0.001 end;if(k~=nil)then self.FoV=k end end;function A:setRenderDistance(O)self.renderDistance=O end
  115. function A:loadModel(O,I)local N={}
  116. for S,H in
  117. pairs(I)do N[#N+1]={}N[#N][1]=H.x1;N[#N][2]=H.y1;N[#N][3]=H.z1
  118. N[#N][4]=H.x2;N[#N][5]=H.y2;N[#N][6]=H.z2;N[#N][7]=H.x3;N[#N][8]=H.y3
  119. N[#N][9]=H.z3;N[#N][10]=H.forceRender;N[#N][11]=H.c;N[#N][12]=H.char
  120. N[#N][13]=H.charc end;self.models[O]=N end
  121. function A:getModel(O)local I=self.models[O]local N={}
  122. for S,H in pairs(I)do
  123. N[S]={x1=H[1],y1=H[2],z1=H[3],x2=H[4],y2=H[5],z2=H[6],x3=H[7],y3=H[8],z3=H[9],forceRender=H[10],c=H[11],char=H[12],charc=H[13]}end;return N end;function A:saveSortedModels(O)self.saveSortedModelsBool=O end
  124. function A:findPointOnScreen(O,I,N)local S=
  125. O-self.cameraX;local H=I-self.cameraY
  126. local R=N-self.cameraZ;local D=math.atan(S/R)
  127. if(R<0)then if(S<0)then D=D-f else D=D+f end end;local L=D+self.cameraDirY
  128. if(L>f)then L=L-w elseif(L<-f)then L=L+w end;local U=S/math.sin(D)local S=U*math.sin(L)
  129. local R=U*math.cos(L)local C=math.atan(H/S)
  130. if(S<0)then if(H<0)then C=C-f else C=C+f end end;local M=C-self.cameraDirZ
  131. if(M>f)then M=M-w elseif(L<-f)then M=M+w end;if(M>=y or M<=-y)then return 0,0,false end
  132. local F=S/math.cos(C)local H=F*math.sin(M)local S=F*math.cos(M)
  133. local W=math.atan(S/R)if(R<0)then if(S<0)then W=W-f else W=W+f end end;local Y=0
  134. if
  135. (W>0 and W<y)then Y=self.d/math.tan(W)elseif(W>=y and W<f)then Y=self.d*-
  136. math.tan(W-y)end;if(W>=f or W<=0)then return 0,0,false end
  137. local P=
  138. Y/self.t*self.width+math.floor(self.width*0.5)+1
  139. local V=-self.d*math.tan(M)*self.width/ (self.t*self.height*
  140. self.pixelratio)*
  141. self.height+
  142. math.floor(self.height*0.5)
  143. if(V~=V)then V=math.floor(self.height*0.5)end;return P,V,true end
  144. function A:loadObject(O)local I=O.x;local N=O.y;local S=O.z
  145. local H,R,D=self:findPointOnScreen(I,N,S)
  146. if(H~=nil)then local L=O.model
  147. if(L=="flip")then local U=O.width;local C=O.height;local p,v=self:findPointOnScreen(I,
  148. N-0.5+C,S)
  149. local b,g=self:findPointOnScreen(I,N-0.5,S)local M=math.abs(g-v)local F=1;if(self.blittleOn)then
  150. F=math.abs(M*U/C)else F=math.abs(M*U/C*1.5)end
  151. M=math.floor(M)F=math.floor(F)local W=O.color
  152. if(p~=nil and v~=nil and b~=nil and
  153. g~=nil)then self.buffer:loadBox(p- (0.5*F),v,b+
  154. (0.5*F),g,W,W," ")end else local U={}if(self.models[L]==nil)then
  155. self:loadModel(L,a(L,self.modelsDir))end;U=self.models[L]
  156. local C=O.rotationY~=nil and
  157. O.rotationY~=0 and i(U,O.rotationY)or U
  158. local M=t(C,I,N,S,self.cameraX,self.cameraY,self.cameraZ)
  159. if(self.saveSortedModelsBool)then
  160. if
  161. (O.rotationY==nil or O.rotationY==0)then if(self.modelsSorted[L]==nil)then self.models[L]=M
  162. self.modelsSorted[L]=true end end end
  163. for F=1,#M do local W=M[F]
  164. local p,v,Y=self:findPointOnScreen(I+W[1],N+W[2],S+W[3])
  165. if(Y)then
  166. local b,g,P=self:findPointOnScreen(I+W[4],N+W[5],S+W[6])
  167. if(P)then
  168. local V,B,G=self:findPointOnScreen(I+W[7],N+W[8],S+W[9])
  169. if(G)then if
  170. (W[10]or r({x=p,y=v},{x=b,y=g},{x=V,y=B}))then
  171. self.buffer:loadTriangle(e(p),e(v),e(b),e(g),e(V),e(B),W[11],W[12],W[13])end end end end end end end end
  172. function A:loadObjects(O)if(not self.loadedBackgroundColor)then
  173. self.buffer:clear(colors.white)end
  174. local I=n(O,self.cameraX,self.cameraY,self.cameraZ,self.renderDistance)self.modelsSorted={}
  175. for N=1,#I do self:loadObject(I[N])end;self.loadedBackgroundColor=false;return I end
  176. function A:drawBuffer()self.buffer:drawBuffer(self.blittleOn)end
  177. function A:loadGround(O,I,N)local S=10
  178. local H=S*math.cos(self.cameraDirY)+self.cameraX
  179. local R=S*math.sin(self.cameraDirY)+self.cameraZ
  180. local D,L=self:findPointOnScreen(H,self.cameraY-0.01,R)if(L<0)then L=0 end;if(L>
  181. (self.frameY2-self.frameY1+2)*3)then L=
  182. (self.frameY2-self.frameY1+2)*3 end
  183. if(not
  184. self.blittleOn)then
  185. self.buffer:loadBox(1,L,self.frameX2-self.frameX1+1,
  186. self.frameY2-self.frameY1+2,N or O,O,I or" ")else
  187. self.buffer:loadBox(1,L,(self.frameX2-self.frameX1+1)*
  188. 2,(self.frameY2-self.frameY1+2)*3,
  189. N or O,O,I or" ")end;self.loadedBackgroundColor=true end
  190. function A:loadSky(O,I,N)local S=10
  191. local H=S*math.cos(self.cameraDirY)+self.cameraX
  192. local R=S*math.sin(self.cameraDirY)+self.cameraZ
  193. local D,L=self:findPointOnScreen(H,self.cameraY-0.01,R)if(L<0)then L=0 end;if(L>
  194. (self.frameY2-self.frameY1+2)*3)then L=
  195. (self.frameY2-self.frameY1+2)*3 end
  196. if(not
  197. self.blittleOn)then
  198. self.buffer:loadBox(1,1,self.frameX2-self.frameX1+1,L,
  199. N or O,O,I or" ")else
  200. self.buffer:loadBox(1,1,(self.frameX2-self.frameX1+1)*
  201. 2,L,N or O,O,I or" ")end;self.loadedBackgroundColor=true end
  202. function A:getObjectIndexTrace(O,I,N,S)local H={}if(self.blittleOn)then I=I*2;N=N*3 end
  203. for W=1,#O do local Y=O[W]
  204. local P={}
  205. if(self.models[Y.model]~=nil)then
  206. P=self.models[Y.model]else P=a(Y.model,self.modelsDir)self.models[Y.model]=P end;local V=
  207. Y.rotationY~=nil and Y.rotationY~=0 and i(P,Y.rotationY)or P
  208. for B=1,#V do
  209. local G=V[B]
  210. local p,v,K=self:findPointOnScreen(Y.x+G[1],Y.y+G[2],Y.z+G[3])
  211. local b,g,Q=self:findPointOnScreen(Y.x+G[4],Y.y+G[5],Y.z+G[6])
  212. local J,X,Z=self:findPointOnScreen(Y.x+G[7],Y.y+G[8],Y.z+G[9])
  213. if
  214. (G[10]or r({x=p,y=v},{x=b,y=g},{x=J,y=X}))then
  215. if(K and Q and Z)then
  216. if(not self.blittleOn)then
  217. if
  218. (m(I,N,e(p),e(v),e(b),e(g),e(J),e(X),self.frameX1,self.frameY1,self.frameX2,self.frameY2,S))then H[#H+1]={objectIndex=W,polygonIndex=B}end else
  219. if
  220. (m(I,N,e(p),e(v),e(b),e(g),e(J),e(X),(self.frameX1-1)*2+1,
  221. (self.frameY1-1)*3+1,(self.frameX2)*2,(self.frameY2+1)*3,S))then H[#H+1]={objectIndex=W,polygonIndex=B}end end end end end end;if(#H<=0)then return end;local R={}local D=-1;local L=math.huge
  222. for W=1,#H do
  223. local Y=O[H[W].objectIndex]local P=self.cameraX-Y.x;local V=self.cameraY-Y.y
  224. local B=self.cameraZ-Y.z;local G=math.pow(P*P+V*V+B*B,0.5)if(G<L)then L=G
  225. D=H[W].objectIndex end end
  226. for W=1,#H do local Y=O[H[W].objectIndex]local P=self.cameraX-Y.x;local V=self.cameraY-
  227. Y.y;local B=self.cameraZ-Y.z
  228. local G=math.pow(P*P+V*V+B*B,0.5)if(G==L)then R[#R+1]=H[W].polygonIndex end end;local U=O[D]local C={}
  229. if(self.models[U.model]~=nil)then
  230. C=self.models[U.model]else C=a(U.model,self.modelsDir)self.models[U.model]=C end;local M=-1;local F=math.huge
  231. for W=1,#R do local Y=C[R[W]]
  232. local P=
  233. (
  234. math.min(unpack({Y[1]+U.x,Y[4]+U.x,Y[7]+U.x}))+
  235. math.max(unpack({Y[1]+U.x,Y[4]+U.x,Y[7]+U.x})))/2
  236. local V=
  237. (
  238. math.min(unpack({Y[2]+U.y,Y[5]+U.y,Y[8]+U.y}))+
  239. math.max(unpack({Y[2]+U.y,Y[5]+U.y,Y[8]+U.y})))/2
  240. local B=
  241. (
  242. math.min(unpack({Y[3]+U.z,Y[6]+U.z,Y[9]+U.z}))+
  243. math.max(unpack({Y[3]+U.z,Y[6]+U.z,Y[9]+U.z})))/2;local G=self.cameraX-P;local K=self.cameraY-V
  244. local Q=self.cameraZ-B;local J=math.pow(G*G+K*K+Q*Q,0.5)
  245. if(J<F)then F=J;M=R[W]end end;return D,M end;return A end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement