Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
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
8 changes: 8 additions & 0 deletions Assets/com.alelievr.NodeGraphProcessor/Editor/Shaders.meta

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

74 changes: 74 additions & 0 deletions Assets/com.alelievr.NodeGraphProcessor/Editor/Shaders/Grid.shader
Original file line number Diff line number Diff line change
@@ -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"
}

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

Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,8 @@ public BaseGraphView(EditorWindow window)
createNodeMenu = ScriptableObject.CreateInstance< CreateNodeMenuWindow >();
createNodeMenu.Initialize(this, window);

InitGrid();

this.StretchToParentSize();
}

Expand Down Expand Up @@ -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
Expand Down
81 changes: 81 additions & 0 deletions Assets/com.alelievr.NodeGraphProcessor/Editor/Views/GridView.cs
Original file line number Diff line number Diff line change
@@ -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<AttachToPanelEvent>(OnAttachToPanel);
RegisterCallback<DetachFromPanelEvent>(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;
}
}
}

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