I am trying to make a function that textures a Minecraft like terrain chunk by only texturing the sides of the blocks that can be seen (for optimization, as rendering unseen faces of blocks would decrease performance).
What I’m trying to do is raycast in six directions from each block, check if a block is blocking that direction, and adding a texture to that direction’s face if not. This is all being done on the client.
Every single RaycastResult returns nil, and the raycast can’t seem to recognize any of the blocks. Before I added the raycast params, the raycast did once detect my character on the server. I believe that the raycast isn’t hitting these blocks because the blocks are all loaded on the client, though I may be wrong.
Is there anyway I can fix this? Or is there a better way to handle this?
These blocks have CanQuery on, Transparent attribute off, and printing the RaycastResult prints nil.
All interior faces shown, top faces removed
Raycast Params
local rayParams = RaycastParams.new()
rayParams.FilterType = Enum.RaycastFilterType.Include
rayParams.FilterDescendantsInstances = {workspace.Worldspace} -- where the blocks are held
Texture Function
function textureFace(result: 'RaycastResult', block:'Block', face:'Enum.NormalId',name:'Face Name')
local old = block:FindFirstChild(name) -- checking if texture was already there
print(result)
if not result or result.Instance:GetAttribute('Transparent') == true then -- if not being blocked
if not old then -- if texture wasn't there already
local new = texture:Clone()
new.Face = face
new.Name = name -- giving texture a name for ease of checking for old texture
new.Parent = block
end
elseif old then
old:Destroy() -- eliminating texture if covered by block
end
end
function textureChunk(chunk: 'Chunk')
for _, block in pairs(chunk:GetChildren()) do
textureFace(workspace:Raycast(block.Position,Vector3.new(0,1,0),rayParams),block,Enum.NormalId.Top,'top')
textureFace(workspace:Raycast(block.Position,Vector3.new(0,-1,0),rayParams),block,Enum.NormalId.Bottom,'bottom')
textureFace(workspace:Raycast(block.Position,Vector3.new(0,0,-1),rayParams),block,Enum.NormalId.Front,'front')
textureFace(workspace:Raycast(block.Position,Vector3.new(0,1,1),rayParams),block,Enum.NormalId.Back,'back')
textureFace(workspace:Raycast(block.Position,Vector3.new(1,0,0),rayParams),block,Enum.NormalId.Left,'left')
textureFace(workspace:Raycast(block.Position,Vector3.new(-1,0,0),rayParams),block,Enum.NormalId.Right,'right')
end
end