升级水插件

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

@@ -0,0 +1,111 @@
// Crest Water System
// Copyright © 2024 Wave Harmonic. All rights reserved.
using UnityEngine;
using UnityEngine.Rendering;
namespace WaveHarmonic.Crest
{
interface ICommandWrapper : IPropertyWrapper
{
void SetInvertCulling(bool invert);
void DrawFullScreenTriangle(Material material, int pass, MaterialPropertyBlock block = null);
void DrawMesh(Mesh mesh, Matrix4x4 matrix, Material material, int pass = -1, MaterialPropertyBlock block = null);
}
readonly struct CommandWrapper : ICommandWrapper
{
public CommandBuffer Commands { get; }
public CommandWrapper(CommandBuffer commands) => Commands = commands;
public void SetFloat(int param, float value) => Commands.SetGlobalFloat(param, value);
public void SetFloatArray(int param, float[] value) => Commands.SetGlobalFloatArray(param, value);
public void SetTexture(int param, Texture value) => Commands.SetGlobalTexture(param, value);
public void SetVector(int param, Vector4 value) => Commands.SetGlobalVector(param, value);
public void SetVectorArray(int param, Vector4[] value) => Commands.SetGlobalVectorArray(param, value);
public void SetMatrix(int param, Matrix4x4 value) => Commands.SetGlobalMatrix(param, value);
public void SetInteger(int param, int value) => Commands.SetGlobalInteger(param, value);
public void SetBoolean(int param, bool value) => Commands.SetGlobalInteger(param, value ? 1 : 0);
public void GetBlock() { }
public void SetBlock() { }
public void SetInvertCulling(bool invert) => Commands.SetInvertCulling(invert);
public void DrawFullScreenTriangle(Material material, int pass = -1, MaterialPropertyBlock block = null)
{
Commands.DrawProcedural
(
Matrix4x4.identity,
material,
pass,
MeshTopology.Triangles,
vertexCount: 3,
instanceCount: 1,
block
);
}
public void DrawMesh(Mesh mesh, Matrix4x4 matrix, Material material, int pass = -1, MaterialPropertyBlock block = null)
{
Commands.DrawMesh
(
mesh,
matrix,
material,
submeshIndex: 0,
pass,
block
);
}
}
#if UNITY_6000_0_OR_NEWER
readonly struct RasterCommandWrapper : ICommandWrapper
{
public RasterCommandBuffer Commands { get; }
public RasterCommandWrapper(RasterCommandBuffer commands) => Commands = commands;
public void SetFloat(int param, float value) => Commands.SetGlobalFloat(param, value);
public void SetFloatArray(int param, float[] value) => Commands.SetGlobalFloatArray(param, value);
// WARNING: bypasses RG checks. Only use for textures external to RG.
public void SetTexture(int param, Texture value) => Commands.m_WrappedCommandBuffer.SetGlobalTexture(param, value);
public void SetVector(int param, Vector4 value) => Commands.SetGlobalVector(param, value);
public void SetVectorArray(int param, Vector4[] value) => Commands.SetGlobalVectorArray(param, value);
public void SetMatrix(int param, Matrix4x4 value) => Commands.SetGlobalMatrix(param, value);
public void SetInteger(int param, int value) => Commands.SetGlobalInteger(param, value);
public void SetBoolean(int param, bool value) => Commands.SetGlobalInteger(param, value ? 1 : 0);
public void GetBlock() { }
public void SetBlock() { }
public void SetInvertCulling(bool invert) => Commands.SetInvertCulling(invert);
public void DrawFullScreenTriangle(Material material, int pass, MaterialPropertyBlock block = null)
{
Commands.DrawProcedural
(
Matrix4x4.identity,
material,
pass,
MeshTopology.Triangles,
vertexCount: 3,
instanceCount: 1,
block
);
}
public void DrawMesh(Mesh mesh, Matrix4x4 matrix, Material material, int pass = -1, MaterialPropertyBlock block = null)
{
Commands.DrawMesh
(
mesh,
matrix,
material,
submeshIndex: 0,
pass,
block
);
}
}
#endif // UNITY_6000_0_OR_NEWER
}

View File

