14.2 素描風格的渲染 另一種非常流行的非真實感渲染時素描風格的渲染。微軟研究院的Praun等人在2001年的SIGGRAPH上發(fā)表了一篇非常著名的論文(Rraun E,Hoppe H,Webb M, et al. Real-time hatching[C]//Proceedings of the 28th annual conference on Computer graphics and interactive techniques.ACM,2001:581)(http://alastaira.wordpress.com/2013/11/01/hand-drawn-shaders-and-craating-tonal-art-maps/)。這篇文章中,他們使用了提前生成的素面紋理來實現(xiàn)進行素描風格渲染,這些紋理組成了一個色調(diào)藝術(shù)映射(Tonal Art Map ,TAM) ,如圖14.4所示。在14.4中,從左到右紋理中的筆觸逐漸增多,用于模擬不同光照下的漫反射效果,從上到下則對應了每張紋理的多級漸遠紋理(mipmaps)。這些多級漸遠紋理的生成并不是簡單的對上一層紋理進行降采樣,而是需要保持筆觸之間的間隔,以便更真事地模擬素描效果。
接下來我們來實現(xiàn)簡化版的論文中提出的算法,我們不考慮多級漸遠紋理的生成,而直接使用6張素描紋理進行渲染。在渲染階段,我們首先在頂點著色器階段計算逐頂點的光照,根據(jù)光照結(jié)果來決定6張紋理的混合權(quán)重,并傳遞給片元著色器。然后,在片元著色器中根據(jù)這些權(quán)重來混合6張紋理的采樣結(jié)果。最終得到下圖的效果:
實現(xiàn) (1)新建場景(Scene_14_2)。 (2)新建材質(zhì)(HatchingMat)。 (3)新建Unity Shader(Chapter14-Hatching)。并賦給第二步中創(chuàng)建的材質(zhì)。 (4)在場景中拖拽一個模型,并把第二步中的材質(zhì)賦給該模型。
///
/// Reference: Praun E, Hoppe H, Webb M, et al. Real-time hatching[C]
/// Proceedings of the
28 th annual conference on Computer graphics
and interactive techniques. ACM,
2001 :
581.
///
Shader
"Unity Shaders Book/Chapter 14/Hatching" {Properties {_Color (
"Color Tint" , Color) = (
1 ,
1 ,
1 ,
1 )//紋理的平鋪系數(shù),值越大,模型上的素描線條越密_TileFactor (
"Tile Factor" , Float) =
1 _Outline (
"Outline" , Range(
0 ,
1 )) =
0.1 _Hatch0 (
"Hatch 0" ,
2 D) =
"white" {}_Hatch1 (
"Hatch 1" ,
2 D) =
"white" {}_Hatch2 (
"Hatch 2" ,
2 D) =
"white" {}_Hatch3 (
"Hatch 3" ,
2 D) =
"white" {}_Hatch4 (
"Hatch 4" ,
2 D) =
"white" {}_Hatch5 (
"Hatch 5" ,
2 D) =
"white" {}}SubShader {//由于素描風格往往也需要在物體周圍渲染輪廓線,因此我們直接使用
14.1 中國渲染輪廓線的PassTags {
"RenderType" =
"Opaque" "Queue" =
"Geometry" }UsePass
"Unity Shaders Book/Chapter 14/Toon Shading/OUTLINE" //Unity Shaders Book/Chapter
14 /Toon Shading/OUTLINE對應了
14.1 中Chapter14-ToonShading文件里Shader的名字//而Unity內(nèi)部會把Pass的名稱全部轉(zhuǎn)成大寫格式,所以我們需要在UsePass中使用大寫格式的Pass名稱。Pass {Tags {
"LightMode" =
"ForwardBase" }CGPROGRAM
#pragma vertex vert #pragma fragment frag #pragma multi_compile_fwdbase #include "UnityCG.cginc" #include "Lighting.cginc" #include "AutoLight.cginc" #include "UnityShaderVariables.cginc" fixed4 _Colorfloat _TileFactorsampler2D _Hatch0sampler2D _Hatch1sampler2D _Hatch2sampler2D _Hatch3sampler2D _Hatch4sampler2D _Hatch5struct a2v {float4 vertex : POSITIONfloat4 tangent : TANGENTfloat3 normal : NORMALfloat2 texcoord : TEXCOORD0}//由于我們需要在頂點著色器中計算
6 張紋理的混合權(quán)重,我們首先需要在v2f結(jié)構(gòu)體中添加相應的變量:struct v2f {float4 pos : SV_POSITIONfloat2 uv : TEXCOORD0fixed3 hatchWeights0 : TEXCOORD1fixed3 hatchWeights1 : TEXCOORD2float3 worldPos : TEXCOORD3SHADOW_COORDS(
4 )}//由于一共聲明了
6 張紋理,這意味著需要
6 個混合權(quán)重,我們把他們存儲在兩個fixed3類型的//變量(hatchWeights0和hatchWeights1)中。為了添加陰影效果,我們還聲明了worldPos變量,//并使用SHADOW_COORDS宏聲明了陰影紋理的采樣坐標。//接下來,我們定義關(guān)鍵的頂點著色器v2f vert(a2v v) {v2f oo
.pos =
mul (UNITY_MATRIX_MVP, v
.vertex )o
.uv = v
.texcoord .xy * _TileFactorfixed3 worldLightDir = normalize(WorldSpaceLightDir(v
.vertex ))fixed3 worldNormal = UnityObjectToWorldNormal(v
.normal )fixed diff = max(
0 , dot(worldLightDir, worldNormal))o
.hatchWeights 0 = fixed3(
0 ,
0 ,
0 )o
.hatchWeights 1 = fixed3(
0 ,
0 ,
0 )float hatchFactor = diff *
7.0 if (hatchFactor >
6.0 ) {// Pure white, do nothing} else if (hatchFactor >
5.0 ) {o
.hatchWeights 0
.x = hatchFactor -
5.0 } else if (hatchFactor >
4.0 ) {o
.hatchWeights 0
.x = hatchFactor -
4.0 o
.hatchWeights 0
.y =
1.0 - o
.hatchWeights 0
.x } else if (hatchFactor >
3.0 ) {o
.hatchWeights 0
.y = hatchFactor -
3.0 o
.hatchWeights 0
.z =
1.0 - o
.hatchWeights 0
.y } else if (hatchFactor >
2.0 ) {o
.hatchWeights 0
.z = hatchFactor -
2.0 o
.hatchWeights 1
.x =
1.0 - o
.hatchWeights 0
.z } else if (hatchFactor >
1.0 ) {o
.hatchWeights 1
.x = hatchFactor -
1.0 o
.hatchWeights 1
.y =
1.0 - o
.hatchWeights 1
.x } else {o
.hatchWeights 1
.y = hatchFactoro
.hatchWeights 1
.z =
1.0 - o
.hatchWeights 1
.y }o
.worldPos =
mul (_Object2World, v
.vertex )
.xyz TRANSFER_SHADOW(o)return o}//我們首先對頂點進行了基本的坐標變換。然后,使用_TileFacotr得到了紋理采樣坐標。在計算
6 //張紋理的混合權(quán)重之前,我們首先需要計算逐頂點光照。因此,我們使用世界空間下的光照方向//和法線方向得到漫反射系數(shù)diff。之后,我們把權(quán)重值初始化為
0 ,并把diff縮放到,[
0 ,
7 ]范圍//,得到hatchFactor。我們把[
0 ,
7 ]的區(qū)間均勻劃分為
7 個子區(qū)間,通過該判斷hatchFactor所處的子區(qū)間//來計算對應的紋理混合權(quán)重。最后,我們計算了頂點的世界坐標,并使用TRANSFER_SHADOW//宏來計算陰影紋理的采樣坐標。//接下來,定義片元著色器部分:fixed4 frag(v2f i) : SV_Target { fixed4 hatchTex0 = tex2D(_Hatch0, i
.uv ) * i
.hatchWeights 0
.x fixed4 hatchTex1 = tex2D(_Hatch1, i
.uv ) * i
.hatchWeights 0
.y fixed4 hatchTex2 = tex2D(_Hatch2, i
.uv ) * i
.hatchWeights 0
.z fixed4 hatchTex3 = tex2D(_Hatch3, i
.uv ) * i
.hatchWeights 1
.x fixed4 hatchTex4 = tex2D(_Hatch4, i
.uv ) * i
.hatchWeights 1
.y fixed4 hatchTex5 = tex2D(_Hatch5, i
.uv ) * i
.hatchWeights 1
.z fixed4 whiteColor = fixed4(
1 ,
1 ,
1 ,
1 ) * (
1 - i
.hatchWeights 0
.x - i
.hatchWeights 0
.y - i
.hatchWeights 0
.z - i
.hatchWeights 1
.x - i
.hatchWeights 1
.y - i
.hatchWeights 1
.z )fixed4 hatchColor = hatchTex0 + hatchTex1 + hatchTex2 + hatchTex3 + hatchTex4 + hatchTex5 + whiteColorUNITY_LIGHT_ATTENUATION(atten, i, i
.worldPos )return fixed4(hatchColor
.rgb * _Color
.rgb * atten,
1.0 )}//當?shù)玫搅?span id="ze8trgl8bvbq" class="hljs-number">6張紋理的混合權(quán)重后,我們對每張紋理進行采樣并和它們對應的權(quán)重值相乘德得到每張//紋理的采樣顏色。我們還計算了純白在渲染中的貢獻度,這是通過從
1 中減去所有
6 張紋理的權(quán)重//來得到的。這是因為素描中往往有留白的部分,因此我們希望在最后的渲染只能光照最亮的部分是純白色的。//最后,我們混合了各個顏色值,并和陰影值atten、模型顏色_Color相乘后返回//最終的渲染結(jié)果。ENDCG}}FallBack
"Diffuse"
}
14.5 擴展閱讀 以及 參考文獻 [1]Gooch A,Gooch B,Shirley P,et al.A non-photorealistic lighting model for automatic technical illustration[C]//Proceedings of the 25th annual conference on Computer graphics and interactive techniques.ACM,1998:447-452。 [2]Anjyo K,Hiramitsu K.Stylized highlights for cartoon rendering and animation[J].Computer Graphics and Applications,IEEE,2003,23(4):54-61。 [3]Mitchell J,Francke M,Eng D.Illustrative rendering in Team Fortress 2[C]//Proceedings of the 5th international symposium on Non-photorealistic animation and rendering.ACM,2007:71-76。 [4]Rraun E,Hoppe H,Webb M, et al. Real-time hatching[C]//Proceedings of the 28th annual conference on Computer graphics and interactive techniques.ACM,2001:581。 [5]Geng W.The Algorithms and Principles of Non-photorealistic Graphics:Artistic Rendering and Cartoon Animation[M].Springer Science &Business Media,2011。
總結(jié)
以上是生活随笔 為你收集整理的Unity_Shader高级篇_14.1_Unity Shader入门精要 的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔 網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔 推薦給好友。