I’m trying to optimise terrain in my game and basically, my function GetVisibleParts() doesn’t return anything, I’m not too sure what’s wrong with this here:
local UIP = game:GetService("UserInputService")
local camera = game.Workspace.Camera
function GetVisibleParts()
local viewportSize = camera.ViewportSize
local UnitRay1 = camera:ScreenPointToRay(0, 0)
local ray1 = Ray.new(UnitRay1.Origin, UnitRay1.Direction * 300)
local Target, Position1 = workspace:FindPartOnRay(ray1)
local UnitRay2 = camera:ScreenPointToRay(viewportSize.X, viewportSize.Y)
local ray2 = Ray.new(UnitRay2.Origin, UnitRay2.Direction * 300)
local Target, Position2 = workspace:FindPartOnRay(ray2)
local Parts = game.workspace:FindPartsInRegion3(Region3.new(Position1 + Vector3.new(0,400,0), Position2 - Vector3.new(0,400,0)))
return Parts
end
If you want to optimize terrain then don’t do this at all. Instead, do it like Minecraft does it. Separate the map into “Chunks” or models/folders, and simply load and unload chunks by distance. If it helps, name the chunks or store them in some sort of 3D array so that you can load them by using neighboring coordinates instead. You can get the chunk you should be standing in by rounding the player’s coordinates.
Atm, i don’t have any chunks made as i still need to find out how to pre-load chunks, merge blocks in chunks to make it smoother etc. I also still need to find out how to generate chunks based on others (so that it’s seamless the line between chunks) That’s why atm i’m using this method, otherwise i’d do the chunk loading method, Do i have to try do the chunk loading method or is there a way for me to simplify and fix my method?
The chunk loading method is one of the most optimized and you only need to change what chunks are loaded in once you change or move to another chunk which saves a huge deal on performance instead of trying to dynamically hide props all the time which seems like you are. You can generate the chunks on the server by grouping things together but it would be best if they were already there.
Common implementation is to have a fixed lookup for chunks by position and also be able to detect when and which chunks are loaded so that if you teleport or leave a chunk when moving, you unload the far ones. Sometimes you might want a cooldown so that it does not lag if you move between two chunks constantly.