JJBB/Assets/Project/Shader/MobileBumpEmit.shader

114 lines
3.4 KiB
Plaintext
Raw Permalink Normal View History

2024-08-23 15:49:34 +08:00
// AuthorBlastom
// Meta Pass和Shadow Caster使用 Fallback
// 这个Shader只用于Non-Directional烘培的物体动态物体使用光照会不正常
// 支持GLES 2.0包括Shader Level 2.0的机器;
// 升级Unity版本如果有算法改变需要特殊处理。
Shader "Zhanyou/Mobile/DiffuseBumpedEmit" {
Properties {
_MainTex ("Base (RGB)", 2D) = "white" {}
[NoScaleOffset]_BumpMap ("Normal Map", 2D) = "bump" {}
_BumpLerp ("Normal Strength", Range(0, 5)) = 1
_Emission ("Emission", Range(0, 1)) = 0
}
SubShader {
Tags { "RenderType"="Opaque" }
// Forward Base
Pass
{
Name "FORWARD"
Tags { "LightMode" = "ForwardBase" }
CGPROGRAM
#define UNITY_PASS_FORWARDBASE
#include "UnityGiBase.cginc"
#pragma multi_compile_fwdbase
#pragma multi_compile_fog
#pragma multi_compile __ PLAYER_HALO_ON
#pragma vertex vert_Base
#pragma fragment frag
uniform sampler2D _MainTex;
uniform float4 _MainTex_ST;
uniform half _Emission;
#if !T4M_NORMAL_OFF
uniform sampler2D _BumpMap;
uniform half _BumpLerp;
#endif
fixed4 frag(v2f_Base i) : SV_TARGET
{
float2 uv = TRANSFORM_TEX(i.uv, _MainTex);
fixed4 c = tex2D(_MainTex, uv);
#if T4M_NORMAL_OFF
fixed3 worldNormal = fixed3(i.tSpace0.z, i.tSpace1.z, i.tSpace2.z);
#else
half3 normal = UnpackNormal(tex2D(_BumpMap, uv));
// 世界法线
fixed3 worldNormal;
worldNormal.x = dot(i.tSpace0.xyz, normal);
worldNormal.y = dot(i.tSpace1.xyz, normal);
worldNormal.z = dot(i.tSpace2.xyz, normal);
worldNormal = normalize(worldNormal);
#endif
// 标准UnityGi流程
GET_UNITY_GI_COLOR;
#if !T4M_NORMAL_OFF
// 法线强度调整功能
fixed ndotlVert = LambertTerm(fixed3(i.tSpace0.z, i.tSpace1.z, i.tSpace2.z), lightDir);
giColor += (ndotl - ndotlVert) * _BumpLerp * SIM_LIGHT_COLOR;
#endif
// 自发光功能
c.rgb = giColor + c.rgb * _Emission;
#if PLAYER_HALO_ON
c.rgb += _DirectionalLightColor * LightColorFromWorld(worldPos);
#endif
UNITY_APPLY_FOG(i.fogCoord, c);
UNITY_OPAQUE_ALPHA(c.a);
return c;
}
ENDCG
}
// Forward Add
// 注实际光照贴图只通过meta通道实现Forward Add仅用于编辑器无光照贴图时预览
// 简单处理Forward Add也使用顶点计算光照和视野方向真实光照参数
// 该通道可以省去镜面高光等所需参数,因此按常规算法做
Pass
{
Name "FORWARD_ADD"
Tags { "LightMode" = "ForwardAdd" }
ZWrite Off
Blend One One
CGPROGRAM
#define UNITY_PASS_FORWARDADD
#include "UnityGiBase.cginc"
#pragma multi_compile_fwdadd
#pragma multi_compile_fog
#pragma skip_variants INSTANCING_ON
#pragma vertex vert_Add
#pragma fragment frag
uniform sampler2D _MainTex;
uniform float4 _MainTex_ST;
uniform sampler2D _BumpMap;
fixed4 frag(v2f_Add i) : SV_TARGET
{
#ifdef LIGHTMAP_ON
fixed4 c = fixed4(0.0, 0.0, 0.0, 0.0);
#else
float2 uv = TRANSFORM_TEX(i.uv, _MainTex);
fixed4 c = tex2D(_MainTex, uv);
half3 normal = UnpackNormal(tex2D(_BumpMap, uv));
half NtoL = max(0.0, dot(normal, i.lightDir));
UNITY_LIGHT_ATTENUATION(atten, i, i.worldPos);
c.rgb = c.rgb * atten * _LightColor0.rgb * NtoL;
#endif
UNITY_APPLY_FOG(i.fogCoord, c);
UNITY_OPAQUE_ALPHA(c.a);
return c;
}
ENDCG
}
}
FallBack "Zhanyou/Mobile/DiffuseEmit"
}