通常游戏渲染以照相写实主义(photorealism)为主要目标,也有许多游戏使用了非真实感渲染(non-photorealistic rendering,NPR)的方法来渲染游戏画面。
卡通风格的渲染
特点:物体被黑色的线条描边,分明的明暗变化,实现卡通渲染有很多方法,其中之一是基于色调的着色技术(tone-based shading)。
使用漫反射系数对一张一维纹理进行采样,以控制漫反射的色调,卡通风格中,模型的高光往往是一块块分界明显的纯色区域。
除了光照模型不同,还需要在物体边缘部分绘制轮廓,这里会使用基于模型的描边方法——实现更简单。
第一步:渲染轮廓线:
许多绘制模型轮廓线的方法:
- 基于观察角度和表面法线的轮廓线渲染。使用视角方向和表面法线的点乘结果来得到轮廓线信息。使用一个pass,效果不太好。
- 过程式几何轮廓线渲染。使用两个pass,第一个pass渲染背面的面片,并让它的轮廓可见,第二个pass再渲染正面的面片,适用于绝大多数表面平滑的模型,不适合类似立方体这样平整的模型。
- 基于图像处理的轮廓线渲染。类似于边缘检测的方法,但也有局限性,一些深度和法线变化很小的轮廓无法被检测出来,例如桌子上的纸张。
- 基于轮廓边检测的轮廓线渲染。上述的方法无法控制轮廓线的风格,需要检测出精确的轮廓线,然后直接渲染,检查一条边是否是轮廓边的公式:(n0 · v>0)≠ (n1 · v>0),n0和n1分别代表两个相邻三角面片的法向,v是从视角到该边上任意顶点的方向,即检查两个相邻的面片是否一个朝正面,一个朝背面,由于逐帧单独提取轮廓,所以帧与帧之间会出现跳跃性。
- 混合上述几种方式。例如,先找到精确的轮廓线,把模型和轮廓线渲染到纹理中,再使用图像处理的方法识别出轮廓线,并在图像空间下进行风格化渲染。
本次采用过程式几何轮廓线渲染的方法来对模型进行轮廓描边,使用两个pass,第一个pass使用轮廓线颜色渲染整个背面的面片,并在视角空间下把模型顶点沿着法线方向向外扩张一段距离。
viewPos=viewPos + viewNormal * _Outline
为了避免一些内凹的模型发生背面面片遮挡正面面片的情况,在扩张背面顶点之前,首先对顶点法线的z分量进行处理,使他们等于一个定值,然后把法线归一化后再对顶点进行扩张,这样扩展后的背面更加扁平化,从而降低了遮挡正面面片的可能性。
viewNormal.z=-0.5; viewNormal = normalize(viewNormal); viewPos = viewPos + viewNormal * _Outline;
第二步:添加高光:
对于卡通渲染需要的高光反射模型,和之前的光照模型一样,需要计算normal和halfDir的点乘结果,然后把该值和一个阈值比较,小于该阈值则高光反射系数为0,否则返回1.
float spec=dot(worldNormal ,worldHalfDir); spec=step(threshold,spec);
step的第一个参数是参考值,第二个参数是代比较的数值,如果第二个参数大于等于第一个参数,则返回1,否则返回0.
但是这种粗暴的判断会造成高光区域边缘的锯齿,由于高光区域的边缘不是平滑渐变的,而是由0突变到1,因此对其进行抗锯齿处理,可以在边界处很小的一块区域内,进行平滑处理:
float spec=dot(worldNormal ,worldHalfDir); spec=lerp(0,1,smoothstep(-w,w,spec-threshold));
w是一个很小的值,spec-threshold小于-w时,返回0,大于w时,返回1,否则在0到1之间进行插值,这样可以在[-w,w]区间内得到一个0到1平滑的spec值,从而进行抗锯齿,在这使用领域像素之间的近似导数值(通过fwidth得到)作为w。
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(0.0001, _SpecularScale)为了在_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"
}
效果:
素描风格的渲染
使用提前生成的素描纹理来实现实时额素描风格渲染,这些纹理组成了一个色调艺术映射(Tonal Art Map,TAM),不考虑多级渐远纹理的生成,直接使用六张素描纹理进行渲染:首先在顶点着色阶段计算逐顶点的光照,根据光照结果来决定六张纹理的混合权重,并传递给片元着色器,然后在片元着色器中根据这些权重来混合6张纹理的采样结果。
///
/// 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
_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"}
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张纹理的混合权重
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);
float hatchFactor = diff * 7.0;//把diff缩放到[0,7]
//[0,7]均匀划分成7个子区间,通过判断hatchFactor所在区间,来计算对应的纹理混合权重
//hatchFactor越小,越接近后面的纹理
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;
//计算纯白的贡献度,素描中往往有留白的部分,在最后的渲染中光照最亮的部分希望是纯白色的。
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"
}
效果: