Advertisement
CluelessDev

radial and square fall off functions to filter perlin noise

Jun 27th, 2021
171
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.10 KB | None | 0 0
  1. --[[
  2.  
  3. This module generates normalized fall off to filter perlin noise with, there are 2 types of filter:
  4. - Radial
  5. - Square
  6.  
  7. these work with my map generation implementation which is found under it
  8. --]]
  9.  
  10. -- module script
  11. local FallOffMap = {}
  12.  
  13. function FallOffMap.GenerateCircularFallOff(value1: number, value2: number, theMapSize)
  14.    
  15.     local radius = .5^2
  16.     local widthFallOff = math.abs(value1/theMapSize * 2 - 1)
  17.     local lengthFallOff = math.abs(value2/theMapSize * 2 - 1)    
  18.    
  19.    
  20.     local result = math.clamp(math.sqrt(widthFallOff^2  + lengthFallOff^2), 0, 1)
  21.     return result
  22. end
  23.  
  24.  
  25. function FallOffMap.GenerateSquareFallOff(value1: number, value2: number, theMapSize)
  26.     local widthFallOff = math.abs(value1/theMapSize * 2 - 1)
  27.     local lengthFallOff = math.abs(value2/theMapSize * 2 - 1)
  28.     local result = math.max(widthFallOff, lengthFallOff)
  29.  
  30.     return math.clamp(result, 0, 1)
  31. end
  32.  
  33.  
  34. -- smooths out both the circular and square filters, substract the return value with the perlin noise value
  35. function FallOffMap.Transform(theFallOffResult, anOffset, aSmoothValue)
  36.  
  37.     local a = aSmoothValue
  38.     local b = anOffset
  39.     local value = theFallOffResult
  40.     return math.pow(value, a)/(math.pow(value, a)+math.pow(b - b * value, a))
  41. end
  42.  
  43. return FallOffMap
  44.  
  45.  
  46. -- another module script
  47.  
  48. --[[
  49.  
  50. This module serves as the map class constructor, it uses an Open source perlin noise library found here:
  51. https://devforum.roblox.com/t/perlin-noise-library-module-4d-and-octaves/600192
  52. --]]
  53. local Map = {}
  54. Map.__index = Map
  55.  
  56. function Map.new(theMapGenValueMap: table, aTile, theTerrainTypesMap: table)
  57.     local self = setmetatable({}, Map)
  58.     self.MapSize = theMapGenValueMap.MapSize
  59.     self.TileSize = theMapGenValueMap.TileSize
  60.    
  61.     local seed = theMapGenValueMap.Seed
  62.     local amplitude = theMapGenValueMap.Amplitude
  63.     local scale = theMapGenValueMap.Scale
  64.     local octaves = theMapGenValueMap.Octaves
  65.     local persistence = theMapGenValueMap.Persistence
  66.  
  67.     local fallOffOffset = theMapGenValueMap.FallOffOffset  
  68.     local fallOffPower = theMapGenValueMap.FallOffSmoothness
  69.  
  70.     for i = 1, self.MapSize do
  71.         for j = 1, self.MapSize do
  72.             -- Noise and fall off calculations
  73.             local noiseResult  = PerlinNoise.new({(i + seed) * scale, (j + seed) * scale}, amplitude, octaves, persistence)
  74.             local fallOff = FallOffMap.GenerateSquareFallOff(i, j , self.MapSize)
  75.             noiseResult  -= FallOffMap.Transform(fallOff, fallOffOffset, fallOffPower)
  76.             noiseResult  = math.clamp(noiseResult  + 0.5 , 0, 1)
  77.  
  78.             -- It needs the tile to be an object, if you dont want that, instance a base part here
  79.             local tile = aTile.Asset:Clone()
  80.             tile.Size = Vector3.new(self.TileSize, self.TileSize, self.TileSize)
  81.             tile.Position = Vector3.new(i * tile.Size.X, tile.Size.Y, j * tile.Size.Z)
  82.             tile.Name = fallOff
  83.  
  84.             tile.Color = Color3.new(noiseResult , noiseResult , noiseResult )
  85.             tile.Parent = workspace
  86.         end
  87.     end
  88.    
  89.     return self
  90. end
  91.  
  92.  
  93.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement