Shader教程:过渡图非真实感渲染
发表于2018-03-28
非真实感渲染就是使用一些渲染方法使得画面达到某些特殊的绘画风格相似的效果,下面就和大家介绍下过渡图非真实感渲染的实现。
代码如下:
Shader "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 // 声明顶点和片段着色器func名字 #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 = mul( UNITY_MATRIX_MVP, 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; 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" }