Shader "Scene/BumpSpecAddLight"
{
Properties
{
_Color ("Color", Color) = (1,1,1,1)
_MainTex ("Texture", 2D) = "white" {}
_BumpMap ("Normalmap", 2D) = "bump" {}
_SpecColor ("Specular Color", Color) = (0.5, 0.5, 0.5, 1)
_Gloss ("Gloss", Range(8.0, 256)) = 20
_BumpScale ("Bump Scale", Float) = 1.0
}
SubShader {
Pass {
Tags { "RenderType"="Opaque" "LightMode" = "ForwardBase"}//////
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
#include "UnityCG.cginc"
#pragma target 3.0
#include "AutoLight.cginc"
#pragma multi_compile_fwdadd_fullshadows
sampler2D _MainTex;
float4 _MainTex_ST;
sampler2D _BumpMap;
float4 _BumpMap_ST;
float _BumpScale;
fixed4 _SpecColor;
float _Gloss;
float4 _Color;
float4 _LightColor0;//////
struct a2v
{
float4 vertex : POSITION; // 顶点位置
float3 normal : NORMAL; // 顶点法线
float4 texcoord : TEXCOORD0; // 第一UV坐标
float4 tangent : TANGENT;
};
struct v2f
{
float4 pos : SV_POSITION; // SV_POSITION( 可以用POSITION来代替)
float2 uv : TEXCOORD0;
float3 lightDir: TEXCOORD1;
float3 viewDir : TEXCOORD2;
half3 binormalWorld : TEXCOORD3;
LIGHTING_COORDS(5,6)
};
v2f vert(a2v v) {
v2f o;
o.pos = UnityObjectToClipPos(v.vertex); //从模型空间变化到裁剪空间
o.uv = v.texcoord.xy * _MainTex_ST.xy + _MainTex_ST.zw;
TRANSFER_VERTEX_TO_FRAGMENT(o);
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.0 - saturate(dot(tangentNormal.xy, tangentNormal.xy)));
float atten = LIGHT_ATTENUATION(i);
fixed3 albedo = tex2D(_MainTex, i.uv).rgb * _Color.rgb; //取贴图像素颜色
fixed3 ambient = UNITY_LIGHTMODEL_AMBIENT.xyz * albedo; //得环境光照
fixed halfLambert = dot(tangentNormal, tangentLightDir) ;
fixed3 diffuse = _LightColor0.rgb * albedo * max(0, dot(tangentNormal, tangentLightDir))*atten;
fixed3 halfDir = normalize(tangentLightDir + tangentViewDir);
fixed3 specular = _LightColor0.rgb * _SpecColor.rgb * pow(max(0, dot(tangentNormal, halfDir)), _Gloss);
return fixed4(ambient + specular+diffuse , 1.0); //颜色相加
}
ENDCG
}
pass
{
Tags { "RenderType"="Opaque" "LightMode" = "ForwardAdd"} //ForwardAdd :多灯混合//////
Blend One One//////
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
#include "UnityCG.cginc"
#pragma target 3.0
#include "AutoLight.cginc"//////
#pragma multi_compile_fwdadd_fullshadows//////
sampler2D _MainTex;
sampler2D _BumpMap;
float4 _BumpMap_ST;
float _BumpScale;
float4 _MainTex_ST;
float4 _SpecColor;
float _Gloss;
float4 _Color;
float4 _LightColor0;//////
struct appdata
{
float4 vertex : POSITION;
float2 uv : TEXCOORD0;
float4 tangent : TANGENT;
float4 normal:NORMAL;//////
};
struct v2f
{
float2 uv : TEXCOORD0;
float4 pos: SV_POSITION;
float3 normal :TEXCOORD1;//////
float4 wPos :TEXCOORD2;//////
float4 TtoW0 : TEXCOORD3;
float4 TtoW1 : TEXCOORD4;
float4 TtoW2 : TEXCOORD5;
//点光源需要的衰减
LIGHTING_COORDS(6,7)
};
v2f vert (appdata v)
{
v2f o;
o.pos= UnityObjectToClipPos(v.vertex);
o.uv = TRANSFORM_TEX(v.uv, _MainTex);
o.wPos= mul(unity_ObjectToWorld, v.vertex);//////
o.normal = v.normal;//////
//点光源需要的衰减
TRANSFER_VERTEX_TO_FRAGMENT(o)//////#include "AutoLight.cginc"
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));
bump = normalize(half3(dot(i.TtoW0.xyz, bump), dot(i.TtoW1.xyz, bump), dot(i.TtoW2.xyz, bump)));
//物体法向量转化为世界法向量
float3 N = normalize(UnityObjectToWorldNormal(i.normal));//////
//世界光向量:这里计算的是点光源,按照灯光的距离来算衰减,第一个pass不需要
float3 L = normalize (lerp(_WorldSpaceLightPos0.xyz , _WorldSpaceLightPos0.xyz - i.wPos.xyz , _WorldSpaceLightPos0.w));//////
fixed3 halfDir = normalize(lightDir + viewDir);
fixed3 specular = _LightColor0.rgb * _SpecColor.rgb * pow(max(0, dot(bump, halfDir)), _Gloss);
//点光源需要的衰减
float atten = LIGHT_ATTENUATION(i);//////#include "AutoLight.cginc"
fixed4 col = tex2D(_MainTex, i.uv)*_Color;
//最终颜色 = 主颜色 x 灯光颜色 x 漫反射系数 x 衰减系数 第一个pass已经有了环境色 这里就不能加了
col.rgb = col.rgb * _LightColor0.rgb * saturate(dot(N,L))*atten*_SpecColor;//////
return col;
}
ENDCG
}
}
//需要产生阴影
FallBack "Diffuse"
}