Summerize Chapter 20: Advanced Lighting

This commit is contained in:
daemyung jang 2022-04-07 09:06:46 +09:00
parent ea2b9fc758
commit de98052d5b
No known key found for this signature in database
GPG Key ID: 44160C9958A9C67B
5 changed files with 95 additions and 0 deletions

View File

@ -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);
```

Binary file not shown.

After

Width:  |  Height:  |  Size: 28 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 3.5 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 84 KiB

View File

@ -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