Add grid coordinate argument encoding stuff and a texture to hold heights
This commit is contained in:
parent
a5c86f9c31
commit
dac8f24568
3 changed files with 28 additions and 7 deletions
|
@ -23,15 +23,17 @@
|
||||||
|
|
||||||
typedef NS_ENUM(NSInteger, BufferIndex)
|
typedef NS_ENUM(NSInteger, BufferIndex)
|
||||||
{
|
{
|
||||||
BufferIndexMeshPositions = 0,
|
BufferIndexMeshPositions = 0,
|
||||||
BufferIndexMeshGenerics = 1,
|
BufferIndexMeshGenerics = 1,
|
||||||
BufferIndexUniforms = 2
|
BufferIndexMeshGridCoords = 2,
|
||||||
|
BufferIndexUniforms = 3,
|
||||||
};
|
};
|
||||||
|
|
||||||
typedef NS_ENUM(NSInteger, VertexAttribute)
|
typedef NS_ENUM(NSInteger, VertexAttribute)
|
||||||
{
|
{
|
||||||
VertexAttributePosition = 0,
|
VertexAttributePosition = 0,
|
||||||
VertexAttributeTexcoord = 1,
|
VertexAttributeTexcoord = 1,
|
||||||
|
VertexAttributeGridCoord = 2,
|
||||||
};
|
};
|
||||||
|
|
||||||
typedef NS_ENUM(NSInteger, TextureIndex)
|
typedef NS_ENUM(NSInteger, TextureIndex)
|
||||||
|
|
|
@ -20,6 +20,7 @@ typedef struct
|
||||||
{
|
{
|
||||||
float3 position [[attribute(VertexAttributePosition)]];
|
float3 position [[attribute(VertexAttributePosition)]];
|
||||||
float2 texCoord [[attribute(VertexAttributeTexcoord)]];
|
float2 texCoord [[attribute(VertexAttributeTexcoord)]];
|
||||||
|
uint2 gridCoord [[attribute(VertexAttributeGridCoord)]];
|
||||||
} Vertex;
|
} Vertex;
|
||||||
|
|
||||||
typedef struct
|
typedef struct
|
||||||
|
@ -29,6 +30,7 @@ typedef struct
|
||||||
} ColorInOut;
|
} ColorInOut;
|
||||||
|
|
||||||
vertex ColorInOut vertexShader(Vertex in [[stage_in]],
|
vertex ColorInOut vertexShader(Vertex in [[stage_in]],
|
||||||
|
texture2d<half> heights [[ texture(1) ]],
|
||||||
constant Uniforms & uniforms [[ buffer(BufferIndexUniforms) ]])
|
constant Uniforms & uniforms [[ buffer(BufferIndexUniforms) ]])
|
||||||
{
|
{
|
||||||
ColorInOut out;
|
ColorInOut out;
|
||||||
|
|
|
@ -15,21 +15,29 @@ class Terrain: NSObject {
|
||||||
class func buildVertexDescriptor() -> MTLVertexDescriptor {
|
class func buildVertexDescriptor() -> MTLVertexDescriptor {
|
||||||
let mtlVertexDescriptor = 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].offset = 0
|
||||||
mtlVertexDescriptor.attributes[VertexAttribute.position.rawValue].bufferIndex = BufferIndex.meshPositions.rawValue
|
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].offset = 0
|
||||||
mtlVertexDescriptor.attributes[VertexAttribute.texcoord.rawValue].bufferIndex = BufferIndex.meshGenerics.rawValue
|
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].stride = 12
|
||||||
mtlVertexDescriptor.layouts[BufferIndex.meshPositions.rawValue].stepRate = 1
|
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].stride = 8
|
||||||
mtlVertexDescriptor.layouts[BufferIndex.meshGenerics.rawValue].stepRate = 1
|
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<uint2>.stride
|
||||||
|
mtlVertexDescriptor.layouts[BufferIndex.meshGridCoords.rawValue].stepRate = 1
|
||||||
|
mtlVertexDescriptor.layouts[BufferIndex.meshGridCoords.rawValue].stepFunction = .perVertex
|
||||||
|
|
||||||
return mtlVertexDescriptor
|
return mtlVertexDescriptor
|
||||||
}
|
}
|
||||||
|
@ -64,6 +72,7 @@ class Terrain: NSObject {
|
||||||
let segments: uint2
|
let segments: uint2
|
||||||
let vertexDescriptor: MTLVertexDescriptor
|
let vertexDescriptor: MTLVertexDescriptor
|
||||||
let mesh: MTKMesh
|
let mesh: MTKMesh
|
||||||
|
let heights: MTLTexture
|
||||||
|
|
||||||
init?(dimensions dim: float2, segments seg: uint2, device: MTLDevice) {
|
init?(dimensions dim: float2, segments seg: uint2, device: MTLDevice) {
|
||||||
dimensions = dim
|
dimensions = dim
|
||||||
|
@ -77,6 +86,14 @@ class Terrain: NSObject {
|
||||||
return nil
|
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()
|
super.init()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue