Add a roughness control
This commit is contained in:
parent
7658210da4
commit
6a43c10b9f
2 changed files with 19 additions and 6 deletions
|
@ -233,12 +233,18 @@ public class DiamondSquareGenerator: TerrainGenerator {
|
||||||
|
|
||||||
struct Algorithm {
|
struct Algorithm {
|
||||||
let grid: Box
|
let grid: Box
|
||||||
private(set) var rng: RandomNumberGenerator
|
|
||||||
|
|
||||||
init(grid: Box, rng: RandomNumberGenerator = SystemRandomNumberGenerator()) {
|
var roughness: Float = 1.0 {
|
||||||
|
didSet {
|
||||||
|
randomRange = -roughness...roughness
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private var randomRange = Float(0)...Float(1)
|
||||||
|
|
||||||
|
init(grid: Box) {
|
||||||
// TODO: Assert log2(w) and log2(h) are integral values.
|
// TODO: Assert log2(w) and log2(h) are integral values.
|
||||||
self.grid = grid
|
self.grid = grid
|
||||||
self.rng = rng
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Run the algorithm and return the genreated height map.
|
/// Run the algorithm and return the genreated height map.
|
||||||
|
@ -248,7 +254,7 @@ public class DiamondSquareGenerator: TerrainGenerator {
|
||||||
// 0. Set the corners to initial values if they haven't been set yet.
|
// 0. Set the corners to initial values if they haven't been set yet.
|
||||||
for p in grid.corners {
|
for p in grid.corners {
|
||||||
let idx = convert(pointToIndex: p)
|
let idx = convert(pointToIndex: p)
|
||||||
heightMap[idx] = Float.random(in: 0...1)
|
heightMap[idx] = Float.random(in: randomRange)
|
||||||
}
|
}
|
||||||
|
|
||||||
grid.breadthFirstSearch { (box: Box) in
|
grid.breadthFirstSearch { (box: Box) in
|
||||||
|
@ -258,7 +264,7 @@ public class DiamondSquareGenerator: TerrainGenerator {
|
||||||
let idx = self.convert(pointToIndex: pt)
|
let idx = self.convert(pointToIndex: pt)
|
||||||
return heightMap[idx]
|
return heightMap[idx]
|
||||||
}
|
}
|
||||||
let midpointValue = Float.random(in: 0...1) + self.average(ofPoints: cornerValues)
|
let midpointValue = Float.random(in: randomRange) + self.average(ofPoints: cornerValues)
|
||||||
heightMap[convert(pointToIndex: midpoint)] = midpointValue
|
heightMap[convert(pointToIndex: midpoint)] = midpointValue
|
||||||
|
|
||||||
// 2. Square step. For each of the side midpoints of this box, compute its value.
|
// 2. Square step. For each of the side midpoints of this box, compute its value.
|
||||||
|
@ -268,7 +274,7 @@ public class DiamondSquareGenerator: TerrainGenerator {
|
||||||
let idx = self.convert(pointToIndex: pt)
|
let idx = self.convert(pointToIndex: pt)
|
||||||
return heightMap[idx]
|
return heightMap[idx]
|
||||||
}
|
}
|
||||||
let ptValue = Float.random(in: 0...1) + self.average(ofPoints: cornerValues)
|
let ptValue = Float.random(in: randomRange) + self.average(ofPoints: cornerValues)
|
||||||
heightMap[convert(pointToIndex: pt)] = ptValue
|
heightMap[convert(pointToIndex: pt)] = ptValue
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -316,6 +322,12 @@ public class DiamondSquareGenerator: TerrainGenerator {
|
||||||
return MTLSize(width: 513, height: 513, depth: 1)
|
return MTLSize(width: 513, height: 513, depth: 1)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
var roughness: Float = 1.0 {
|
||||||
|
didSet {
|
||||||
|
algorithm.roughness = roughness
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
var algorithm: Algorithm
|
var algorithm: Algorithm
|
||||||
let texture: MTLTexture
|
let texture: MTLTexture
|
||||||
let textureSemaphore = DispatchSemaphore(value: 1)
|
let textureSemaphore = DispatchSemaphore(value: 1)
|
||||||
|
|
|
@ -93,6 +93,7 @@ class Terrain: NSObject {
|
||||||
print("Couldn't create algorithm")
|
print("Couldn't create algorithm")
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
(gen as DiamondSquareGenerator).roughness = 0.075
|
||||||
generator = gen
|
generator = gen
|
||||||
|
|
||||||
super.init()
|
super.init()
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue