Untitled

mail@pastecode.io avatar
unknown
plain_text
7 months ago
3.4 kB
2
Indexable
Never
local SizeToDistanceMode = true --Becomes blocky father depending on the size of the block
local LOD_Distance = 50
local CollisonBasedOnPart = true --Depends if the collison of the fake part is based on the real part collision
local Rep = game.ReplicatedStorage

local function AddFake(ShadowState, Part)
	Part.Parent = Rep.PartHolder

	local FakePart = Instance.new("Part")
	FakePart.Anchored = true
	FakePart.Color = Part.Color
	FakePart.CanCollide = CollisonBasedOnPart and Part.CanCollide or false
	FakePart.CastShadow = false
	FakePart.Size = Part.Size
	FakePart.CFrame = Part.CFrame
	FakePart.Parent = workspace
	FakePart.Transparency = Part.Transparency
	FakePart.Name = "FakePart"

	Part.CastShadow = ShadowState
end

local WaitLoad = 0
local MaxWaitLoad = 500

local function WaitTimer()
	WaitLoad += 1
	
	if WaitLoad%MaxWaitLoad == 0 then
		WaitLoad = 0
		wait()
	end
end

spawn(function()
	while wait() do
		--////////////////////////////
		--/////LEVEL OF DETAIL!!!/////
		--////////////////////////////
		
		

		local CamPos = workspace.CurrentCamera.CFrame.Position

		--Checks if the fake part is to close to the camera, then removes the fake part
		for _, Part in pairs(workspace:GetDescendants()) do
			if Part:IsA("Part") then
				if Part.Name == "FakePart" then
					WaitTimer()
					if not SizeToDistanceMode then
						if (Part.Position - CamPos).Magnitude <= LOD_Distance then
							delay(0.5,function()
								Part:Destroy()
							end)
						end
					else
						local AverageSize = (Part.Size.X + Part.Size.Y + Part.Size.Z) / 3

						if (Part.Position - CamPos).Magnitude <= LOD_Distance * AverageSize then
							delay(0.5,function()
								Part:Destroy()
							end)
						end
					end
				end
			end
		end

		--Checks if the real part is to close to the camera, then adds the real part
		for _, Part in pairs(Rep.PartHolder:GetChildren()) do
			if Part:IsA("MeshPart") or Part:FindFirstChildOfClass("Mesh") or Part:IsA("UnionOperation") then
				WaitTimer()
				if not SizeToDistanceMode then
					if (Part.Position - CamPos).Magnitude <= LOD_Distance then
						Part.Parent = workspace
					end
				else
					local AverageSize = (Part.Size.X + Part.Size.Y + Part.Size.Z) / 3

					if (Part.Position - CamPos).Magnitude <= LOD_Distance * AverageSize then
						Part.Parent = workspace
					end
				end
			end
		end

		--Checks all of the real parts and moves them to the storage, then creates a fake part on top of the real part
		for _, Part in pairs(workspace:GetDescendants()) do
			if not game.Players:GetPlayerFromCharacter(Part.Parent) then
				if Part:IsA("MeshPart") or Part:FindFirstChildOfClass("Mesh") or Part:IsA("UnionOperation") then
					local State = false
					for _, Plr in pairs(game.Players:GetPlayers()) do
						if Part:IsDescendantOf(Plr.Character) then
							State = true
						end
					end
					WaitTimer()
					if not State then
						if not SizeToDistanceMode then
							if (Part.Position - CamPos).Magnitude >= LOD_Distance then
								AddFake(false, Part)
							end
						else
							local AverageSize = (Part.Size.X + Part.Size.Y + Part.Size.Z) / 3
							
							if (Part.Position - CamPos).Magnitude >= LOD_Distance * AverageSize then
								AddFake(false, Part)
							end
						end
					end
				end
			end
		end
	end
end)