diff --git a/ShaderLibrary/GlobalIllumination.hlsl b/ShaderLibrary/GlobalIllumination.hlsl index 31cbea4..67d59c8 100644 --- a/ShaderLibrary/GlobalIllumination.hlsl +++ b/ShaderLibrary/GlobalIllumination.hlsl @@ -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 _AmbientProbeData; @@ -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); @@ -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); diff --git a/ShaderLibrary/UnityGBuffer.hlsl b/ShaderLibrary/UnityGBuffer.hlsl index b20dbab..bf63100 100644 --- a/ShaderLibrary/UnityGBuffer.hlsl +++ b/ShaderLibrary/UnityGBuffer.hlsl @@ -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. @@ -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 @@ -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 diff --git a/Shaders/Lighting/DeferredLighting.compute b/Shaders/Lighting/DeferredLighting.compute index 876418e..c9706ec 100644 --- a/Shaders/Lighting/DeferredLighting.compute +++ b/Shaders/Lighting/DeferredLighting.compute @@ -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 @@ -435,6 +437,7 @@ DeferredLightingOutput DeferredLit(PositionInputs posInput, ShadingData shadingD directSpecular *= shadowAttenuation; } #endif /* _RAYTRACING_SHADOWS */ + } // Punctual Lights @@ -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 diff --git a/Shaders/LitGBufferPass.hlsl b/Shaders/LitGBufferPass.hlsl index 9a1202c..b95b7ca 100644 --- a/Shaders/LitGBufferPass.hlsl +++ b/Shaders/LitGBufferPass.hlsl @@ -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