Errata for Metal by Tutorials 3rd Edition

[Chapter 10 Lighting Fundamentals]

In Ambient Reflection part, I noticed the intensity attribute is not there as it says in the book description

Common.h:

typedef struct {
  LightType type;
  vector_float3 position;
  vector_float3 color;
  vector_float3 specularColor;
  float radius;
  vector_float3 attenuation;
  float coneAngle;
  vector_float3 coneDirection;
  float coneAttenuation;
  float intensity;                                 // <-- insert here
} Light;

SceneLighting.swift:

struct Lighting {
  static func buildDefaultLight() -> Light {
    var light = Light()
    light.position = [0, 0, 0]
    light.color = [1, 1, 1]
    light.specularColor = [0.6, 0.6, 0.6]
    light.attenuation = [1, 0, 0]
    light.type = Sun
    light.intensity = 0.1                          // <-- insert here
    return light
  }
  let ambientLight: Light = {
    var light = Self.buildDefaultLight()
    light.color = [0.05, 0.1, 0]
    light.intensity = 0.5                         // <-- change property here
    light.type = Ambient
    return light
  }()

Lighting.metal:

float3 phongLighting(...) {
  // code
  case Ambient: {
     ambientColor += light.color * light.intensity; // <-- that right ?
     break;
  }
  // code
}
1 Like

Thank you @nghiaphamsg! Yes, you are correct.

In chapter 11: Maps & Materials
I think .float type roughness will be more reasonable with the code below

1 Like

Thank you. I think you are correct.

Thinking back to olden times, I believe float didn’t work for this, but I can’t remember what file format I was using. So I would advise that whatever file format you’re using, you check that the output matches your expectation. If this isn’t a recent Apple change, I obviously didn’t.

When using texture maps, the roughness value is ignored in the current code. That’s another point. I was looking at some code that uses the diffuse material colour as a scalar on the diffuse texture, whereas the book’s code just ignores the material if there is a texture. Maybe the code should use the roughness value as a scalar. Up to you.

Chapter 23 broken link WWDC17 - Videos - Apple Developer

Hi,
Chapter 6: When building the ViewMatrix there’s a typo in the code used to indicate where the insertion is to be made:

renderEncoder.setVertexBytes(
   &uniforms,
   length: MemoryLayout<Uniforms>.stride,
   index: 1)

It refers to index: 1 where it should be index: 11

Also, could I suggest that this line be removed from the Renderer’s init(metalView:) as it achieves nothing?

uniforms.viewMatrix = float4x4(translation: [0.8, 0, 0]).inverse

Regards

1 Like

Huh. Well spotted!

Yes, it’s overridden by the assignment in draw(in:).

Thank you.