Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- --!native
- --!strict
- local ReplicatedStorage = game:GetService("ReplicatedStorage")
- local CollectionService = game:GetService("CollectionService")
- local Camera = workspace.CurrentCamera
- local originalParents = {}
- local function switchToCulledParent(obj: BasePart): ()
- if obj.Parent and obj.Parent ~= ReplicatedStorage.InteractiveGrass.CulledGrass then
- originalParents[obj] = obj.Parent
- obj.Parent = ReplicatedStorage.InteractiveGrass.CulledGrass
- end
- end
- local function switchToOriginalParent(obj: BasePart): ()
- local originalParent = originalParents[obj]
- if originalParent and obj.Parent == ReplicatedStorage.InteractiveGrass.CulledGrass then
- obj.Parent = originalParent
- originalParents[obj] = nil
- end
- end
- local function updateVisibility(): ()
- local viewDirection = Camera.CFrame.LookVector.Unit
- local function processObject(obj: BasePart): ()
- local partPosition = obj.Position
- local toObject = (partPosition - Camera.CFrame.Position).Unit
- local dotProduct = viewDirection:Dot(toObject)
- if dotProduct > 0 then
- switchToOriginalParent(obj)
- else
- switchToCulledParent(obj)
- end
- end
- for _, obj in CollectionService:GetTagged("Grass") do
- processObject(obj)
- end
- end
- Camera:GetPropertyChangedSignal("CFrame"):Connect(function()
- updateVisibility()
- end)
Advertisement
Add Comment
Please, Sign In to add comment