Use a fixed size 512x512 texture for the height map
This will make implementing other algorithms easier. :D
This commit is contained in:
parent
af5e5a6123
commit
96730ecd41
3 changed files with 17 additions and 16 deletions
|
@ -61,7 +61,7 @@ class Renderer: NSObject, MTKViewDelegate {
|
||||||
metalKitView.colorPixelFormat = MTLPixelFormat.bgra8Unorm_srgb
|
metalKitView.colorPixelFormat = MTLPixelFormat.bgra8Unorm_srgb
|
||||||
metalKitView.sampleCount = 1
|
metalKitView.sampleCount = 1
|
||||||
|
|
||||||
terrain = Terrain(dimensions: float2(8, 8), segments: uint2(20, 20), device: device)!
|
terrain = Terrain(dimensions: float2(10, 10), segments: uint2(100, 100), device: device)!
|
||||||
|
|
||||||
do {
|
do {
|
||||||
pipelineState = try Renderer.buildRenderPipelineWithDevice(device: device,
|
pipelineState = try Renderer.buildRenderPipelineWithDevice(device: device,
|
||||||
|
@ -145,11 +145,11 @@ class Renderer: NSObject, MTKViewDelegate {
|
||||||
|
|
||||||
uniforms[0].projectionMatrix = projectionMatrix
|
uniforms[0].projectionMatrix = projectionMatrix
|
||||||
|
|
||||||
let rotationAxis = float3(1, 1, 0)
|
let rotationAxis = float3(0, 1, 0)
|
||||||
let modelMatrix = matrix4x4_rotation(radians: rotation, axis: rotationAxis)
|
let modelMatrix = matrix4x4_rotation(radians: rotation, axis: rotationAxis)
|
||||||
let viewMatrix = matrix4x4_translation(0.0, 0.0, -8.0)
|
let viewMatrix = matrix4x4_translation(0.0, -2.0, -8.0)
|
||||||
uniforms[0].modelViewMatrix = simd_mul(viewMatrix, modelMatrix)
|
uniforms[0].modelViewMatrix = simd_mul(viewMatrix, modelMatrix)
|
||||||
rotation += 0.01
|
rotation += 0.0025
|
||||||
}
|
}
|
||||||
|
|
||||||
func draw(in view: MTKView) {
|
func draw(in view: MTKView) {
|
||||||
|
@ -203,7 +203,7 @@ class Renderer: NSObject, MTKViewDelegate {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
renderEncoder.setVertexTexture(terrain.heights, index: 0)
|
renderEncoder.setVertexTexture(terrain.heightMap, index: 0)
|
||||||
renderEncoder.setFragmentTexture(colorMap, index: TextureIndex.color.rawValue)
|
renderEncoder.setFragmentTexture(colorMap, index: TextureIndex.color.rawValue)
|
||||||
|
|
||||||
for submesh in terrain.mesh.submeshes {
|
for submesh in terrain.mesh.submeshes {
|
||||||
|
|
|
@ -33,14 +33,13 @@ vertex ColorInOut vertexShader(Vertex in [[stage_in]],
|
||||||
texture2d<float> heights [[texture(0)]],
|
texture2d<float> heights [[texture(0)]],
|
||||||
constant Uniforms & uniforms [[buffer(BufferIndexUniforms)]])
|
constant Uniforms & uniforms [[buffer(BufferIndexUniforms)]])
|
||||||
{
|
{
|
||||||
constexpr sampler s(coord::pixel, address::clamp_to_zero, filter::linear);
|
constexpr sampler s(coord::normalized, address::clamp_to_zero, filter::linear);
|
||||||
|
|
||||||
ColorInOut out;
|
ColorInOut out;
|
||||||
|
|
||||||
// TODO: Concerned about this.
|
float4 height = heights.sample(s, in.texCoord);
|
||||||
uint2 gridCoord(in.texCoord.x * heights.get_width(), in.texCoord.y * heights.get_height());
|
|
||||||
float4 height = heights.read(gridCoord).rrrr;
|
|
||||||
|
|
||||||
|
// Replace the y coordinate with the height we read from the texture.
|
||||||
float4 position(in.position.x, height.r, in.position.z, 1.0);
|
float4 position(in.position.x, height.r, in.position.z, 1.0);
|
||||||
out.position = uniforms.projectionMatrix * uniforms.modelViewMatrix * position;
|
out.position = uniforms.projectionMatrix * uniforms.modelViewMatrix * position;
|
||||||
out.texCoord = in.texCoord;
|
out.texCoord = in.texCoord;
|
||||||
|
@ -58,5 +57,5 @@ fragment float4 fragmentShader(ColorInOut in [[stage_in]],
|
||||||
|
|
||||||
half4 colorSample = colorMap.sample(colorSampler, in.texCoord.xy);
|
half4 colorSample = colorMap.sample(colorSampler, in.texCoord.xy);
|
||||||
|
|
||||||
return float4(colorSample);
|
return float4(1.0);
|
||||||
}
|
}
|
||||||
|
|
|
@ -70,9 +70,12 @@ class Terrain: NSObject {
|
||||||
return try MTKMesh(mesh:plane, device:device)
|
return try MTKMesh(mesh:plane, device:device)
|
||||||
}
|
}
|
||||||
|
|
||||||
class func buildHeightsTexture(dimensions: uint2, device: MTLDevice) -> MTLTexture? {
|
private static let heightMapSize = MTLSize(width: 512, height: 512, depth: 1)
|
||||||
let heightsDesc = MTLTextureDescriptor.texture2DDescriptor(pixelFormat: .r32Float, width: Int(dimensions.x), height: Int(dimensions.y), mipmapped: false)
|
|
||||||
|
class func buildHeightsTexture(device: MTLDevice) -> MTLTexture? {
|
||||||
|
let heightsDesc = MTLTextureDescriptor.texture2DDescriptor(pixelFormat: .r32Float, width: heightMapSize.width, height: heightMapSize.height, mipmapped: false)
|
||||||
heightsDesc.usage = [.shaderRead, .shaderWrite]
|
heightsDesc.usage = [.shaderRead, .shaderWrite]
|
||||||
|
|
||||||
let tex = device.makeTexture(descriptor: heightsDesc)
|
let tex = device.makeTexture(descriptor: heightsDesc)
|
||||||
|
|
||||||
if let tex = tex {
|
if let tex = tex {
|
||||||
|
@ -96,7 +99,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
|
let heightMap: MTLTexture
|
||||||
|
|
||||||
init?(dimensions dim: float2, segments seg: uint2, device: MTLDevice) {
|
init?(dimensions dim: float2, segments seg: uint2, device: MTLDevice) {
|
||||||
dimensions = dim
|
dimensions = dim
|
||||||
|
@ -110,12 +113,11 @@ class Terrain: NSObject {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
let heightsDimensions = segments
|
guard let tex = Terrain.buildHeightsTexture(device: device) else {
|
||||||
guard let tex = Terrain.buildHeightsTexture(dimensions: heightsDimensions, device: device) else {
|
|
||||||
print("Couldn't create heights texture")
|
print("Couldn't create heights texture")
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
heights = tex
|
heightMap = tex
|
||||||
|
|
||||||
super.init()
|
super.init()
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue