From 5a586447bb5997699eaf0acf23f80fa3145e5058 Mon Sep 17 00:00:00 2001 From: labbbirder <502100554@qq.com> Date: Mon, 17 Mar 2025 18:05:43 +0800 Subject: [PATCH 1/2] feat: gpu-based grid background --- .../Editor/Shaders.meta | 8 ++ .../Editor/Shaders/Grid.shader | 74 +++++++++++++++++ .../Editor/Shaders/Grid.shader.meta | 9 +++ .../Editor/Views/BaseGraphView.cs | 12 +++ .../Editor/Views/GridView.cs | 81 +++++++++++++++++++ .../Editor/Views/GridView.cs.meta | 11 +++ 6 files changed, 195 insertions(+) create mode 100644 Assets/com.alelievr.NodeGraphProcessor/Editor/Shaders.meta create mode 100644 Assets/com.alelievr.NodeGraphProcessor/Editor/Shaders/Grid.shader create mode 100644 Assets/com.alelievr.NodeGraphProcessor/Editor/Shaders/Grid.shader.meta create mode 100644 Assets/com.alelievr.NodeGraphProcessor/Editor/Views/GridView.cs create mode 100644 Assets/com.alelievr.NodeGraphProcessor/Editor/Views/GridView.cs.meta diff --git a/Assets/com.alelievr.NodeGraphProcessor/Editor/Shaders.meta b/Assets/com.alelievr.NodeGraphProcessor/Editor/Shaders.meta new file mode 100644 index 00000000..b4e89930 --- /dev/null +++ b/Assets/com.alelievr.NodeGraphProcessor/Editor/Shaders.meta @@ -0,0 +1,8 @@ +fileFormatVersion: 2 +guid: 0312b6c3d76df4a4c8957f08a1d024e7 +folderAsset: yes +DefaultImporter: + externalObjects: {} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/com.alelievr.NodeGraphProcessor/Editor/Shaders/Grid.shader b/Assets/com.alelievr.NodeGraphProcessor/Editor/Shaders/Grid.shader new file mode 100644 index 00000000..bb4cbc7f --- /dev/null +++ b/Assets/com.alelievr.NodeGraphProcessor/Editor/Shaders/Grid.shader @@ -0,0 +1,74 @@ +Shader "GraphView/Grid" +{ + Properties + { + _GridColor ("Grid Color", Color) = (0.25, 0.25, 0.25, 1) + _BackgroundColor ("Background Color", Color) = (0.1, 0.1, 0.1, 1) + _GridSize ("Grid Size", Float) = 16.0 + _ScaleLimit ("Upscale Limit", Int) = 4 + } + SubShader + { + Tags { "RenderType" = "Opaque" } + LOD 200 + + Pass + { + CGPROGRAM + #pragma vertex vert + #pragma fragment frag + + #include "UnityCG.cginc" + + struct appdata + { + float4 vertex : POSITION; + float2 uv : TEXCOORD0; + }; + + struct v2f + { + float2 uv : TEXCOORD0; + float4 vertex : SV_POSITION; + float scaleFactor : TEXCOORD1; + }; + + float4 _GridColor; + float _ScaleLimit; + float4 _BackgroundColor; + float4 _RtTransform; + float _Scale; + float _GridSize; + + fixed4 grid(float2 uv, float scaleFactor) + { + float2 d = fwidth(uv); + float2 d2 = smoothstep(0.5 - d, 0.5, frac(uv + 0.5)) - smoothstep(0.5, 0.5 + d, frac(uv + 0.5)); + return max(d2.x, d2.y) * smoothstep(0.1, 0.9, scaleFactor / _ScaleLimit); + } + + v2f vert (appdata v) + { + v2f o; + o.vertex = UnityObjectToClipPos(v.vertex); + v.uv.y = 1 - v.uv.y; + float scale = pow(_ScaleLimit, frac(log(_Scale) / log(_ScaleLimit))); + o.uv = (v.uv * _RtTransform.xy - _RtTransform.zw) / _GridSize / scale; + o.scaleFactor = scale; + return o; + } + + fixed4 frag (v2f i) : SV_Target + { + float2 uv = i.uv; + float scaleFactor = i.scaleFactor; + float weight = grid(uv, scaleFactor) + + grid(uv / _ScaleLimit, scaleFactor * _ScaleLimit); + fixed4 col = lerp(_BackgroundColor, _GridColor, weight); + return col; + } + ENDCG + } + } + FallBack "Diffuse" +} diff --git a/Assets/com.alelievr.NodeGraphProcessor/Editor/Shaders/Grid.shader.meta b/Assets/com.alelievr.NodeGraphProcessor/Editor/Shaders/Grid.shader.meta new file mode 100644 index 00000000..d7a81885 --- /dev/null +++ b/Assets/com.alelievr.NodeGraphProcessor/Editor/Shaders/Grid.shader.meta @@ -0,0 +1,9 @@ +fileFormatVersion: 2 +guid: 60e522bb5d8e277449e30b6aa4665dc6 +ShaderImporter: + externalObjects: {} + defaultTextures: [] + nonModifiableTextures: [] + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/com.alelievr.NodeGraphProcessor/Editor/Views/BaseGraphView.cs b/Assets/com.alelievr.NodeGraphProcessor/Editor/Views/BaseGraphView.cs index 7940f6ee..ed266d1d 100644 --- a/Assets/com.alelievr.NodeGraphProcessor/Editor/Views/BaseGraphView.cs +++ b/Assets/com.alelievr.NodeGraphProcessor/Editor/Views/BaseGraphView.cs @@ -159,6 +159,8 @@ public BaseGraphView(EditorWindow window) createNodeMenu = ScriptableObject.CreateInstance< CreateNodeMenuWindow >(); createNodeMenu.Initialize(this, window); + InitGrid(); + this.StretchToParentSize(); } @@ -686,6 +688,16 @@ void DragUpdatedCallback(DragUpdatedEvent e) #region Initialization + void InitGrid() + { + var bgGrid = new GridView(contentViewContainer) + { + name = "GridView", + }; + this.Add(bgGrid); + bgGrid.PlaceBehind(contentViewContainer); + } + void ReloadView() { // Force the graph to reload his data (Undo have updated the serialized properties of the graph diff --git a/Assets/com.alelievr.NodeGraphProcessor/Editor/Views/GridView.cs b/Assets/com.alelievr.NodeGraphProcessor/Editor/Views/GridView.cs new file mode 100644 index 00000000..5e3ba93f --- /dev/null +++ b/Assets/com.alelievr.NodeGraphProcessor/Editor/Views/GridView.cs @@ -0,0 +1,81 @@ +using UnityEditor; +using UnityEngine; +using UnityEngine.UIElements; + +namespace GraphProcessor +{ + public class GridView : VisualElement + { + private static readonly int ID_RT_TRANSFORM = Shader.PropertyToID("_RtTransform"); + private static readonly int ID_SCALE = Shader.PropertyToID("_Scale"); + + private Material material; + private RenderTexture renderTexture; + private VisualElement transformSource; + + public GridView() + { + + } + + public GridView(VisualElement transformSource) + { + this.transformSource = transformSource; + + RegisterCallback(OnAttachToPanel); + RegisterCallback(OnDetachFromPanel); + + style.position = Position.Absolute; + style.backgroundSize = new BackgroundSize(BackgroundSizeType.Cover); + style.width = new Length(Screen.currentResolution.width, LengthUnit.Pixel); + style.height = new Length(Screen.currentResolution.height, LengthUnit.Pixel); + } + + private void OnAttachToPanel(AttachToPanelEvent e) + { + renderTexture = RenderTexture.GetTemporary( + Screen.currentResolution.width, + Screen.currentResolution.height, + 0, RenderTextureFormat.ARGB32, + RenderTextureReadWrite.Default); + renderTexture.Create(); + material = new Material(Shader.Find("GraphView/Grid")); + style.backgroundImage = Background.FromRenderTexture(renderTexture); + + Redraw(); + + EditorApplication.update -= Redraw; + EditorApplication.update += Redraw; + } + + private void Redraw() + { + if (transformSource == null || renderTexture == null || material == null) + return; + + material.SetVector(ID_RT_TRANSFORM, new Vector4( + Screen.currentResolution.width, Screen.currentResolution.height, + transformSource.resolvedStyle.translate.x, transformSource.resolvedStyle.translate.y + )); + material.SetFloat(ID_SCALE, transformSource.resolvedStyle.scale.value.x); + Graphics.SetRenderTarget(renderTexture); + Graphics.Blit(null, material); + } + + private void OnDetachFromPanel(DetachFromPanelEvent e) + { + if (renderTexture) + { + RenderTexture.ReleaseTemporary(renderTexture); + } + + if (material) + { + Object.DestroyImmediate(material); + } + + style.backgroundImage = null; + EditorApplication.update -= Redraw; + } + } +} diff --git a/Assets/com.alelievr.NodeGraphProcessor/Editor/Views/GridView.cs.meta b/Assets/com.alelievr.NodeGraphProcessor/Editor/Views/GridView.cs.meta new file mode 100644 index 00000000..fdd95b24 --- /dev/null +++ b/Assets/com.alelievr.NodeGraphProcessor/Editor/Views/GridView.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: 4d7d896ca61f6d4459b3eea2d74a9510 +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: From e921eee344b023d557bdc167c9ec1116ee988abe Mon Sep 17 00:00:00 2001 From: labbbirder <502100554@qq.com> Date: Mon, 14 Apr 2025 11:51:44 +0800 Subject: [PATCH 2/2] fix: ui events eaten by grid --- .../Editor/Views/BaseGraphView.cs | 1 + 1 file changed, 1 insertion(+) diff --git a/Assets/com.alelievr.NodeGraphProcessor/Editor/Views/BaseGraphView.cs b/Assets/com.alelievr.NodeGraphProcessor/Editor/Views/BaseGraphView.cs index ed266d1d..34f5b87a 100644 --- a/Assets/com.alelievr.NodeGraphProcessor/Editor/Views/BaseGraphView.cs +++ b/Assets/com.alelievr.NodeGraphProcessor/Editor/Views/BaseGraphView.cs @@ -693,6 +693,7 @@ void InitGrid() var bgGrid = new GridView(contentViewContainer) { name = "GridView", + pickingMode = PickingMode.Ignore, }; this.Add(bgGrid); bgGrid.PlaceBehind(contentViewContainer);