Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local outlineBufferDepth = 1
- local outlineParts = {}
- local camera = workspace.CurrentCamera
- local RunService = game:GetService 'RunService'
- local function RecursiveGetChildren(parent)
- local returnValue = {}
- local rgc
- rgc = function(localParent)
- for index, child in next, localParent:GetChildren() do
- table.insert(returnValue, child)
- rgc(child)
- end
- end
- rgc(parent)
- return returnValue
- end
- function AddOutlineToPart(part, color3Color)
- if outlineParts[part] ~= nil then
- RemoveOutlineFromPart(part)
- end
- local outline = Instance.new 'Part'
- outline.Anchored = true
- outline.CanCollide = false
- outline.FormFactor = 'Custom'
- outline.Size = Vector3.new(1, 1, 1)
- outline.Color = color3Color or part.Color
- outline.Name = part.Name
- outline.Material = 'SmoothPlastic'
- outline.TopSurface = 'SmoothNoOutlines'
- outline.BottomSurface = 'SmoothNoOutlines'
- outline.FrontSurface = 'SmoothNoOutlines'
- outline.BackSurface = 'SmoothNoOutlines'
- outline.LeftSurface = 'SmoothNoOutlines'
- outline.RightSurface = 'SmoothNoOutlines'
- local partmesh = nil
- for index, child in next, RecursiveGetChildren(part) do
- if child:IsA 'DataModelMesh' then
- thispartmesh = child:Clone()
- local meshChangedConnection = child.Changed:connect(function(prop)
- if prop == 'MeshType' or prop == 'MeshId' or prop == 'TextureId' then
- thispartmesh[prop] = child[prop]
- end
- end)
- partmesh = thispartmesh
- elseif child:IsA 'FaceInstance' then
- child:Clone().Parent = outline
- end
- end
- if partmesh == nil then
- local mesh = Instance.new('BlockMesh', outline)
- else
- partmesh:Clone().Parent = outline
- end
- outline.Parent = camera
- outlineParts[part] = {outline, partmesh, 0}
- end
- function RemoveOutlineFromPart(part)
- local outline = outlineParts[part]
- if outline ~= nil then
- outline:Destroy()
- end
- end
- function SetPartOutlineTransparencyScale(part, scale)
- local data = outlineParts[part]
- if data ~= nil then
- data[3] = scale
- end
- end
- function SetModelOutlineTransparencyScale(model, scale)
- for index, child in next, RecursiveGetChildren(model) do
- SetPartOutlineTransparencyScale(child, scale)
- end
- end
- function AddOutlineToModel(model, color)
- for index, child in next, RecursiveGetChildren(model) do
- if child:IsA 'BasePart' then
- AddOutlineToPart(child, color)
- end
- end
- end
- function RemoveOutlineFromModel(model, color)
- for index, child in next, RecursiveGetChildren(model) do
- if child:IsA 'BasePart' then
- RemoveOutlineFromPart(child, color)
- end
- end
- end
- function UpdateLoop()
- local viewpoint = camera.CoordinateFrame
- local partDistances = {}
- for part, list in next, outlineParts do
- table.insert(partDistances, (viewpoint.p - part.Position).magnitude)
- end
- local nearestDistance = math.min(unpack(partDistances))
- local furthestDistance = math.max(unpack(partDistances))
- for part, list in next, outlineParts do
- local outline, partmesh, transparencyScale = unpack(list)
- local distance = (part.Position - viewpoint.p).magnitude
- local distanceMultiplier = 1 + (distance - nearestDistance) / (furthestDistance - nearestDistance)
- outline.Transparency = math.max(transparencyScale + (part.Transparency * (1-transparencyScale)), 0.001)
- local unit = (part.Position - viewpoint.p).unit * distanceMultiplier
- local pos = viewpoint.p + unit
- outlineframe = CFrame.new(pos) * CFrame.Angles(part.CFrame:toEulerAnglesXYZ())
- outline.CFrame = outlineframe
- local outlineSize = distanceMultiplier/distance
- if partmesh == nil then
- outline.Mesh.Scale = part.Size * outlineSize
- else
- if (partmesh:IsA 'SpecialMesh' and partmesh.MeshType.Name == 'FileMesh') or (partmesh:IsA 'SpecialMesh' == false) then
- outline[partmesh.Name].Scale = partmesh.Scale * outlineSize
- else
- outline[partmesh.Name].Scale = partmesh.Scale * part.Size * outlineSize
- end
- end
- end
- end
- camera:ClearAllChildren()
- RunService.RenderStepped:connect(UpdateLoop)
- -- Use demonstration
- local model = workspace.Runner
- AddOutlineToModel(model, Color3.new(0, 1, 0))
- RunService.RenderStepped:connect(function()
- local cameraPos = camera.CoordinateFrame.p
- local modelPos = model:GetModelCFrame().p
- local distance = (cameraPos - modelPos).magnitude
- local fadeStart = 10
- local fadeEnd = 5
- local fadeAlpha = 1-(math.max(math.min(distance, fadeStart), fadeEnd) - fadeEnd) / (fadeStart - fadeEnd)
- SetModelOutlineTransparencyScale(model, fadeAlpha)
- end)
Advertisement
Add Comment
Please, Sign In to add comment