Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

HDRP下 FGUI的单相机渲染支持 #223

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 32 additions & 0 deletions Assets/Scripts/Core/StageCamera.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,9 @@ public class StageCamera : MonoBehaviour
bool isMain;
[NonSerialized]
Display _display;
#if HDRP
FGUIHDRPRenderPass hdrpPass;
#endif

/// <summary>
///
Expand Down Expand Up @@ -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<UnityEngine.Rendering.HighDefinition.CustomPassVolume>();
if (!volume)
{
volume = this.gameObject.AddComponent<UnityEngine.Rendering.HighDefinition.CustomPassVolume>();
volume.injectionPoint = UnityEngine.Rendering.HighDefinition.CustomPassInjectionPoint.AfterPostProcess;
hdrpPass = (FGUIHDRPRenderPass)volume.AddPassOfType<FGUIHDRPRenderPass>();
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
Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -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<UnityEngine.Rendering.HighDefinition.CustomPassVolume>();
volume.injectionPoint = UnityEngine.Rendering.HighDefinition.CustomPassInjectionPoint.AfterPostProcess;
var pass = (FGUIHDRPRenderPass)volume.AddPassOfType<FGUIHDRPRenderPass>();
pass.name = nameof(FGUIHDRPRenderPass);
pass.layer = cullingMask;
#endif
cameraObject.AddComponent<StageCamera>();
return camera;
}
Expand Down
12 changes: 10 additions & 2 deletions Assets/Scripts/FairyGUI.asmdef
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@
"name": "FairyGUI",
"rootNamespace": "",
"references": [
"Unity.TextMeshPro"
"Unity.TextMeshPro",
"Unity.RenderPipelines.HighDefinition.Runtime",
"Unity.RenderPipelines.Core.Runtime"
],
"includePlatforms": [],
"excludePlatforms": [],
Expand All @@ -11,6 +13,12 @@
"precompiledReferences": [],
"autoReferenced": true,
"defineConstraints": [],
"versionDefines": [],
"versionDefines": [
{
"name": "com.unity.render-pipelines.high-definition",
"expression": "",
"define": "HDRP"
}
],
"noEngineReferences": false
}
63 changes: 63 additions & 0 deletions Assets/Scripts/Utils/FGUIHDRPRenderPass.cs
Original file line number Diff line number Diff line change
@@ -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
2 changes: 2 additions & 0 deletions Assets/Scripts/Utils/FGUIHDRPRenderPass.cs.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.