@@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: 9a5766c3523cc4af586139878192e12d
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@@ -9,6 +9,12 @@ using UnityEngine.Rendering.HighDefinition;
namespace WaveHarmonic.Crest
{
class CustomPass : UnityEngine.Rendering.HighDefinition.CustomPass
{
internal GameObject _GameObject;
internal CustomPassVolume _Volume;
}
static class CustomPassHelpers
{
internal static List<CustomPassVolume> s_Volumes = new();
@@ -62,7 +68,8 @@ namespace WaveHarmonic.Crest
GameObject gameObject,
ref T pass,
string name,
CustomPassInjectionPoint injectionPoint
CustomPassInjectionPoint injectionPoint,
int priority = 0
)
where T : CustomPass, new()
{
@@ -85,14 +92,15 @@ namespace WaveHarmonic.Crest
volume = gameObject.AddComponent<CustomPassVolume>();
volume.injectionPoint = injectionPoint;
volume.isGlobal = true;
volume.priority = priority;
}
// Create custom pass.
pass ??= new()
{
name = $"Crest {name}",
targetColorBuffer = CustomPass.TargetBuffer.None,
targetDepthBuffer = CustomPass.TargetBuffer.None,
name = name,
targetColorBuffer = UnityEngine.Rendering.HighDefinition.CustomPass.TargetBuffer.None,
targetDepthBuffer = UnityEngine.Rendering.HighDefinition.CustomPass.TargetBuffer.None,
};
// Add custom pass.
@@ -100,6 +108,27 @@ namespace WaveHarmonic.Crest
{
volume.customPasses.Add(pass);
}
pass._GameObject = gameObject;
pass._Volume = volume;
}
public static void Update<T>(GameObject go, T pass, CustomPassInjectionPoint point, int priority = 0) where T : CustomPass
{
CustomPassVolume volume = null;
go.GetComponents(s_Volumes);
foreach (var v in s_Volumes)
{
if (v.customPasses.Contains(pass))
{
volume = v;
break;
}
}
volume.injectionPoint = point;
volume.priority = priority;
}
}
}

View File

@@ -0,0 +1,52 @@
// Crest Water System
// Copyright © 2024 Wave Harmonic. All rights reserved.
// Adapted from:
// https://github.com/keijiro/LightProbeUtility/blob/85c93577338e10a52dd53f263056de08d883337a/Assets/LightProbeUtility.cs
// With fixes from:
// https://github.com/keijiro/LightProbeUtility/pull/2
using UnityEngine;
namespace WaveHarmonic.Crest
{
static class LightProbeUtility
{
static readonly int[] s_SHA =
{
Shader.PropertyToID("unity_SHAr"),
Shader.PropertyToID("unity_SHAg"),
Shader.PropertyToID("unity_SHAb")
};
static readonly int[] s_SHB =
{
Shader.PropertyToID("unity_SHBr"),
Shader.PropertyToID("unity_SHBg"),
Shader.PropertyToID("unity_SHBb")
};
static readonly int s_SHC = Shader.PropertyToID("unity_SHC");
public static void SetSHCoefficients<T>(this T properties, Vector3 position) where T : IPropertyWrapper
{
LightProbes.GetInterpolatedProbe(position, null, out var sh);
// Constant + Linear.
for (var i = 0; i < 3; i++)
{
properties.SetVector(s_SHA[i], new(sh[i, 3], sh[i, 1], sh[i, 2], sh[i, 0] - sh[i, 6]));
}
// Quadratic polynomials.
for (var i = 0; i < 3; i++)
{
properties.SetVector(s_SHB[i], new(sh[i, 4], sh[i, 5], sh[i, 6] * 3, sh[i, 7]));
}
// Final quadratic polynomial.
properties.SetVector(s_SHC, new(sh[0, 8], sh[1, 8], sh[2, 8], 1));
}
}
}

View File

@@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: cf4aa5298ac5b4562a8c1f0466045eff
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@@ -1,4 +1,4 @@
// Crest Water System
// Crest Water System
// Copyright © 2024 Wave Harmonic. All rights reserved.
using UnityEngine;

View File

@@ -0,0 +1,37 @@
// Crest Water System
// Copyright © 2024 Wave Harmonic. All rights reserved.
// RTHandles for Built-In Render Pipeline.
// We cannot call dispose ourselves, but it does not seem to be a problem.
using UnityEngine;
namespace WaveHarmonic.Crest.Utility
{
static class RTHandles
{
public static void Initialize()
{
if (!RenderPipelineHelper.IsLegacy)
{
return;
}
// Check whether already initialized.
if (UnityEngine.Rendering.RTHandles.maxWidth > 1)
{
return;
}
UnityEngine.Rendering.RTHandles.Initialize(Screen.width, Screen.height);
UnityEngine.Rendering.RTHandles.SetHardwareDynamicResolutionState(false);
}
public static void OnBeginCameraRendering(Camera camera)
{
// Forget Dynamic Scaling, as is broken for Shader Graph and Post-Processing anyway.
// The only foreseeable problem is if a third party calls this with a different size.
UnityEngine.Rendering.RTHandles.SetReferenceSize(camera.pixelWidth, camera.pixelHeight);
}
}
}

