Guest User

Untitled

a guest
Oct 10th, 2014
6
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 9.50 KB | None | 0 0
  1. _G.Imported_Model=nil
  2. repeat wait() until _G.Imported_Model
  3. local loadspeed=_G.Imported_Model.loadspeed
  4. local Scale=_G.Imported_Model.Scale
  5. local wrongAngleFixer=_G.Imported_Model.WrongAngleFixer
  6. local loadColors=_G.Imported_Model.loadColors
  7. local Offset=_G.Imported_Model.Offset
  8. local objFile=_G.Imported_Model.Mesh
  9. local mtlFile=_G.Imported_Model.Materials
  10.  
  11. local shouldrenderVertexes=false
  12. local shouldrenderWireFrame=false
  13. local shouldrenderFaces=true
  14. local Vertexes
  15. local Materials
  16. local Faces
  17. local PolyDraw={}
  18.  
  19. function parseMaterials()
  20.     Materials={}
  21.     local FilePos=0
  22.     local EndFile=false
  23.     while not EndFile do
  24.         EndFile=true
  25.         local MaterialPos=string.find(mtlFile,"newmtl",FilePos)
  26.         if MaterialPos then
  27.             local MaterialNameStartPos=string.find(mtlFile,"%s",MaterialPos+1)
  28.             local MaterialNameEndPos=string.find(mtlFile,"%s",MaterialNameStartPos+1)
  29.             local MaterialName=string.sub(mtlFile,MaterialNameStartPos+1,MaterialNameEndPos-1)
  30.             local ColorPos=string.find(mtlFile,"Kd",MaterialNameEndPos)
  31.             local ColorStartPos=string.find(mtlFile,"%s",ColorPos)
  32.             local ColorEndPos=string.find(mtlFile,"Ks",ColorStartPos)
  33.             local Color=string.sub(mtlFile,ColorStartPos+1,ColorEndPos-2)
  34.             local Color1EndPos=string.find(Color," ")
  35.             local Color1=string.sub(Color,0,Color1EndPos-1)
  36.             local Color2EndPos= string.find(Color," ",Color1EndPos+1)
  37.             local Color2=string.sub(Color,Color1EndPos+1,Color2EndPos-1)
  38.             local Color_3=string.sub(Color,Color2EndPos+1)
  39.             local EndColor=Color3.new(tonumber(Color1),tonumber(Color2),tonumber(Color_3))
  40.             Materials[MaterialName]=EndColor
  41.             FilePos=ColorEndPos
  42.         else
  43.             EndFile=true
  44.             break
  45.         end
  46.     end
  47. end
  48.  
  49. function parseVertexes()
  50.     Vertexes={}
  51.     local FilePos=0
  52.     local EndFile=false
  53.     while not EndFile do
  54.         local VertexPos=string.find(objFile,"v ",FilePos)
  55.         if VertexPos then
  56.             local StartPos=string.find(objFile,"%S",VertexPos+1)
  57.             local FirstCordPos=string.find(objFile," ",StartPos)
  58.             local FirstCord=string.sub(objFile,VertexPos+1,FirstCordPos-1)
  59.             local SecondCordPos=string.find(objFile," ",FirstCordPos+1)
  60.             local SecondCord=string.sub(objFile,FirstCordPos+1,SecondCordPos-1)
  61.             local ThirdCordPos=string.find(objFile,"%s",SecondCordPos+1)
  62.             local ThirdCord=string.sub(objFile,SecondCordPos+1,ThirdCordPos-1)
  63.             local FilePos=ThirdCordPos
  64.             local Vertex=Vector3.new(tonumber(FirstCord)*Scale,tonumber(SecondCord)*Scale,tonumber(ThirdCord)*Scale)+Offset*Scale
  65.             if wrongAngleFixer then
  66.                 Vertex=Vector3.new(tonumber(FirstCord)*Scale,tonumber(ThirdCord)*Scale,tonumber(SecondCord)*Scale)+Offset*Scale
  67.             end
  68.             table.insert(Vertexes,Vertex)
  69.         else
  70.             print("Number of Vertices: "..#Vertexes)
  71.             EndFile=true
  72.         end
  73.     end
  74. end
  75.  
  76. local UseColor
  77.  
  78. function parseFaces()
  79.     Faces={}
  80.     local FilePos=0
  81.     local EndFile=false
  82.     while not EndFile do
  83.         local FaceStringPos=string.find(objFile,"f",FilePos)
  84.         if loadColors then
  85.             local UseMaterial=string.find(objFile,"usemtl",FilePos)
  86.             if UseMaterial and UseMaterial<FaceStringPos then
  87.                 local MaterialNameStartPos=string.find(objFile,"%s",UseMaterial)
  88.                 local MaterialNameEndPos=string.find(objFile,"%s",MaterialNameStartPos+1)
  89.                 local MaterialName=string.sub(objFile,MaterialNameStartPos+1,MaterialNameEndPos-1)
  90.                 UseColor=Materials[MaterialName]
  91.             end
  92.         end
  93.         if FaceStringPos then
  94.             local Face2StartPos=string.find(objFile," ",FaceStringPos+2)
  95.             local Face3StartPos=string.find(objFile," ",Face2StartPos+1)
  96.             local FacePos1String=objFile:match("%d+",FaceStringPos)
  97.             local FacePos2String=objFile:match("%d+",Face2StartPos)
  98.             local FacePos3String=objFile:match("%d+",Face3StartPos)
  99.             local Face1Pos=Vertexes[tonumber(FacePos1String)]
  100.             local Face2Pos=Vertexes[tonumber(FacePos2String)]
  101.             local Face3Pos=Vertexes[tonumber(FacePos3String)]
  102.  
  103.             table.insert(Faces,{Face1Pos,Face2Pos,Face3Pos,UseColor})
  104.  
  105.             FilePos=Face3StartPos+1
  106.         else
  107.             print("Number of Faces: "..#Faces)
  108.             EndFile=true
  109.         end
  110.     end
  111. end
  112.  
  113. function renderVertexes()
  114.     local speedLoad=0
  115.     for k,v in pairs(Vertexes) do
  116.         wait()
  117.         if speedLoad>loadspeed then
  118.             wait()
  119.             speedLoad=0
  120.         end
  121.         speedLoad=speedLoad+1
  122.         local VertexPart=Instance.new("Part",workspace)
  123.         VertexPart.Size=Vector3.new(1,1,1)
  124.         VertexPart.CFrame=CFrame.new(v.X,v.Y,v.Z)
  125.         VertexPart.Name=k
  126.         VertexPart.Anchored=true
  127.         VertexPart.BrickColor=BrickColor.new("Really red")
  128.     end
  129. end
  130.  
  131. function renderVertexesOfFace(Face)
  132.     for i,v in pairs(Face) do
  133.         wait()
  134.         local VertexPart=Instance.new("Part",workspace)
  135.         VertexPart.Size=Vector3.new(1,1,1)
  136.         VertexPart.CFrame=CFrame.new(v.X,v.Y,v.Z)
  137.         VertexPart.Anchored=true
  138.         VertexPart.BrickColor=BrickColor.new("Really red")
  139.     end
  140. end
  141. local FacesRendered
  142. function renderFace(Face)
  143.     FacesRendered=FacesRendered+1
  144.     PolyDraw.Tri.new(Face[1],Face[2],Face[3],Face[4])
  145. end
  146.  
  147. function createFaceInfoHint()
  148.     coroutine.wrap(function()
  149.         local Hint=Instance.new("Hint",workspace)
  150.         local StartTime=tick()
  151.         while wait() do
  152.             Hint.Text="" --Damn you OCD
  153.             .."Faces Rendered: "
  154.             ..FacesRendered
  155.             .."    Percent done: "
  156.             ..math.floor((FacesRendered/#Faces*10000)/100)
  157.             .."%"
  158.             .."    Approximate time left: "..math.floor((((tick()-StartTime)/FacesRendered)*(#Faces-FacesRendered))*10)/10
  159.             .." seconds"
  160.         end
  161.     end)()
  162. end
  163.  
  164. function renderFaces()
  165.     local speedLoad=0
  166.     FacesRendered=0
  167.     createFaceInfoHint()
  168.     for i,v in pairs(Faces) do
  169.         wait()
  170.         if speedLoad>loadspeed then
  171.             wait()
  172.             speedLoad=0
  173.         end
  174.         speedLoad=speedLoad+1
  175.         renderFace(v)
  176.     end
  177.     print("Render finished")
  178. end
  179.  
  180. function renderWireFrame()
  181.     local speedLoad=0
  182.     for i,v in pairs(Faces) do
  183.         wait()
  184.         if speedLoad>loadspeed then
  185.             wait()
  186.             speedLoad=0
  187.         end
  188.         speedLoad=speedLoad+1
  189.         renderWiresForFace(v)
  190.     end
  191. end
  192.  
  193. local Wire=Instance.new("Part")
  194. Wire.Anchored=true
  195. Wire.FormFactor=Enum.FormFactor.Custom
  196. Wire.BrickColor=BrickColor.new("New Yeller")
  197.  
  198. function renderWiresForFace(Face)
  199.     renderWire(Face[1],Face[2])
  200.     renderWire(Face[2],Face[3])
  201.     renderWire(Face[3],Face[1])
  202. end
  203.  
  204. function renderWire(Pos1,Pos2)
  205.     local Distance=(Pos1-Pos2).magnitude
  206.     local wire=Wire:Clone()
  207.     wire.Parent=workspace
  208.     wire.Size=Vector3.new(.2,.2,Distance)
  209.     wire.CFrame=CFrame.new(Pos1,Pos2)*CFrame.new(0,0,-Distance/2)
  210. end
  211.  
  212. local Mesh=Instance.new("Model",workspace)
  213. Mesh.Name="Mesh"
  214.  
  215. PolyDraw.BrickColor=BrickColor.new(21)
  216. PolyDraw.CanCollide=false
  217.  
  218. local function setupPart(Part)
  219.     Part.Anchored=true
  220.     Part.FormFactor="Custom"
  221.     Part.CanCollide=PolyDraw.CanCollide
  222.     Part.BrickColor=PolyDraw.BrickColor
  223.     Part.TopSurface="Smooth"
  224.     Part.BottomSurface="Smooth"
  225.  
  226.     local mesh=Instance.new("SpecialMesh",Part)
  227.     mesh.MeshType="Wedge"
  228.     mesh.Scale=Vector3.new(0,1,1)
  229. end
  230.  
  231. local Point={}
  232. PolyDraw.Point=Point
  233.  
  234. function Point.new(Parent,At)
  235.     local This={}
  236.     local mPart=Instance.new("Part")
  237.     setupPart(mPart)
  238.     mPart.Size=Vector3.new(1,1,1)
  239.     mPart.Parent=Parent
  240.     function This:Set(At)
  241.         mPart.CFrame=CFrame.new(At)
  242.     end
  243.     function This:Destroy()
  244.         mPart:Destroy()
  245.     end
  246.     function This:SetProperty(Prop,Value)
  247.         mPart[Prop]=Value
  248.     end
  249.     This:Set(At)
  250.     return This
  251. end
  252.  
  253. local Edge={}
  254. PolyDraw.Edge=Edge
  255.  
  256. function Edge.new(Parent,A,B)
  257.     local This={}
  258.     local mPart=Instance.new("Part")
  259.     setupPart(mPart)
  260.     mPart.Parent=Parent
  261.     function This:Set(A,B)
  262.         local Sep=(A-B).magnitude
  263.         mPart.Size=Vector3.new(.5,.5,Sep)
  264.         mPart.CFrame=CFrame.new(A,B)*CFrame.new(0,0,-.5*Sep)
  265.     end
  266.     function This:Destroy()
  267.         mPart:Destroy()
  268.     end
  269.     function This:SetProperty(Prop,Value)
  270.         mPart[Prop]=Value
  271.     end
  272.     This:Set(A,B)
  273.     return This
  274. end
  275.  
  276. local Tri={}
  277. PolyDraw.Tri=Tri
  278.  
  279. local function CFrameFromTopBack(At,Top,Back)
  280.     local Right=Top:Cross(Back)
  281.     return CFrame.new(At.X,At.Y,At.Z,Right.X,Top.X,Back.X,Right.Y,Top.Y,Back.Y,Right.Z,Top.Z,Back.Z)
  282. end
  283.  
  284. function Tri.new(A,B,C,Color)
  285.     local This={}
  286.     local mPart1=Instance.new("WedgePart")
  287.     setupPart(mPart1)
  288.     local mPart2=Instance.new("WedgePart")
  289.     setupPart(mPart2)
  290.     function This:Set(A,B,C)
  291.         local AB,BC,CA=B-A,C-B,A-C
  292.         local ABM,BCM,CAM=AB.magnitude,BC.magnitude,CA.magnitude
  293.         local Edge1=math.abs(.5+CA:Dot(AB)/(ABM*ABM))
  294.         local Edge2=math.abs(.5+AB:Dot(BC)/(BCM*BCM))
  295.         local Edge3=math.abs(.5+BC:Dot(CA)/(CAM*CAM))
  296.         if Edge1<Edge2 then
  297.             if Edge1>Edge3 then
  298.                 A,B,C=C,A,B
  299.                 AB,BC,CA=CA,AB,BC
  300.                 ABM=CAM
  301.             end
  302.         else
  303.             if Edge2<Edge3 then
  304.                 A,B,C=B,C,A
  305.                 AB,BC,CA=BC,CA,AB
  306.                 ABM=BCM
  307.             else
  308.                 A,B,C=C,A,B
  309.                 AB,BC,CA=CA,AB,BC
  310.                 ABM=CAM
  311.             end
  312.         end
  313.  
  314.         local Len1=-CA:Dot(AB)/ABM
  315.         local Len2=ABM-Len1
  316.         local Width=(CA+AB.unit*Len1).magnitude
  317.  
  318.         local maincf=CFrameFromTopBack(A,AB:Cross(BC).unit,-AB.unit)
  319.  
  320.         mPart1.Parent=Mesh
  321.         mPart1.Size=Vector3.new(.2,.2,.2)
  322.         mPart1.Mesh.Scale=Vector3.new(0.00001,Width,Len1)/.2
  323.         mPart1.CFrame=maincf*CFrame.Angles(math.pi,0,math.pi/2)*CFrame.new(0,Width/2,Len1/2)
  324.         mPart1.Color=Color or Color3.new(1,1,1)
  325.         mPart2.Parent=Mesh
  326.         mPart2.Size=Vector3.new(.2,.2,.2)
  327.         mPart2.Mesh.Scale=Vector3.new(0.00001,Width,Len2)/.2
  328.         mPart2.CFrame=maincf*CFrame.Angles(math.pi,math.pi,-math.pi/2)*CFrame.new(0,Width/2,-Len1-Len2/2)
  329.         mPart2.Color=Color or Color3.new(1,1,1)
  330.     end
  331.     function This:SetProperty(Prop,Value)
  332.         mPart1[Prop]=Value
  333.         mPart2[Prop]=Value
  334.     end
  335.     This:Set(A,B,C)
  336.     function This:Destroy()
  337.         mPart1:Destroy()
  338.         mPart2:Destroy()
  339.     end
  340.     return This
  341. end
  342.  
  343.  
  344. if loadColors then
  345.     parseMaterials()
  346. end
  347. parseVertexes()
  348. parseFaces()
  349.  
  350. if shouldrenderVertexes then
  351.     renderVertexes()
  352. end
  353. if shouldrenderWireFrame then
  354.     renderWireFrame()
  355. end
  356. if shouldrenderFaces then
  357.     renderFaces()
  358. end
Advertisement
Add Comment
Please, Sign In to add comment