diff --git a/20-advanced-lighting/README.md b/20-advanced-lighting/README.md new file mode 100644 index 0000000..0ba8165 --- /dev/null +++ b/20-advanced-lighting/README.md @@ -0,0 +1,94 @@ +# Chapter 20: Advanced Lighting + +As I read this book, I summarize what I think is wrong. If you think my comments are wrong then please let me know. We can dicuss more and update your opinion. + +## Getting started + +There is a runtime error in because Shaders.metal files isn't included in the bundle. But we have the default library. + +In Renderer.swift, in initializeMetal(), replace this: + +``` +guard let path = Bundle.main.path(forResource: "Shaders", ofType: "metal") else { fatalError() } +let input = try String(contentsOfFile: path, encoding: String.Encoding.utf8) +let library = try device.makeLibrary(source: input, options: nil) +``` + +With: + +``` +let library = device.makeDefaultLibrary()! +``` + +## Drawing a checkerboard pattern + +This book explains the different between truncated division and floored division but it's not cinch to understand. Below figure will help you understand easily. + +![](./difference-divisions.jpeg) + +## Refraction + +There is a runtime error in because Shaders.metal files isn't included in the bundle. But we have the default library. + +In Renderer.swift, in initializeMetal(), replace this: + +``` +guard let path = Bundle.main.path(forResource: "Shaders", ofType: "metal") else { fatalError() } +let input = try String(contentsOfFile: path, encoding: String.Encoding.utf8) +let library = try device.makeLibrary(source: input, options: nil) +``` + +With: + +``` +let library = device.makeDefaultLibrary()! +``` + +## Raytraced water + +There is a runtime error in because Shaders.metal files isn't included in the bundle. But we have the default library. + +In Renderer.swift, in initializeMetal(), remove this: + +``` +let path = Bundle.main.path(forResource: "Shaders", ofType: "metal") +``` + +Replace this: + +``` +let input = try String(contentsOfFile: path!, encoding: String.Encoding.utf8) +let library = try device!.makeLibrary(source: input, options: nil) +``` + +With: + +``` +let library = device.makeDefaultLibrary()! +``` + +## 3. Clipping planes + +This book uses vector_float4 to define a clip plane without any explanation. The plane consists of the normal of plane and the distance from the origin. From a below figure, we can know the plane can be represented by `ax + by + cz + d = 0`. You can get more detail at [here](https://brilliant.org/wiki/3d-coordinate-geometry-equation-of-a-plane) + +![](./plane.png) + +Thus float4 is the best type to store the plane. First xyz is the normal and w is distance. + +In Renderer.swift, you have added this line in draw(in:) below // Water render: + +``` +var clipPlane = float4(0, 1, 0, 0.1) +``` + +Now you can understand what it is. This plane is pointing towards the positive y axis and located at 0.1 on the y axis. + +Maybe you can't understand why this book uses dot product for the clip distance. Let's recap the dot product to understand this. + +![](./dot-product.png) + +One of the feature of the dot product is projection. From doing dot product between a vertex position and a clip plane, you can know this vertex position is before that plane or after that plane. If a position is located before the plane then the result of dot product is negative. In other case, the result is positive. Now you can easily understand this: + +``` +vertex_out.clip_distance[0] = dot(uniforms.modelMatrix * vertex_in.position, uniforms.clipPlane); +``` diff --git a/20-advanced-lighting/difference-divisions.jpeg b/20-advanced-lighting/difference-divisions.jpeg new file mode 100644 index 0000000..15cd6bb Binary files /dev/null and b/20-advanced-lighting/difference-divisions.jpeg differ diff --git a/20-advanced-lighting/dot-product.png b/20-advanced-lighting/dot-product.png new file mode 100644 index 0000000..7cc452c Binary files /dev/null and b/20-advanced-lighting/dot-product.png differ diff --git a/20-advanced-lighting/plane.png b/20-advanced-lighting/plane.png new file mode 100644 index 0000000..775c513 Binary files /dev/null and b/20-advanced-lighting/plane.png differ diff --git a/README.md b/README.md index 46dacdc..9d5c65e 100644 --- a/README.md +++ b/README.md @@ -33,6 +33,7 @@ You can see color images in this book at [here](https://www.raywenderlich.com/bo * [Chapter 16: Particle Systems](https://github.com/daemyung/metal-by-tutorials/tree/main/16-particle-systems) * [Chapter 18: Rendering with Rays](https://github.com/daemyung/metal-by-tutorials/tree/main/18-rendering-with-rays) * [Chapter 19: Advanced Shadows](https://github.com/daemyung/metal-by-tutorials/tree/main/19-shadows) + * [Chapter 20: Advanced Lighting](https://github.com/daemyung/metal-by-tutorials/tree/main/20-advanced-lighting) ## Copyright