Insanely huge initial commit

This commit is contained in:
2026-02-21 16:40:15 -08:00
parent 208d626100
commit f74c547a13
33825 changed files with 5213498 additions and 0 deletions

View File

@@ -0,0 +1,73 @@
using UnityEngine;
using UnityEngine.Rendering;
namespace LeTai.Asset.TranslucentImage
{
public static class BlurExecutor
{
static readonly int[] TEMP_RT = new int[14];
static BlurExecutor()
{
for (var i = 0; i < TEMP_RT.Length; i++)
{
TEMP_RT[i] = Shader.PropertyToID($"TI_intermediate_rt_{i}");
}
}
public readonly struct BlurExecutionData
{
public readonly RenderTargetIdentifier sourceTex;
public readonly TranslucentImageSource blurSource;
public readonly IBlurAlgorithm blurAlgorithm;
public BlurExecutionData(
RenderTargetIdentifier sourceTex,
TranslucentImageSource blurSource,
IBlurAlgorithm blurAlgorithm
)
{
this.sourceTex = sourceTex;
this.blurSource = blurSource;
this.blurAlgorithm = blurAlgorithm;
}
}
public static void ExecuteBlurWithTempTextures(CommandBuffer cmd, ref BlurExecutionData data)
{
var scratchesCount = data.blurAlgorithm.GetScratchesCount();
var desc = data.blurSource.BlurredScreen.descriptor;
desc.msaaSamples = 1;
desc.useMipMap = false;
desc.depthBufferBits = 0;
for (int i = 0; i < scratchesCount; i++)
{
data.blurAlgorithm.GetScratchDescriptor(i, ref desc);
cmd.GetTemporaryRT(TEMP_RT[i], desc, FilterMode.Bilinear);
data.blurAlgorithm.SetScratch(i, TEMP_RT[i]);
}
{
ExecuteBlur(cmd, ref data);
}
for (int i = 0; i < scratchesCount; i++)
cmd.ReleaseTemporaryRT(TEMP_RT[i]);
}
public static void ExecuteBlur(CommandBuffer cmd, ref BlurExecutionData data)
{
var blurSource = data.blurSource;
var blurredScreen = blurSource.BlurredScreen;
var blurRegion = blurSource.BlurRegion;
data.blurAlgorithm.Blur(cmd,
data.sourceTex,
blurRegion,
blurSource.BackgroundFill,
blurredScreen);
}
}
}

View File

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

View File

@@ -0,0 +1,130 @@
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Rendering;
namespace LeTai.Asset.TranslucentImage
{
public static class Extensions
{
static Mesh fullscreenTriangle;
/// <summary>
/// A fullscreen triangle mesh.
/// </summary>
static Mesh FullscreenTriangle
{
get
{
if (fullscreenTriangle != null)
return fullscreenTriangle;
fullscreenTriangle = new Mesh { name = "Fullscreen Triangle" };
fullscreenTriangle.SetVertices(
new List<Vector3> {
new Vector3(-1f, -1f, 0f),
new Vector3(-1f, 3f, 0f),
new Vector3(3f, -1f, 0f)
}
);
fullscreenTriangle.SetIndices(new[] { 0, 1, 2 }, MeshTopology.Triangles, 0, false);
fullscreenTriangle.UploadMeshData(false);
return fullscreenTriangle;
}
}
public static void BlitCustom(
this CommandBuffer cmd,
RenderTargetIdentifier source,
RenderTargetIdentifier destination,
Material material,
int passIndex,
bool useBuiltin = false
)
{
if (useBuiltin)
cmd.Blit(source, destination, material, passIndex);
else if (
SystemInfo.graphicsShaderLevel >= 30
#if !UNITY_2023_1_OR_NEWER
&& SystemInfo.graphicsDeviceType != GraphicsDeviceType.OpenGLES2
#endif
)
cmd.BlitProcedural(source, destination, material, passIndex);
else
cmd.BlitFullscreenTriangle(source, destination, material, passIndex);
}
public static void BlitFullscreenTriangle(
this CommandBuffer cmd,
RenderTargetIdentifier source,
RenderTargetIdentifier destination,
Material material,
int pass
)
{
cmd.SetGlobalTexture("_MainTex", source);
#if UNITY_2018_2_OR_NEWER
cmd.SetRenderTarget(destination, RenderBufferLoadAction.DontCare, RenderBufferStoreAction.Store);
#else
cmd.SetRenderTarget(destination);
#endif
cmd.DrawMesh(FullscreenTriangle, Matrix4x4.identity, material, 0, pass);
}
public static void BlitProcedural(
this CommandBuffer cmd,
RenderTargetIdentifier source,
RenderTargetIdentifier destination,
Material material,
int passIndex
)
{
cmd.SetGlobalTexture(ShaderId.MAIN_TEX, source);
cmd.SetRenderTarget(new RenderTargetIdentifier(destination, 0, CubemapFace.Unknown, -1),
RenderBufferLoadAction.DontCare,
RenderBufferStoreAction.Store,
RenderBufferLoadAction.DontCare,
RenderBufferStoreAction.DontCare);
cmd.DrawProcedural(Matrix4x4.identity, material, passIndex, MeshTopology.Quads, 4, 1, null);
}
/// For normalized screen size
internal static bool Approximately(this Rect self, Rect other)
{
return QuickApproximate(self.x, other.x)
&& QuickApproximate(self.y, other.y)
&& QuickApproximate(self.width, other.width)
&& QuickApproximate(self.height, other.height);
}
const float EPSILON01 = 5.9604644e-8f; // different between 1 and largest float < 1
private static bool QuickApproximate(float a, float b)
{
return Mathf.Abs(b - a) < EPSILON01;
}
public static Vector4 ToMinMaxVector(this Rect self)
{
return new Vector4(
self.xMin,
self.yMin,
self.xMax,
self.yMax
);
}
public static Vector4 ToVector4(this Rect self)
{
return new Vector4(
self.xMin,
self.yMin,
self.width,
self.height
);
}
}
}

