Unity Shader总结(十四)——卡通和素描风格的渲染


许多游戏使用非真实感渲染(NPR)的方法渲染游戏画面,例如卡通和水彩风格等;

卡通渲染

渲染轮廓线

这里使用过程式几何轮廓线渲染:使用两个Pass,第一个Pass使用轮廓线颜色渲染整个背面的面片,并在视角空间下把模型顶点沿着法线方向向外扩张一段距离,让背部轮廓线清晰可见;为了防止对于一些内凹的模型,背面面片遮挡正面面片的情况,在扩张背面顶点之前,先对顶点法线的z分量处理,使它们等于一个定值,然后把法线归一化后再对顶点扩张,使扩展后的背面更加扁平化,降低了遮挡正面面片的可能性。
优点是快速有效,适用于绝大多数表面平滑的模型,但不适合立方体这样平整的模型;

viewNormal.z=-0.5;
viewNormal=normalize(viewnormal);
viewPos=viewPos+viewNormal*_Outline;

高光

以往的高光模型不再可用,这里计算normal和halfDir的点乘结果,和一个阈值比较,为了防止高光边界出现锯齿,可以使用以下代码:

float spec=dot(worldNormal,worldHalfDir);
spec=lerp(0,1,smoothstep(-w,w,spec-threshold));

w是一个很小的值,当spec-threshold小于-w时,高光系数返回0,大于w时返回1,否则在0到1之间插值,这样可以使高光区域的边界处得到一个从0到1平滑变化的spec值;
这里使用领域像素之间的近似导数值,使用fwidth函数实现;

实现

// Upgrade NOTE: replaced '_Object2World' with 'unity_ObjectToWorld'
// Upgrade NOTE: replaced 'mul(UNITY_MATRIX_MVP,*)' with 'UnityObjectToClipPos(*)'

Shader "Unity Shaders Book/Chapter 14/Toon Shading" {
    
    
	Properties {
    
    
		_Color ("Color Tint", Color) = (1, 1, 1, 1)
		_MainTex ("Main Tex", 2D) = "white" {
    
    }
		_Ramp ("Ramp Texture", 2D) = "white" {
    
    } //控制漫反射色调的渐变纹理
		_Outline ("Outline", Range(0, 1)) = 0.1 //控制轮廓线宽度
		_OutlineColor ("Outline Color", Color) = (0, 0, 0, 1) //轮廓线颜色
		_Specular ("Specular", Color) = (1, 1, 1, 1)	//高光反射颜色
		_SpecularScale ("Specular Scale", Range(0, 0.1)) = 0.01	//计算高光反射时使用的阈值
	}
    SubShader {
    
    
		Tags {
    
     "RenderType"="Opaque" "Queue"="Geometry"}
		
		Pass {
    
    
			NAME "OUTLINE"
			//只渲染背面
			Cull Front 
			
			CGPROGRAM
			
			#pragma vertex vert
			#pragma fragment frag
			
			#include "UnityCG.cginc"
			
			float _Outline;
			fixed4 _OutlineColor;
			
			struct a2v {
    
    
				float4 vertex : POSITION;
				float3 normal : NORMAL;
			}; 
			
			struct v2f {
    
    
			    float4 pos : SV_POSITION;
			};
			
			v2f vert (a2v v) {
    
    
				v2f o;
				//变化到视角空间是为了让描边在观察空间达到最好的效果
				float4 pos = mul(UNITY_MATRIX_MV, v.vertex); 
				float3 normal = mul((float3x3)UNITY_MATRIX_IT_MV, v.normal);  
				normal.z = -0.5;
				pos = pos + float4(normalize(normal), 0) * _Outline;
				o.pos = mul(UNITY_MATRIX_P, pos);
				
				return o;
			}
			
			float4 frag(v2f i) : SV_Target {
    
     
				return float4(_OutlineColor.rgb, 1);               
			}
			
			ENDCG
		}
		
		Pass {
    
    
			Tags {
    
     "LightMode"="ForwardBase" }
			//渲染正面
			Cull Back
		
			CGPROGRAM
		
			#pragma vertex vert
			#pragma fragment frag
			
			#pragma multi_compile_fwdbase
		
			#include "UnityCG.cginc"
			#include "Lighting.cginc"
			#include "AutoLight.cginc"
			#include "UnityShaderVariables.cginc"
			
			fixed4 _Color;
			sampler2D _MainTex;
			float4 _MainTex_ST;
			sampler2D _Ramp;
			fixed4 _Specular;
			fixed _SpecularScale;
		
			struct a2v {
    
    
				float4 vertex : POSITION;
				float3 normal : NORMAL;
				float4 texcoord : TEXCOORD0;
				float4 tangent : TANGENT;
			}; 
		
			struct v2f {
    
    
				float4 pos : POSITION;
				float2 uv : TEXCOORD0;
				float3 worldNormal : TEXCOORD1;
				float3 worldPos : TEXCOORD2;
				SHADOW_COORDS(3)
			};
			
			v2f vert (a2v v) {
    
    
				v2f o;
				
				o.pos = UnityObjectToClipPos( v.vertex);
				o.uv = TRANSFORM_TEX (v.texcoord, _MainTex);
				o.worldNormal  = UnityObjectToWorldNormal(v.normal);
				o.worldPos = mul(unity_ObjectToWorld, v.vertex).xyz;
				
				TRANSFER_SHADOW(o);
				
				return o;
			}
			
			float4 frag(v2f i) : SV_Target {
    
     
				fixed3 worldNormal = normalize(i.worldNormal);
				fixed3 worldLightDir = normalize(UnityWorldSpaceLightDir(i.worldPos));
				fixed3 worldViewDir = normalize(UnityWorldSpaceViewDir(i.worldPos));
				fixed3 worldHalfDir = normalize(worldLightDir + worldViewDir);
				
				fixed4 c = tex2D (_MainTex, i.uv);
				fixed3 albedo = c.rgb * _Color.rgb;
				
				fixed3 ambient = UNITY_LIGHTMODEL_AMBIENT.xyz * albedo;
				
				UNITY_LIGHT_ATTENUATION(atten, i, i.worldPos);
				//漫反射系数和阴影值相乘得到最终的漫反射系数
				fixed diff =  dot(worldNormal, worldLightDir);
				diff = (diff * 0.5 + 0.5) * atten;
				//对渐变纹理采样并和反射率、光照颜色相乘
				fixed3 diffuse = _LightColor0.rgb * albedo * tex2D(_Ramp, float2(diff, diff)).rgb;
				
				fixed spec = dot(worldNormal, worldHalfDir);
				fixed w = fwidth(spec) * 2.0; //抗锯齿处理
				//step是为了_SpecularScale=0时能完全消除高光反射的光照
				fixed3 specular = _Specular.rgb * lerp(0, 1, smoothstep(-w, w, spec + _SpecularScale - 1)) * step(0.0001, _SpecularScale);
				
				return fixed4(ambient + diffuse + specular, 1.0);
			}
		
			ENDCG
		}
	}
	FallBack "Diffuse"
}



