Skip to content

Commit

Permalink
Add baked Lightmaps feature implements.
Browse files Browse the repository at this point in the history
  • Loading branch information
danbaidong1111 committed Dec 14, 2024
1 parent 5f9541b commit 3ccefb0
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 4 deletions.
11 changes: 11 additions & 0 deletions ShaderLibrary/GlobalIllumination.hlsl
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@
#include "Packages/com.unity.render-pipelines.danbaidong/ShaderLibrary/GPUCulledLights.hlsl"
#include "Packages/com.unity.render-pipelines.danbaidong/ShaderLibrary/Material.hlsl"

// DanbaidongRP GlobalIllumination only handles bakedGI(Lightmaps).
#define USE_BAKED_GI_ONLY 1

#define AMBIENT_PROBE_BUFFER 1
TEXTURECUBE(_SkyTexture);
StructuredBuffer<float4> _AmbientProbeData;
Expand Down Expand Up @@ -488,6 +491,10 @@ half3 GlobalIllumination(BRDFData brdfData, BRDFData brdfDataClearCoat, float cl
half3 bakedGI, half occlusion, float3 positionWS,
half3 normalWS, half3 viewDirectionWS, float2 normalizedScreenSpaceUV)
{
#if USE_BAKED_GI_ONLY
return brdfData.diffuse * bakedGI * occlusion;
#endif

half3 reflectVector = reflect(-viewDirectionWS, normalWS);
half NoV = saturate(dot(normalWS, viewDirectionWS));
half fresnelTerm = Pow4(1.0 - NoV);
Expand Down Expand Up @@ -537,6 +544,10 @@ half3 GlobalIllumination(BRDFData brdfData, BRDFData brdfDataClearCoat, float cl
half3 bakedGI, half occlusion,
half3 normalWS, half3 viewDirectionWS)
{
#if USE_BAKED_GI_ONLY
return brdfData.diffuse * bakedGI * occlusion;
#endif

half3 reflectVector = reflect(-viewDirectionWS, normalWS);
half NoV = saturate(dot(normalWS, viewDirectionWS));
half fresnelTerm = Pow4(1.0 - NoV);
Expand Down
10 changes: 10 additions & 0 deletions ShaderLibrary/UnityGBuffer.hlsl
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,8 @@
#define kMaterialFlagSubtractiveMixedLighting 4 // The geometry uses subtractive mixed lighting
#define kMaterialFlagSpecularSetup 8 // Lit material use specular setup instead of metallic setup

#define kMaterialFlagUseBakedGI 16 // The geometry uses baked GlobalIllumination, so we don't need to add env ambient to result in DefferredLighting.

// Light flags.
#define kLightFlagSubtractiveMixedLighting 4 // The light uses subtractive mixed lighting.

Expand Down Expand Up @@ -133,6 +135,10 @@ FragmentOutput SurfaceDataToGbuffer(SurfaceData surfaceData, InputData inputData
materialFlags |= kMaterialFlagSubtractiveMixedLighting;
#endif

#if defined(LIGHTMAP_ON)
materialFlags |= kMaterialFlagUseBakedGI;
#endif

FragmentOutput output;
output.GBuffer0 = float4(surfaceData.albedo.rgb, PackMaterialFlags(materialFlags)); // albedo albedo albedo materialFlags (sRGB rendertarget)
output.GBuffer1 = float4(surfaceData.specular.rgb, surfaceData.occlusion); // specular specular specular occlusion
Expand Down Expand Up @@ -206,6 +212,10 @@ FragmentOutput BRDFDataToGbuffer(BRDFData brdfData, InputData inputData, float s
materialFlags |= kMaterialFlagSubtractiveMixedLighting;
#endif

#if defined(LIGHTMAP_ON)
materialFlags |= kMaterialFlagUseBakedGI;
#endif

FragmentOutput output;
output.GBuffer0 = float4(brdfData.albedo.rgb, PackMaterialFlags(materialFlags)); // diffuse diffuse diffuse materialFlags (sRGB rendertarget)
output.GBuffer1 = float4(packedSpecular, occlusion); // metallic/specular specular specular occlusion
Expand Down
11 changes: 10 additions & 1 deletion Shaders/Lighting/DeferredLighting.compute
Original file line number Diff line number Diff line change
Expand Up @@ -379,6 +379,8 @@ DeferredLightingOutput DeferredLit(PositionInputs posInput, ShadingData shadingD

}
// Apply Shadows
if (!materialReceiveShadowsOff)
{
#ifdef _RAYTRACING_SHADOWS
float shadowAttenuation = LoadScreenSpaceShadowmap(posInput.positionSS);
// Apply shadow strength
Expand Down Expand Up @@ -435,6 +437,7 @@ DeferredLightingOutput DeferredLit(PositionInputs posInput, ShadingData shadingD
directSpecular *= shadowAttenuation;
}
#endif /* _RAYTRACING_SHADOWS */
}


// Punctual Lights
Expand Down Expand Up @@ -523,9 +526,15 @@ DeferredLightingOutput DeferredLit(PositionInputs posInput, ShadingData shadingD
// 1. Screen Space Refraction / Reflection
// 2. Environment Reflection / Refraction
// 3. Sky Reflection / Refraction
bool materialUseBakedGI = (shadingData.materialFlags & kMaterialFlagUseBakedGI) != 0;

float3 SHColor = SampleSH9(_AmbientProbeData, normalWS);//EvaluateAmbientProbe(normalWS);
float3 SHColor = SampleSH9(_AmbientProbeData, normalWS);
indirectDiffuse += diffuseFGD * SHColor * shadingData.diffuseColor;
if (materialUseBakedGI)
{
// GI is from geometry's lightmap, stores in lighting buffer.
indirectDiffuse = 0;
}
// TODO: ModifyBakedDiffuseLighting Function


Expand Down
5 changes: 2 additions & 3 deletions Shaders/LitGBufferPass.hlsl
Original file line number Diff line number Diff line change
Expand Up @@ -235,10 +235,9 @@ FragmentOutput LitGBufferPassFragment(Varyings input)

Light mainLight = GetMainLight(inputData.shadowCoord, inputData.positionWS, inputData.shadowMask);
MixRealtimeAndBakedGI(mainLight, inputData.normalWS, inputData.bakedGI, inputData.shadowMask);
// half3 color = GlobalIllumination(brdfData, inputData.bakedGI, surfaceData.occlusion, inputData.positionWS, inputData.normalWS, inputData.viewDirectionWS);
half3 color = GlobalIllumination(brdfData, inputData.bakedGI, surfaceData.occlusion, inputData.positionWS, inputData.normalWS, inputData.viewDirectionWS);

// return BRDFDataToGbuffer(brdfData, inputData, surfaceData.smoothness, surfaceData.emission + color, surfaceData.occlusion);
return BRDFDataToGbuffer(brdfData, inputData, surfaceData.smoothness, surfaceData.emission, surfaceData.occlusion);
return BRDFDataToGbuffer(brdfData, inputData, surfaceData.smoothness, surfaceData.emission + color, surfaceData.occlusion);
}

#endif

0 comments on commit 3ccefb0

Please sign in to comment.