Guest User

Untitled

a guest
Jan 9th, 2014
167
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 4.52 KB | None | 0 0
  1. local outlineBufferDepth = 1
  2.  
  3. local outlineParts = {}
  4. local camera = workspace.CurrentCamera
  5. local RunService = game:GetService 'RunService'
  6.  
  7. local function RecursiveGetChildren(parent)
  8.     local returnValue = {}
  9.    
  10.     local rgc
  11.     rgc = function(localParent)
  12.         for index, child in next, localParent:GetChildren() do
  13.             table.insert(returnValue, child)
  14.            
  15.             rgc(child)
  16.         end
  17.     end
  18.    
  19.     rgc(parent)
  20.    
  21.     return returnValue
  22. end
  23.  
  24. function AddOutlineToPart(part, color3Color)
  25.     if outlineParts[part] ~= nil then
  26.         RemoveOutlineFromPart(part)
  27.     end
  28.    
  29.     local outline = Instance.new 'Part'
  30.     outline.Anchored = true
  31.     outline.CanCollide = false
  32.     outline.FormFactor = 'Custom'
  33.     outline.Size = Vector3.new(1, 1, 1)
  34.     outline.Color = color3Color or part.Color
  35.     outline.Name = part.Name
  36.    
  37.     outline.Material = 'SmoothPlastic'
  38.     outline.TopSurface = 'SmoothNoOutlines'
  39.     outline.BottomSurface = 'SmoothNoOutlines'
  40.     outline.FrontSurface = 'SmoothNoOutlines'
  41.     outline.BackSurface = 'SmoothNoOutlines'
  42.     outline.LeftSurface = 'SmoothNoOutlines'
  43.     outline.RightSurface = 'SmoothNoOutlines'
  44.  
  45.    
  46.     local partmesh = nil
  47.     for index, child in next, RecursiveGetChildren(part) do
  48.         if child:IsA 'DataModelMesh' then
  49.             thispartmesh = child:Clone()
  50.            
  51.             local meshChangedConnection = child.Changed:connect(function(prop)
  52.                 if prop == 'MeshType' or prop == 'MeshId' or prop == 'TextureId' then
  53.                     thispartmesh[prop] = child[prop]
  54.                 end
  55.             end)
  56.            
  57.             partmesh = thispartmesh
  58.         elseif child:IsA 'FaceInstance' then
  59.             child:Clone().Parent = outline
  60.         end
  61.     end
  62.    
  63.     if partmesh == nil then
  64.         local mesh = Instance.new('BlockMesh', outline)
  65.     else
  66.         partmesh:Clone().Parent = outline
  67.     end
  68.    
  69.     outline.Parent = camera
  70.    
  71.     outlineParts[part] = {outline, partmesh, 0}
  72. end
  73.  
  74. function RemoveOutlineFromPart(part)
  75.     local outline = outlineParts[part]
  76.    
  77.     if outline ~= nil then
  78.         outline:Destroy()
  79.     end
  80. end
  81.  
  82. function SetPartOutlineTransparencyScale(part, scale)
  83.     local data = outlineParts[part]
  84.    
  85.     if data ~= nil then
  86.         data[3] = scale
  87.     end
  88. end
  89.  
  90. function SetModelOutlineTransparencyScale(model, scale)
  91.     for index, child in next, RecursiveGetChildren(model) do
  92.         SetPartOutlineTransparencyScale(child, scale)
  93.     end
  94. end
  95.  
  96. function AddOutlineToModel(model, color)
  97.     for index, child in next, RecursiveGetChildren(model) do
  98.         if child:IsA 'BasePart' then
  99.             AddOutlineToPart(child, color)
  100.         end
  101.     end
  102. end
  103.  
  104. function RemoveOutlineFromModel(model, color)
  105.     for index, child in next, RecursiveGetChildren(model) do
  106.         if child:IsA 'BasePart' then
  107.             RemoveOutlineFromPart(child, color)
  108.         end
  109.     end
  110. end
  111.  
  112. function UpdateLoop()
  113.     local viewpoint = camera.CoordinateFrame
  114.    
  115.     local partDistances = {}
  116.     for part, list in next, outlineParts do
  117.         table.insert(partDistances, (viewpoint.p - part.Position).magnitude)
  118.     end
  119.    
  120.     local nearestDistance = math.min(unpack(partDistances))
  121.     local furthestDistance = math.max(unpack(partDistances))
  122.     for part, list in next, outlineParts do
  123.         local outline, partmesh, transparencyScale = unpack(list)
  124.        
  125.         local distance = (part.Position - viewpoint.p).magnitude
  126.         local distanceMultiplier = 1 + (distance - nearestDistance) / (furthestDistance - nearestDistance)
  127.        
  128.        
  129.         outline.Transparency = math.max(transparencyScale + (part.Transparency * (1-transparencyScale)), 0.001)
  130.        
  131.        
  132.         local unit = (part.Position - viewpoint.p).unit * distanceMultiplier
  133.         local pos = viewpoint.p + unit
  134.        
  135.         outlineframe = CFrame.new(pos) * CFrame.Angles(part.CFrame:toEulerAnglesXYZ())
  136.        
  137.         outline.CFrame = outlineframe
  138.        
  139.         local outlineSize = distanceMultiplier/distance
  140.        
  141.         if partmesh == nil then
  142.             outline.Mesh.Scale = part.Size * outlineSize
  143.         else
  144.             if (partmesh:IsA 'SpecialMesh' and partmesh.MeshType.Name == 'FileMesh') or (partmesh:IsA 'SpecialMesh' == false) then
  145.                 outline[partmesh.Name].Scale = partmesh.Scale * outlineSize
  146.             else
  147.                 outline[partmesh.Name].Scale = partmesh.Scale * part.Size * outlineSize
  148.             end
  149.         end
  150.     end
  151. end
  152.  
  153. camera:ClearAllChildren()
  154. RunService.RenderStepped:connect(UpdateLoop)
  155.  
  156. -- Use demonstration
  157. local model = workspace.Runner
  158.  
  159. AddOutlineToModel(model, Color3.new(0, 1, 0))
  160.  
  161. RunService.RenderStepped:connect(function()
  162.     local cameraPos = camera.CoordinateFrame.p
  163.     local modelPos = model:GetModelCFrame().p
  164.     local distance = (cameraPos - modelPos).magnitude
  165.    
  166.     local fadeStart = 10
  167.     local fadeEnd = 5
  168.    
  169.     local fadeAlpha = 1-(math.max(math.min(distance, fadeStart), fadeEnd) - fadeEnd) / (fadeStart - fadeEnd)
  170.    
  171.     SetModelOutlineTransparencyScale(model, fadeAlpha)
  172. end)
Advertisement
Add Comment
Please, Sign In to add comment