diff --git a/06-textures/README.md b/06-textures/README.md new file mode 100644 index 0000000..860bbf1 --- /dev/null +++ b/06-textures/README.md @@ -0,0 +1,19 @@ +# Chapter 6: Textures + +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. + +## sRGB color space + +* Actually we can't do math on non-linear space because `1 + 1 != 2` in non-linear space. Let's explain a reason in detail. + +![](./gamma-correction-gamma-curves.png) + +Data in the reality are in linear space and those are encoded non-linear space because the limitation of memory. +It made sRGB color format that compromised between how cathode ray tube monitors worked and what colors the human eye sees. +Let's think about that you are calculating `0.5 + 0.5` and the result must be `1.0`. However `0.5` is in non-linear space +so it is `0.729` in linear space. So `0.5 + 0.5` in non-linear space equals to `0.729 + 0.729` so the result is `1.458` not `1.0`. +Thus when you do the math, you must check values are in linear space. + +* If texture's format is sRGB then OpenGL, Vulkan and DirectX convert from non-linear space to linear space automatically +when you fetch a texel with a sampler. However Metal don't do that. This book give a code how to convert from non-linear space to linear space. +But the code is not compiled when you copy. Try this code `baseColor = pow(baseColor, 1.0 / 2.2);`. You can do the gamma correction by yourself. diff --git a/06-textures/gamma-correction-gamma-curves.png b/06-textures/gamma-correction-gamma-curves.png new file mode 100644 index 0000000..d047c98 Binary files /dev/null and b/06-textures/gamma-correction-gamma-curves.png differ diff --git a/06-textures/starter/Texture/Texture/Texturable.swift b/06-textures/starter/Texture/Texture/Texturable.swift new file mode 100644 index 0000000..0cf990f --- /dev/null +++ b/06-textures/starter/Texture/Texture/Texturable.swift @@ -0,0 +1,33 @@ +// +/** + * Copyright (c) 2019 Razeware LLC + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * Notwithstanding the foregoing, you may not use, copy, modify, merge, publish, + * distribute, sublicense, create a derivative work, and/or sell copies of the + * Software in any work that is designed, intended, or marketed for pedagogical or + * instructional purposes related to programming, coding, application development, + * or information technology. Permission for such use, copying, modification, + * merger, publication, distribution, sublicensing, creation of derivative works, + * or sale is expressly withheld. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + */ + + +import Foundation diff --git a/06-textures/starter/Texture/Texture/Textures.xcassets/Contents.json b/06-textures/starter/Texture/Texture/Textures.xcassets/Contents.json new file mode 100644 index 0000000..73c0059 --- /dev/null +++ b/06-textures/starter/Texture/Texture/Textures.xcassets/Contents.json @@ -0,0 +1,6 @@ +{ + "info" : { + "author" : "xcode", + "version" : 1 + } +} diff --git a/06-textures/starter/Texture/Texture/Textures.xcassets/barn.textureset/Contents.json b/06-textures/starter/Texture/Texture/Textures.xcassets/barn.textureset/Contents.json new file mode 100644 index 0000000..6efbe32 --- /dev/null +++ b/06-textures/starter/Texture/Texture/Textures.xcassets/barn.textureset/Contents.json @@ -0,0 +1,16 @@ +{ + "info" : { + "author" : "xcode", + "version" : 1 + }, + "properties" : { + "interpretation" : "data" + }, + "textures" : [ + { + "filename" : "Universal.mipmapset", + "idiom" : "universal", + "pixel-format" : "astc-8x8" + } + ] +} diff --git a/06-textures/starter/Texture/Texture/Textures.xcassets/barn.textureset/Universal.mipmapset/Contents.json b/06-textures/starter/Texture/Texture/Textures.xcassets/barn.textureset/Universal.mipmapset/Contents.json new file mode 100644 index 0000000..27f83e8 --- /dev/null +++ b/06-textures/starter/Texture/Texture/Textures.xcassets/barn.textureset/Universal.mipmapset/Contents.json @@ -0,0 +1,12 @@ +{ + "info" : { + "author" : "xcode", + "version" : 1 + }, + "levels" : [ + { + "filename" : "lowpoly-barn-color.png", + "mipmap-level" : "base" + } + ] +} diff --git a/06-textures/starter/Texture/Texture/Textures.xcassets/barn.textureset/Universal.mipmapset/lowpoly-barn-color.png b/06-textures/starter/Texture/Texture/Textures.xcassets/barn.textureset/Universal.mipmapset/lowpoly-barn-color.png new file mode 100644 index 0000000..4760c8a Binary files /dev/null and b/06-textures/starter/Texture/Texture/Textures.xcassets/barn.textureset/Universal.mipmapset/lowpoly-barn-color.png differ diff --git a/06-textures/starter/Texture/Texture/Textures.xcassets/grass.textureset/Contents.json b/06-textures/starter/Texture/Texture/Textures.xcassets/grass.textureset/Contents.json new file mode 100644 index 0000000..6efbe32 --- /dev/null +++ b/06-textures/starter/Texture/Texture/Textures.xcassets/grass.textureset/Contents.json @@ -0,0 +1,16 @@ +{ + "info" : { + "author" : "xcode", + "version" : 1 + }, + "properties" : { + "interpretation" : "data" + }, + "textures" : [ + { + "filename" : "Universal.mipmapset", + "idiom" : "universal", + "pixel-format" : "astc-8x8" + } + ] +} diff --git a/06-textures/starter/Texture/Texture/Textures.xcassets/grass.textureset/Universal.mipmapset/Contents.json b/06-textures/starter/Texture/Texture/Textures.xcassets/grass.textureset/Universal.mipmapset/Contents.json new file mode 100644 index 0000000..e68fa22 --- /dev/null +++ b/06-textures/starter/Texture/Texture/Textures.xcassets/grass.textureset/Universal.mipmapset/Contents.json @@ -0,0 +1,12 @@ +{ + "info" : { + "author" : "xcode", + "version" : 1 + }, + "levels" : [ + { + "filename" : "barn-ground.png", + "mipmap-level" : "base" + } + ] +} diff --git a/06-textures/starter/Texture/Texture/Textures.xcassets/grass.textureset/Universal.mipmapset/barn-ground.png b/06-textures/starter/Texture/Texture/Textures.xcassets/grass.textureset/Universal.mipmapset/barn-ground.png new file mode 100644 index 0000000..1e51322 Binary files /dev/null and b/06-textures/starter/Texture/Texture/Textures.xcassets/grass.textureset/Universal.mipmapset/barn-ground.png differ diff --git a/README.md b/README.md index 0e28a45..937964b 100644 --- a/README.md +++ b/README.md @@ -17,6 +17,7 @@ You can also download the source code from [here](https://store.raywenderlich.co * [Chapter 3: The Rendering Pipeline](https://github.com/daemyung/MetalByTutorials/tree/main/03-rendering-pipeline) * [Chapter 4: Coordinate Spaces](https://github.com/daemyung/MetalByTutorials/tree/main/04-3d-transforms) * [Chapter 5: Lighting Fundamentals](https://github.com/daemyung/MetalByTutorials/tree/main/05-lighting-fundamentals) + * [Chapter 6: Textures](https://github.com/daemyung/MetalByTutorials/tree/main/06-textures) ## Copyright