diff --git a/Terrain2/ShaderTypes.h b/Terrain2/ShaderTypes.h index b1e3ff6..eff3ef0 100644 --- a/Terrain2/ShaderTypes.h +++ b/Terrain2/ShaderTypes.h @@ -23,15 +23,17 @@ typedef NS_ENUM(NSInteger, BufferIndex) { - BufferIndexMeshPositions = 0, - BufferIndexMeshGenerics = 1, - BufferIndexUniforms = 2 + BufferIndexMeshPositions = 0, + BufferIndexMeshGenerics = 1, + BufferIndexMeshGridCoords = 2, + BufferIndexUniforms = 3, }; typedef NS_ENUM(NSInteger, VertexAttribute) { VertexAttributePosition = 0, VertexAttributeTexcoord = 1, + VertexAttributeGridCoord = 2, }; typedef NS_ENUM(NSInteger, TextureIndex) diff --git a/Terrain2/Shaders.metal b/Terrain2/Shaders.metal index 36759a9..86cf8fe 100644 --- a/Terrain2/Shaders.metal +++ b/Terrain2/Shaders.metal @@ -20,6 +20,7 @@ typedef struct { float3 position [[attribute(VertexAttributePosition)]]; float2 texCoord [[attribute(VertexAttributeTexcoord)]]; + uint2 gridCoord [[attribute(VertexAttributeGridCoord)]]; } Vertex; typedef struct @@ -29,6 +30,7 @@ typedef struct } ColorInOut; vertex ColorInOut vertexShader(Vertex in [[stage_in]], + texture2d heights [[ texture(1) ]], constant Uniforms & uniforms [[ buffer(BufferIndexUniforms) ]]) { ColorInOut out; diff --git a/Terrain2/Terrain.swift b/Terrain2/Terrain.swift index 23c08f7..d11cf3a 100644 --- a/Terrain2/Terrain.swift +++ b/Terrain2/Terrain.swift @@ -15,21 +15,29 @@ class Terrain: NSObject { class func buildVertexDescriptor() -> MTLVertexDescriptor { let mtlVertexDescriptor = MTLVertexDescriptor() - mtlVertexDescriptor.attributes[VertexAttribute.position.rawValue].format = MTLVertexFormat.float3 + mtlVertexDescriptor.attributes[VertexAttribute.position.rawValue].format = .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].format = .float2 mtlVertexDescriptor.attributes[VertexAttribute.texcoord.rawValue].offset = 0 mtlVertexDescriptor.attributes[VertexAttribute.texcoord.rawValue].bufferIndex = BufferIndex.meshGenerics.rawValue + mtlVertexDescriptor.attributes[VertexAttribute.gridCoord.rawValue].format = .uint2 + mtlVertexDescriptor.attributes[VertexAttribute.gridCoord.rawValue].offset = 0 + mtlVertexDescriptor.attributes[VertexAttribute.gridCoord.rawValue].bufferIndex = BufferIndex.meshGridCoords.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.meshPositions.rawValue].stepFunction = .perVertex mtlVertexDescriptor.layouts[BufferIndex.meshGenerics.rawValue].stride = 8 mtlVertexDescriptor.layouts[BufferIndex.meshGenerics.rawValue].stepRate = 1 - mtlVertexDescriptor.layouts[BufferIndex.meshGenerics.rawValue].stepFunction = MTLVertexStepFunction.perVertex + mtlVertexDescriptor.layouts[BufferIndex.meshGenerics.rawValue].stepFunction = .perVertex + + mtlVertexDescriptor.layouts[BufferIndex.meshGridCoords.rawValue].stride = MemoryLayout.stride + mtlVertexDescriptor.layouts[BufferIndex.meshGridCoords.rawValue].stepRate = 1 + mtlVertexDescriptor.layouts[BufferIndex.meshGridCoords.rawValue].stepFunction = .perVertex return mtlVertexDescriptor } @@ -64,6 +72,7 @@ class Terrain: NSObject { let segments: uint2 let vertexDescriptor: MTLVertexDescriptor let mesh: MTKMesh + let heights: MTLTexture init?(dimensions dim: float2, segments seg: uint2, device: MTLDevice) { dimensions = dim @@ -77,6 +86,14 @@ class Terrain: NSObject { return nil } + let heightsDesc = MTLTextureDescriptor.texture2DDescriptor(pixelFormat: .r16Float, width: Int(segments.x), height: Int(segments.y), mipmapped: false) + heightsDesc.usage = [.shaderRead, .shaderWrite] + guard let tex = device.makeTexture(descriptor: heightsDesc) else { + print("Couldn't create heights texture") + return nil + } + heights = tex + super.init() } }