diff --git a/Assets/Materials/Chapter13/IntersectionHighlightsMat.mat b/Assets/Materials/Chapter13/IntersectionHighlightsMat.mat deleted file mode 100644 index b3e86b65..00000000 Binary files a/Assets/Materials/Chapter13/IntersectionHighlightsMat.mat and /dev/null differ diff --git a/Assets/Materials/Chapter13/IntersectionHighlightsMat.mat.meta b/Assets/Materials/Chapter13/IntersectionHighlightsMat.mat.meta deleted file mode 100644 index e35bdb25..00000000 --- a/Assets/Materials/Chapter13/IntersectionHighlightsMat.mat.meta +++ /dev/null @@ -1,8 +0,0 @@ -fileFormatVersion: 2 -guid: 20907a2f498e14d2aae4bd1d58abb151 -timeCreated: 1443588251 -licenseType: Free -NativeFormatImporter: - userData: - assetBundleName: - assetBundleVariant: diff --git a/Assets/Materials/Chapter6/LIghtSourceMat.mat b/Assets/Materials/Chapter6/LIghtSourceMat.mat deleted file mode 100644 index 3387f71c..00000000 Binary files a/Assets/Materials/Chapter6/LIghtSourceMat.mat and /dev/null differ diff --git a/Assets/Materials/Chapter6/LIghtSourceMat.mat.meta b/Assets/Materials/Chapter6/LIghtSourceMat.mat.meta deleted file mode 100644 index e11857ed..00000000 --- a/Assets/Materials/Chapter6/LIghtSourceMat.mat.meta +++ /dev/null @@ -1,8 +0,0 @@ -fileFormatVersion: 2 -guid: 8101f26df3d954611becbee9bbf26ba2 -timeCreated: 1437700548 -licenseType: Free -NativeFormatImporter: - userData: - assetBundleName: - assetBundleVariant: diff --git a/Assets/Shaders/Chapter11/Chapter11-Billboard.shader b/Assets/Shaders/Chapter11/Chapter11-Billboard.shader index 3d616976..59340868 100644 --- a/Assets/Shaders/Chapter11/Chapter11-Billboard.shader +++ b/Assets/Shaders/Chapter11/Chapter11-Billboard.shader @@ -59,7 +59,7 @@ // Use the three vectors to rotate the quad float3 centerOffs = v.vertex.xyz - center; - float3 localPos = center + rightDir * centerOffs.x + upDir * centerOffs.y + normalDir.z * centerOffs.z; + float3 localPos = center + rightDir * centerOffs.x + upDir * centerOffs.y + normalDir * centerOffs.z; o.pos = mul(UNITY_MATRIX_MVP, float4(localPos, 1)); o.uv = TRANSFORM_TEX(v.texcoord,_MainTex); diff --git a/Assets/Shaders/Chapter11/Chapter11-ImageSequenceAnimation.shader b/Assets/Shaders/Chapter11/Chapter11-ImageSequenceAnimation.shader index 9fd3c4cb..c2337494 100644 --- a/Assets/Shaders/Chapter11/Chapter11-ImageSequenceAnimation.shader +++ b/Assets/Shaders/Chapter11/Chapter11-ImageSequenceAnimation.shader @@ -49,7 +49,7 @@ fixed4 frag (v2f i) : SV_Target { float time = floor(_Time.y * _Speed); float row = floor(time / _HorizontalAmount); - float column = time - row * _VerticalAmount; + float column = time - row * _HorizontalAmount; // half2 uv = float2(i.uv.x /_HorizontalAmount, i.uv.y / _VerticalAmount); // uv.x += column / _HorizontalAmount; diff --git a/Assets/Shaders/Chapter12/Chapter12-EdgeDetection.shader b/Assets/Shaders/Chapter12/Chapter12-EdgeDetection.shader index 58987f8d..dc919dee 100644 --- a/Assets/Shaders/Chapter12/Chapter12-EdgeDetection.shader +++ b/Assets/Shaders/Chapter12/Chapter12-EdgeDetection.shader @@ -1,4 +1,4 @@ -Shader "Unity Shaders Book/Chapter 12/Edge Detection" { +Shader "Unity Shaders Book/Chapter 12/Edge Detection" { Properties { _MainTex ("Base (RGB)", 2D) = "white" {} _EdgeOnly ("Edge Only", Float) = 1.0 @@ -51,12 +51,12 @@ } half Sobel(v2f i) { - const half Gx[9] = {-1, -2, -1, - 0, 0, 0, - 1, 2, 1}; - const half Gy[9] = {-1, 0, 1, + const half Gx[9] = {-1, 0, 1, -2, 0, 2, - -1, 0, 1}; + -1, 0, 1}; + const half Gy[9] = {-1, -2, -1, + 0, 0, 0, + 1, 2, 1}; half texColor; half edgeX = 0; diff --git a/Assets/Shaders/Chapter12/Chapter12-GaussianBlur.shader b/Assets/Shaders/Chapter12/Chapter12-GaussianBlur.shader index c9b05a39..dd284392 100644 --- a/Assets/Shaders/Chapter12/Chapter12-GaussianBlur.shader +++ b/Assets/Shaders/Chapter12/Chapter12-GaussianBlur.shader @@ -53,8 +53,8 @@ fixed3 sum = tex2D(_MainTex, i.uv[0]).rgb * weight[0]; for (int it = 1; it < 3; it++) { - sum += tex2D(_MainTex, i.uv[it]).rgb * weight[it]; - sum += tex2D(_MainTex, i.uv[2*it]).rgb * weight[it]; + sum += tex2D(_MainTex, i.uv[it*2-1]).rgb * weight[it]; + sum += tex2D(_MainTex, i.uv[it*2]).rgb * weight[it]; } return fixed4(sum, 1.0); diff --git a/Assets/Shaders/Chapter5/Chapter5-SimpleShader.shader b/Assets/Shaders/Chapter5/Chapter5-SimpleShader.shader index e929c3a0..03f7daf0 100644 --- a/Assets/Shaders/Chapter5/Chapter5-SimpleShader.shader +++ b/Assets/Shaders/Chapter5/Chapter5-SimpleShader.shader @@ -22,7 +22,7 @@ fixed3 color : COLOR0; }; - v2f vert(a2v v) : SV_POSITION { + v2f vert(a2v v) { v2f o; o.pos = mul(UNITY_MATRIX_MVP, v.vertex); o.color = v.normal * 0.5 + fixed3(0.5, 0.5, 0.5); diff --git a/Assets/Shaders/Chapter6/Chapter6-BlinnPhong.shader b/Assets/Shaders/Chapter6/Chapter6-BlinnPhong.shader index 8bad33b5..808f005b 100644 --- a/Assets/Shaders/Chapter6/Chapter6-BlinnPhong.shader +++ b/Assets/Shaders/Chapter6/Chapter6-BlinnPhong.shader @@ -35,7 +35,7 @@ // Transform the vertex from object space to projection space o.pos = mul(UNITY_MATRIX_MVP, v.vertex); - // Transform the normal fram object space to world space + // Transform the normal from object space to world space o.worldNormal = mul(v.normal, (float3x3)_World2Object); // Transform the vertex from object spacet to world space diff --git a/Assets/Shaders/Chapter6/Chapter6-DiffusePixelLevel.shader b/Assets/Shaders/Chapter6/Chapter6-DiffusePixelLevel.shader index 4f024b65..b4cc4f92 100644 --- a/Assets/Shaders/Chapter6/Chapter6-DiffusePixelLevel.shader +++ b/Assets/Shaders/Chapter6/Chapter6-DiffusePixelLevel.shader @@ -30,7 +30,7 @@ // Transform the vertex from object space to projection space o.pos = mul(UNITY_MATRIX_MVP, v.vertex); - // Transform the normal fram object space to world space + // Transform the normal from object space to world space o.worldNormal = mul(v.normal, (float3x3)_World2Object); return o; diff --git a/Assets/Shaders/Chapter6/Chapter6-DiffuseVertexLevel.shader b/Assets/Shaders/Chapter6/Chapter6-DiffuseVertexLevel.shader index 219daa67..2af58067 100644 --- a/Assets/Shaders/Chapter6/Chapter6-DiffuseVertexLevel.shader +++ b/Assets/Shaders/Chapter6/Chapter6-DiffuseVertexLevel.shader @@ -33,7 +33,7 @@ // Get ambient term fixed3 ambient = UNITY_LIGHTMODEL_AMBIENT.xyz; - // Transform the normal fram object space to world space + // Transform the normal from object space to world space fixed3 worldNormal = normalize(mul(v.normal, (float3x3)_World2Object)); // Get the light direction in world space fixed3 worldLight = normalize(_WorldSpaceLightPos0.xyz); diff --git a/Assets/Shaders/Chapter6/Chapter6-HalfLambert.shader b/Assets/Shaders/Chapter6/Chapter6-HalfLambert.shader index 67541ab8..eaaef128 100644 --- a/Assets/Shaders/Chapter6/Chapter6-HalfLambert.shader +++ b/Assets/Shaders/Chapter6/Chapter6-HalfLambert.shader @@ -30,7 +30,7 @@ // Transform the vertex from object space to projection space o.pos = mul(UNITY_MATRIX_MVP, v.vertex); - // Transform the normal fram object space to world space + // Transform the normal from object space to world space o.worldNormal = mul(v.normal, (float3x3)_World2Object); return o; diff --git a/Assets/Shaders/Chapter6/Chapter6-SpecularPixelLevel.shader b/Assets/Shaders/Chapter6/Chapter6-SpecularPixelLevel.shader index aec13a3f..02ac2836 100644 --- a/Assets/Shaders/Chapter6/Chapter6-SpecularPixelLevel.shader +++ b/Assets/Shaders/Chapter6/Chapter6-SpecularPixelLevel.shader @@ -35,7 +35,7 @@ // Transform the vertex from object space to projection space o.pos = mul(UNITY_MATRIX_MVP, v.vertex); - // Transform the normal fram object space to world space + // Transform the normal from object space to world space o.worldNormal = mul(v.normal, (float3x3)_World2Object); // Transform the vertex from object spacet to world space o.worldPos = mul(_Object2World, v.vertex).xyz; diff --git a/Assets/Shaders/Chapter6/Chapter6-SpecularVertexLevel.shader b/Assets/Shaders/Chapter6/Chapter6-SpecularVertexLevel.shader index ac6cbcdf..ab1ee409 100644 --- a/Assets/Shaders/Chapter6/Chapter6-SpecularVertexLevel.shader +++ b/Assets/Shaders/Chapter6/Chapter6-SpecularVertexLevel.shader @@ -37,7 +37,7 @@ // Get ambient term fixed3 ambient = UNITY_LIGHTMODEL_AMBIENT.xyz; - // Transform the normal fram object space to world space + // Transform the normal from object space to world space fixed3 worldNormal = normalize(mul(v.normal, (float3x3)_World2Object)); // Get the light direction in world space fixed3 worldLightDir = normalize(_WorldSpaceLightPos0.xyz); diff --git a/Assets/Shaders/Chapter7/Chapter7-NormalMapTangentSpace.shader b/Assets/Shaders/Chapter7/Chapter7-NormalMapTangentSpace.shader index 44501664..c8bb1701 100644 --- a/Assets/Shaders/Chapter7/Chapter7-NormalMapTangentSpace.shader +++ b/Assets/Shaders/Chapter7/Chapter7-NormalMapTangentSpace.shader @@ -1,4 +1,4 @@ -Shader "Unity Shaders Book/Chapter 7/Normal Map In Tangent Space" { +Shader "Unity Shaders Book/Chapter 7/Normal Map In Tangent Space" { Properties { _Color ("Color Tint", Color) = (1, 1, 1, 1) _MainTex ("Main Tex", 2D) = "white" {} @@ -40,25 +40,86 @@ float3 lightDir: TEXCOORD1; float3 viewDir : TEXCOORD2; }; - + + // Unity doesn't support the 'inverse' function in native shader + // so we write one by our own + // Note: this function is just a demonstration, not too confident on the math or the speed + // Reference: http://answers.unity3d.com/questions/218333/shader-inversefloat4x4-function.html + float4x4 inverse(float4x4 input) { + #define minor(a,b,c) determinant(float3x3(input.a, input.b, input.c)) + + float4x4 cofactors = float4x4( + minor(_22_23_24, _32_33_34, _42_43_44), + -minor(_21_23_24, _31_33_34, _41_43_44), + minor(_21_22_24, _31_32_34, _41_42_44), + -minor(_21_22_23, _31_32_33, _41_42_43), + + -minor(_12_13_14, _32_33_34, _42_43_44), + minor(_11_13_14, _31_33_34, _41_43_44), + -minor(_11_12_14, _31_32_34, _41_42_44), + minor(_11_12_13, _31_32_33, _41_42_43), + + minor(_12_13_14, _22_23_24, _42_43_44), + -minor(_11_13_14, _21_23_24, _41_43_44), + minor(_11_12_14, _21_22_24, _41_42_44), + -minor(_11_12_13, _21_22_23, _41_42_43), + + -minor(_12_13_14, _22_23_24, _32_33_34), + minor(_11_13_14, _21_23_24, _31_33_34), + -minor(_11_12_14, _21_22_24, _31_32_34), + minor(_11_12_13, _21_22_23, _31_32_33) + ); + #undef minor + return transpose(cofactors) / determinant(input); + } + v2f vert(a2v v) { v2f o; o.pos = mul(UNITY_MATRIX_MVP, v.vertex); o.uv.xy = v.texcoord.xy * _MainTex_ST.xy + _MainTex_ST.zw; o.uv.zw = v.texcoord.xy * _BumpMap_ST.xy + _BumpMap_ST.zw; + + /// + /// Note that the code below can handle both uniform and non-uniform scales + /// + + // Construct a matrix that transforms a point/vector from tangent space to world space + fixed3 worldNormal = UnityObjectToWorldNormal(v.normal); + fixed3 worldTangent = UnityObjectToWorldDir(v.tangent.xyz); + fixed3 worldBinormal = cross(worldNormal, worldTangent) * v.tangent.w; + + /* + float4x4 tangentToWorld = float4x4(worldTangent.x, worldBinormal.x, worldNormal.x, 0.0, + worldTangent.y, worldBinormal.y, worldNormal.y, 0.0, + worldTangent.z, worldBinormal.z, worldNormal.z, 0.0, + 0.0, 0.0, 0.0, 1.0); + // The matrix that transforms from world space to tangent space is inverse of tangentToWorld + float3x3 worldToTangent = inverse(tangentToWorld); + */ + //wToT = the inverse of tToW = the transpose of tToW as long as tToW is an orthogonal matrix. + float3x3 worldToTangent = float3x3(worldTangent, worldBinormal, worldNormal); + + // Transform the light and view dir from world space to tangent space + o.lightDir = mul(worldToTangent, WorldSpaceLightDir(v.vertex)); + o.viewDir = mul(worldToTangent, WorldSpaceViewDir(v.vertex)); + + /// + /// Note that the code below can only handle uniform scales, not including non-uniform scales + /// + // Compute the binormal // float3 binormal = cross( normalize(v.normal), normalize(v.tangent.xyz) ) * v.tangent.w; // // Construct a matrix which transform vectors from object space to tangent space // float3x3 rotation = float3x3(v.tangent.xyz, binormal, v.normal); // Or just use the built-in macro - TANGENT_SPACE_ROTATION; - - // Transform the light direction from object space to tangent space - o.lightDir = mul(rotation, ObjSpaceLightDir(v.vertex)).xyz; - // Transform the view direction from object space to tangent space - o.viewDir = mul(rotation, ObjSpaceViewDir(v.vertex)).xyz; +// TANGENT_SPACE_ROTATION; +// +// // Transform the light direction from object space to tangent space +// o.lightDir = mul(rotation, normalize(ObjSpaceLightDir(v.vertex))).xyz; +// // Transform the view direction from object space to tangent space +// o.viewDir = mul(rotation, normalize(ObjSpaceViewDir(v.vertex))).xyz; return o; } diff --git a/Assets/Shaders/Chapter9/Chapter9-AlphaBlendWithShadow.shader b/Assets/Shaders/Chapter9/Chapter9-AlphaBlendWithShadow.shader index 6c808ed4..548dd460 100644 --- a/Assets/Shaders/Chapter9/Chapter9-AlphaBlendWithShadow.shader +++ b/Assets/Shaders/Chapter9/Chapter9-AlphaBlendWithShadow.shader @@ -52,7 +52,7 @@ o.uv = TRANSFORM_TEX(v.texcoord, _MainTex); - // Pass shadow coordinates to pixel shade + // Pass shadow coordinates to pixel shader TRANSFER_SHADOW(o); return o; diff --git a/Assets/Shaders/Chapter9/Chapter9-AlphaTestWithShadow.shader b/Assets/Shaders/Chapter9/Chapter9-AlphaTestWithShadow.shader index d0330dfe..284053ab 100644 --- a/Assets/Shaders/Chapter9/Chapter9-AlphaTestWithShadow.shader +++ b/Assets/Shaders/Chapter9/Chapter9-AlphaTestWithShadow.shader @@ -51,7 +51,7 @@ o.uv = TRANSFORM_TEX(v.texcoord, _MainTex); - // Pass shadow coordinates to pixel shade + // Pass shadow coordinates to pixel shader TRANSFER_SHADOW(o); return o; diff --git a/Assets/Shaders/Chapter9/Chapter9-AttenuationAndShadowUseBuildInFunctions.shader b/Assets/Shaders/Chapter9/Chapter9-AttenuationAndShadowUseBuildInFunctions.shader index d815967f..dc42d7e6 100644 --- a/Assets/Shaders/Chapter9/Chapter9-AttenuationAndShadowUseBuildInFunctions.shader +++ b/Assets/Shaders/Chapter9/Chapter9-AttenuationAndShadowUseBuildInFunctions.shader @@ -47,7 +47,7 @@ o.worldPos = mul(_Object2World, v.vertex).xyz; - // Pass shadow coordinates to pixel shade + // Pass shadow coordinates to pixel shader TRANSFER_SHADOW(o); return o; @@ -117,7 +117,7 @@ o.worldPos = mul(_Object2World, v.vertex).xyz; - // Pass shadow coordinates to pixel shade + // Pass shadow coordinates to pixel shader TRANSFER_SHADOW(o); return o; diff --git a/Assets/Shaders/Chapter9/Chapter9-ForwardRendering.shader b/Assets/Shaders/Chapter9/Chapter9-ForwardRendering.shader index 3beeb2d7..a1619a41 100644 --- a/Assets/Shaders/Chapter9/Chapter9-ForwardRendering.shader +++ b/Assets/Shaders/Chapter9/Chapter9-ForwardRendering.shader @@ -127,10 +127,17 @@ #ifdef USING_DIRECTIONAL_LIGHT fixed atten = 1.0; #else - float3 lightCoord = mul(_LightMatrix0, float4(i.worldPos, 1)).xyz; - fixed atten = tex2D(_LightTexture0, dot(lightCoord, lightCoord).rr).UNITY_ATTEN_CHANNEL; + #if defined (POINT) + float3 lightCoord = mul(_LightMatrix0, float4(i.worldPos, 1)).xyz; + fixed atten = tex2D(_LightTexture0, dot(lightCoord, lightCoord).rr).UNITY_ATTEN_CHANNEL; + #elif defined (SPOT) + float4 lightCoord = mul(_LightMatrix0, float4(i.worldPos, 1)); + fixed atten = (lightCoord.z > 0) * tex2D(_LightTexture0, lightCoord.xy / lightCoord.w + 0.5).w * tex2D(_LightTextureB0, dot(lightCoord, lightCoord).rr).UNITY_ATTEN_CHANNEL; + #else + fixed atten = 1.0; + #endif #endif - + return fixed4((diffuse + specular) * atten, 1.0); } diff --git a/README.md b/README.md index 6f816986..6f3262e5 100755 --- a/README.md +++ b/README.md @@ -1,10 +1,95 @@ # 《Unity Shader入门精要》源代码 -本项目是书籍《Unity Shader入门精要》的配套源代码。最新版本请移步本项目的Github页面(https://github.com/candycat1992/Unity_Shaders_Book)。 +本项目是书籍《Unity Shader入门精要》的配套源代码。最新版本请移步本项目的Github页面。 + +# 源码下载 + +我们推荐您从Github上clone项目源码并及时检查更新。如果下载速度过慢可以移步百度网盘地址。 + +纸质版书籍可在以下链接购买:亚马逊当当京东 + +# 随书彩图 + +我们提供了包含书中所有插图的彩色版插图集锦:HTMLPDF。 + +# 2019年新增:改版后的第十八章 + +当年乐观地以为自己有时间写第二版,是我太年轻了…… 第一版由于我能力有限的确有很多问题,感谢大家一直以来的支持。当年在我有时间的时候曾经花了一些时间重写了第18章,本来是想要放到第二版里的,但是目前第二版遥遥无期,索性直接把改版后的第18章直接放出来,希望能让读者有新的收获:改版后的第18章。 + +# 第四章勘误 + +由于数学章是全书的重要基础,我们决定把第四章公开,来及时让读者获取最新的第四章数学章的内容:PDF。 + +**注意**:我们可能会根据读者勘误随时更新该文档,内容和页号可能会与读者手中的版本不同,实体书中的勘误会在每次重印时进行修正。 + +# 读者反馈和勘误 + +尽管我们在本书的编写过程中多次检查内容的正确性,但不可避免书中仍然会出现一些错误,欢迎读者批评指正。任何关于本书内容、源码等方面的问题,欢迎读者反映到本书源码所在的Github讨论页,也可以发邮件(lelefeng1992@gmail.com)联系笔者。 + +关于目前**已发现的错误**我们会及时在网上更新:勘误列表。 + +我们也维护了读者反馈的问题列表:FAQ。 # Unity版本 -我们**推荐使用Unity 5.0以上的版本来编译本项目**。如果你打算**使用更低版本的Unity,那么在学习本书时可能就会遇到一些问题**。例如 ,你发现可能会有些菜单或变量在你的Unity中找不到,可能就是因为Unity版本不同造成的。绝大多数情况下,本书的代码和指令仍然可以工作良好,但在一些特殊情况下,Unity可能会更改底层的实现细节,造成同样的代码会得到不一样的效果(例如,在非统一缩放时对法线进行变换)。还有一些问题是Unity提供的内置变量、宏和函数,例如我们在书中经常会使用UnityObjectToWorldNormal内置函数把法线从模型空间变换到世界空间中,但这个函数是在Unity 5中被引入的,因此如果读者使用的是Unity 5之前的版本就会报错。类似的情况还有和阴影相关的宏和变量等。 和Unity 4.x版本相比,Unity 5.x最大的变化之一就是很多以前只有在专业版才支持的功能,在免费版也同样提供了。因此,如果读者使用的是Unity 4.x免费版,可能会发现本书中的某些材质会出错。 +我们**推荐使用Unity 5.0以上的版本来编译本项目**。如果你打算**使用更低版本的Unity,那么在学习本书时可能就会遇到一些问题**: + +* 你可能发现会有些菜单或变量在你的Unity中找不到,这可能就是由于Unity版本不同造成的。绝大多数情况下,本书的代码和指令仍然可以工作良好,但在一些特殊情况下,Unity可能会更改底层的实现细节,造成同样的代码会得到不一样的效果(例如,在非统一缩放时对法线进行变换)。 + +* 还有一些问题是Unity提供的内置变量、宏和函数,例如我们在书中经常会使用UnityObjectToWorldNormal内置函数把法线从模型空间变换到世界空间中,但这个函数是在Unity 5中才被引入的,因此如果读者使用的是Unity 5之前的版本就会报错。类似的情况还有和阴影相关的宏和变量等。 + +* 和Unity 4.x版本相比,Unity 5.x最大的变化之一就是很多以前只有在专业版才支持的功能,在免费版也同样提供了。因此,如果读者使用的是Unity 4.x免费版,可能会发现本书中的某些材质会出错。 + +## Unity 5.3及其以下Unity 5.x版本 + +**分支链接**:[master](https://github.com/candycat1992/Unity_Shaders_Book/tree/master) + +在本书编写时,我们使用的版本是Unity 5.3,因此使用这些Unity版本的读者请使用本项目[master](https://github.com/candycat1992/Unity_Shaders_Book/tree/master)分支的相关代码。 + +## Unity 5.4及其以上Unity 5.x版本 + +**分支链接**:[unity_5_4](https://github.com/candycat1992/Unity_Shaders_Book/tree/unity_5_4) + +Unity 5.4对Shader部分进行了一些比较大的更新,比较明显的变化有: + +* 使用了unity_XXX来代替原有的XXX变换矩阵,例如_Object2World被替换成了unity_ObjectToWorld,_World2Object被替换成了unity_WorldToObject(均在UnityShaderVariables.cginc文件中被声明),_LightMatrix0被替换成了unity_WorldToLight(在AutoLight.cginc文件中被声明)。 + +* 使用了一些内置函数来代替某些运算,例如mul(UNITY_MATRIX_MVP,*)相关计算被替换成了UnityObjectToClipPos(*)。 + +在学习本书时,读者需要注意代码中一些由于更新造成的变化。 + +### 升级Unity 5.5 + +从Unity 5.5开始,Unity在某些平台(如DX11、DX12、PS4、Xbox One、Metal)等平台对深度缓存进行了反转操作,使得在近平面处的深度值为1,而远平面处为0。这样做的原因主要是为了更加充分得利用浮点深度缓存,具体原因可以参见NVIDIA的相关博客[Depth Precision Visualized](https://developer.nvidia.com/content/depth-precision-visualized)。Unity在[Upgrading to Unity 5.5](https://docs.unity3d.com/Manual/UpgradeGuide55.html)和[Platform-specific rendering differences](https://docs.unity3d.com/Manual/SL-PlatformDifferences.html)文档中对此进行了说明。 + +在本书代码中,我们在第13章用到了深度纹理,其中对于使用了Linear01Depth、LinearEyeDepth等Unity内置函数的部分不受此变化影响,但我们在Chapter13-MotionBlurWithDepthTexture中直接访问了深度值来计算世界空间下的坐标: + + +``` +// Get the depth buffer value at this pixel. +float d = SAMPLE_DEPTH_TEXTURE(_CameraDepthTexture, i.uv_depth); +``` + +这使得在反转深度缓存的情况下会得到错误的结果。为了解决这个问题,我们可以使用内置宏来判断深度是否已被反转,并据此来做出相应的计算。变化后的代码如下: + + +``` +// Get the depth buffer value at this pixel. +float d = SAMPLE_DEPTH_TEXTURE(_CameraDepthTexture, i.uv_depth); +#if defined(UNITY_REVERSED_Z) + d = 1.0 - d; +#endif +``` + +## 截止到目前的Unity 2017版本 + +**分支链接**:[unity_2017_1](https://github.com/candycat1992/Unity_Shaders_Book/tree/unity_2017_1) + +Unity 2017对Shader部分没有较大更新,我们主要做了以下更改来消除升级造成的Shader Warning信息: + +* 使用内置的UnityObjectToViewPos(*)函数来代替mul(UNITY_MATRIX_MV, *)对顶点进行变换。 + +在学习本书时,读者需要注意代码中一些由于更新造成的变化。 # 使用说明 @@ -14,13 +99,10 @@ | ------------- |:-------------| |Assets/Scenes|包含了各章对应的场景,每个章节对应一个子文件夹,例如第七章所有场景所在的子文件夹为Assets/Scenes/Chapter7。每个场景的命名方式为Scene_章号_小节号_次小节号,例如7.2.3节对应的场景名为Scene_7_2_3。如果同一个小节包含了多个场景,那么会使用英文字母作为后缀依次表示,例如7.1.2节包含了两个场景Scene_7_1_2_a和Scene_7_1_2_b。| |Assets/Shaders|包含了各章实现的Unity Shader文件,每个章节对应一个子文件夹,例如第七章实现的所有Unity Shader所在的子文件夹为Assets/Shaders/Chapter7。每个Unity Shader的命名方式为ChapterX-功能,例如第七章使用渐变纹理的Unity Shader名为Chapter7-RampTexture。| -|Assets/Materials|包含了各章对应的材质,每个章节对应一个子文件夹,例如第七章所有材质所在的子文件夹为Assets/ Scenes/Chapter7。每个材质的命名方式与它使用的Unity Shader名称相匹配,并以Mat作为后缀,例如使用名为Chapter7-RampTexture的Unity Shader的材质名称是RampTextureMat。| +|Assets/Materials|包含了各章对应的材质,每个章节对应一个子文件夹,例如第七章所有材质所在的子文件夹为Assets/Materials/Chapter7。每个材质的命名方式与它使用的Unity Shader名称相匹配,并以Mat作为后缀,例如使用名为Chapter7-RampTexture的Unity Shader的材质名称是RampTextureMat。| |Assets/Scripts|包含了各章对应的C#脚本,每个章节对应一个子文件夹,例如第五章所有脚本所在的子文件夹为Assets/Scripts/Chapter5。| |Assets/Textures|包含了各章使用的纹理贴图,每个章节对应一个子文件夹,例如第七章使用的所有纹理所在的子文件夹为Assets/Textures/Chapter7。| 除了上述文件夹外,源码中还包含了一些辅助文件夹,例如Assets/Editor文件夹中包含了一些需要在编辑器状态下运行的脚本,Assets/Prefabs文件夹下包含了各章使用的预设模型和其他常用预设模型等。 -# 读者反馈和勘误 - -尽管我们在本书的编写过程中多次检查内容的正确性,但不可避免书中仍然会出现一些错误,欢迎读者批评指正。任何关于本书内容、源码等方面的问题,欢迎读者反映到本书源码所在的Github讨论页(https://github.com/candycat1992/Unity_Shaders_Book/issues),也可以发邮件(lelefeng1992@gmail.com)联系笔者。