Cg入门18:Fragment shader - 边缘泛光和透明
泛光:
处理后效果:
透明:
1.tags{"queue" = "transparent” } //调整渲染顺序
2.blend srcAlpha oneMinussrcalpha //混合透明
3. zwrite off //写入的z轴深度关掉
关掉z轴深度与不关掉的差别:
代码:
Shader "Sbin/trans" { Properties { _Scale("Scale",Range(1,8)) = 2 } SubShader { tags{"queue" = "transparent" } pass{ blend srcAlpha oneMinussrcalpha zwrite off //写入的z轴深度关掉 CGPROGRAM #pragma vertex vert #pragma fragment frag #include "UnityCG.cginc" struct v2f{ float4 pos : POSITION; float4 col : COLOR; float3 normal:TEXCOORD0; float4 vertex:TEXCOORD1; }; v2f vert(appdata_base v){ v2f o; o.pos = mul(UNITY_MATRIX_MVP,v.vertex); o.col = fixed4(0,1,1,1); o.vertex = v.vertex; o.normal = v.normal; return o; } float _Scale; fixed4 frag(v2f v):COLOR { float3 N = mul((float3x3)_World2Object,v.normal); N = normalize(N); float4 wpos = mul(_Object2World,v.vertex); float3 V = _WorldSpaceCameraPos.xyz - wpos.xyz; V = normalize(V); float bright = 1 -saturate(dot(N,V)); bright = pow(bright,_Scale); return fixed4(1,1,1,1)*bright; } ENDCG } } FallBack "Diffuse" }
优化部分:
怎么关掉pass方法。
方法1:凝视掉
方法2:blend zero one (解释:zero 表示0乘以当前pass颜色输出。0就是不输出;one就是所有输出前面的pass通道计算的颜色值)
dstAlpha : 当前渲染的alpha值乘以当前alpha值
srcAlpha :採用原始alpha乘以当前alpha值
blendop revsub:用之前pass渲染的颜色值减去当前的pass颜色值
代码:
Shader "Sbin/trans" { Properties { _MainColor("MainColor",Color) = (1,1,1,1) _Scale("Scale",Range(1,10)) = 2 _Outer("Outer",Range(0,1)) = 0.2 } SubShader { tags{"queue" = "transparent" } pass{ blend srcAlpha oneMinussrcalpha zwrite off //写入的z轴深度关掉 CGPROGRAM #pragma vertex vert #pragma fragment frag #include "UnityCG.cginc" struct v2f{ float4 pos : POSITION; float4 col : COLOR; float3 normal:TEXCOORD0; float4 vertex:TEXCOORD1; }; float _Scale; fixed4 _MainColor; float _Outer; v2f vert(appdata_base v){ v2f o; v.vertex.xyz += v.normal*_Outer; o.pos = mul(UNITY_MATRIX_MVP,v.vertex); o.col = fixed4(0,1,1,1); o.vertex = v.vertex; o.normal = v.normal; return o; } fixed4 frag(v2f v):COLOR { float3 N = mul((float3x3)_World2Object,v.normal); N = normalize(N); float4 wpos = mul(_Object2World,v.vertex); float3 V = _WorldSpaceCameraPos.xyz - wpos.xyz; V = normalize(V); float bright = saturate(dot(N,V)); bright = pow(bright,_Scale); _MainColor.a *= bright; return _MainColor; } ENDCG } //============================================================================== pass{ blendop revsub blend dstAlpha one //之前的alpha值 //blend zero one zwrite off //写入的z轴深度关掉 CGPROGRAM #pragma vertex vert #pragma fragment frag #include "UnityCG.cginc" struct v2f{ float4 pos : POSITION; }; v2f vert(appdata_base v){ v2f o; o.pos = mul(UNITY_MATRIX_MVP,v.vertex); return o; } fixed4 frag(v2f v):COLOR { return fixed4(1,1,1,1); } ENDCG } //============================================================================== pass{ blend srcAlpha oneMinussrcalpha //blend zero one zwrite off //写入的z轴深度关掉 CGPROGRAM #pragma vertex vert #pragma fragment frag #include "UnityCG.cginc" struct v2f{ float4 pos : POSITION; float4 col : COLOR; float3 normal:TEXCOORD0; float4 vertex:TEXCOORD1; }; v2f vert(appdata_base v){ v2f o; o.pos = mul(UNITY_MATRIX_MVP,v.vertex); o.col = fixed4(0,1,1,1); o.vertex = v.vertex; o.normal = v.normal; return o; } float _Scale; fixed4 frag(v2f v):COLOR { float3 N = mul((float3x3)_World2Object,v.normal); N = normalize(N); float4 wpos = mul(_Object2World,v.vertex); float3 V = _WorldSpaceCameraPos.xyz - wpos.xyz; V = normalize(V); float bright = 1 - saturate(dot(N,V)); bright = pow(bright,_Scale); return fixed4(1,1,1,1)*bright; } ENDCG } } FallBack "Diffuse" }