View File

@@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: 29b4a2967894477c93934b7e21ce2d5b
timeCreated: 1558521365

View File

@@ -0,0 +1,167 @@
// Due to the use of asmdef in the ../Editor folder, this class cannot be put there,
// as then it cannot be referenced outside of the assembly
// The Blur region GUI requires the use of OnGUI for interactivity,
// so it cannot be completely done within the custom Editor either
#if UNITY_EDITOR
using UnityEditor;
using UnityEngine;
namespace LeTai.Asset.TranslucentImage
{
public static class ResizableScreenRect
{
const float BORDER_THICKNESS = 2;
const float DRAG_EXTEND = 16;
delegate void DragHandlerDelegate(Vector2 delta, ref Rect rect);
static DragHandlerDelegate currentDragHandler;
static void DrawSquareFromCenter(Vector2 postion, float extent)
{
var v = Vector2.one * extent;
GUI.DrawTexture(new Rect(postion - v, v * 2), Texture2D.whiteTexture);
}
public static Rect Draw(Rect normalizedScreenRect)
{
var guiRect = normalizedScreenRect;
guiRect.y = 1 - guiRect.y - guiRect.height;
guiRect.x *= Screen.width;
guiRect.width *= Screen.width;
guiRect.y *= Screen.height;
guiRect.height *= Screen.height;
var borderThickness = BORDER_THICKNESS * EditorGUIUtility.pixelsPerPoint;
GUI.DrawTexture(new Rect(guiRect.x - borderThickness,
guiRect.y - borderThickness,
borderThickness,
guiRect.height + borderThickness * 2),
Texture2D.whiteTexture);
GUI.DrawTexture(new Rect(guiRect.x,
guiRect.y - borderThickness,
guiRect.width + 1,
borderThickness),
Texture2D.whiteTexture);
GUI.DrawTexture(new Rect(guiRect.xMax,
guiRect.y - borderThickness,
borderThickness,
guiRect.height + borderThickness * 2),
Texture2D.whiteTexture);
GUI.DrawTexture(new Rect(guiRect.x,
guiRect.yMax,
guiRect.width + 1,
borderThickness),
Texture2D.whiteTexture);
var boxExtend = borderThickness * 2;
DrawSquareFromCenter(guiRect.min, boxExtend);
DrawSquareFromCenter(guiRect.max, boxExtend);
DrawSquareFromCenter(new Vector2(guiRect.xMax, guiRect.yMin), boxExtend);
DrawSquareFromCenter(new Vector2(guiRect.xMin, guiRect.yMax), boxExtend);
return HandleEvent(guiRect);
}
static Rect HandleEvent(Rect guiRect)
{
if (Event.current.type == EventType.MouseDown)
{
currentDragHandler = ChooseDragHandler(guiRect, Event.current.mousePosition);
}
else if (Event.current.type == EventType.MouseUp)
{
currentDragHandler = null;
}
else if (Event.current.type == EventType.MouseDrag)
{
currentDragHandler?.Invoke(Event.current.delta, ref guiRect);
}
var result = guiRect;
result.x /= Screen.width;
result.y /= Screen.height;
result.width /= Screen.width;
result.height /= Screen.height;
result.y = 1 - result.y - result.height;
result.xMin = Mathf.Max(0, result.xMin);
result.yMin = Mathf.Max(0, result.yMin);
result.width = Mathf.Min(1, result.width);
result.height = Mathf.Min(1, result.height);
return result;
}
static DragHandlerDelegate ChooseDragHandler(Rect rect, Vector2 pointer)
{
float extend = DRAG_EXTEND * EditorGUIUtility.pixelsPerPoint;
bool PointerXNear(float point) => Mathf.Abs(point - pointer.x) <= extend;
bool PointerYNear(float point) => Mathf.Abs(point - pointer.y) <= extend;
if (PointerXNear(rect.xMin))
{
if (PointerYNear(rect.yMin)) return DRAG_HANDLER_TOP_LEFT;
if (PointerYNear(rect.yMax)) return DRAG_HANDLER_BOTTOM_LEFT;
return DRAG_HANDLER_LEFT;
}
if (PointerXNear(rect.xMax))
{
if (PointerYNear(rect.yMin)) return DRAG_HANDLER_TOP_RIGHT;
if (PointerYNear(rect.yMax)) return DRAG_HANDLER_BOTTOM_RIGHT;
return DRAG_HANDLER_RIGHT;
}
if (PointerYNear(rect.yMin)) return DRAG_HANDLER_TOP;
if (PointerYNear(rect.yMax)) return DRAG_HANDLER_BOTTOM;
return DRAG_HANDLER_CENTER;
}
static readonly DragHandlerDelegate DRAG_HANDLER_CENTER =
(Vector2 delta, ref Rect rect) => { rect.position += delta; };
static readonly DragHandlerDelegate DRAG_HANDLER_LEFT =
(Vector2 delta, ref Rect rect) => { rect.xMin += delta.x; };
static readonly DragHandlerDelegate DRAG_HANDLER_TOP =
(Vector2 delta, ref Rect rect) => { rect.yMin += delta.y; };
static readonly DragHandlerDelegate DRAG_HANDLER_BOTTOM =
(Vector2 delta, ref Rect rect) => { rect.yMax += delta.y; };
static readonly DragHandlerDelegate DRAG_HANDLER_RIGHT =
(Vector2 delta, ref Rect rect) => { rect.xMax += delta.x; };
static readonly DragHandlerDelegate DRAG_HANDLER_TOP_LEFT =
(Vector2 delta, ref Rect rect) =>
{
rect.xMin += delta.x;
rect.yMin += delta.y;
};
static readonly DragHandlerDelegate DRAG_HANDLER_TOP_RIGHT =
(Vector2 delta, ref Rect rect) =>
{
rect.xMax += delta.x;
rect.yMin += delta.y;
};
static readonly DragHandlerDelegate DRAG_HANDLER_BOTTOM_RIGHT =
(Vector2 delta, ref Rect rect) =>
{
rect.xMax += delta.x;
rect.yMax += delta.y;
};
static readonly DragHandlerDelegate DRAG_HANDLER_BOTTOM_LEFT =
(Vector2 delta, ref Rect rect) =>
{
rect.xMin += delta.x;
rect.yMax += delta.y;
};
}
}
#endif