View File

@@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: fed5f2fe3fcaa450f9c621080f7989ec
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@@ -34,18 +34,48 @@ namespace WaveHarmonic.Crest
public static implicit operator TextureHandle(Handle texture) => texture._TextureHandle;
}
static readonly FieldInfo s_RenderContext = typeof(InternalRenderGraphContext).GetField("renderContext", BindingFlags.NonPublic | BindingFlags.Instance);
static readonly FieldInfo s_WrappedContext = typeof(UnsafeGraphContext).GetField("wrappedContext", BindingFlags.NonPublic | BindingFlags.Instance);
static readonly FieldInfo s_FrameData = typeof(RenderingData).GetField("frameData", BindingFlags.NonPublic | BindingFlags.Instance);
public static ScriptableRenderContext GetRenderContext(this UnsafeGraphContext unsafeContext)
{
return (ScriptableRenderContext)s_RenderContext.GetValue((InternalRenderGraphContext)s_WrappedContext.GetValue(unsafeContext));
return ((InternalRenderGraphContext)s_WrappedContext.GetValue(unsafeContext)).renderContext;
}
public static ContextContainer GetFrameData(this ref RenderingData renderingData)
{
return (ContextContainer)s_FrameData.GetValue(renderingData);
return renderingData.frameData;
}
internal class PassData
{
#pragma warning disable IDE1006 // Naming Styles
public UniversalCameraData cameraData;
public UniversalRenderingData renderingData;
public Handle colorTargetHandle;
public Handle depthTargetHandle;
#pragma warning restore IDE1006 // Naming Styles
public void Init(ContextContainer frameData, IUnsafeRenderGraphBuilder builder = null)
{
var resources = frameData.Get<UniversalResourceData>();
cameraData = frameData.Get<UniversalCameraData>();
renderingData = frameData.Get<UniversalRenderingData>();
if (builder == null)
{
#pragma warning disable CS0618 // Type or member is obsolete
colorTargetHandle = cameraData.renderer.cameraColorTargetHandle;
depthTargetHandle = cameraData.renderer.cameraDepthTargetHandle;
#pragma warning restore CS0618 // Type or member is obsolete
}
else
{
colorTargetHandle = resources.activeColorTexture;
depthTargetHandle = resources.activeDepthTexture;
builder.UseTexture(colorTargetHandle, AccessFlags.ReadWrite);
builder.UseTexture(depthTargetHandle, AccessFlags.ReadWrite);
}
}
}
}
}

View File

