Untitled

mail@pastecode.io avatar
unknown
lua
a year ago
3.0 kB
5
Indexable
Never
--[[ Now that we have the lookup of solid voxels ]]
	
	local Chunks = Instance.new("Folder",workspace.CurrentCamera)
	Chunks.Name = "Chunks"
	
	table.clear(jobs)
	
	--[[ Split area bounds into chunksize regions ]]
	local ChunkSize = 200
	ChunkSize = Round(ChunkSize,4)
	local chunksToCreate = {}
	local chunkParts = {}
	for x = cf.x - size.x/2,cf.x + size.x/2,ChunkSize do 
		for y = cf.y - size.y/2,cf.y + size.y/2,ChunkSize do 
			for z = cf.z - size.z/2,cf.z + size.z/2,ChunkSize do 
				local chunkPos = Round3D(Vector3.new(x,y,z),4)
				table.insert(chunksToCreate,chunkPos)
			end
		end
	end
	local finished
	
	while true do
		for i = 1,1 do 
			local chunkPos = table.remove(chunksToCreate)
			if not chunkPos then
				finished = true
				break
			end
			
			local chunkLookup = {}	
			local encodedChunks = {}
			local startAt
			
			for voxelX = chunkPos.X - ChunkSize/2,chunkPos.X + ChunkSize/2,4 do
				if startAt then
					break
				end
				for voxelY = chunkPos.Y - ChunkSize/2,chunkPos.Y + ChunkSize/2,4 do
					if startAt then
						break
					end
					for voxelZ = chunkPos.Z - ChunkSize/2,chunkPos.Z + ChunkSize/2,4 do
						--[[ For each voxel in the chunk ]]
						local voxelPos = Vector3.new(voxelX,voxelY,voxelZ)
						
						--[[ If solid, populate ]]
						
						if SolidLookup[voxelPos] == 1 then
							VoxelEncoder:Store(chunkLookup,encodedChunks,voxelPos,1)
						end
						
						--[[ Determine if under terrain if startAt isnt set ]]
						if not startAt then
							local count = 0				
							for checkY = voxelY,roundTop,4 do
								local checkCell = Round3D(Vector3.new(voxelX,checkY,voxelZ),4)
								local solidType = SolidLookup[checkCell]
								if solidType == 1 then
									count += 1
								end
							end
							local underTerrain = not (count % 2 == 0)
							--[[ If under terrain, set startAt here ]]
							if underTerrain and not SolidLookup[voxelPos] then
								startAt = voxelPos
							end
						end
					end
				end
				task.wait()
			end
	
			if startAt then	
				local chunkPart = script.Chunk:Clone()
				chunkPart.Parent = Chunks
				chunkPart.Anchored = true
				chunkPart.Position = chunkPos
				chunkPart.Size = Vector3.one * ChunkSize
				chunkPart.Material = Enum.Material.SmoothPlastic
				chunkPart.Transparency = 0.3
				chunkPart.Color = Color3.new(0,1,0)	
				local startAtP = Instance.new("Part",chunkPart)
				startAtP.Anchored = true
				startAtP.Size = Vector3.one * 4
				startAtP.Position = startAt 
				startAtP.Color = Color3.new(1,1,0)
				
				local chunk = {
					startAt = startAt,
					roundTop = roundTop,
					roundBottom = roundBottom,
					chunkPos = chunkPos,
					ChunkSize = ChunkSize,
					chunkLookup = chunkLookup,
					encodedChunks = encodedChunks
				}
				table.insert(jobs,chunk)
			end
		end
		if finished then
			break
		end
		task.wait()
	end
		
	local results = Foreman:DoJobs("Floodfill",jobs)