View File

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

View File

@@ -0,0 +1,14 @@
using UnityEngine;
namespace LeTai.Asset.TranslucentImage
{
public static class ShaderId
{
public static readonly int MAIN_TEX = Shader.PropertyToID("_MainTex");
public static readonly int RADIUS = Shader.PropertyToID("_Radius");
public static readonly int COLOR = Shader.PropertyToID("_Color");
// public static readonly int ENV_TEX = Shader.PropertyToID("_EnvTex");
public static readonly int BACKGROUND_COLOR = Shader.PropertyToID("_BackgroundColor");
public static readonly int CROP_REGION = Shader.PropertyToID("_CropRegion");
}
}

View File

@@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: e531f31ceeeb4a2d99d46564b2754c17
timeCreated: 1558518328

View File

@@ -0,0 +1,36 @@
using System.Runtime.CompilerServices;
using UnityEngine;
namespace LeTai.Asset.TranslucentImage
{
public static class Shims
{
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static T FindObjectOfType<T>(bool includeInactive = false, bool sorted = true) where T : Object
{
#if UNITY_2023_1_OR_NEWER
if (sorted)
return Object.FindFirstObjectByType<T>(includeInactive ? FindObjectsInactive.Include : FindObjectsInactive.Exclude);
else
return Object.FindAnyObjectByType<T>(includeInactive ? FindObjectsInactive.Include : FindObjectsInactive.Exclude);
#elif UNITY_2020_1_OR_NEWER
return Object.FindObjectOfType<T>(includeInactive);
#else
return Object.FindObjectOfType<T>();
#endif
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static T[] FindObjectsOfType<T>(bool includeInactive = false) where T : Object
{
#if UNITY_2023_1_OR_NEWER
return Object.FindObjectsByType<T>(includeInactive ? FindObjectsInactive.Include : FindObjectsInactive.Exclude,
FindObjectsSortMode.None);
#elif UNITY_2020_1_OR_NEWER
return Object.FindObjectsOfType<T>(includeInactive);
#else
return Object.FindObjectsOfType<T>();
#endif
}
}
}

View File

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