diff --git a/Terrain2/Renderer.swift b/Terrain2/Renderer.swift index 6b9ecb8..69294e3 100644 --- a/Terrain2/Renderer.swift +++ b/Terrain2/Renderer.swift @@ -210,8 +210,11 @@ class Renderer: NSObject, MTKViewDelegate { uniforms[0].modelViewMatrix = modelViewMatrix rotation += 0.003 + // Remove the fourth row and column from our model-view matrix and find its inverse-transpose, if it exists. let rotSclModelViewMatrix = float3x3(modelViewMatrix.columns.0.xyz, modelViewMatrix.columns.1.xyz, modelViewMatrix.columns.2.xyz) - uniforms[0].normalMatrix = rotSclModelViewMatrix.inverse.transpose + if rotSclModelViewMatrix.determinant != 0 { + uniforms[0].normalMatrix = rotSclModelViewMatrix.inverse.transpose + } uniforms[0].terrainDimensions = terrain.dimensions uniforms[0].terrainSegments = terrain.segments diff --git a/Terrain2/Shaders/Shaders.metal b/Terrain2/Shaders/Shaders.metal index 0db45a5..0198505 100644 --- a/Terrain2/Shaders/Shaders.metal +++ b/Terrain2/Shaders/Shaders.metal @@ -33,18 +33,12 @@ typedef struct #pragma mark - Geometry Shaders vertex ColorInOut vertexShader(Vertex in [[stage_in]], - texture2d heights [[texture(0)]], - constant Uniforms & uniforms [[buffer(BufferIndexUniforms)]]) + constant Uniforms &uniforms [[buffer(BufferIndexUniforms)]]) { -// constexpr sampler s(coord::normalized, address::clamp_to_zero, filter::linear); - ColorInOut out; -// float4 height = heights.sample(s, in.texCoord); - - // Replace the y coordinate with the height we read from the texture. out.position = uniforms.projectionMatrix * uniforms.modelViewMatrix * float4(in.position, 1.0); - out.normal = in.normal; + out.normal = uniforms.normalMatrix * in.normal; out.texCoord = in.texCoord; return out;