@@ -0,0 +1,260 @@
// Crest Water System
// Copyright © 2024 Wave Harmonic. All rights reserved.
// ENABLE_VR is defined if the platform supports XR.
// d_UnityModuleVR is defined if the VR module is installed.
// VR module depends on XR module (which does nothing by itself) so we only need to check the VR module.
#if ENABLE_VR && d_UnityModuleVR
#define _XR_ENABLED
#endif
using System.Collections.Generic;
using System.Diagnostics;
using UnityEngine;
using UnityEngine.Rendering;
using UnityEngine.Rendering.HighDefinition;
using UnityEngine.Rendering.Universal;
using UnityEngine.XR;
namespace WaveHarmonic.Crest
{
static partial class Rendering
{
// Adaptor layer for XR module similar to Unity's XRGraphics/XRSRPSettings.
// We cannot use theirs as they keep on renaming it…
static readonly GlobalKeyword s_SinglePassInstancedKeyword = new("STEREO_INSTANCING_ON");
static readonly GlobalKeyword s_SinglePassMultiViewKeyword = new("STEREO_MULTIVIEW_ON");
#if _XR_ENABLED
internal static GlobalKeyword SinglePassKeyword => XRSettings.stereoRenderingMode switch
{
XRSettings.StereoRenderingMode.SinglePassInstanced => s_SinglePassInstancedKeyword,
XRSettings.StereoRenderingMode.SinglePassMultiview => s_SinglePassMultiViewKeyword,
_ => throw new System.NotImplementedException(),
};
#endif
public static bool EnabledXR
{
get
{
#if _XR_ENABLED
return XRSettings.enabled;
#else
return false;
#endif
}
}
static bool SinglePassXR
{
get
{
#if _XR_ENABLED
return XRSettings.enabled && (XRSettings.stereoRenderingMode is XRSettings.StereoRenderingMode.SinglePassInstanced or XRSettings.StereoRenderingMode.SinglePassMultiview);
#else
return false;
#endif
}
}
static bool MultiPassXR
{
get
{
#if _XR_ENABLED
return XRSettings.enabled && XRSettings.stereoRenderingMode is XRSettings.StereoRenderingMode.MultiPass;
#else
return false;
#endif
}
}
public static partial class BIRP
{
[Conditional("_XR_ENABLED")]
public static void EnableXR(CommandBuffer commands, Camera camera)
{
#if _XR_ENABLED
if (!SinglePassXR || !camera.stereoEnabled)
{
return;
}
commands.EnableKeyword(SinglePassKeyword);
#endif
}
[Conditional("_XR_ENABLED")]
public static void DisableXR(CommandBuffer commands, Camera camera)
{
#if _XR_ENABLED
if (!SinglePassXR || !camera.stereoEnabled)
{
return;
}
commands.DisableKeyword(SinglePassKeyword);
#endif
}
}
//
// Stereo Rendering
//
#if _XR_ENABLED
public static partial class BIRP
{
// NOTE: This is the same value as Unity, but in the future it could be higher.
const int k_MaximumViewsXR = 2;
static partial class ShaderIDs
{
public static readonly int s_StereoInverseViewProjection = Shader.PropertyToID("_Crest_StereoInverseViewProjection");
}
static readonly List<XRDisplaySubsystem> s_DisplayListXR = new();
// Unity only supports one display right now.
static XRDisplaySubsystem DisplayXR => XRSettings.enabled ? s_DisplayListXR[0] : null;
static Matrix4x4[] InverseViewProjectionMatrixXR { get; set; } = new Matrix4x4[2];
static Texture2DArray s_WhiteTextureXR = null;
public static Texture2DArray WhiteTextureXR
{
get
{
if (s_WhiteTextureXR == null)
{
s_WhiteTextureXR = TextureArrayHelpers.CreateTexture2DArray(Texture2D.whiteTexture, k_MaximumViewsXR);
s_WhiteTextureXR.name = "_Crest_WhiteTextureXR";
}
return s_WhiteTextureXR;
}
}
public static void SetMatricesXR(Camera camera)
{
if (!camera.stereoEnabled || !SinglePassXR)
{
return;
}
SubsystemManager.GetSubsystems(s_DisplayListXR);
// XR SPI only has one pass by definition.
DisplayXR.GetRenderPass(renderPassIndex: 0, out var xrPass);
xrPass.GetRenderParameter(camera, renderParameterIndex: 0, out var xrLeftEye);
xrPass.GetRenderParameter(camera, renderParameterIndex: 1, out var xrRightEye);
// We must opt for renderIntoTexture for Unity to handle Y flip.
InverseViewProjectionMatrixXR[0] = (GL.GetGPUProjectionMatrix(xrLeftEye.projection, true) * xrLeftEye.view).inverse;
InverseViewProjectionMatrixXR[1] = (GL.GetGPUProjectionMatrix(xrRightEye.projection, true) * xrRightEye.view).inverse;
Shader.SetGlobalMatrixArray(ShaderIDs.s_StereoInverseViewProjection, InverseViewProjectionMatrixXR);
}
}
#endif // _XR_ENABLED
}
}
#if d_UnityURP
namespace WaveHarmonic.Crest
{
#if !UNITY_6000_0_OR_NEWER
using UniversalCameraData = CameraData;
#endif
static partial class Rendering
{
public static class URP
{
[Conditional("_XR_ENABLED")]
public static void EnableXR(CommandBuffer commands, UniversalCameraData camera)
{
#if _XR_ENABLED
// We need to check the mask or it will cause entire pipeline to output black. Appears to only affect URP.
if (!SinglePassXR || !camera.xrRendering || camera.camera.stereoTargetEye != StereoTargetEyeMask.Both)
{
return;
}
commands.EnableKeyword(SinglePassKeyword);
#endif
}
[Conditional("_XR_ENABLED")]
public static void DisableXR(CommandBuffer commands, UniversalCameraData camera)
{
#if _XR_ENABLED
if (!SinglePassXR || !camera.xrRendering || camera.camera.stereoTargetEye != StereoTargetEyeMask.Both)
{
return;
}
commands.DisableKeyword(SinglePassKeyword);
#endif
}
}
}
}
#endif // d_UnityURP
#if d_UnityHDRP
namespace WaveHarmonic.Crest
{
static partial class Rendering
{
public static class HDRP
{
[Conditional("_XR_ENABLED")]
public static void EnableXR(CommandBuffer commands, HDAdditionalCameraData camera)
{
#if _XR_ENABLED
if (!SinglePassXR || camera == null || !camera.xrRendering)
{
return;
}
commands.EnableKeyword(SinglePassKeyword);
#endif
}
[Conditional("_XR_ENABLED")]
public static void DisableXR(CommandBuffer commands, HDAdditionalCameraData camera)
{
#if _XR_ENABLED
if (!SinglePassXR || camera == null || !camera.xrRendering)
{
return;
}
commands.DisableKeyword(SinglePassKeyword);
#endif
}
public static bool SkipPassXR(ref int index, HDAdditionalCameraData data)
{
#if _XR_ENABLED
if (MultiPassXR && data != null && data.xrRendering)
{
// Alternate between left and right eye.
index += 1;
index %= 2;
}
else
#endif
{
index = -1;
}
// Skip if rendering the right eye.
return index == 1;
}
}
}
}
#endif // d_UnityHDRP

