Files
PrinceOfGlory/Assets/D5VR/Shader/ShaderLibrary/StandardLighting.hlsl
kridoo 6e91a0c7f0 111
2025-09-15 17:32:08 +08:00

94 lines
4.1 KiB
HLSL

#include "LightingCommon.hlsl"
#include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/Lighting.hlsl"
//BRDF
float3 StandardBRDF(float3 DiffuseColor, float3 SpecularColor, float Roughness, float3 L , float3 V,float3 N,float3 LightColor,float Shadow )
{
float a2 = Pow4( Roughness );
float3 H = normalize(V + L);
float NoH = saturate(dot(N,H));
float NoV = saturate(abs(dot(N,V))+1e-5); // Both side
float NoL = saturate(dot(N,L));
float VoH = saturate(dot(V,H));
float3 Radiance = NoL * LightColor * Shadow * PI;
float3 DiffuseTerm = Diffuse_Lambert(DiffuseColor) * Radiance ;
// Generalized microfacet specular
float D = D_GGX_UE4( a2, NoH ) ;
float Vis = Vis_SmithJointApprox( a2, NoV, NoL );
float3 F = F_Schlick_UE4( SpecularColor, VoH );
half3 SpecularTerm =(( D * Vis )* F)* Radiance;
float3 DirectLighting = (DiffuseTerm + SpecularTerm) ;
return DirectLighting;
}
//IndirectLighting
void IndirectLighting_float(float3 DiffuseColor, float3 SpecularColor, float Roughness, float3 WorldPos, float3 N, float3 V,
float Occlusion,half HeightLight,float2 lightUV,float3 lDir,half _LightIntensitiy, out float3 IndirectLighting)
{
float NoV = saturate(abs(dot(N,V)) + 1e-5);
float3 DiffuseAO = AOMultiBounce(DiffuseColor,Occlusion);
float3 Irradiance = float3(0,0,0);
#if defined(LIGHTMAP_ON)
float4 encodedIrradiance = SAMPLE_TEXTURE2D(unity_Lightmap,samplerunity_Lightmap,lightUV);
Irradiance = DecodeLightmap(encodedIrradiance, float4(LIGHTMAP_HDR_MULTIPLIER, LIGHTMAP_HDR_EXPONENT, 0.0h, 0.0h));
#if defined(DIRLIGHTMAP_COMBINED)
float4 direction = SAMPLE_TEXTURE2D(unity_LightmapInd,samplerunity_Lightmap,lightUV);
half3 LightDir = direction * 2.0f - 1.0f;
half halfLambert = saturate(dot(N,LightDir)) * 0.5 + 0.5;
Irradiance = Irradiance * halfLambert / max(1e-4,direction.w);
//Irradiance = smoothstep(0,_dayNight,Irradiance);
half BlinnPhong = pow(max(0,saturate(dot(N,normalize(lDir + V)))),20);
Irradiance = Irradiance * _LightIntensitiy + Irradiance * BlinnPhong * HeightLight;
#endif
#else
Irradiance = SampleSH(N)* _LightIntensitiy;
#endif
float3 IndirectDiffuse = Irradiance * DiffuseColor * DiffuseAO;
half3 R = reflect(-V,N);
half3 SpeucularLD = GlossyEnvironmentReflection_revert(R,Roughness,DiffuseAO);
half3 SpecularDFG = EnvBRDFApprox(SpecularColor,Roughness,NoV);
half SpecularOcclusion = GetSpecularOcclusion(NoV,Roughness * Roughness,Occlusion);
half3 SpecularAO = AOMultiBounce(SpecularColor,SpecularOcclusion);
half3 IndirectSpecular = SpeucularLD * SpecularDFG * SpecularAO;
IndirectLighting = IndirectDiffuse +IndirectSpecular ;
}
//DirectLighting
void DirectLighting_float(float3 DiffuseColor, float3 SpecularColor, float Roughness,float3 WorldPos, float3 N, float3 V,
out float3 DirectLighting)
{
DirectLighting = half3(0,0,0);
#ifndef SHADERGRAPH_PREVIEW
#if defined(_MAIN_LIGHT_SHADOWS_SCREEN) && !defined(_SURFACE_TYPE_TRANSPARENT)
float4 positionCS = TransformWorldToHClip(WorldPos);
float4 ShadowCoord = ComputeScreenPos(positionCS);
#else
float4 ShadowCoord = TransformWorldToShadowCoord(WorldPos);
#endif
float4 ShadowMask = float4(1.0,1.0,1.0,1.0);
//mainLight
float3 DirectLight_MainLight = half3(0,0,0);
{
Light light = GetMainLight(ShadowCoord,WorldPos,ShadowMask);
float3 L = light.direction;
float3 LightColor = light.color;
float Shadow = light.shadowAttenuation;
DirectLight_MainLight = StandardBRDF(DiffuseColor,SpecularColor,Roughness,L,V,N,LightColor,Shadow) ;
}
//AddLight
float3 DirectLight_AddLight = half3(0,0,0);
uint pixelLightCount = GetAdditionalLightsCount();
for(uint lightIntex = 0 ;lightIntex < pixelLightCount;++lightIntex)
{
Light light = GetAdditionalLight(lightIntex,WorldPos,ShadowMask);
half3 L = light.direction;
half3 LightColor = light.color;
half Shadow = light.shadowAttenuation * light.distanceAttenuation;
DirectLight_AddLight += StandardBRDF(DiffuseColor,SpecularColor,Roughness,L,V,N,LightColor,Shadow) ;
}
DirectLighting = DirectLight_MainLight + DirectLight_AddLight;
#endif
}