升级水插件

This commit is contained in:
2026-01-08 22:30:55 +08:00
parent febff82d24
commit ca68084264
415 changed files with 18138 additions and 7134 deletions

View File

@@ -21,6 +21,18 @@ namespace WaveHarmonic.Crest
/// </summary>
static partial class Helpers
{
// Adapted from:
// Packages/com.unity.render-pipelines.universal/Runtime/UniversalRenderer.cs
#if UNITY_SWITCH || UNITY_ANDROID || UNITY_EMBEDDED_LINUX || UNITY_QNX
internal const GraphicsFormat k_DepthStencilFormat = GraphicsFormat.D24_UNorm_S8_UInt;
internal const int k_DepthBufferBits = 24;
internal const DepthBits k_DepthBits = DepthBits.Depth24;
#else
internal const GraphicsFormat k_DepthStencilFormat = GraphicsFormat.D32_SFloat_S8_UInt;
internal const int k_DepthBufferBits = 32;
internal const DepthBits k_DepthBits = DepthBits.Depth32;
#endif
public static class ShaderIDs
{
public static readonly int s_MainTexture = Shader.PropertyToID("_Utility_MainTexture");
@@ -128,31 +140,6 @@ namespace WaveHarmonic.Crest
public static WaitForEndOfFrame WaitForEndOfFrame { get; } = new();
static Material s_UtilityMaterial;
public static Material UtilityMaterial
{
get
{
if (s_UtilityMaterial == null)
{
s_UtilityMaterial = new(Shader.Find("Hidden/Crest/Utility/Blit"));
}
return s_UtilityMaterial;
}
}
// Need to cast to int but no conversion cost.
// https://stackoverflow.com/a/69148528
internal enum UtilityPass
{
CopyColor,
CopyDepth,
ClearDepth,
ClearStencil,
Copy,
}
// Taken from:
// https://github.com/Unity-Technologies/Graphics/blob/871df5563d88e1ba778c82a43f39c9afc95368e6/Packages/com.unity.render-pipelines.core/ShaderLibrary/Common.hlsl#L1149-L1152
// Z buffer to linear 0..1 depth (0 at camera position, 1 at far plane).
@@ -220,15 +207,22 @@ namespace WaveHarmonic.Crest
}
/// <summary>
/// Uses PrefabUtility.InstantiatePrefab in editor and GameObject.Instantiate in standalone.
/// Uses PrefabUtility.InstantiatePrefab in edit mode, otherwise uses GameObject.Instantiate.
/// </summary>
public static GameObject InstantiatePrefab(GameObject prefab)
{
#if UNITY_EDITOR
return (GameObject)UnityEditor.PrefabUtility.InstantiatePrefab(prefab);
#else
return GameObject.Instantiate(prefab);
if (!Application.isPlaying)
{
// Previously we always used this in the editor, including play mode. But it was
// reported to have failed (null return) when Asset Bundles were used in play mode.
return (GameObject)UnityEditor.PrefabUtility.InstantiatePrefab(prefab);
}
else
#endif
{
return Object.Instantiate(prefab);
}
}
// Taken from Unity
@@ -343,7 +337,7 @@ namespace WaveHarmonic.Crest
if (RenderPipelineHelper.IsUniversal)
{
// MSAA will be the same for every camera if XR rendering.
isMSAA = isMSAA || XRHelpers.IsRunning;
isMSAA = isMSAA || Rendering.EnabledXR;
}
#endif
@@ -588,7 +582,7 @@ namespace WaveHarmonic.Crest
/// <summary>
/// Uses Destroy in play mode or DestroyImmediate in edit mode.
/// </summary>
public static void Destroy(Object @object)
public static void Destroy(Object @object, bool undo = false)
{
#if UNITY_EDITOR
// We must use DestroyImmediate in edit mode. As it apparently has an overhead, use recommended Destroy in
@@ -596,7 +590,14 @@ namespace WaveHarmonic.Crest
// https://docs.unity3d.com/ScriptReference/Object.DestroyImmediate.html
if (!Application.isPlaying)
{
Object.DestroyImmediate(@object);
if (undo)
{
UnityEditor.Undo.DestroyObjectImmediate(@object);
}
else
{
Object.DestroyImmediate(@object);
}
}
else
#endif
@@ -605,11 +606,13 @@ namespace WaveHarmonic.Crest
}
}
static readonly Matrix4x4 s_ScaleMatrix = Matrix4x4.Scale(new(1f, 1f, -1f));
// Borrowed from SRP code:
// https://github.com/Unity-Technologies/Graphics/blob/7d292932bec3b4257a4defaf698fc7d77e2027f5/com.unity.render-pipelines.high-definition/Runtime/Core/Utilities/GeometryUtils.cs#L181-L184
public static Matrix4x4 CalculateWorldToCameraMatrixRHS(Vector3 position, Quaternion rotation)
{
return Matrix4x4.Scale(new(1, 1, -1)) * Matrix4x4.TRS(position, rotation, Vector3.one).inverse;
return s_ScaleMatrix * Matrix4x4.TRS(position, rotation, Vector3.one).inverse;
}
/// <summary>
@@ -618,14 +621,7 @@ namespace WaveHarmonic.Crest
/// </summary>
public static void Blit(CommandBuffer buffer, RenderTargetIdentifier target, Material material, int pass = -1, MaterialPropertyBlock properties = null)
{
if (!RenderPipelineHelper.IsLegacy)
{
CoreUtils.SetRenderTarget(buffer, target);
}
else
{
buffer.SetRenderTarget(target);
}
CoreUtils.SetRenderTarget(buffer, target);
buffer.DrawProcedural
(
@@ -795,7 +791,7 @@ namespace WaveHarmonic.Crest
static readonly UnityEngine.Rendering.RenderPipeline.StandardRequest s_RenderStandardRequest = new();
public static void RenderCamera(Camera camera, ScriptableRenderContext context)
public static void RenderCamera(Camera camera, ScriptableRenderContext context, int slice)
{
#if d_UnityURP
if (RenderPipelineHelper.IsUniversal)
@@ -804,7 +800,9 @@ namespace WaveHarmonic.Crest
// SingleCameraRequest does not render the full camera stack, thus should exclude
// overlays which is likely desirable. Alternative approach worth investigating:
// https://docs.unity3d.com/6000.0/Documentation/Manual/urp/User-Render-Requests.html
// TODO: pass destination texture and slice instead of copying later.
// Setting destination silences a warning if Opaque Texture enabled.
s_RenderSingleCameraRequest.destination = camera.targetTexture;
s_RenderSingleCameraRequest.slice = slice;
UnityEngine.Rendering.RenderPipeline.SubmitRenderRequest(camera, s_RenderSingleCameraRequest);
#else
#pragma warning disable CS0618 // Type or member is obsolete
@@ -828,7 +826,44 @@ namespace WaveHarmonic.Crest
}
// Undo
static partial class Helpers
{
public static class Undo
{
static class Symbols
{
public const string k_UnityEditor = "UNITY_EDITOR";
}
[System.Diagnostics.Conditional(Symbols.k_UnityEditor)]
public static void RecordObject(Object @object, string label)
{
#if UNITY_EDITOR
UnityEditor.Undo.RecordObject(@object, label);
#endif
}
[System.Diagnostics.Conditional(Symbols.k_UnityEditor)]
public static void SetSiblingIndex(Transform transform, int index, string label)
{
#if UNITY_EDITOR
UnityEditor.Undo.SetSiblingIndex(transform, index, label);
#endif
}
[System.Diagnostics.Conditional(Symbols.k_UnityEditor)]
public static void RegisterCreatedObjectUndo(Object @object, string label)
{
#if UNITY_EDITOR
UnityEditor.Undo.RegisterCreatedObjectUndo(@object, label);
#endif
}
}
}
// Terrain
#if d_Unity_Terrain
static partial class Helpers
{
static readonly List<Terrain> s_Terrains = new();
@@ -838,6 +873,11 @@ namespace WaveHarmonic.Crest
foreach (var terrain in s_Terrains)
{
if (terrain.terrainData == null)
{
continue;
}
var rect = new Rect(terrain.transform.position.XZ(), terrain.terrainData.size.XZ());
// Return the first one.
@@ -850,6 +890,7 @@ namespace WaveHarmonic.Crest
return null;
}
}
#endif // d_Unity_Terrain
namespace Internal
{
@@ -869,6 +910,7 @@ namespace WaveHarmonic.Crest
public static Vector4 XYNN(this Vector2 v, float n = 0f) => new(v.x, v.y, n, n);
public static Vector4 XYNN(this Vector2 v, Vector2 n) => new(v.x, v.y, n.x, n.y);
public static Vector4 NNZW(this Vector2 v, float n = 0f) => new(n, n, v.x, v.y);
public static Vector4 XNZW(this Vector4 v, float n) => new(v.x, n, v.z, v.w);
public static float Maximum(this Vector3 v) => Mathf.Max(Mathf.Max(v.x, v.y), v.z);
public static Vector2 Absolute(this Vector2 v) => new
@@ -961,6 +1003,12 @@ namespace WaveHarmonic.Crest
return GeometryUtility.CalculateBounds(s_BoundsPoints, transform.localToWorldMatrix);
}
public static bool IntersectsXZ(this Bounds a, Bounds b)
{
return a.min.x <= b.max.x && a.max.x >= b.min.x &&
a.min.z <= b.max.z && a.max.z >= b.min.z;
}
public static Rect RectXZ(this Bounds bounds)
{
return Rect.MinMaxRect(bounds.min.x, bounds.min.z, bounds.max.x, bounds.max.z);