- Home /
Shader - Custom Dilate Node for Shader Graph
Can anyone assist me creating a custom node?
I'm new to shaders in general and I'm trying to achieve a Dilate effect combined with other visuals available through nodes already.
I got the desired effect I want minus the Dilate effect which I've got in a separate shader made by user poisoned_banana here: http://answers.unity.com/answers/1495613/view.html
I want to turn this Dilate effect into a node so I can apply it together with the effect I've created using Shader graph.
I tried this myself using the Custom Node API/Tutorial, but I can't get it to compile, I'm not quite sure which part of the shader I can put into the shader string and which variables to assign to which slots.
I've illustrated the problem I have in the following image.
Outside red is my current effect which I want to combine with a custom Dilate effect node to get a sort of outline shader with effects.
I tried creating a custom node, but I'm not experienced enough with shaders to grasp what I'm doing. This doesn't work:
using UnityEngine;
using UnityEditor.ShaderGraph;
using System.Reflection;
[Title("Custom", "Dilate Unlit Node")]
public class DilateUnlitNode : CodeFunctionNode
{
public DilateUnlitNode()
{
name = "Dilate Unlit Node";
}
protected override MethodInfo GetFunctionToConvert()
{
return GetType().GetMethod("DilateFunction", BindingFlags.Static | BindingFlags.NonPublic);
}
static string DilateFunction(
[Slot(0, Binding.None)] Texture2D _MainTex,
[Slot(1, Binding.None)] Vector1 _PixelOffset,
[Slot(2, Binding.None)] out ColorRGBA finalColor)
{
return
@"
{
//min and max Vector
float2 _min = float2(0,0);
float2 _max = float2(1,1);
//get the color of 8 neighbour pixels
fixed4 U = tex2D(_MainTex,clamp(i.uv + float2(0,_PixelOffset),_min,_max));
fixed4 UR = tex2D(_MainTex,clamp(i.uv + float2(_PixelOffset,_PixelOffset),_min,_max));
fixed4 R = tex2D(_MainTex,clamp(i.uv + float2(_PixelOffset,0),_min,_max));
fixed4 DR = tex2D(_MainTex,clamp(i.uv + float2(_PixelOffset,-_PixelOffset),_min,_max));
fixed4 D = tex2D(_MainTex,clamp(i.uv + float2(0,-_PixelOffset),_min,_max));
fixed4 DL = tex2D(_MainTex,clamp(i.uv + float2(-_PixelOffset,-_PixelOffset),_min,_max));
fixed4 L = tex2D(_MainTex,clamp(i.uv + float2(-_PixelOffset,0),_min,_max));
fixed4 UL = tex2D(_MainTex,clamp(i.uv + float2(-_PixelOffset,_PixelOffset),_min,_max));
//add all colors up to one final color
fixed4 finalColor = U + UR + R + DR + D + DL + L + UL;
//return final color
return finalColor;
}
";
}
}
Hey, I'm trying to create a custom node as well, did you resolve you problem?
Answer by ParadoxSolutions · Apr 05 at 05:52 PM
I needed a dilate node too so I did the conversion. Use the below code in the body of a custom shader graph node of the type "string" name it "Dilate" and add the inputs: "tex" as Texture2D, "uv" as Vector2, "offset" as float, "state" as Sampler State. For outputs add "dilated" as a Vector4.
float2 _min = float2(0,0);
float2 _max = float2(1,1);
float4 U = tex.Sample(state, clamp(uv + float2(0, offset),_min,_max));
float4 UR = tex.Sample(state, clamp(uv + float2(offset, offset),_min,_max));
float4 R = tex.Sample(state, clamp(uv + float2(offset, 0),_min,_max));
float4 DR = tex.Sample(state, clamp(uv + float2(offset, -offset),_min,_max));
float4 D = tex.Sample(state, clamp(uv + float2(0, -offset),_min,_max));
float4 DL = tex.Sample(state, clamp(uv + float2(-offset, -offset),_min,_max));
float4 L = tex.Sample(state, clamp(uv + float2(-offset, 0),_min,_max));
float4 UL = tex.Sample(state, clamp(uv + float2(-offset, offset),_min,_max));
dilated = U + UR + R + DR + D + DL + L + UL;
Confirmed to work in Shader Graph using URP in Unity 2020 LTS.
Your answer
Follow this Question
Related Questions
How to add Emission to my custom shader? 2 Answers
Shader: Modify XY Position of Texture 0 Answers
Adding a clip() to the default shader? 0 Answers
Shader - What is float3.xy? 1 Answer
Drawing silhouette shader 0 Answers