Check if a part is surrounded by other parts

So in my voxel game ive been working on i have been trying to make so if the block is surrounded by other blocks it will not be rendered. So if a cube is surrounded by other cubes it would not render the cube since it is out of sight and that would just cause performance issues/lag is there a way to fix this?

Is there an efficient way to d this?

How you would do this is divide your game into chunks. Each chunk is a 3d matrix (tables inside tables inside tables). Have a main tables full of your chunks. Make a modules to handle chunk functions. When you load a chunk there is a function to check table values on all sides. This function starts by looping through the chunk and getting all the parts that are air (probably nil or false). It then gets all the table elements to the sides of those blocks that are nil. It then makes only those parts visible and the rest are set to transparent and (optional but recommend if your system is good) non-collide.

1 Like

I have been trying to do this and many people have told me this but i am not sure if there is a function for checking if there is empty space or not

You can also use folders instead of tables inside code if you like that better. I believe this is less effective but works better for a server-client system. Ex.
Chunks (folder)
–Chunk12983 (folder)
----Plain3 (folder)
------Row6 (folder)
--------Block (model, folder, or part)
--------…
------…
----…
–…

1 Like

Voxel is where your game is organized by simple units. You dont think of parts by their positions, instead you use their location in tables/ matrices. Basicly, you make functions to get parts based on coordinates in tables. Good luck!

I tried to us GetTouchingParts() or something like that how can i check if the part is touched by 6 parts since it has 6 faces so the then the block doesnt get rendered thats what 8m trying to do.

You can fire rays in 6 directions from the part. This is less intensive than get touching. Gtg

You should be able to perform that function without using an parts at all.
Your terrain should be stored in an abstract array or matrix without existing as real parts in the game at all.
Then you can check the array for the existence of parts around the part of interest, without having to use any physics calculations.
Once you have determined which parts should be rendered or not, then you can actually instantiate the parts into the game.

Sorry i am new to coding so i dont know all of that stuff but is there a way to check if it is touched by 6 parts?

The least intensive way would be firing rays. Also, your game doesn’t seem to be voxel. Voxel is where you store your elements in a matrix(arrays/tables inside of arrays/tables). If you want to find parts to the sides of a cube, assuming that the parts are directly to the side lined up, you would fire 6 rays from the center of the cube your checking for surrounding parts. These six rays would get rayresults if there is a part there. If you do choose to make your game voxel, you can use a custom get coordinate function to find blocks in any relative position to a block.

I do not know that much about rays other than it making a way from origin and direction is there any chance that you have a code to detect blocks from the 6 surfaces?

Here is a non-voxel code example of a get surrounding function:

local CUBESIZE = 4
--local CUBESTART -- It is assumed that your cubes start at 0,0,0 (assumes that one of your cubes would be at 0, 0, 0)
local BLOCKFOLDER = workspace.Blocks -- folder to look in for blocks

local rayCastParams = RaycastParams.new()
rayCastParams.FilterDescendantsInstances = BLOCKFOLDER:GetChildren()
rayCastParams.FilterType = Enum.RaycastFilterType.Whitelist

local RETURNTABLEDIRECTIONS = {
	Top = Vector3.new(1, 0, 0),
	Bottom = Vector3.new(-1, 0, 0),
	Front = Vector3.new(0, 0, 1),
	Back = Vector3.new(0, 0, -1),
	Right = Vector3.new(0, 1, 0),
	Left = Vector3.new(0, -1, 0)
}

function getAdjacent(blockPart)
	--print("getAdjacent "..blockPart.Name)
	local position = blockPart.Position
	local returnTable = RETURNTABLEDIRECTIONS
	local blockR = CUBESIZE / 2 + 0.01
	for str, value in pairs(returnTable) do
		--print("firing ray")
		local direction = value * blockR
		local origin = position
		local result = workspace:Raycast(origin, direction, rayCastParams)
		if result then
			returnTable[str] = result.Instance
			print(returnTable[str].Name)
		else
			returnTable[str] = nil
		end
		
	end
	print("return")
	return returnTable
end

--local list = getAdjacent(game.Workspace.Blocks.Grass)
--for i, p in pairs(list) do
--	print(i.." "..p.Name)
--end

This code assumes that your blocks orientation is 0,0,0.

By the way, scripting support is for when your code doesn’t do what it is intended and you don’t know why or for when you need a general hint about how to do something. It really isn’t for requesting code. Also, with you coding experience I wouldn’t recommend trying to make something so ambitious unless you learn more about the engine and about lua unless you already know another language or something.
Edit: Don’t ditch it, learn a bit about the engine then go back and finish it. Also, I highly recommend making your game voxel. It’s much better for performance and is easier to make.

1 Like

Just turn on Streaming Enabled. That’s it. It’ll render the parts of the workspace closest to the player within a certain range that you set.

i do know lua and coding and that stuff but i never went into that stuff also i think it would be dumb ditching my project after have been working on it for so long.

In your case i would use camera:WorldToViewportPoint() to check if the part is visible and render it
Example to check if the part is visible with rays

local function IsNotVisible(Part)
    local camera = workspace.CurrentCamera
    local _, onScreen = camera:WorldToViewportPoint(Part.Position)
    
    if onScreen then
        local origin = camera.CFrame.p
        local params=RaycastParams.new()
        local direction= ( Part - origin).Unit*500)
        local result = workspace:Raycast(origin,direction,params)
        if  result and (result.Instance:IsDescendantOf(part) or result.Instance == part) then
            return false
        end
    else
        return true
    end
    return true
end