From f575b3e9f05647283d84b2682a153634c4e55590 Mon Sep 17 00:00:00 2001 From: huangjunxiang Date: Wed, 8 May 2024 11:19:03 +0800 Subject: [PATCH] =?UTF-8?q?HDRP=E4=B8=8B=20FGUI=E7=9A=84=E5=8D=95=E7=9B=B8?= =?UTF-8?q?=E6=9C=BA=E6=B8=B2=E6=9F=93=E6=94=AF=E6=8C=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Assets/Scripts/Core/StageCamera.cs | 32 ++++++++++ Assets/Scripts/FairyGUI.asmdef | 12 +++- Assets/Scripts/Utils/FGUIHDRPRenderPass.cs | 63 +++++++++++++++++++ .../Scripts/Utils/FGUIHDRPRenderPass.cs.meta | 2 + 4 files changed, 107 insertions(+), 2 deletions(-) create mode 100644 Assets/Scripts/Utils/FGUIHDRPRenderPass.cs create mode 100644 Assets/Scripts/Utils/FGUIHDRPRenderPass.cs.meta diff --git a/Assets/Scripts/Core/StageCamera.cs b/Assets/Scripts/Core/StageCamera.cs index b59ffd30..0b00fdad 100644 --- a/Assets/Scripts/Core/StageCamera.cs +++ b/Assets/Scripts/Core/StageCamera.cs @@ -34,6 +34,9 @@ public class StageCamera : MonoBehaviour bool isMain; [NonSerialized] Display _display; +#if HDRP + FGUIHDRPRenderPass hdrpPass; +#endif /// /// @@ -74,6 +77,20 @@ void OnEnable() if (Display.displays.Length > 1 && cachedCamera.targetDisplay != 0 && cachedCamera.targetDisplay < Display.displays.Length) _display = Display.displays[cachedCamera.targetDisplay]; +#if HDRP + var volume = this.gameObject.GetComponent(); + if (!volume) + { + volume = this.gameObject.AddComponent(); + volume.injectionPoint = UnityEngine.Rendering.HighDefinition.CustomPassInjectionPoint.AfterPostProcess; + hdrpPass = (FGUIHDRPRenderPass)volume.AddPassOfType(); + hdrpPass.name = nameof(FGUIHDRPRenderPass); + hdrpPass.layer = 1 << LayerMask.NameToLayer(LayerName); + } + else + hdrpPass = volume.customPasses.Find(t => t is FGUIHDRPRenderPass) as FGUIHDRPRenderPass; +#endif + if (_display == null) OnScreenSizeChanged(Screen.width, Screen.height); else @@ -132,6 +149,14 @@ void OnScreenSizeChanged(int newWidth, int newHeight) UIContentScaler.scaleFactor = 1; } } +#if HDRP + if (hdrpPass != null) + hdrpPass.ApplyChange(this.transform); + //涉及到摄像机修改的太多 这里就只enable=false + //ui点击 如果屏幕size发生了变化 由于使用了缓存的ui摄像机 所以会有问题 + if (Application.isPlaying) + cachedCamera.enabled = false; +#endif } void OnRenderObject() @@ -194,6 +219,13 @@ public static Camera CreateCamera(string name, int cullingMask) camera.stereoTargetEye = StereoTargetEyeMask.None; camera.allowHDR = false; camera.allowMSAA = false; +#if HDRP + var volume = cameraObject.AddComponent(); + volume.injectionPoint = UnityEngine.Rendering.HighDefinition.CustomPassInjectionPoint.AfterPostProcess; + var pass = (FGUIHDRPRenderPass)volume.AddPassOfType(); + pass.name = nameof(FGUIHDRPRenderPass); + pass.layer = cullingMask; +#endif cameraObject.AddComponent(); return camera; } diff --git a/Assets/Scripts/FairyGUI.asmdef b/Assets/Scripts/FairyGUI.asmdef index a2a73d76..f8e25516 100644 --- a/Assets/Scripts/FairyGUI.asmdef +++ b/Assets/Scripts/FairyGUI.asmdef @@ -2,7 +2,9 @@ "name": "FairyGUI", "rootNamespace": "", "references": [ - "Unity.TextMeshPro" + "Unity.TextMeshPro", + "Unity.RenderPipelines.HighDefinition.Runtime", + "Unity.RenderPipelines.Core.Runtime" ], "includePlatforms": [], "excludePlatforms": [], @@ -11,6 +13,12 @@ "precompiledReferences": [], "autoReferenced": true, "defineConstraints": [], - "versionDefines": [], + "versionDefines": [ + { + "name": "com.unity.render-pipelines.high-definition", + "expression": "", + "define": "HDRP" + } + ], "noEngineReferences": false } \ No newline at end of file diff --git a/Assets/Scripts/Utils/FGUIHDRPRenderPass.cs b/Assets/Scripts/Utils/FGUIHDRPRenderPass.cs new file mode 100644 index 00000000..ce4b56d1 --- /dev/null +++ b/Assets/Scripts/Utils/FGUIHDRPRenderPass.cs @@ -0,0 +1,63 @@ +using UnityEngine.Rendering; +using UnityEngine; +#if HDRP +using UnityEngine.Rendering.HighDefinition; +#endif + +#if HDRP +class FGUIHDRPRenderPass : CustomPass +{ + public LayerMask layer; + + Matrix4x4 worldToCameraMatrix; + Matrix4x4 projectionMatrix; + Matrix4x4 cullingMatrix; + Plane[] planes = new Plane[6]; + + public void ApplyChange(Transform stage) + { + var v3 = stage.position; + worldToCameraMatrix = Matrix4x4.TRS(-stage.position, Quaternion.identity, new Vector3(1, 1, -1)); + projectionMatrix = Matrix4x4.TRS(Vector3.zero, Quaternion.identity, new Vector3(2 / (v3.x * 2 - 0), 2 / -(v3.y * 2 - 0), 0)); + cullingMatrix = projectionMatrix * worldToCameraMatrix; + GeometryUtility.CalculateFrustumPlanes(cullingMatrix, planes); + } + protected override void Setup(ScriptableRenderContext renderContext, CommandBuffer cmd) + { + + } + protected override void AggregateCullingParameters(ref ScriptableCullingParameters cullingParameters, HDCamera hdCamera) + { + if (hdCamera.camera.cameraType == CameraType.Game) + { + cullingParameters.cullingMask |= (uint)layer.value; + cullingParameters.origin = new Vector3(0, 0, -100); + cullingParameters.cullingMatrix = cullingMatrix; + + for (int i = 0; i < 6; i++) + cullingParameters.SetCullingPlane(i, planes[i]); + } + } + protected override void Execute(CustomPassContext ctx) + { + if (ctx.hdCamera.camera.cameraType == CameraType.Game) + { + var pm = ctx.hdCamera.camera.projectionMatrix; + var vm = ctx.hdCamera.camera.worldToCameraMatrix; + + ctx.cmd.SetViewProjectionMatrices(worldToCameraMatrix, projectionMatrix); + + CustomPassUtils.DrawRenderers(ctx, layer); + CoreUtils.SetRenderTarget(ctx.cmd, ctx.cameraColorBuffer, ClearFlag.None); + + //还原矩阵 + ctx.cmd.SetViewProjectionMatrices(vm, pm); + } + } + + protected override void Cleanup() + { + + } +} +#endif \ No newline at end of file diff --git a/Assets/Scripts/Utils/FGUIHDRPRenderPass.cs.meta b/Assets/Scripts/Utils/FGUIHDRPRenderPass.cs.meta new file mode 100644 index 00000000..c1220a26 --- /dev/null +++ b/Assets/Scripts/Utils/FGUIHDRPRenderPass.cs.meta @@ -0,0 +1,2 @@ +fileFormatVersion: 2 +guid: 9bb290c156b2e454d9da692396cc873d \ No newline at end of file