基础纹理
单张颜色纹理
Shader "Unlit/MainTex"
{
Properties
{
_Color("Color", color) = (1,1,1,1)
_MainTex("MainTex", 2D) = "white" {}
_Specular("Specular", color) = (1,1,1,1)
_Gloss("Gloss", Range(8, 256)) = 20
}
SubShader
{
Tags { "LightMode"="ForwardBase" }
LOD 100
Pass
{
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
#include "UnityCG.cginc"
#include "Lighting.cginc"
fixed4 _Color;
sampler2D _MainTex;
float4 _MainTex_ST;
fixed4 _Specular;
float _Gloss;
struct appdata
{
float4 vertex : POSITION;
float3 normal : NORMAL;
float4 texcoord : TEXCOORD0;
};
struct v2f
{
float4 pos : SV_POSITION;
fixed3 worldNormal : TEXCOORD0;
fixed3 worldPos : TEXCOORD1;
float2 uv : TEXCOORD2;
};
v2f vert (appdata v)
{
v2f o;
o.pos = UnityObjectToClipPos(v.vertex);
//计算世界法线方向
o.worldNormal = UnityObjectToWorldNormal(v.normal);
//计算顶点世界位置
o.worldPos = mul(unity_ObjectToWorld, v.vertex).xyz;
//顶点纹理坐标变换
o.uv = TRANSFORM_TEX(v.texcoord, _MainTex);
return o;
}
fixed4 frag (v2f i) : SV_Target
{
//归一化世界法线方向
fixed3 worldNormalDir = normalize(i.worldNormal);
//计算世界灯光方向
fixed3 worldLightDir = normalize(UnityWorldSpaceLightDir(i.worldPos));
//纹理采样
fixed3 albedo = tex2D(_MainTex, i.uv).rgb * _Color.rgb;
//环境光
fixed3 ambient = UNITY_LIGHTMODEL_AMBIENT.xyz * albedo;
//计算半兰伯特光照(把世界法线和世界灯光点乘后的标量乘0.5在加0.5,意为把点乘的结果从[-1,1]映射到[0,1]的范围内)
fixed halfLambert = dot(worldLightDir, worldNormalDir) * 0.5 +0.5;
fixed3 diffuse = _LightColor0.rgb * albedo * halfLambert;
//计算世界空间下视角方向(世界空间下摄像机位置减去世界空间下顶点位置)
fixed3 viewDir = normalize(UnityWorldSpaceViewDir(i.worldPos.xyz));
//计算视角方向和灯光方向的半角向量
fixed3 halfDir = normalize(worldLightDir + viewDir);
//计算高光
fixed3 specular = _LightColor0.rgb * _Specular.rgb * pow(saturate(dot(worldNormalDir, halfDir)), _Gloss);
return fixed4(ambient + diffuse + specular, 1);
}
ENDCG
}
}
Fallback "Diffuse"
}
法线纹理
切线空间下法线纹理计算
Shader "Unlit/TangentSpaceNormal"
{
Properties
{
_Color("Color", color) = (1,1,1,1)
_MainTex("MainTex", 2D) = "white" {}
_BumpMap("Bump Map", 2D) = "bump" {}
_BumpScale("Bump Scale", float) = 1
_Specular("Specular", color) = (1,1,1,1)
_Gloss("Gloss", Range(8, 256)) = 20
}
SubShader
{
Tags { "LightMode"="ForwardBase" }
LOD 100
Pass
{
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
#include "UnityCG.cginc"
#include "Lighting.cginc"
fixed4 _Color;
sampler2D _MainTex;
float4 _MainTex_ST;
sampler2D _BumpMap;
float4 _BumpMap_ST;
float _BumpScale;
fixed4 _Specular;
float _Gloss;
struct appdata
{
float4 vertex : POSITION;
float3 normal : NORMAL;
float4 tangent : TANGENT;
float4 texcoord : TEXCOORD0;
};
struct v2f
{
float4 pos : SV_POSITION;
fixed4 uv : TEXCOORD0;
float3 lightDir : TEXCOORD1;
float3 viewDir : TEXCOORD2;
};
v2f vert (appdata v)
{
v2f o;
o.pos = UnityObjectToClipPos(v.vertex);
//顶点纹理坐标变换
o.uv.xy = TRANSFORM_TEX(v.texcoord, _MainTex);
o.uv.zw = TRANSFORM_TEX(v.texcoord, _BumpMap);
//unity内置宏,得到从模型空间到切线空间的变换矩阵rotation
TANGENT_SPACE_ROTATION;
o.lightDir = mul(rotation, ObjSpaceLightDir(v.vertex)).xyz;
o.viewDir = mul(rotation, ObjSpaceViewDir(v.vertex)).xyz;
return o;
}
fixed4 frag (v2f i) : SV_Target
{
fixed3 tangentLightDir = normalize(i.lightDir);
fixed3 tangentViewDir = normalize(i.viewDir);
//采样法线纹理
fixed4 packdeNormal = tex2D(_BumpMap, i.uv.zw);
fixed3 tangentNormal;
//解压法线
tangentNormal = UnpackNormal(packdeNormal);
tangentNormal.xy *= _BumpScale;
tangentNormal.z = sqrt(1 - saturate(dot(tangentNormal.xy, tangentNormal.xy)));
//颜色纹理采样
fixed3 albedo = tex2D(_MainTex, i.uv).rgb * _Color.rgb;
//环境光
fixed3 ambient = UNITY_LIGHTMODEL_AMBIENT.xyz * albedo;
//计算半兰伯特光照(把世界法线和世界灯光点乘后的标量乘0.5在加0.5,意为把点乘的结果从[-1,1]映射到[0,1]的范围内)
fixed halfLambert = dot(tangentLightDir, tangentNormal) * 0.5 +0.5;
fixed3 diffuse = _LightColor0.rgb * albedo * halfLambert;
//计算视角方向和灯光方向的半角向量
fixed3 halfDir = normalize(tangentLightDir + tangentViewDir);
//计算高光
fixed3 specular = _LightColor0.rgb * _Specular.rgb * pow(saturate(dot(tangentNormal, halfDir)), _Gloss);
return fixed4(ambient + diffuse + specular, 1);
}
ENDCG
}
}
Fallback "Diffuse"
}
世界空间下法线纹理计算
Shader "Unlit/WorldSpaceNormal"
{
Properties
{
_Color("Color", color) = (1,1,1,1)
_MainTex("MainTex", 2D) = "white" {}
_BumpMap("Bump Map", 2D) = "bump" {}
_BumpScale("Bump Scale", float) = 1
_Specular("Specular", color) = (1,1,1,1)
_Gloss("Gloss", Range(8, 256)) = 20
}
SubShader
{
Tags { "LightMode"="ForwardBase" }
LOD 100
Pass
{
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
#include "UnityCG.cginc"
#include "Lighting.cginc"
fixed4 _Color;
sampler2D _MainTex;
float4 _MainTex_ST;
sampler2D _BumpMap;
float4 _BumpMap_ST;
float _BumpScale;
fixed4 _Specular;
float _Gloss;
struct appdata
{
float4 vertex : POSITION;
float3 normal : NORMAL;
float4 tangent : TANGENT;
float4 texcoord : TEXCOORD0;
};
struct v2f
{
float4 pos : SV_POSITION;
fixed4 uv : TEXCOORD0;
float4 TtoW0 : TEXCOORD1;
float4 TtoW1 : TEXCOORD2;
float4 TtoW2 : TEXCOORD3;
};
v2f vert (appdata v)
{
v2f o;
o.pos = UnityObjectToClipPos(v.vertex);
//顶点纹理坐标变换
o.uv.xy = TRANSFORM_TEX(v.texcoord, _MainTex);
o.uv.zw = TRANSFORM_TEX(v.texcoord, _BumpMap);
//世界空间顶点位置
float3 worldPos = mul(unity_ObjectToWorld, v.vertex).xyz;
//世界空间法线方向
fixed3 worldNormal = UnityObjectToWorldNormal(v.normal);
//世界空间切线方向
fixed3 worldTangent = UnityObjectToWorldDir(v.tangent.xyz);
//计算世界空间下副切线
fixed3 worldBinormal = cross(worldNormal, worldTangent) * v.tangent.w;
//切线空间到世界空间的变化矩阵
o.TtoW0 = float4(worldTangent.x, worldBinormal.x, worldNormal.x, worldPos.x);
o.TtoW1 = float4(worldTangent.y, worldBinormal.y, worldNormal.y, worldPos.y);
o.TtoW2 = float4(worldTangent.z, worldBinormal.z, worldNormal.z, worldPos.z);
return o;
}
fixed4 frag (v2f i) : SV_Target
{
//构建世界空间下的坐标
float3 worldPos = float3(i.TtoW0.w, i.TtoW1.w, i.TtoW2.w);
//得到世界空间下的光照和视角方向
fixed3 lightDir = normalize(UnityWorldSpaceLightDir(worldPos));
fixed3 viewDir = normalize(UnityWorldSpaceViewDir(worldPos));
//采样法线纹理并解码
fixed3 bump = UnpackNormal(tex2D(_BumpMap, i.uv.zw));
bump.xy *= _BumpScale;
bump.z = sqrt(1 - saturate(dot(bump.xy, bump.xy)));
//把法线变换到世界空间下
bump = normalize(half3(dot(i.TtoW0.xyz, bump), dot(i.TtoW1.xyz, bump), dot(i.TtoW2.xyz, bump)));
//纹理颜色采样
fixed3 albedo = tex2D(_MainTex, i.uv).rgb * _Color.rgb;
//环境光
fixed3 ambient = UNITY_LIGHTMODEL_AMBIENT.xyz * albedo;
//计算半兰伯特光照(把世界法线和世界灯光点乘后的标量乘0.5在加0.5,意为把点乘的结果从[-1,1]映射到[0,1]的范围内)
fixed halfLambert = dot(lightDir, bump) * 0.5 +0.5;
fixed3 diffuse = _LightColor0.rgb * albedo * halfLambert;
//计算视角方向和灯光方向的半角向量
fixed3 halfDir = normalize(lightDir + viewDir);
//计算高光
fixed3 specular = _LightColor0.rgb * _Specular.rgb * pow(saturate(dot(bump, halfDir)), _Gloss);
return fixed4(ambient + diffuse + specular, 1);
}
ENDCG
}
}
Fallback "Diffuse"
}
渐变纹理
Shader "Unlit/Ramp Texture"
{
Properties
{
_Color("Color", Color) = (1,1,1,1)
_RampTex ("RampTex", 2D) = "white" {}
_Specular("Specular", color) = (1,1,1,1)
_Gloss("Gloss", Range(8, 256)) = 20
}
SubShader
{
Tags { "RenderType"="Opaque" }
LOD 100
Pass
{
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
#include "Lighting.cginc"
#include "UnityCG.cginc"
fixed4 _Color;
sampler2D _RampTex;
float4 _RampTex_ST;
fixed4 _Specular;
float _Gloss;
struct appdata
{
float4 vertex : POSITION;
float3 normal : NORMAL;
float2 texcoord : TEXCOORD0;
};
struct v2f
{
float4 pos : SV_POSITION;
float3 worldNormal : TEXCOORD0;
float3 worldPos : TEXCOORD1;
};
v2f vert (appdata v)
{
v2f o;
o.pos = UnityObjectToClipPos(v.vertex);
o.worldNormal = UnityObjectToWorldNormal(v.normal);
o.worldPos = mul(unity_ObjectToWorld, v.vertex).xyz;
return o;
}
fixed4 frag (v2f i) : SV_Target
{
fixed3 worldNormal = normalize(i.worldNormal);
fixed3 worldLightDir = normalize(UnityWorldSpaceLightDir(i.worldPos));
fixed3 ambient = UNITY_LIGHTMODEL_AMBIENT.xyz;
fixed halfLambert = dot(worldNormal, worldLightDir) * 0.5 + 0.5;
//使用兰伯特构建新的纹理坐标(兰伯特为0到1之间,使用兰伯特照到片元上的)
fixed3 diffuseColor = tex2D(_RampTex, fixed2(halfLambert, halfLambert)).rgb * _Color.rgb;
fixed3 diffuse = _LightColor0.rgb * diffuseColor;
fixed3 viewDir = normalize(UnityWorldSpaceViewDir(i.worldPos));
fixed3 halfDir = normalize(viewDir + worldLightDir);
fixed3 specular = _LightColor0.rgb * _Specular.rgb * pow(saturate(dot(worldNormal, halfDir)), _Gloss);
return fixed4(ambient + diffuse + specular, 1);
}
ENDCG
}
}
Fallback "Diffuse"
}
遮罩纹理
Shader "Unlit/Mask Texture"
{
Properties
{
_Color("Color", Color) = (1,1,1,1)
_MainTex ("Texture", 2D) = "white" {}
_BumpMap("Bump Map", 2D) = "bump" {}
_BumpScale("Bump Scale", float) = 1
_SpecularMask("Specular Mask", 2D) = "white" {}
_SpecularScale("SpecularScale", float) = 1
_Specular("Specular", color) = (1,1,1,1)
_Gloss("Gloss", Range(8, 256)) = 20
}
SubShader
{
Tags { "LightMode"="ForwardBase" }
Pass
{
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
#include "Lighting.cginc"
#include "UnityCG.cginc"
fixed4 _Color;
sampler2D _MainTex;
float4 _MainTex_ST;
sampler2D _BumpMap;
float _BumpScale;
sampler2D _SpecularMask;
float _SpecularScale;
fixed4 _Specular;
float _Gloss;
struct a2v
{
float4 vertex : POSITION;
float3 normal : NORMAL;
float4 tangent : TANGENT;
float2 texcoord : TEXCOORD0;
};
struct v2f
{
float4 pos : SV_POSITION;
float2 uv : TEXCOORD0;
float3 lightDir : TEXCOORD1;
float3 viewDir : TEXCOORD2;
};
v2f vert (a2v v)
{
v2f o;
o.pos = UnityObjectToClipPos(v.vertex);
o.uv = TRANSFORM_TEX(v.texcoord, _MainTex);
TANGENT_SPACE_ROTATION;
o.lightDir = mul(rotation, ObjSpaceLightDir(v.vertex)).xyz;
o.viewDir = mul(rotation, ObjSpaceViewDir(v.vertex)).xyz;
return o;
}
fixed4 frag (v2f i) : SV_Target
{
fixed3 tangentLightDir = normalize(i.lightDir);
fixed3 tangentViewDir = normalize(i.viewDir);
fixed3 tangentNormal = UnpackNormal(tex2D(_BumpMap, i.uv));
tangentNormal.xy *= _BumpScale;
tangentNormal.z = sqrt(1 - saturate(dot(tangentNormal.xy, tangentNormal.xy)));
fixed3 albedo = tex2D(_MainTex, i.uv).rgb * _Color.rgb;
fixed3 ambient = UNITY_LIGHTMODEL_AMBIENT.xyz * albedo;
fixed3 diffuse = _LightColor0.rgb * albedo * saturate(dot(tangentNormal, tangentLightDir));
fixed3 halfDir = normalize(tangentLightDir + tangentViewDir);
//用遮罩贴图的R通道乘高光强度
fixed specularMask = tex2D(_SpecularMask, i.uv).r * _SpecularScale;
fixed3 specular = _LightColor0.rgb * _Specular.rgb * pow(saturate(dot(tangentNormal, halfDir)), _Gloss) * specularMask;
return fixed4(ambient + diffuse + specular, 1);
}
ENDCG
}
}
Fallback "Specular"
}