-
Notifications
You must be signed in to change notification settings - Fork 414
feat: gpu-based grid background #234
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
labbbirder
wants to merge
2
commits into
alelievr:master
Choose a base branch
from
labbbirder:master
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
74 changes: 74 additions & 0 deletions
74
Assets/com.alelievr.NodeGraphProcessor/Editor/Shaders/Grid.shader
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
} |
9 changes: 9 additions & 0 deletions
9
Assets/com.alelievr.NodeGraphProcessor/Editor/Shaders/Grid.shader.meta
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
81 changes: 81 additions & 0 deletions
81
Assets/com.alelievr.NodeGraphProcessor/Editor/Views/GridView.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} | ||
} |
11 changes: 11 additions & 0 deletions
11
Assets/com.alelievr.NodeGraphProcessor/Editor/Views/GridView.cs.meta
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.