Files
Pal3.Unity/Assets/Scripts/Pal3.Game/Effect/PostProcessing/PostProcessManager.cs
T
Jiaqi Liu 8ef2c946db Migrate PAL3/PAL3A rendering to URP Forward+ with independent Toon lighting (#90)
* Migrate original rendering and frosted UI to URP Forward+

Port the original materials, fog and screen passes to URP. Capture the finished scene for frosted UI and keep the renderer assets ready for clustered lighting.

* Add standalone Toon lighting and shared model rendering support

Add independent Toon shaders, scene and character lighting, contact shadows and fire controls. Share POL materials, preserve MV3 seams and texture swaps across both styles, and support scene-specific Toon water.

* Skip redundant CVD mesh updates

Reuse unchanged poses and UVs, and recompute Toon normals only when geometry or the linear transform changes.

* Manage game Bloom and screen effect state by camera

Expose a persisted Bloom switch, restore the active style default on reset, and release camera and volume state when the manager is disposed.

* Restore opt-in story VFX loading

Use the existing local VFX resource path for synchronous and asynchronous loading. Keep the preference off by default and document compatible optional assets.

* Save Unity 6.6 and URP project settings

Persist the current graphics, quality, player and shader graph settings plus the URP material upgrade marker.

* Fix Unity 6.6 startup compiler warnings

Use Unity assembly discovery and object lookup APIs, keep PAL3 command reflection independent of UnityEngine, and preserve the runtime GDB dictionaries in JSON exports.

* Use the new Input System as the only input backend

Select Input System Package as the sole backend. Remove the extra scroll-sensitivity reductions in the console and file browser, preserving their previous Both-mode scrolling behavior.

* Preserve two-sided model normals across animation

Recognize coincident MV3 vertices by their complete animation trajectories and skeletal vertices by their bind positions and bone. Separate opposing faces only where a source normal group is shared, preserving UV seams and adjacent single-sided faces.

* Isolate optional VFX lifetimes and private asset builds

Centralize optional prefab loading, persistent attachments, active-effect cleanup and cache invalidation. Runtime toggles hide active effects and restore surviving attachments. Block private VFX and legacy Toon assets in public builds, with an explicit local private-build override.

Release portal attachments on destruction, reject dead parents and prune orphaned persistent attachments.

* Manage camera post-processing, anti-aliasing and menu glass

Give each game camera its own VolumeStack without mutating shared profiles, and restore inherited camera and effect state on disposal. Map the old PPv2 Bloom intensity through its exponential compositing coefficient; this does not establish visual equivalence across pipelines. Share saved and live MSAA handling with ResourceViewer, and make the main menu acrylic background whiter with less scene color showing through.

Restore pipeline settings even after partial initialization or service cleanup failure. Protect the persistent URP asset from ordinary Play Mode saves and restore the two runtime fields across Play exit and script reloads without reverting unrelated edits.

* Tune Toon lighting, water and bounded point-light shadows

Strengthen indoor and outdoor lighting contrast, integrate SSAO and baked shading while retaining character detail, and light transparent water with the shared Toon shader. Expose independent switches and strengths through the console. Support up to four shadowed fires with bounded allocation-free selection, preserve saved preferences, adapt the shadow atlas, and default desktop devices to four shadowed fires.

Report invalid console strengths and shadow budgets without saving rejected values.

* Fix DXT3 alpha decoding that produces foliage stripes

Expand the high alpha nibble into both halves of the output byte. Fully opaque DXT3 texels now decode to 255 in both pixel positions, preventing alternating coverage in the cutout and blended rendering passes. Add editor regressions for opaque RGB/alpha, all sixteen alpha levels in pixel order, and partial blocks with buffer-tail protection.

* Document Unity CI after disabling the Codacy integration
2026-11-12 02:28:17 +08:00

225 lines
8.5 KiB
C#

// ---------------------------------------------------------------------------------------------
// Copyright (c) 2021-2025, Jiaqi (0x7c13) Liu. All rights reserved.
// See LICENSE file in the project root for license information.
// ---------------------------------------------------------------------------------------------
namespace Pal3.Game.Effect.PostProcessing
{
using System;
using Command;
using Command.Extensions;
using Core.Command;
using Core.Command.SceCommands;
using Core.Utilities;
using Rendering.ScreenEffects;
using Settings;
using UnityEngine;
using UnityEngine.Rendering;
using UnityEngine.Rendering.Universal;
public sealed class PostProcessManager : IDisposable,
ICommandExecutor<EffectSetScreenEffectCommand>,
ICommandExecutor<EffectShowSnowCommand>,
ICommandExecutor<ScenePostLoadingNotification>,
ICommandExecutor<SceneLeavingCurrentSceneNotification>,
ICommandExecutor<ResetGameStateCommand>,
ICommandExecutor<SettingChangedNotification>
{
private readonly Camera _camera;
private readonly UniversalAdditionalCameraData _cameraData;
private readonly GameSettings _settings;
private readonly VolumeFrameworkUpdateMode _originalVolumeUpdateMode;
private readonly VolumeStack _originalVolumeStack;
private readonly bool _originalPostProcessing;
private readonly bool _originalHdr;
private readonly Pal3ScreenEffects _screenEffects;
private readonly bool _originalScreenEffectsEnabled;
private readonly bool _originalDistortion;
private readonly bool _originalSnow;
private readonly Bloom _bloom;
private readonly ColorAdjustments _colorGrading;
private readonly Vignette _vignette;
private VolumeStack _volumeStack;
private bool _volumeDirty = true;
private bool _gradingEnabled;
private int _currentAppliedEffectMode = -1;
private bool _disposed;
public PostProcessManager(VolumeProfile profile, Camera camera, GameSettings settings)
{
_camera = camera;
_settings = settings;
_cameraData = camera.GetUniversalAdditionalCameraData();
_originalVolumeUpdateMode = camera.GetVolumeFrameworkUpdateMode();
_originalVolumeStack = _cameraData.volumeStack;
_originalPostProcessing = _cameraData.renderPostProcessing;
_originalHdr = camera.allowHDR;
_screenEffects = camera.TryGetComponent(out Pal3ScreenEffects existingEffects)
? existingEffects
: camera.gameObject.AddComponent<Pal3ScreenEffects>();
_originalScreenEffectsEnabled = existingEffects != null && existingEffects.enabled;
_originalDistortion = _screenEffects.Distortion;
_originalSnow = _screenEffects.Snow;
_screenEffects.enabled = true;
profile.TryGet(out _bloom);
profile.TryGet(out _colorGrading);
profile.TryGet(out _vignette);
UpdateBloom();
RenderPipelineManager.beginCameraRendering += RefreshVolumeStack;
CommandExecutorRegistry<ICommand>.Instance.Register(this);
}
private bool BloomEnabled => _bloom != null && _settings.BloomEnabled;
private void UpdateBloom()
{
_camera.allowHDR = BloomEnabled;
_volumeDirty = true;
UpdateCameraPostProcessing();
}
public void Execute(SettingChangedNotification command)
{
if (command.SettingName == nameof(GameSettings.BloomEnabled)) UpdateBloom();
}
private void UpdateCameraPostProcessing()
{
_cameraData.renderPostProcessing = BloomEnabled || _gradingEnabled;
}
private void RefreshVolumeStack(ScriptableRenderContext context, Camera camera)
{
if (camera != _camera || (!_volumeDirty && _volumeStack?.isValid == true)) return;
// Rebuild from URP defaults only when settings change. A private stack avoids
// blending another camera's Volume even when both cameras share a profile.
var manager = VolumeManager.instance;
var previous = _volumeStack;
_volumeStack = manager.CreateStack();
_cameraData.volumeStack = _volumeStack;
camera.SetVolumeFrameworkUpdateMode(VolumeFrameworkUpdateMode.ViaScripting);
manager.Update(_volumeStack, null, 0);
if (BloomEnabled) _bloom.Override(_volumeStack.GetComponent<Bloom>(), 1f);
if (_gradingEnabled)
{
_colorGrading?.Override(_volumeStack.GetComponent<ColorAdjustments>(), 1f);
_vignette?.Override(_volumeStack.GetComponent<Vignette>(), 1f);
}
DestroyVolumeStack(previous);
_volumeDirty = false;
}
private static void DestroyVolumeStack(VolumeStack stack)
{
if (stack?.isValid != true) return;
var manager = VolumeManager.instance;
if (manager.stack == stack) manager.ResetMainStack();
manager.DestroyStack(stack);
}
public void Dispose()
{
if (_disposed) return;
_disposed = true;
CommandExecutorRegistry<ICommand>.Instance.UnRegister(this);
RenderPipelineManager.beginCameraRendering -= RefreshVolumeStack;
if (_screenEffects != null)
{
_screenEffects.Distortion = _originalDistortion;
_screenEffects.Snow = _originalSnow;
_screenEffects.enabled = _originalScreenEffectsEnabled;
}
DestroyVolumeStack(_volumeStack);
if (_cameraData != null)
{
if (_volumeStack != null && _cameraData.volumeStack == _volumeStack)
{
_cameraData.volumeStack = _originalVolumeStack?.isValid == true ? _originalVolumeStack : null;
_camera.SetVolumeFrameworkUpdateMode(_originalVolumeUpdateMode);
}
_cameraData.renderPostProcessing = _originalPostProcessing;
}
if (_camera != null) _camera.allowHDR = _originalHdr;
}
public int GetCurrentAppliedEffectMode()
{
return _currentAppliedEffectMode;
}
public void Execute(EffectSetScreenEffectCommand command)
{
switch (command.Mode)
{
// Disable all post-processing effects used by the game
case -1:
{
SetColorGrading(false);
_screenEffects.Distortion = false;
break;
}
// Distortion effect
case 0:
{
_screenEffects.Distortion = true;
break;
}
// Vintage + color filter effect
case 1:
{
SetColorGrading(true);
break;
}
}
_currentAppliedEffectMode = command.Mode;
UpdateCameraPostProcessing();
}
private void SetColorGrading(bool enabled)
{
if (_gradingEnabled == enabled) return;
_gradingEnabled = enabled;
_volumeDirty = true;
}
public void Execute(ResetGameStateCommand command)
{
Execute(new EffectSetScreenEffectCommand(-1));
}
public void Execute(EffectShowSnowCommand command)
{
// Ignoring this command since now we control the snow effect based
// on pre-configured scene list
}
public void Execute(ScenePostLoadingNotification command)
{
#if PAL3
if (command.NewSceneInfo.Is("q15", "q15") ||
command.NewSceneInfo.IsCity("m22"))
{
_screenEffects.Snow = true;
}
#elif PAL3A
if (command.NewSceneInfo.Is("q02", "hs") ||
command.NewSceneInfo.Is("q02", "qs") ||
command.NewSceneInfo.Is("q02", "xs") ||
command.NewSceneInfo.Is("q02", "zs"))
{
_screenEffects.Snow = true;
}
#endif
}
public void Execute(SceneLeavingCurrentSceneNotification command)
{
_screenEffects.Snow = false;
}
}
}