Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- --[[
- This module generates normalized fall off to filter perlin noise with, there are 2 types of filter:
- - Radial
- - Square
- these work with my map generation implementation which is found under it
- --]]
- -- module script
- local FallOffMap = {}
- function FallOffMap.GenerateCircularFallOff(value1: number, value2: number, theMapSize)
- local radius = .5^2
- local widthFallOff = math.abs(value1/theMapSize * 2 - 1)
- local lengthFallOff = math.abs(value2/theMapSize * 2 - 1)
- local result = math.clamp(math.sqrt(widthFallOff^2 + lengthFallOff^2), 0, 1)
- return result
- end
- function FallOffMap.GenerateSquareFallOff(value1: number, value2: number, theMapSize)
- local widthFallOff = math.abs(value1/theMapSize * 2 - 1)
- local lengthFallOff = math.abs(value2/theMapSize * 2 - 1)
- local result = math.max(widthFallOff, lengthFallOff)
- return math.clamp(result, 0, 1)
- end
- -- smooths out both the circular and square filters, substract the return value with the perlin noise value
- function FallOffMap.Transform(theFallOffResult, anOffset, aSmoothValue)
- local a = aSmoothValue
- local b = anOffset
- local value = theFallOffResult
- return math.pow(value, a)/(math.pow(value, a)+math.pow(b - b * value, a))
- end
- return FallOffMap
- -- another module script
- --[[
- This module serves as the map class constructor, it uses an Open source perlin noise library found here:
- https://devforum.roblox.com/t/perlin-noise-library-module-4d-and-octaves/600192
- --]]
- local Map = {}
- Map.__index = Map
- function Map.new(theMapGenValueMap: table, aTile, theTerrainTypesMap: table)
- local self = setmetatable({}, Map)
- self.MapSize = theMapGenValueMap.MapSize
- self.TileSize = theMapGenValueMap.TileSize
- local seed = theMapGenValueMap.Seed
- local amplitude = theMapGenValueMap.Amplitude
- local scale = theMapGenValueMap.Scale
- local octaves = theMapGenValueMap.Octaves
- local persistence = theMapGenValueMap.Persistence
- local fallOffOffset = theMapGenValueMap.FallOffOffset
- local fallOffPower = theMapGenValueMap.FallOffSmoothness
- for i = 1, self.MapSize do
- for j = 1, self.MapSize do
- -- Noise and fall off calculations
- local noiseResult = PerlinNoise.new({(i + seed) * scale, (j + seed) * scale}, amplitude, octaves, persistence)
- local fallOff = FallOffMap.GenerateSquareFallOff(i, j , self.MapSize)
- noiseResult -= FallOffMap.Transform(fallOff, fallOffOffset, fallOffPower)
- noiseResult = math.clamp(noiseResult + 0.5 , 0, 1)
- -- It needs the tile to be an object, if you dont want that, instance a base part here
- local tile = aTile.Asset:Clone()
- tile.Size = Vector3.new(self.TileSize, self.TileSize, self.TileSize)
- tile.Position = Vector3.new(i * tile.Size.X, tile.Size.Y, j * tile.Size.Z)
- tile.Name = fallOff
- tile.Color = Color3.new(noiseResult , noiseResult , noiseResult )
- tile.Parent = workspace
- end
- end
- return self
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement