diff --git a/Terrain2/Shaders/TerrainAlgorithms.metal b/Terrain2/Shaders/TerrainAlgorithms.metal index b6a14a9..04c2b93 100644 --- a/Terrain2/Shaders/TerrainAlgorithms.metal +++ b/Terrain2/Shaders/TerrainAlgorithms.metal @@ -13,6 +13,11 @@ using namespace metal; +inline uint segmentIndex(uint2 pos, uint2 dims) +{ + return pos.y * dims.x + pos.x; +} + kernel void updateGeometryHeights(texture2d texture [[texture(GeneratorTextureIndexIn)]], constant float2 *texCoords [[buffer(GeneratorBufferIndexTexCoords)]], constant Uniforms &uniforms [[buffer(GeneratorBufferIndexUniforms)]], @@ -50,12 +55,67 @@ kernel void updateGeometryNormals(constant packed_float3 *meshPositions [[buffer faceMidpoints[tid] = (1.0 / 3.0) * (v1 + v2 + v3); } +/// Update the vertex normals based on the computed face normals. +/// +/// Credit for this procedure goes to https://github.com/emilyhorsman. +/// +/// {A..F} are the adjacent face normals to the vertex @. +/// +/// (0, 0) *----*----* +/// | /| /| +/// | / | / | +/// | / |F/ | +/// |/ A|/E | +/// *----@----* +/// | B/|D /| +/// | /C| / | +/// | / | / | +/// |/ |/ | +/// *----*----* (2, 2) +/// +/// Adding each vector divided by n would be better, but numerical stability +/// isn't low risk here since these vectors are all normalized. +/// kernel void updateGeometryVertexNormals(constant packed_float3 *meshPositions [[buffer(GeneratorBufferIndexMeshPositions)]], - constant packed_ushort3 *indexes [[buffer(GeneratorBufferIndexIndexes)]], constant packed_float3 *faceNormals [[buffer(GeneratorBufferIndexFaceNormals)]], + constant Uniforms &uniforms [[buffer(GeneratorBufferIndexUniforms)]], device packed_float3 *vertexNormals [[buffer(GeneratorBufferIndexNormals)]], uint2 tid [[thread_position_in_grid]]) { + float3 normal = float3(); + + uint adjacent = 0; + + if (tid.y > 0 && tid.x > 0) { + uint aIndex = 2 * segmentIndex(uint2(tid.x - 1, tid.y - 1), uniforms.terrainSegments) + 1; + normal += faceNormals[aIndex]; + adjacent += 1; + } + if (tid.y > 0 && tid.x < (uniforms.terrainSegments.x - 1)) { + uint segment = segmentIndex(uint2(tid.x, tid.y - 1), uniforms.terrainSegments); + uint bIndex = 2 * segment; + uint cIndex = 2 * segment + 1; + normal += faceNormals[bIndex] + faceNormals[cIndex]; + adjacent += 2; + } + if (tid.x < (uniforms.terrainSegments.x - 1) && tid.y < (uniforms.terrainSegments.y - 1)) { + uint dIndex = 2 * segmentIndex(tid, uniforms.terrainSegments); + normal += faceNormals[dIndex]; + adjacent += 1; + } + if (tid.x > 0 && tid.y < (uniforms.terrainSegments.y - 1)) { + uint segment = segmentIndex(uint2(tid.x - 1, tid.y), uniforms.terrainSegments); + uint eIndex = 2 * segment + 1; + uint fIndex = 2 * segment; + normal += faceNormals[eIndex] + faceNormals[fIndex]; + adjacent += 2; + } + + if (adjacent != 0) { + normal = normalize(normal / float(adjacent)); + uint vertexNormalIndex = segmentIndex(tid, uniforms.terrainSegments); + vertexNormals[vertexNormalIndex] = normal; + } } #pragma mark - ZeroGenerator diff --git a/Terrain2/Terrain.swift b/Terrain2/Terrain.swift index b34396c..5f6c26b 100644 --- a/Terrain2/Terrain.swift +++ b/Terrain2/Terrain.swift @@ -200,6 +200,7 @@ class Terrain: NSObject { computeEncoder.setBuffer(faceNormalsBuffer, offset: 0, index: GeneratorBufferIndex.faceNormals.rawValue) let normalsBuffer = mesh.vertexBuffers[BufferIndex.normals.rawValue] computeEncoder.setBuffer(normalsBuffer.buffer, offset: normalsBuffer.offset, index: GeneratorBufferIndex.normals.rawValue) + computeEncoder.setBuffer(uniforms.buffer, offset: uniforms.offset, index: GeneratorBufferIndex.uniforms.rawValue) computeEncoder.dispatchThreads(MTLSize(width: 2 * Int(segments.x * segments.y), height: 1, depth: 1), threadsPerThreadgroup: MTLSize(width: 8, height: 8, depth: 1)) computeEncoder.popDebugGroup() computeEncoder.endEncoding()