素描风格渲染

这里不采用mipmap,使用6张素描纹理渲染

// Upgrade NOTE: replaced '_Object2World' with 'unity_ObjectToWorld'
// Upgrade NOTE: replaced 'mul(UNITY_MATRIX_MVP,*)' with 'UnityObjectToClipPos(*)'

///
///  Reference: 	Praun 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.
///
Shader "Unity Shaders Book/Chapter 14/Hatching" {
    
    
	Properties {
    
    
		_Color ("Color Tint", Color) = (1, 1, 1, 1)
		_TileFactor ("Tile Factor", Float) = 1 //纹理平铺系数,值越大,素描线条越密
		_Outline ("Outline", Range(0, 1)) = 0.1
		//渲染时使用的6张纹理,线条密度一次增大
		_Hatch0 ("Hatch 0", 2D) = "white" {
    
    }
		_Hatch1 ("Hatch 1", 2D) = "white" {
    
    }
		_Hatch2 ("Hatch 2", 2D) = "white" {
    
    }
		_Hatch3 ("Hatch 3", 2D) = "white" {
    
    }
		_Hatch4 ("Hatch 4", 2D) = "white" {
    
    }
		_Hatch5 ("Hatch 5", 2D) = "white" {
    
    }
	}
	
	SubShader {
    
    
		Tags {
    
     "RenderType"="Opaque" "Queue"="Geometry"}
		//必须大写,因为unity会自动把Pass名字转换成大写
		UsePass "Unity Shaders Book/Chapter 14/Toon Shading/OUTLINE"
		
		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 _Color;
			float _TileFactor;
			sampler2D _Hatch0;
			sampler2D _Hatch1;
			sampler2D _Hatch2;
			sampler2D _Hatch3;
			sampler2D _Hatch4;
			sampler2D _Hatch5;
			
			struct a2v {
    
    
				float4 vertex : POSITION;
				float4 tangent : TANGENT; 
				float3 normal : NORMAL; 
				float2 texcoord : TEXCOORD0; 
			};
			
			struct v2f {
    
    
				float4 pos : SV_POSITION;
				float2 uv : TEXCOORD0;
				//6张纹理的需要6个混合权重,存在两个fixed3中
				fixed3 hatchWeights0 : TEXCOORD1;
				fixed3 hatchWeights1 : TEXCOORD2;
				//阴影效果使用
				float3 worldPos : TEXCOORD3;
				SHADOW_COORDS(4)
			};
			
			v2f vert(a2v v) {
    
    
				v2f o;
				
				o.pos = UnityObjectToClipPos(v.vertex);
				//纹理采样坐标
				o.uv = v.texcoord.xy * _TileFactor;
				//计算漫反射系数
				fixed3 worldLightDir = normalize(WorldSpaceLightDir(v.vertex));
				fixed3 worldNormal = UnityObjectToWorldNormal(v.normal);
				fixed diff = max(0, dot(worldLightDir, worldNormal));
				//初始化权重
				o.hatchWeights0 = fixed3(0, 0, 0);
				o.hatchWeights1 = fixed3(0, 0, 0);
				//把diff缩放[0,7],得到hatchFactor
				float hatchFactor = diff * 7.0;
				//将其分为7个子区间,判断所处区间计算混合权重
				if (hatchFactor > 6.0) {
    
    
					// Pure white, do nothing
				} else if (hatchFactor > 5.0) {
    
    
					o.hatchWeights0.x = hatchFactor - 5.0;
				} else if (hatchFactor > 4.0) {
    
    
					o.hatchWeights0.x = hatchFactor - 4.0;
					o.hatchWeights0.y = 1.0 - o.hatchWeights0.x;
				} else if (hatchFactor > 3.0) {
    
    
					o.hatchWeights0.y = hatchFactor - 3.0;
					o.hatchWeights0.z = 1.0 - o.hatchWeights0.y;
				} else if (hatchFactor > 2.0) {
    
    
					o.hatchWeights0.z = hatchFactor - 2.0;
					o.hatchWeights1.x = 1.0 - o.hatchWeights0.z;
				} else if (hatchFactor > 1.0) {
    
    
					o.hatchWeights1.x = hatchFactor - 1.0;
					o.hatchWeights1.y = 1.0 - o.hatchWeights1.x;
				} else {
    
    
					o.hatchWeights1.y = hatchFactor;
					o.hatchWeights1.z = 1.0 - o.hatchWeights1.y;
				}
				
				o.worldPos = mul(unity_ObjectToWorld, v.vertex).xyz;
				
				TRANSFER_SHADOW(o);
				
				return o; 
			}
			
			fixed4 frag(v2f i) : SV_Target {
    
    
				//采样并和对应的权重相乘得到每张纹理的采样颜色
				fixed4 hatchTex0 = tex2D(_Hatch0, i.uv) * i.hatchWeights0.x;
				fixed4 hatchTex1 = tex2D(_Hatch1, i.uv) * i.hatchWeights0.y;
				fixed4 hatchTex2 = tex2D(_Hatch2, i.uv) * i.hatchWeights0.z;
				fixed4 hatchTex3 = tex2D(_Hatch3, i.uv) * i.hatchWeights1.x;
				fixed4 hatchTex4 = tex2D(_Hatch4, i.uv) * i.hatchWeights1.y;
				fixed4 hatchTex5 = tex2D(_Hatch5, i.uv) * i.hatchWeights1.z;
				//计算纯白在渲染中的贡献度,通过1减去所有6张纹理的权重得到。是光照最亮的部分是纯白色
				fixed4 whiteColor = fixed4(1, 1, 1, 1) * (1 - i.hatchWeights0.x - i.hatchWeights0.y - i.hatchWeights0.z - 
							i.hatchWeights1.x - i.hatchWeights1.y - i.hatchWeights1.z);
				
				fixed4 hatchColor = hatchTex0 + hatchTex1 + hatchTex2 + hatchTex3 + hatchTex4 + hatchTex5 + whiteColor;
				
				UNITY_LIGHT_ATTENUATION(atten, i, i.worldPos);
								
				return fixed4(hatchColor.rgb * _Color.rgb * atten, 1.0);
			}
			
			ENDCG
		}
	}
	FallBack "Diffuse"
}

猜你喜欢

转载自blog.csdn.net/memory_MM_forever/article/details/118516593