Advertisement
CluelessDev

Chunked 3D tile map generation roblox

Feb 5th, 2023 (edited)
872
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.19 KB | None | 0 0
  1.  
  2. function ComputeLinearIndexing(index, size)
  3.     local row = math.floor((index - 1) / size) + 1
  4.     local column = (index - 1) % size + 1
  5.     return column, row
  6. end
  7.  
  8.  
  9.  
  10.  
  11. --[[
  12.     Generates a 3D tilemap subdivided into chunks of equal size
  13. ]]
  14. function GenerateChunkedTileMap(gridSize: number, subdivisionBase: number) 
  15.     gridSize = math.floor((gridSize/4) * 4)  --> optional, this keeps the map size a multiple of 4
  16.    
  17.    
  18.     --# The number of chunks is derived this way because I have to guarantee
  19.     --# that it is gonna be a square number(1, 4, 9, 16, etc). this is so the
  20.     --# map remains square shaped, and so each chunk has an equal ammount of cells
  21.     --! ↓ ↓ ↓ ↓ ↓ ↓
  22.    
  23.     local numberOfChunks = subdivisionBase^2
  24.     local chunkSize = gridSize/numberOfChunks  --> area of the chunk
  25.    
  26.  
  27.     local Map: Model = Instance.new("Model")
  28.     Map.Name = "Map"
  29.    
  30.     -- Generate an array of chunk matrixes.
  31.     local chunks = table.create(numberOfChunks)
  32.     for chunkIndex = 1, numberOfChunks do
  33.         local ChunkModel = Instance.new("Model")
  34.         local collumns = table.create(chunkSize)
  35.        
  36.         -- Generate the chunk grid/matrix
  37.         for x = 1, chunkSize do
  38.             local rows = table.create(chunkSize)
  39.            
  40.             for z = 1, chunkSize do
  41.                 local tile = {X = x, Z = z}
  42.                 rows[z] = tile
  43.                
  44.                 local Part = Instance.new("Part")
  45.                 Part.Name = x..","..z
  46.                 Part.Size = Vector3.one
  47.                 Part.Position = Vector3.new(x * Part.Size.X, 10, z * Part.Size.Z)
  48.                 Part.Anchored = true
  49.                 Part.Material = Enum.Material.SmoothPlastic
  50.                 Part.Parent = ChunkModel
  51.             end
  52.            
  53.             collumns[x] = rows
  54.         end
  55.        
  56.         --[[
  57.             Since Chunks are represented as models in the workspace... I got to position them
  58.             But I have no X or Z position to do so! Therefor I use Linear indexing so I can get
  59.             an X & Z index from an array ↓ ↓ ↓ ↓ ↓ ↓
  60.         --]]
  61.        
  62.         local x, z = ComputeLinearIndexing(chunkIndex, subdivisionBase)
  63.        
  64.         ChunkModel.Name = chunkIndex
  65.         ChunkModel.Parent = Map
  66.         ChunkModel:PivotTo(CFrame.new(x * ChunkModel:GetExtentsSize().X, 10, z * ChunkModel:GetExtentsSize().Z))
  67.        
  68.         chunks[chunkIndex] = collumns
  69.     end
  70.    
  71.    
  72.     Map.Parent = workspace
  73.     return chunks
  74. end
  75.  
  76.  
  77. print(GenerateChunkedTileMap(144, 3))
  78.  
  79.  
  80.  
  81.  
  82.  
  83.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement