Advertisement
Descaii

Polydraw

Sep 13th, 2014
369
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.68 KB | None | 0 0
  1. local setupPart = function(part)
  2.     part.Anchored = true
  3.     part.FormFactor = 'Custom'
  4.     part.CanCollide = true
  5.     part.BrickColor = BrickColor.new()
  6.     part.TopSurface = 'Smooth'
  7.     part.BottomSurface = 'Smooth'
  8.     local m = Instance.new("SpecialMesh",part)
  9.     m.MeshType = "Wedge"
  10.     m.Scale = Vector3.new(0,1,1)
  11.     return part
  12. end
  13. function CFrameFromTopBack(at, top, back)
  14.     local right = top:Cross(back)
  15.     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)
  16. end
  17.  
  18. function CreateTriangle(parent, a, b, c)
  19.     local this = {}
  20.     local mPart1 = Instance.new('WedgePart')
  21.     setupPart(mPart1)
  22.     local mPart2 = Instance.new('WedgePart')
  23.     setupPart(mPart2)
  24.     function this:Set(a, b, c)
  25.         local ab, bc, ca = b-a, c-b, a-c
  26.         local abm, bcm, cam = ab.magnitude, bc.magnitude, ca.magnitude
  27.         local edg1 = math.abs(0.5 + ca:Dot(ab)/(abm*abm))
  28.         local edg2 = math.abs(0.5 + ab:Dot(bc)/(bcm*bcm))
  29.         local edg3 = math.abs(0.5 + bc:Dot(ca)/(cam*cam))
  30.         if edg1 < edg2 then            
  31.             if edg1 < edg3 then  
  32.                 a, b, c = c, a, b ab, bc, ca = ca, ab, bc abm = cam
  33.             elseif edg2 < edg3 then
  34.                 a, b, c = b, c, a
  35.                 ab, bc, ca = bc, ca, ab
  36.                 abm = bcm
  37.             else
  38.                 a, b, c = c, a, b ab, bc, ca = ca, ab, bc abm = cam
  39.             end
  40.         end
  41.         local len1 = -ca:Dot(ab)/abm
  42.         local len2 = abm - len1
  43.         local width = (ca + ab.unit*len1).magnitude
  44.         local maincf = CFrameFromTopBack(a, ab:Cross(bc).unit, -ab.unit)
  45.         if len1 > 0.2 then
  46.             mPart1.Parent = parent mPart1.Size = Vector3.new(0.2, width, len1)
  47.             mPart1.CFrame = maincf*CFrame.Angles(math.pi,0,math.pi/2)*CFrame.new(0,width/2,len1/2)
  48.         else
  49.             mPart1.Parent = nil
  50.         end
  51.         if len2 > 0.2 then
  52.             mPart2.Parent = parent
  53.             mPart2.Size = Vector3.new(0.2, width, len2)
  54.             mPart2.CFrame = maincf*CFrame.Angles(math.pi,math.pi,-math.pi/2)*CFrame.new(0,width/2,-len1 - len2/2)
  55.         else
  56.             mPart2.Parent = nil
  57.         end
  58.     end
  59.     function this:SetProperty(prop, value)
  60.         mPart1[prop] = value
  61.         mPart2[prop] = value
  62.     end
  63.     this:Set(a, b, c)
  64.     function this:Destroy()
  65.         mPart1:Destroy()
  66.         mPart2:Destroy()
  67.     end
  68.     return this
  69. end
  70. local t = {}
  71. local vertices = {}
  72. Mouse = game.Players.LocalPlayer:GetMouse()
  73. Player = game.Players.LocalPlayer
  74. local N = Instance.new("Part",Player.Character)
  75. N.Anchored = true
  76. N.FormFactor = "Custom"
  77. N.Size = Vector3.new(0.4,0.4,0.4)
  78. N.TopSurface = "Smooth"
  79. N.BottomSurface = "Smooth"
  80. TarP = Mouse.Hit.p
  81. Off = CFrame.new(0,0,0)
  82. X,Y,Z = 0,0,0
  83. On = true
  84. Mouse.Button1Down:connect(function()
  85.     if On then
  86.         local P = N:clone()
  87.         P.Parent = Player.Character
  88.         P.CFrame = CFrame.new(TarP)
  89.         table.insert(t,{Part=P,CF=TarP})
  90.         if #t >= 3 then
  91.             local n = {}
  92.             for i,v in pairs(t) do
  93.                 table.insert(vertices,v.CF)
  94.                 n[i]=v.CF
  95.                 v.Part:Destroy()
  96.             end
  97.             CreateTriangle(Workspace,unpack(n))
  98.             t = {}
  99.         end
  100.     end
  101. end)
  102. Mouse.Move:connect(function()
  103.     if On then
  104.         local p = Mouse.Hit.p
  105.         local pos = p
  106.         for i,v in pairs(vertices) do
  107.             if (p-v).magnitude < 0.5 then
  108.                 pos = v
  109.             end
  110.         end
  111.         TarP = (CFrame.new(pos)*CFrame.Angles(math.rad(X),math.rad(Y),math.rad(Z))*Off).p
  112.     end
  113. end)
  114. Mouse.KeyDown:connect(function(k)
  115.  
  116.     if k == "r" then -- up
  117.         Off = Off+Vector3.new(0,1,0)
  118.     end
  119.     if k == "f" then -- down
  120.         Off = Off-Vector3.new(0,1,0)
  121.     end
  122.     if k == "m" then
  123.         X,Y,Z = 0,0,0
  124.         Off = CFrame.new(0,0,0)
  125.     end
  126.     if k == "z" then
  127.         X = X+10
  128.     end
  129.     if k == "x" then --|Rotation
  130.         Z = Z+10
  131.     end
  132.     --[[if k == "c" then
  133.         Z = Z+10
  134.     end]]
  135.     if k == "q" then
  136.         On = not On
  137.     end
  138. end)
  139. game:GetService("RunService").RenderStepped:connect(function()
  140.     N.CFrame = CFrame.new(TarP)
  141.     N.Parent = On and Player.Character or nil
  142. end)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement