Pass the normal matrix through and multiply the normal by it
This commit is contained in:
parent
e99cd03446
commit
952204a5b6
2 changed files with 6 additions and 9 deletions
|
@ -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
|
||||
|
|
|
@ -33,18 +33,12 @@ typedef struct
|
|||
#pragma mark - Geometry Shaders
|
||||
|
||||
vertex ColorInOut vertexShader(Vertex in [[stage_in]],
|
||||
texture2d<float> 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;
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue