Files

134 lines
3.7 KiB
HLSL

#ifndef OPAQUEOBJECT_BASE
#define OPAQUEOBJECT_BASE
#include "./Lighting_Base.hlsl"
TEXTURE2D(_MainTex); SAMPLER(sampler_MainTex);
TEXTURE2D(_SpecularTex); SAMPLER(sampler_SpecularTex);
CBUFFER_START(UnityPerMaterial)
float4 _MainTex_ST;
float4 _SpecularTex_ST;
half4 _SpecularColor;
half _SpecularFactor;
half _Smoothness;
half4 _MainColor;
half _UseCustomLight;
half4 _CustomLightDir;
half _TintStrength;
half _FogFactor;
half3 _lightDir;
half3 _lightColor;
half _lightStrength;
CBUFFER_END
struct a2v
{
half4 vertex : POSITION;
half4 normal : NORMAL;
half4 tangent: TANGENT;
half2 texcoord : TEXCOORD0;
half2 lightmapUV : TEXCOORD1;
UNITY_VERTEX_INPUT_INSTANCE_ID
};
struct v2f
{
float4 pos : SV_POSITION;
half4 uvTex0 : TEXCOORD0;
half3 worldPos : TEXCOORD1;
half3 worldNormal : TEXCOORD2;
half3 worldViewDir : TEXCOORD3;
float4 shadowCoord : TEXCOORD5;
#if defined(_SG_EXPFOG)
half4 _fogCoord : TEXCOORD7;
#endif
DECLARE_LIGHTMAP_OR_SH(lightmapUV, vertexSH, 6);
UNITY_VERTEX_INPUT_INSTANCE_ID
UNITY_VERTEX_OUTPUT_STEREO
};
v2f VertexCommon(a2v v)
{
v2f o = (v2f)0;
UNITY_SETUP_INSTANCE_ID(v);
UNITY_TRANSFER_INSTANCE_ID(v, o);
UNITY_INITIALIZE_VERTEX_OUTPUT_STEREO(o);
VertexPositionInputs vertexInput = GetVertexPositionInputs(v.vertex.xyz);
VertexNormalInputs normalInput = GetVertexNormalInputs(v.normal, v.tangent);
o.pos = vertexInput.positionCS;
o.uvTex0.xy = TRANSFORM_TEX(v.texcoord, _MainTex);
half3 worldPos = vertexInput.positionWS;
half3 worldNormal = normalInput.normalWS;
o.worldPos.xyz = worldPos;
o.worldNormal = worldNormal;
o.worldViewDir = GetCameraPositionWS() - worldPos;
o.shadowCoord = GetShadowCoord(vertexInput);
OUTPUT_LIGHTMAP_UV(v.lightmapUV, unity_LightmapST, o.lightmapUV);
OUTPUT_SH(worldNormal, o.vertexSH);
return o;
}
// vertex shader
v2f VertexForwardBase (a2v v)
{
v2f o = VertexCommon(v);
#if defined(_SG_EXPFOG)
CUSTOM_TRANSFER_FOG(o, v.vertex);
#endif
return o;
}
// fragment shader
half4 FragForwardBase (v2f i) : SV_Target
{
UNITY_SETUP_INSTANCE_ID(i);
UNITY_SETUP_STEREO_EYE_INDEX_POST_VERTEX(i);
half4 mainColor = SAMPLE_TEXTURE2D(_MainTex, sampler_MainTex, i.uvTex0) * _MainColor * _TintStrength;
half4 specularTex = SAMPLE_TEXTURE2D(_SpecularTex, sampler_SpecularTex, i.uvTex0);
float3 worldPos = i.worldPos;
float3 worldNormal = normalize(i.worldNormal);
half3 worldViewDir = normalize(i.worldViewDir);
half smoothness = _Smoothness;
half3 specularColor = _SpecularFactor * _SpecularColor * specularTex.a;
////阴影相关、烘焙相关/////////////////////////////////////////////////////////////////////////
half atten = 1;
float3 shLight = 1;
#if defined(LIGHTMAP_ON)
i.shadowCoord = TransformWorldToShadowCoord(worldNormal);
shLight = SAMPLE_GI(i.lightmapUV, i.vertexSH, worldNormal);
float shadowMask = SAMPLE_SHADOWMASK(i.lightmapUV);
atten = GetMainLight(i.shadowCoord, worldPos, shadowMask).shadowAttenuation;
atten = min(atten, shadowMask);
#endif
//////////////////////////////////////////////////////////////////////////////////////////////
#ifdef _LIGHTSWITCH_ON
specularColor *= _lightColor * _lightStrength;
half3 finalColor = BlinnPhoneLightingFwdBase_CustomLight(mainColor, worldPos, worldNormal, worldViewDir, shLight, atten, specularColor, smoothness, _lightDir);
#else
half3 finalColor = BlinnPhoneLightingFwdBase(mainColor, worldPos, worldNormal, worldViewDir, shLight, atten, specularColor, smoothness);
#endif
finalColor = ApplyDark(finalColor);
#if defined(_SG_EXPFOG)
CUSTOM_APPLY_FOG(i, finalColor, distance(_WorldSpaceCameraPos, worldPos));
#endif
return half4(finalColor, mainColor.a * _MainColor.a);
}
#endif