--- title: Untitled date: 2024-12-08 12:18:54 excerpt: tags: rating: ⭐ --- # 阴影 - [剖析虚幻渲染体系(05)- 光源和阴影](https://www.cnblogs.com/timlly/p/14817455.html) ## 阴影类型 - **Static Shadow** - **Cascading Shadow Map** - **Per Object Shadow**:可移动组件使用的逐物体阴影应用阴影图到物体的包围盒,因此包围盒必须是精确的。对于骨骼网格,这意味着它们应该有一个物理资产。对于粒子系统,任何固定的边界框必须足够大,以容纳所有的粒子。_在网格的Lighting属性组中,Dynamic Inset Shadow可以开启逐物体阴影,对需要高质量高精度的物体非常有用。_ - **Dynamic Shadow**:可移动光源在所有物体上投射出完全动态的阴影(和光)。这种光源的任何数据不会被烘焙到光照图中,它可以自由地在所有东西上投射动态阴影。静态网格、骨架网格、粒子效果等等将完全从可移动光源投射和接收动态阴影。 - **Capsule Shadow** - **Contact Shadow** - **Distance Field Shadow** ## 相关类型 - ShadowRendering.h **FProjectedShadowInfo**:存储投影阴影先关信息。包含各种变换矩阵、阴影渲染函数以及渲染参数、灯光&场景&图元信息。 - SceneRendering.h FVisibleLightInfo:可见光源信息, 主要是阴影相关的信息. FVisibleLightViewInfo: ## DynamicShadows ### InitDynamicShadows() => CreateDynamicShadows() InitViews() => FSceneRenderer::InitDynamicShadows() PS. UE5.3中相关逻辑移动到***CreateDynamicShadows()*** 中了。InitDynamicShadows() => BeginInitDynamicShadows() => BeginGatherShadowPrimitives() => CreateDynamicShadows() 计算各种灯光类型,之后调用: - CreateWholeSceneProjectedShadow() - AddViewDependentWholeSceneShadowsForView() - SetupInteractionShadows() - ***CreatePerObjectProjectedShadow()*** - InitProjectedShadowVisibility() 丛越文章中的注释: ```c++ void FSceneRenderer::InitDynamicShadows(FRHICommandListImmediate& RHICmdList, FGlobalDynamicIndexBuffer& DynamicIndexBuffer, FGlobalDynamicVertexBuffer& DynamicVertexBuffer, FGlobalDynamicReadBuffer& DynamicReadBuffer) { // 初始化各类标记和数量. const bool bMobile = FeatureLevel < ERHIFeatureLevel::SM5; bool bStaticSceneOnly = false; for (int32 ViewIndex = 0; ViewIndex < Views.Num(); ViewIndex++) { FViewInfo& View = Views[ViewIndex]; bStaticSceneOnly = bStaticSceneOnly || View.bStaticSceneOnly; } const bool bProjectEnablePointLightShadows = Scene->ReadOnlyCVARCache.bEnablePointLightShadows; uint32 NumPointShadowCachesUpdatedThisFrame = 0; uint32 NumSpotShadowCachesUpdatedThisFrame = 0; // 预计算阴影. TArray PreShadows; // 视图关联的全景阴影. TArray ViewDependentWholeSceneShadows; // 视图关联的需要裁剪的全景阴影. TArray ViewDependentWholeSceneShadowsThatNeedCulling; { // 遍历所有光源, 将不同类型的光源加入不同类型的待渲染的阴影列表中. for (TSparseArray::TConstIterator LightIt(Scene->Lights); LightIt; ++LightIt) { const FLightSceneInfoCompact& LightSceneInfoCompact = *LightIt; FLightSceneInfo* LightSceneInfo = LightSceneInfoCompact.LightSceneInfo; FScopeCycleCounter Context(LightSceneInfo->Proxy->GetStatId()); FVisibleLightInfo& VisibleLightInfo = VisibleLightInfos[LightSceneInfo->Id]; // LightOcclusionType有阴影图和光追两种, 如果非阴影图类型, 则忽略. const FLightOcclusionType OcclusionType = GetLightOcclusionType(LightSceneInfoCompact); if (OcclusionType != FLightOcclusionType::Shadowmap) continue; // 如果光源没有开启阴影或阴影质量太小, 则忽略阴影图. if ((LightSceneInfoCompact.bCastStaticShadow || LightSceneInfoCompact.bCastDynamicShadow) && GetShadowQuality() > 0) { // 检测该光源是否在某个view里可见, 如果不可见, 则忽略. bool bIsVisibleInAnyView = false; for (int32 ViewIndex = 0; ViewIndex < Views.Num(); ViewIndex++) { bIsVisibleInAnyView = LightSceneInfo->ShouldRenderLight(Views[ViewIndex]); if (bIsVisibleInAnyView) { break; } } // 所有裁剪条件都通过了, 处理光源的阴影. if (bIsVisibleInAnyView) { // 初始化阴影的各种标记和变量. static const auto AllowStaticLightingVar = IConsoleManager::Get().FindTConsoleVariableDataInt(TEXT("r.AllowStaticLighting")); const bool bAllowStaticLighting = (!AllowStaticLightingVar || AllowStaticLightingVar->GetValueOnRenderThread() != 0); // 是否点光源阴影. 注意矩形光也当做点光源处理. const bool bPointLightShadow = LightSceneInfoCompact.LightType == LightType_Point || LightSceneInfoCompact.LightType == LightType_Rect; // 对不预计算阴影的移动光源只创建全景阴影(whole scene shadow). const bool bShouldCreateShadowForMovableLight = LightSceneInfoCompact.bCastDynamicShadow && (!LightSceneInfo->Proxy->HasStaticShadowing() || !bAllowStaticLighting); const bool bCreateShadowForMovableLight = bShouldCreateShadowForMovableLight && (!bPointLightShadow || bProjectEnablePointLightShadows); // 对带有预计算阴影的尚未构建的光源创建全景阴影. const bool bShouldCreateShadowToPreviewStaticLight = LightSceneInfo->Proxy->HasStaticShadowing() && LightSceneInfoCompact.bCastStaticShadow && !LightSceneInfo->IsPrecomputedLightingValid(); const bool bCreateShadowToPreviewStaticLight = bShouldCreateShadowToPreviewStaticLight && (!bPointLightShadow || bProjectEnablePointLightShadows); // 对需要静态阴影但由于重叠导致没有有效阴影图的光源创建全景阴影. const bool bShouldCreateShadowForOverflowStaticShadowing = LightSceneInfo->Proxy->HasStaticShadowing() && !LightSceneInfo->Proxy->HasStaticLighting() && LightSceneInfoCompact.bCastStaticShadow && LightSceneInfo->IsPrecomputedLightingValid() && LightSceneInfo->Proxy->GetShadowMapChannel() == INDEX_NONE; const bool bCreateShadowForOverflowStaticShadowing = bShouldCreateShadowForOverflowStaticShadowing && (!bPointLightShadow || bProjectEnablePointLightShadows); // 添加点光源的全景阴影. const bool bPointLightWholeSceneShadow = (bShouldCreateShadowForMovableLight || bShouldCreateShadowForOverflowStaticShadowing || bShouldCreateShadowToPreviewStaticLight) && bPointLightShadow; if (bPointLightWholeSceneShadow) { UsedWholeScenePointLightNames.Add(LightSceneInfoCompact.LightSceneInfo->Proxy->GetComponentName()); } // 创建光源的全景阴影. if (bCreateShadowForMovableLight || bCreateShadowToPreviewStaticLight || bCreateShadowForOverflowStaticShadowing) { CreateWholeSceneProjectedShadow(LightSceneInfo, NumPointShadowCachesUpdatedThisFrame, NumSpotShadowCachesUpdatedThisFrame); } // 允许移动和固定的光源创建CSM(级联阴影), 或者是尚未构建的静态光源. if ((!LightSceneInfo->Proxy->HasStaticLighting() && LightSceneInfoCompact.bCastDynamicShadow) || bCreateShadowToPreviewStaticLight) { // 增加视图关联的全景阴影. if( !bMobile || ((LightSceneInfo->Proxy->UseCSMForDynamicObjects() || LightSceneInfo->Proxy->IsMovable()) && (LightSceneInfo == Scene->MobileDirectionalLights[0] || LightSceneInfo == Scene->MobileDirectionalLights[1] || LightSceneInfo == Scene->MobileDirectionalLights[2]))) { AddViewDependentWholeSceneShadowsForView(ViewDependentWholeSceneShadows, ViewDependentWholeSceneShadowsThatNeedCulling, VisibleLightInfo, *LightSceneInfo); } // 处理交互阴影, 此处的交互是指光源和图元之间的影响. 包含PerObject阴影、透明阴影、自阴影等. if( !bMobile || (LightSceneInfo->Proxy->CastsModulatedShadows() && !LightSceneInfo->Proxy->UseCSMForDynamicObjects())) { Scene->FlushAsyncLightPrimitiveInteractionCreation(); // 处理动态图元的交互阴影. for (FLightPrimitiveInteraction* Interaction = LightSceneInfo->GetDynamicInteractionOftenMovingPrimitiveList(false); Interaction; Interaction = Interaction->GetNextPrimitive()) { SetupInteractionShadows(RHICmdList, Interaction, VisibleLightInfo, bStaticSceneOnly, ViewDependentWholeSceneShadows, PreShadows); } // 处理静态图元的交互阴影. for (FLightPrimitiveInteraction* Interaction = LightSceneInfo->GetDynamicInteractionStaticPrimitiveList(false); Interaction; Interaction = Interaction->GetNextPrimitive()) { SetupInteractionShadows(RHICmdList, Interaction, VisibleLightInfo, bStaticSceneOnly, ViewDependentWholeSceneShadows, PreShadows); } } } } } } // 计算投射阴影的可见性. InitProjectedShadowVisibility(RHICmdList); } // 清理旧的预计算阴影, 尝试增加新的到缓存中. UpdatePreshadowCache(FSceneRenderTargets::Get(RHICmdList)); // 收集图元列表, 以绘制不同类型的阴影. GatherShadowPrimitives(PreShadows, ViewDependentWholeSceneShadowsThatNeedCulling, bStaticSceneOnly); // 分配阴影深度渲染纹理. AllocateShadowDepthTargets(RHICmdList); // 收集阴影的动态网格元素, 跟之前剖析的GatherDynamicMeshElements类似. GatherShadowDynamicMeshElements(DynamicIndexBuffer, DynamicVertexBuffer, DynamicReadBuffer); } ``` ### 阴影渲染 # 阴影偏移 可以考虑的Buffer有 - ShadowDepths - CustomDepth # 相关Paas 1. ShadowDepths => RenderShadowDepthMaps() 2. Lights => RenderLights() 1. DirectLighting 1. UnbatchedLights 1. ShadowProjectionOnOpaque # ShadowDepths - FSceneRenderer::RenderShadowDepthMaps():位于CustomDepth之前。 - **RenderVirtualShadowMaps()** - RenderShadowDepthMapAtlases() - SortedShadowsForShadowDepthPass.ShadowMapCubemaps循环。渲染点光源阴影立方体图 - **FProjectedShadowInfo::RenderDepth()** - **FProjectedShadowInfo::RenderTranslucencyDepths** ## RenderDepth FShadowParallelCommandListSet # Lights ### Shader ShadowProjectionPixelShader.usf - TShadowProjectionPS: - TDirectionalPercentageCloserShadowProjectionPS:方向光投影 - TSpotPercentageCloserShadowProjectionPS:SpotLight - FOnePassPointShadowProjectionPS(Moible?) ### 相关函数 - FDeferredShadingSceneRenderer::RenderLights() - FDeferredShadingSceneRenderer::RenderDeferredShadowProjections() - FSceneRenderer::RenderShadowProjections() - FProjectedShadowInfo::SetupFrustumForProjection():构建阴影投影4棱椎平面信息。 - FProjectedShadowInfo::SetupProjectionStencilMask(): # 其他 顺序: RenderCustomDepthPass FSceneRenderer::CreateDynamicShadows => FSceneRenderer::CreatePerObjectProjectedShadow ```c++ if (!IsForwardShadingEnabled(ShaderPlatform)) { // Dynamic shadows are synced later when using the deferred path to make more headroom for tasks. FinishInitDynamicShadows(GraphBuilder, InitViewTaskDatas.DynamicShadows, InstanceCullingManager, ExternalAccessQueue); } ``` ```c++ if (RendererOutput == ERendererOutput::DepthPrepassOnly) { RenderOcclusionLambda(); if (bUpdateNaniteStreaming) { Nanite::GStreamingManager.SubmitFrameStreamingRequests(GraphBuilder); } CopySceneCaptureComponentToTarget(GraphBuilder, SceneTextures, ViewFamilyTexture, ViewFamily, Views); } else { GVRSImageManager.PrepareImageBasedVRS(GraphBuilder, ViewFamily, SceneTextures); if (!IsForwardShadingEnabled(ShaderPlatform)) { // Dynamic shadows are synced later when using the deferred path to make more headroom for tasks. FinishInitDynamicShadows(GraphBuilder, InitViewTaskDatas.DynamicShadows, InstanceCullingManager, ExternalAccessQueue); } // Update groom only visible in shadow if (IsHairStrandsEnabled(EHairStrandsShaderType::All, Scene->GetShaderPlatform()) && RendererOutput == ERendererOutput::FinalSceneColor) { UpdateHairStrandsBookmarkParameters(Scene, Views, HairStrandsBookmarkParameters); // Interpolation for cards/meshes only visible in shadow needs to happen after the shadow jobs are completed const bool bRunHairStrands = HairStrandsBookmarkParameters.HasInstances() && (Views.Num() > 0); if (bRunHairStrands) { RunHairStrandsBookmark(GraphBuilder, EHairStrandsBookmark::ProcessCardsAndMeshesInterpolation_ShadowView, HairStrandsBookmarkParameters); } } // NOTE: The ordering of the lights is used to select sub-sets for different purposes, e.g., those that support clustered deferred. FSortedLightSetSceneInfo& SortedLightSet = *GraphBuilder.AllocObject(); { RDG_CSV_STAT_EXCLUSIVE_SCOPE(GraphBuilder, SortLights); RDG_GPU_STAT_SCOPE(GraphBuilder, SortLights); ComputeLightGridOutput = GatherLightsAndComputeLightGrid(GraphBuilder, bComputeLightGrid, SortedLightSet); } ```