Rename DiamondSquareAlgorithm -> DiamondSquareGenerator
This commit is contained in:
parent
dce01100cc
commit
359c7ef987
2 changed files with 16 additions and 16 deletions
|
@ -125,7 +125,7 @@ class RandomAlgorithm: Kernel, TerrainGenerator {
|
||||||
|
|
||||||
/// Implementation of the Diamond-Squares algorithm.
|
/// Implementation of the Diamond-Squares algorithm.
|
||||||
/// - https://en.wikipedia.org/wiki/Diamond-square_algorithm
|
/// - https://en.wikipedia.org/wiki/Diamond-square_algorithm
|
||||||
public class DiamondSquareAlgorithm: TerrainGenerator {
|
public class DiamondSquareGenerator: TerrainGenerator {
|
||||||
public struct Point {
|
public struct Point {
|
||||||
let x: Int
|
let x: Int
|
||||||
let y: Int
|
let y: Int
|
||||||
|
@ -304,7 +304,7 @@ public class DiamondSquareAlgorithm: TerrainGenerator {
|
||||||
let textureSemaphore = DispatchSemaphore(value: 1)
|
let textureSemaphore = DispatchSemaphore(value: 1)
|
||||||
|
|
||||||
init?(device: MTLDevice) {
|
init?(device: MTLDevice) {
|
||||||
let size = DiamondSquareAlgorithm.textureSize
|
let size = DiamondSquareGenerator.textureSize
|
||||||
let desc = MTLTextureDescriptor.texture2DDescriptor(pixelFormat: .r32Float, width: size.width, height: size.height, mipmapped: false)
|
let desc = MTLTextureDescriptor.texture2DDescriptor(pixelFormat: .r32Float, width: size.width, height: size.height, mipmapped: false)
|
||||||
desc.usage = [.shaderRead, .shaderWrite]
|
desc.usage = [.shaderRead, .shaderWrite]
|
||||||
desc.resourceOptions = .storageModeShared
|
desc.resourceOptions = .storageModeShared
|
||||||
|
@ -314,12 +314,12 @@ public class DiamondSquareAlgorithm: TerrainGenerator {
|
||||||
}
|
}
|
||||||
texture = tex
|
texture = tex
|
||||||
|
|
||||||
algorithm = Algorithm(grid: Box(origin: Point(), size: Size(w: DiamondSquareAlgorithm.textureSize.width, h: DiamondSquareAlgorithm.textureSize.height)))
|
algorithm = Algorithm(grid: Box(origin: Point(), size: Size(w: DiamondSquareGenerator.textureSize.width, h: DiamondSquareGenerator.textureSize.height)))
|
||||||
}
|
}
|
||||||
|
|
||||||
func render() {
|
func render() {
|
||||||
let heightMap = algorithm.render()
|
let heightMap = algorithm.render()
|
||||||
let region = MTLRegion(origin: MTLOrigin(), size: DiamondSquareAlgorithm.textureSize)
|
let region = MTLRegion(origin: MTLOrigin(), size: DiamondSquareGenerator.textureSize)
|
||||||
texture.replace(region: region, mipmapLevel: 0, withBytes: heightMap, bytesPerRow: MemoryLayout<Float>.stride * size.width)
|
texture.replace(region: region, mipmapLevel: 0, withBytes: heightMap, bytesPerRow: MemoryLayout<Float>.stride * size.width)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -336,32 +336,32 @@ public class DiamondSquareAlgorithm: TerrainGenerator {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
extension DiamondSquareAlgorithm.Point: Equatable {
|
extension DiamondSquareGenerator.Point: Equatable {
|
||||||
public static func == (lhs: DiamondSquareAlgorithm.Point, rhs: DiamondSquareAlgorithm.Point) -> Bool {
|
public static func == (lhs: DiamondSquareGenerator.Point, rhs: DiamondSquareGenerator.Point) -> Bool {
|
||||||
return lhs.x == rhs.x && lhs.y == rhs.y
|
return lhs.x == rhs.x && lhs.y == rhs.y
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
extension DiamondSquareAlgorithm.Point: CustomStringConvertible {
|
extension DiamondSquareGenerator.Point: CustomStringConvertible {
|
||||||
public var description: String {
|
public var description: String {
|
||||||
return "(x: \(x), y: \(y))"
|
return "(x: \(x), y: \(y))"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
extension DiamondSquareAlgorithm.Size: Equatable {
|
extension DiamondSquareGenerator.Size: Equatable {
|
||||||
public static func == (lhs: DiamondSquareAlgorithm.Size, rhs: DiamondSquareAlgorithm.Size) -> Bool {
|
public static func == (lhs: DiamondSquareGenerator.Size, rhs: DiamondSquareGenerator.Size) -> Bool {
|
||||||
return lhs.w == rhs.w && lhs.h == rhs.h
|
return lhs.w == rhs.w && lhs.h == rhs.h
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
extension DiamondSquareAlgorithm.Size: CustomStringConvertible {
|
extension DiamondSquareGenerator.Size: CustomStringConvertible {
|
||||||
public var description: String {
|
public var description: String {
|
||||||
return "(w: \(w), h: \(h))"
|
return "(w: \(w), h: \(h))"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
extension DiamondSquareAlgorithm.Box: Equatable {
|
extension DiamondSquareGenerator.Box: Equatable {
|
||||||
public static func == (lhs: DiamondSquareAlgorithm.Box, rhs: DiamondSquareAlgorithm.Box) -> Bool {
|
public static func == (lhs: DiamondSquareGenerator.Box, rhs: DiamondSquareGenerator.Box) -> Bool {
|
||||||
return lhs.origin == rhs.origin && lhs.size == rhs.size
|
return lhs.origin == rhs.origin && lhs.size == rhs.size
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -9,15 +9,15 @@
|
||||||
import XCTest
|
import XCTest
|
||||||
@testable import Terrain2
|
@testable import Terrain2
|
||||||
|
|
||||||
fileprivate typealias Box = DiamondSquareAlgorithm.Box
|
fileprivate typealias Box = DiamondSquareGenerator.Box
|
||||||
fileprivate typealias Point = DiamondSquareAlgorithm.Point
|
fileprivate typealias Point = DiamondSquareGenerator.Point
|
||||||
fileprivate typealias Size = DiamondSquareAlgorithm.Size
|
fileprivate typealias Size = DiamondSquareGenerator.Size
|
||||||
|
|
||||||
class DiamondSquareAlgorithmTests: XCTestCase {
|
class DiamondSquareAlgorithmTests: XCTestCase {
|
||||||
fileprivate let grid = Box(origin: Point(x: 0, y: 0), size: Size(w: 5, h: 5))
|
fileprivate let grid = Box(origin: Point(x: 0, y: 0), size: Size(w: 5, h: 5))
|
||||||
|
|
||||||
lazy var alg = {
|
lazy var alg = {
|
||||||
DiamondSquareAlgorithm.Algorithm(grid: grid)
|
DiamondSquareGenerator.Algorithm(grid: grid)
|
||||||
}()
|
}()
|
||||||
|
|
||||||
func testPointToIndexConversion() {
|
func testPointToIndexConversion() {
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue