透明测试 - 将透明度不足的像素不进行渲染,否则渲染
透明混合 - 就是我们常看到的透明物体
透明测试
Shader "Transparent/AlphaTest"
{
Properties
{
[NoScaleOffset] _MainTex ("Main Tex", 2D) = "white" {}
_Color ("Main Tint", Color) = (1, 1, 1, 1)
_Cutoff ("Alpha Cutoff", Range(0, 1)) = 0.5
}
SubShader
{
Tags { "Queue" = "AlphaTest" "IgnoreProjector" = "True" "RenderType" = "TransparentCutout"}
Pass
{
Tags { "LightMode" = "ForwardBase" }
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
#include <UnityCG.cginc>
#include <UnityShaderUtilities.cginc>
#include <UnityLightingCommon.cginc>
struct a2v
{
float4 vertex : POSITION;
float3 normal : NORMAL;
float4 texcoord : TEXCOORD0;
};
struct v2f
{
float4 pos : SV_POSITION;
float2 uv : TEXCOORD0;
float3 worldPos : TEXCOORD1;
float3 worldNormal : TEXCOORD2;
};
v2f vert(a2v v)
{
v2f o;
o.pos = UnityObjectToClipPos(v.vertex.xyz);
o.worldPos = mul(unity_ObjectToWorld, v.vertex).xyz;
o.worldNormal = UnityObjectToWorldNormal(v.normal);
o.uv = TRANSFORM_UV(v.texcoord);
return o;
}
sampler2D _MainTex;
fixed4 _Color;
fixed _Cutoff;
fixed4 frag(v2f i) : SV_Target
{
fixed4 albedo = tex2D(_MainTex, i.uv) * _Color;
fixed3 worNormal = normalize(i.worldNormal);
fixed3 litDir = normalize(UnityWorldSpaceLightDir(i.worldPos));
fixed3 viewDir = normalize(UnityWorldSpaceViewDir(i.worldPos));
//当给定值小于0时,舍弃这个像素
/*
* void chip(float4 x)
* {
* if (any(x < 0))
* discard
* }
*/
clip(albedo.a - _Cutoff);
//获得环境光
fixed3 ambient = UNITY_LIGHTMODEL_AMBIENT.xyz * albedo.rgb;
//获得漫反射 半兰伯特
fixed3 diffuse = _LightColor0.rgb * albedo.rgb * (dot(worNormal, litDir) * 0.5 + 0.5);
//获得高光反射 Phong
fixed3 reflectDir = normalize(reflect(-litDir, worNormal));
fixed3 specular = _LightColor0.rgb * pow(saturate(dot(reflectDir, viewDir)), 20);
//获得高光反射 Blinm - Phong
//fixed3 halfDir = normalize(litDir + viewDir);
//fixed3 specular = _LightColor0.rgb * pow(saturate(dot(halfDir, worNormal)), 20);
return fixed4(ambient + diffuse + specular, 1.0);
}
ENDCG
}
}
Fallback "Transparent/Cutout/VertexLit"
}
透明混合 - (渲染简单物体,无法渲染复杂的非凸网格)
Shader "Transparent/AlphaBlend"
{
Properties
{
[NoScaleOffset] _MainTex ("Main Tex", 2D) = "white" {}
_Color ("Color", Color) = (1, 1, 1, 1)
_AlphaScale ("Alpha Scale", Float) = 1
}
SubShader
{
Tags { "Queue" = "Transparent" "IgnoreProjector" = "True" "RenderType" = "Transparent" }
Pass
{
//关闭深度写入
ZWrite Off
Blend SrcAlpha OneMinusSrcAlpha
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
#include <UnityCG.cginc>
#include <UnityShaderUtilities.cginc>
#include <UnityLightingCommon.cginc>
struct a2v
{
float4 vertex : POSITION;
float3 normal : NORMAL;
float4 texcoord : TEXCOORD0;
};
struct v2f
{
float4 pos : SV_POSITION;
float2 uv : TEXCOORD0;
float3 worldPos : TEXCOORD1;
float3 worldNormal : TEXCOORD2;
};
sampler2D _MainTex;
fixed4 _Color;
fixed _AlphaScale;
v2f vert(a2v v)
{
v2f o;
o.pos = UnityObjectToClipPos(v.vertex.xyz);
o.worldPos = mul(unity_ObjectToWorld, v.vertex).xyz;
o.worldNormal = UnityObjectToWorldNormal(v.normal);
o.uv = TRANSFORM_UV(v.texcoord);
return o;
}
fixed4 frag(v2f i) : SV_Target
{
//获得对应像素
fixed4 albedo = tex2D(_MainTex, i.uv) * _Color;
fixed3 worldNormal = normalize(i.worldNormal);
fixed3 litDir = normalize(UnityWorldSpaceLightDir(i.worldPos));
fixed3 viewDir = normalize(UnityWorldSpaceViewDir(i.worldPos));
//获得环境光
fixed3 ambient = UNITY_LIGHTMODEL_AMBIENT.xyz * albedo.rgb;
//获得漫反射
fixed3 diffuse = _LightColor0.rgb * albedo * saturate(dot(litDir, worldNormal));
return fixed4(ambient + diffuse, albedo.a * _AlphaScale);
}
ENDCG
}
}
Fallback "Transparent/VertexLit"
}
透明混合 - (对复杂非凸网格有效,但消耗性能)
Shader "Transparent/AlphaBlend"
{
Properties
{
[NoScaleOffset] _MainTex ("Main Tex", 2D) = "white" {}
_Color ("Color", Color) = (1, 1, 1, 1)
_AlphaScale ("Alpha Scale", Range(0, 1)) = 1
}
SubShader
{
Tags { "Queue" = "Transparent" "IgnoreProjector" = "True" "RenderType" = "Transparent" }
Pass
{
//开启深度写入
ZWrite On
//颜色通道掩码
ColorMAsk 0
//将深度信息写入缓存
}
Pass
{
//关闭深度写入
ZWrite Off
Blend SrcAlpha OneMinusSrcAlpha
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
#include <UnityCG.cginc>
#include <UnityShaderUtilities.cginc>
#include <UnityLightingCommon.cginc>
struct a2v
{
float4 vertex : POSITION;
float3 normal : NORMAL;
float4 texcoord : TEXCOORD0;
};
struct v2f
{
float4 pos : SV_POSITION;
float2 uv : TEXCOORD0;
float3 worldPos : TEXCOORD1;
float3 worldNormal : TEXCOORD2;
};
sampler2D _MainTex;
fixed4 _Color;
fixed _AlphaScale;
v2f vert(a2v v)
{
v2f o;
o.pos = UnityObjectToClipPos(v.vertex.xyz);
o.worldPos = mul(unity_ObjectToWorld, v.vertex).xyz;
o.worldNormal = UnityObjectToWorldNormal(v.normal);
o.uv = TRANSFORM_UV(v.texcoord);
return o;
}
fixed4 frag(v2f i) : SV_Target
{
//获得对应像素
fixed4 albedo = tex2D(_MainTex, i.uv) * _Color;
fixed3 worldNormal = normalize(i.worldNormal);
fixed3 litDir = normalize(UnityWorldSpaceLightDir(i.worldPos));
fixed3 viewDir = normalize(UnityWorldSpaceViewDir(i.worldPos));
//获得环境光
fixed3 ambient = UNITY_LIGHTMODEL_AMBIENT.xyz * albedo.rgb;
//获得漫反射
fixed3 diffuse = _LightColor0.rgb * albedo * saturate(dot(litDir, worldNormal));
return fixed4(ambient + diffuse, albedo.a * _AlphaScale);
}
ENDCG
}
}
Fallback "Transparent/VertexLit"
}
双面渲染透明 - (凸网格才行,否则渲染将会出错)
Shader "Transparent/AlphaBlend"
{
Properties
{
[NoScaleOffset] _MainTex ("Main Tex", 2D) = "white" {}
_Color ("Color", Color) = (1, 1, 1, 1)
_AlphaScale ("Alpha Scale", Range(0, 1)) = 1
}
SubShader
{
Tags { "Queue" = "Transparent" "IgnoreProjector" = "True" "RenderType" = "Transparent" }
Pass
{
Tags { "LightMode" = "ForwardBase" }
//剔除
Cull Front
//开启深度写入
ZWrite On
//颜色通道掩码
ColorMAsk 0
//将深度信息写入缓存
}
Pass
{
Tags { "LightMode" = "ForwardBase" }
//剔除
Cull Back
//关闭深度写入
ZWrite Off
Blend SrcAlpha OneMinusSrcAlpha
BlendOp Add
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
#include <UnityCG.cginc>
#include <UnityShaderUtilities.cginc>
#include <UnityLightingCommon.cginc>
struct a2v
{
float4 vertex : POSITION;
float3 normal : NORMAL;
float4 texcoord : TEXCOORD0;
};
struct v2f
{
float4 pos : SV_POSITION;
float2 uv : TEXCOORD0;
float3 worldPos : TEXCOORD1;
float3 worldNormal : TEXCOORD2;
};
sampler2D _MainTex;
fixed4 _Color;
fixed _AlphaScale;
v2f vert(a2v v)
{
v2f o;
o.pos = UnityObjectToClipPos(v.vertex.xyz);
o.worldPos = mul(unity_ObjectToWorld, v.vertex).xyz;
o.worldNormal = UnityObjectToWorldNormal(v.normal);
o.uv = TRANSFORM_UV(v.texcoord);
return o;
}
fixed4 frag(v2f i) : SV_Target
{
//获得对应像素
fixed4 albedo = tex2D(_MainTex, i.uv) * _Color;
fixed3 worldNormal = normalize(i.worldNormal);
fixed3 litDir = normalize(UnityWorldSpaceLightDir(i.worldPos));
fixed3 viewDir = normalize(UnityWorldSpaceViewDir(i.worldPos));
//获得环境光
fixed3 ambient = UNITY_LIGHTMODEL_AMBIENT.xyz * albedo.rgb;
//获得漫反射
fixed3 diffuse = _LightColor0.rgb * albedo * saturate(dot(litDir, worldNormal));
//获得高光
fixed3 reflectDir = normalize(reflect(-litDir, worldNormal));
fixed3 specular = _LightColor0.rgb * saturate(pow(dot(reflectDir, viewDir), 20));
return fixed4(ambient + diffuse, albedo.a * _AlphaScale);
}
ENDCG
}
}
Fallback "Transparent/VertexLit"
}
来源为 Unity Shader 入门精要这本书 - 基础纹理和法线纹理的实现