Inoob8C

Grass Culling

Jul 7th, 2024 (edited)
232
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.30 KB | None | 0 0
  1. --!native
  2. --!strict
  3. local ReplicatedStorage = game:GetService("ReplicatedStorage")
  4. local CollectionService = game:GetService("CollectionService")
  5.  
  6. local Camera = workspace.CurrentCamera
  7.  
  8. local originalParents = {}
  9.  
  10. local function switchToCulledParent(obj: BasePart): ()
  11. if obj.Parent and obj.Parent ~= ReplicatedStorage.InteractiveGrass.CulledGrass then
  12. originalParents[obj] = obj.Parent
  13. obj.Parent = ReplicatedStorage.InteractiveGrass.CulledGrass
  14. end
  15. end
  16.  
  17. local function switchToOriginalParent(obj: BasePart): ()
  18. local originalParent = originalParents[obj]
  19. if originalParent and obj.Parent == ReplicatedStorage.InteractiveGrass.CulledGrass then
  20. obj.Parent = originalParent
  21. originalParents[obj] = nil
  22. end
  23. end
  24.  
  25. local function updateVisibility(): ()
  26. local viewDirection = Camera.CFrame.LookVector.Unit
  27.  
  28. local function processObject(obj: BasePart): ()
  29. local partPosition = obj.Position
  30. local toObject = (partPosition - Camera.CFrame.Position).Unit
  31. local dotProduct = viewDirection:Dot(toObject)
  32.  
  33. if dotProduct > 0 then
  34. switchToOriginalParent(obj)
  35. else
  36. switchToCulledParent(obj)
  37. end
  38. end
  39. for _, obj in CollectionService:GetTagged("Grass") do
  40. processObject(obj)
  41. end
  42. end
  43.  
  44. Camera:GetPropertyChangedSignal("CFrame"):Connect(function()
  45. updateVisibility()
  46. end)
Advertisement
Add Comment
Please, Sign In to add comment