Extract Terrain into its own class
This commit is contained in:
parent
ee2431d9c4
commit
5fa89d1e47
3 changed files with 89 additions and 62 deletions
79
Terrain2/Terrain.swift
Normal file
79
Terrain2/Terrain.swift
Normal file
|
@ -0,0 +1,79 @@
|
|||
//
|
||||
// Terrain.swift
|
||||
// Terrain
|
||||
//
|
||||
// Created by Eryn Wells on 11/3/18.
|
||||
// Copyright © 2018 Eryn Wells. All rights reserved.
|
||||
//
|
||||
|
||||
import Cocoa
|
||||
import MetalKit
|
||||
|
||||
class Terrain: NSObject {
|
||||
|
||||
/// Creete a Metal vertex descriptor specifying how vertices will by laid out for input into our render pipeline and how we'll layout our Model IO vertices.
|
||||
class func buildVertexDescriptor() -> MTLVertexDescriptor {
|
||||
let mtlVertexDescriptor = MTLVertexDescriptor()
|
||||
|
||||
mtlVertexDescriptor.attributes[VertexAttribute.position.rawValue].format = MTLVertexFormat.float3
|
||||
mtlVertexDescriptor.attributes[VertexAttribute.position.rawValue].offset = 0
|
||||
mtlVertexDescriptor.attributes[VertexAttribute.position.rawValue].bufferIndex = BufferIndex.meshPositions.rawValue
|
||||
|
||||
mtlVertexDescriptor.attributes[VertexAttribute.texcoord.rawValue].format = MTLVertexFormat.float2
|
||||
mtlVertexDescriptor.attributes[VertexAttribute.texcoord.rawValue].offset = 0
|
||||
mtlVertexDescriptor.attributes[VertexAttribute.texcoord.rawValue].bufferIndex = BufferIndex.meshGenerics.rawValue
|
||||
|
||||
mtlVertexDescriptor.layouts[BufferIndex.meshPositions.rawValue].stride = 12
|
||||
mtlVertexDescriptor.layouts[BufferIndex.meshPositions.rawValue].stepRate = 1
|
||||
mtlVertexDescriptor.layouts[BufferIndex.meshPositions.rawValue].stepFunction = MTLVertexStepFunction.perVertex
|
||||
|
||||
mtlVertexDescriptor.layouts[BufferIndex.meshGenerics.rawValue].stride = 8
|
||||
mtlVertexDescriptor.layouts[BufferIndex.meshGenerics.rawValue].stepRate = 1
|
||||
mtlVertexDescriptor.layouts[BufferIndex.meshGenerics.rawValue].stepFunction = MTLVertexStepFunction.perVertex
|
||||
|
||||
return mtlVertexDescriptor
|
||||
}
|
||||
|
||||
/// Create and condition mesh data to feed into a pipeline using the given vertex descriptor.
|
||||
class func buildMesh(withDimensions dimensions: float2, segments: uint2, device: MTLDevice, vertexDescriptor: MTLVertexDescriptor) throws -> MTKMesh {
|
||||
let metalAllocator = MTKMeshBufferAllocator(device: device)
|
||||
|
||||
let plane = MDLMesh.newPlane(withDimensions: dimensions,
|
||||
segments: segments,
|
||||
geometryType: .triangles,
|
||||
allocator: metalAllocator)
|
||||
|
||||
let mdlVertexDescriptor = MTKModelIOVertexDescriptorFromMetal(vertexDescriptor)
|
||||
|
||||
guard let attributes = mdlVertexDescriptor.attributes as? [MDLVertexAttribute] else {
|
||||
throw RendererError.badVertexDescriptor
|
||||
}
|
||||
attributes[VertexAttribute.position.rawValue].name = MDLVertexAttributePosition
|
||||
attributes[VertexAttribute.texcoord.rawValue].name = MDLVertexAttributeTextureCoordinate
|
||||
|
||||
plane.vertexDescriptor = mdlVertexDescriptor
|
||||
|
||||
return try MTKMesh(mesh:plane, device:device)
|
||||
}
|
||||
|
||||
let dimensions: float2
|
||||
let segments: uint2
|
||||
|
||||
let vertexDescriptor: MTLVertexDescriptor
|
||||
let mesh: MTKMesh
|
||||
|
||||
init?(dimensions dim: float2, segments seg: uint2, device: MTLDevice) {
|
||||
dimensions = dim
|
||||
segments = seg
|
||||
vertexDescriptor = Terrain.buildVertexDescriptor()
|
||||
|
||||
do {
|
||||
mesh = try Terrain.buildMesh(withDimensions: dimensions, segments: segments, device: device, vertexDescriptor: vertexDescriptor)
|
||||
} catch let e {
|
||||
print("Couldn't create mesh. Error: \(e)")
|
||||
return nil
|
||||
}
|
||||
|
||||
super.init()
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue