From ed764d7f837ef6fb2cc114c9be6bdc15ec3294d0 Mon Sep 17 00:00:00 2001 From: user Date: Tue, 5 May 2026 22:32:58 +0900 Subject: [PATCH] =?UTF-8?q?Fix=20:=20NiloToon=20=EC=85=80=ED=94=84=20?= =?UTF-8?q?=EC=84=80=EB=8F=84=EC=9A=B0=20=EC=B9=B4=EB=A9=94=EB=9D=BC=20?= =?UTF-8?q?=EB=B0=96=20=EC=BA=90=EB=A6=AD=ED=84=B0=20=EA=B7=B8=EB=A6=BC?= =?UTF-8?q?=EC=9E=90=20=EB=88=84=EB=9D=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Unity 6 RG path가 main camera cullResults만 사용하여 카메라가 캐릭터를 프러스텀 밖으로 두면 shadow map RT에 캐릭터가 안 그려져 바닥 그림자가 사라지던 문제 해결. - Pass.cs ExecutePass(RG)/Execute(Legacy) 에 manual cmd.DrawRenderer 추가하여 NiloToonAllInOneRendererFeature.characterList 의 모든 활성 캐릭터를 cullResults 의존성 없이 직접 그림 - shader 별 NiloToonSelfShadowCaster pass index 캐싱 - validCharList의 frustum AABB 필터 제거하여 키워드/ortho box 항상 유지 - SkinnedMeshRenderer.updateWhenOffscreen 강제로 카메라 밖에서도 본 매트릭스 갱신 (localBounds는 GetCharacterBoundCenter에 영향 주므로 건드리지 않음) Co-Authored-By: Claude Opus 4.7 (1M context) --- .../NiloToonPerCharacterRenderController.cs | 24 ++- .../Passes/NiloToonCharSelfShadowMapRTPass.cs | 140 +++++++++++++----- 2 files changed, 125 insertions(+), 39 deletions(-) diff --git a/Assets/NiloToonURP/Runtime/NiloToonPerCharacterRenderController.cs b/Assets/NiloToonURP/Runtime/NiloToonPerCharacterRenderController.cs index fbf69a2cd..122144b1c 100644 --- a/Assets/NiloToonURP/Runtime/NiloToonPerCharacterRenderController.cs +++ b/Assets/NiloToonURP/Runtime/NiloToonPerCharacterRenderController.cs @@ -1109,10 +1109,20 @@ namespace NiloToon.NiloToonURP if (regenerateSmoothedNormalInUV8) { - NiloBakeSmoothNormalTSToMeshUv8.GenGOSmoothedNormalToUV8(gameObject); + NiloBakeSmoothNormalTSToMeshUv8.GenGOSmoothedNormalToUV8(gameObject); } - + AutoFillInMissingProperties(); + + // Streamingle: 이미 등록된 renderer에도 updateWhenOffscreen 적용 (씬에 미리 있던 캐릭터 대응). + // RefillAllRenderers는 새 등록 때만 호출되므로 OnEnable에서 한 번 더 강제. + for (int i = 0; i < allRenderers.Count; i++) + { + if (allRenderers[i] is SkinnedMeshRenderer existingSmr && !existingSmr.updateWhenOffscreen) + { + existingSmr.updateWhenOffscreen = true; + } + } // To support VRMBlendShapeProxy, NiloToon now generates material instances on OnEnable(), which is before VRMBlendShapeProxy's Start() // Note: @@ -1537,7 +1547,15 @@ namespace NiloToon.NiloToonURP // we don't want to add particle/vfx/trail....renderers if(!(renderer is MeshRenderer or SkinnedMeshRenderer)) continue; - + + // Streamingle: Pass.cs의 manual DrawRenderer로 cullResults 우회 시, + // SkinnedMeshRenderer가 화면 밖이면 Unity가 본 매트릭스 갱신을 skip하여 stale pose가 그려질 수 있음. + // updateWhenOffscreen=true 강제. (localBounds는 GetCharacterBoundCenter에 영향 주므로 건드리지 않음) + if (renderer is SkinnedMeshRenderer smr && !smr.updateWhenOffscreen) + { + smr.updateWhenOffscreen = true; + } + var NiloToonPerCharacterRenderControllerFound = renderer.transform.GetComponentInParent(); if(NiloToonPerCharacterRenderControllerFound) { diff --git a/Assets/NiloToonURP/Runtime/RendererFeatures/Passes/NiloToonCharSelfShadowMapRTPass.cs b/Assets/NiloToonURP/Runtime/RendererFeatures/Passes/NiloToonCharSelfShadowMapRTPass.cs index 29f957be0..70c9b37b1 100644 --- a/Assets/NiloToonURP/Runtime/RendererFeatures/Passes/NiloToonCharSelfShadowMapRTPass.cs +++ b/Assets/NiloToonURP/Runtime/RendererFeatures/Passes/NiloToonCharSelfShadowMapRTPass.cs @@ -174,6 +174,31 @@ namespace NiloToon.NiloToonURP List validCharList = new List(); List finalValidCharList = new List(); + // Streamingle: shader → "NiloToonSelfShadowCaster" pass index 캐시. + // Unity 6 RG path는 main camera cullResults 만 쓰므로 카메라가 캐릭터 안 보면 shadow map 비어 그림자 사라짐. + // ExecutePass에서 manual DrawRenderer로 cullResults 우회. shader 별 pass index 조회는 비싸 캐시 필요. + static readonly Dictionary s_shadowCasterPassIndexCache = new Dictionary(); + static readonly ShaderTagId s_lightModeTagId = new ShaderTagId("LightMode"); + static readonly ShaderTagId s_shadowCasterTagId = new ShaderTagId("NiloToonSelfShadowCaster"); + + static int GetShadowCasterPassIndex(Shader shader) + { + if (shader == null) return -1; + if (s_shadowCasterPassIndexCache.TryGetValue(shader, out int cached)) return cached; + int found = -1; + int passCount = shader.passCount; + for (int i = 0; i < passCount; i++) + { + if (shader.FindPassTagValue(i, s_lightModeTagId) == s_shadowCasterTagId) + { + found = i; + break; + } + } + s_shadowCasterPassIndexCache[shader] = found; + return found; + } + // Constructor(will not call on every frame) public NiloToonCharSelfShadowMapRTPass(NiloToonRendererFeatureSettings allSettings) @@ -448,27 +473,15 @@ namespace NiloToon.NiloToonURP // https://docs.microsoft.com/en-us/windows/win32/dxtecharts/common-techniques-to-improve-shadow-depth-maps //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// - GeometryUtility.CalculateFrustumPlanes(camera, cameraPlanes); - validCharList.Clear(); - // [1] filter list + // [1] Streamingle: 메인 카메라 frustum cull 제거. (controller bound 기반 필터) + // 캐릭터가 메인 카메라 frustum 밖이어도 ortho box / keyword 가 정상 동작해야 함. + // 실제 cullResults 우회는 ExecutePass의 manual DrawRenderer 가 담당. foreach (var targetChar in NiloToonAllInOneRendererFeature.characterList) { - // if target is not valid, skip it if (targetChar == null) continue; - if (!targetChar.isActiveAndEnabled) continue; // character GameObject not enabled(not rendering) but in list - - // if character bounding sphere is completely not visible in game camera frustum, skip it - var boundRadius = targetChar.GetCharacterBoundRadius(); - var centerPosWS = targetChar.GetCharacterBoundCenter(); - // TODO: this section is not correct, which may incorrectly cull effective shadow caster that is OUTSIDE of main camera frustum - if (!GeometryUtility.TestPlanesAABB(cameraPlanes, new Bounds(centerPosWS, Vector3.one * boundRadius))) - { - continue; - } - - // it is a valid visible char, add to list + if (!targetChar.isActiveAndEnabled) continue; validCharList.Add(targetChar); } @@ -715,6 +728,13 @@ namespace NiloToon.NiloToonURP var filterSetting = new FilteringSettings(RenderQueueRange.opaque); context.DrawRenderers(cullResults, ref drawSetting, ref filterSetting); // using custom cullResults from shadow camera's perspective, instead of main camera's cull result + // Streamingle: cullResults 우회용 manual draw. + // perfectCullingForShadowCasters=false 또는 terrain 존재 시 cullResults가 main camera cull로 fallback → + // 캐릭터가 frustum 밖이면 shadow map에 안 그려짐. 같은 renderer가 두 번 그려질 수 있으나 depth test가 처리. + DrawNiloToonCharsManuallyLegacy(cmd); + context.ExecuteCommandBuffer(cmd); + cmd.Clear(); + // Note: Since we are providing our own _NiloToonSelfShadowWorldToClip to shader for VP transform, // this section is not needed anymore, it will trigger a bug in multi pass mode XR /* @@ -1036,29 +1056,15 @@ namespace NiloToon.NiloToonURP // https://docs.microsoft.com/en-us/windows/win32/dxtecharts/common-techniques-to-improve-shadow-depth-maps //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// - GeometryUtility.CalculateFrustumPlanes(camera, cameraPlanes); - validCharList.Clear(); - // [1] filter list + // [1] Streamingle: 메인 카메라 frustum cull 제거. (controller bound 기반 필터) + // 캐릭터가 메인 카메라 frustum 밖이어도 ortho box / keyword 가 정상 동작해야 함. + // 실제 cullResults 우회는 ExecutePass의 manual DrawRenderer 가 담당. foreach (var targetChar in NiloToonAllInOneRendererFeature.characterList) { - // if target is not valid, skip it if (targetChar == null) continue; - if (!targetChar.isActiveAndEnabled) - continue; // character GameObject not enabled(not rendering) but in list - - // if character bounding sphere is completely not visible in game camera frustum, skip it - var boundRadius = targetChar.GetCharacterBoundRadius(); - var centerPosWS = targetChar.GetCharacterBoundCenter(); - // TODO: this section is not correct, which may incorrectly cull effective shadow caster that is OUTSIDE of main camera frustum - if (!GeometryUtility.TestPlanesAABB(cameraPlanes, - new Bounds(centerPosWS, Vector3.one * boundRadius))) - { - continue; - } - - // it is a valid visible char, add to list + if (!targetChar.isActiveAndEnabled) continue; validCharList.Add(targetChar); } @@ -1328,10 +1334,72 @@ namespace NiloToon.NiloToonURP cmd.SetGlobalVector("_NiloToonSelfShadowSoftShadowParam", data._NiloToonSelfShadowSoftShadowParam); cmd.SetKeyword(GlobalKeyword.Create(_NILOTOON_RECEIVE_SELF_SHADOW_Keyword), true); - - // Draw the objects in the list + + // Draw the objects in the list (uses main camera cullResults — character가 frustum 안일 때만) cmd.DrawRendererList(data.rendererListHandle); + + // Streamingle: cullResults 우회용 manual draw. + // 카메라가 캐릭터 frustum 밖이면 rendererListHandle에 캐릭터가 없어 shadow map이 비어 그림자 사라짐. + // 모든 NiloToon 캐릭터의 모든 NiloToonSelfShadowCaster pass를 수동으로 한 번 더 그려 cullResults 의존성 제거. + // 같은 renderer가 두 번 그려질 수 있으나 depth test가 처리하므로 시각적 영향 없음. + DrawNiloToonCharsManually(cmd); + } + + static void DrawNiloToonCharsManually(RasterCommandBuffer cmd) + { + var charList = NiloToonAllInOneRendererFeature.characterList; + if (charList == null) return; + for (int c = 0; c < charList.Count; c++) + { + var character = charList[c]; + if (character == null || !character.isActiveAndEnabled) continue; + var renderers = character.allRenderers; + if (renderers == null) continue; + for (int r = 0; r < renderers.Count; r++) + { + var renderer = renderers[r]; + if (renderer == null || !renderer.enabled || !renderer.gameObject.activeInHierarchy) continue; + var sharedMats = renderer.sharedMaterials; + if (sharedMats == null) continue; + for (int subIdx = 0; subIdx < sharedMats.Length; subIdx++) + { + var mat = sharedMats[subIdx]; + if (mat == null) continue; + int passIdx = GetShadowCasterPassIndex(mat.shader); + if (passIdx < 0) continue; + cmd.DrawRenderer(renderer, mat, subIdx, passIdx); + } + } + } } #endif + + static void DrawNiloToonCharsManuallyLegacy(CommandBuffer cmd) + { + var charList = NiloToonAllInOneRendererFeature.characterList; + if (charList == null) return; + for (int c = 0; c < charList.Count; c++) + { + var character = charList[c]; + if (character == null || !character.isActiveAndEnabled) continue; + var renderers = character.allRenderers; + if (renderers == null) continue; + for (int r = 0; r < renderers.Count; r++) + { + var renderer = renderers[r]; + if (renderer == null || !renderer.enabled || !renderer.gameObject.activeInHierarchy) continue; + var sharedMats = renderer.sharedMaterials; + if (sharedMats == null) continue; + for (int subIdx = 0; subIdx < sharedMats.Length; subIdx++) + { + var mat = sharedMats[subIdx]; + if (mat == null) continue; + int passIdx = GetShadowCasterPassIndex(mat.shader); + if (passIdx < 0) continue; + cmd.DrawRenderer(renderer, mat, subIdx, passIdx); + } + } + } + } } } \ No newline at end of file