View File

@@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: beeb15d60da12461bb224ab1b3a6a12a
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@@ -0,0 +1,124 @@
// Crest Water System
// Copyright © 2024 Wave Harmonic. All rights reserved.
#if ENABLE_VR && d_UnityModuleVR
#define _XR_ENABLED
#endif
using UnityEngine;
using UnityEngine.Experimental.Rendering;
using UnityEngine.XR;
namespace WaveHarmonic.Crest
{
static partial class Rendering
{
public static partial class BIRP
{
static partial class ShaderIDs
{
public static readonly int s_InverseViewProjection = Shader.PropertyToID("_Crest_InverseViewProjection");
}
public static Texture GetWhiteTexture(Camera camera)
{
#if _XR_ENABLED
if (camera.stereoEnabled && SinglePassXR)
{
return WhiteTextureXR;
}
#endif
return Texture2D.whiteTexture;
}
public static void SetMatrices(Camera camera)
{
Shader.SetGlobalMatrix(ShaderIDs.s_InverseViewProjection, (GL.GetGPUProjectionMatrix(camera.projectionMatrix, true) * camera.worldToCameraMatrix).inverse);
#if _XR_ENABLED
SetMatricesXR(camera);
#endif
}
public enum FrameBufferFormatOverride
{
None,
LDR,
HDR,
}
public static RenderTextureDescriptor GetCameraTargetDescriptor(Camera camera, FrameBufferFormatOverride hdrOverride = FrameBufferFormatOverride.None)
{
RenderTextureDescriptor descriptor;
#if _XR_ENABLED
if (camera.stereoEnabled)
{
// Will not set the following correctly:
// - HDR format
descriptor = XRSettings.eyeTextureDesc;
}
else
#endif
{
// As recommended by Unity, in 2021.2 using SystemInfo.GetGraphicsFormat with DefaultFormat.LDR is
// necessary or gamma color space texture is returned:
// https://docs.unity3d.com/ScriptReference/Experimental.Rendering.DefaultFormat.html
descriptor = new(camera.pixelWidth, camera.pixelHeight, SystemInfo.GetGraphicsFormat(DefaultFormat.LDR), 0);
}
// Set HDR format.
if (camera.allowHDR && QualitySettings.activeColorSpace == ColorSpace.Linear)
{
var format = DefaultFormat.HDR;
if (hdrOverride is not FrameBufferFormatOverride.None)
{
format = hdrOverride is FrameBufferFormatOverride.HDR ? DefaultFormat.HDR : DefaultFormat.LDR;
}
#if UNITY_ANDROID || UNITY_IOS || UNITY_TVOS
else
{
format = DefaultFormat.LDR;
}
#endif
descriptor.graphicsFormat = SystemInfo.GetGraphicsFormat(format);
}
return descriptor;
}
}
}
static partial class Rendering
{
// Blit
public static partial class BIRP
{
// Need to cast to int but no conversion cost.
// https://stackoverflow.com/a/69148528
internal enum UtilityPass
{
CopyDepth,
Copy,
MergeDepth,
}
static Material s_UtilityMaterial;
public static Material UtilityMaterial
{
get
{
if (s_UtilityMaterial == null)
{
s_UtilityMaterial = new(Shader.Find("Hidden/Crest/Legacy/Blit"));
}
return s_UtilityMaterial;
}
}
}
}
}

View File

@@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: 33d8472e9ea11461b8bf02d7c4a72ec4
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@@ -1,4 +1,4 @@
// Crest Water System
// Crest Water System
// Copyright © 2024 Wave Harmonic. All rights reserved.
using UnityEngine;
@@ -7,7 +7,7 @@ namespace WaveHarmonic.Crest
{
static class TextureArrayHelpers
{
const int k_SmallTextureSize = 4;
internal const int k_SmallTextureSize = 4;
public static Texture2D CreateTexture2D(Color color, TextureFormat format)
{