This commit is contained in:
2024-09-20 20:30:10 +02:00
commit 4fabf1a6fd
29169 changed files with 1706941 additions and 0 deletions

View File

@@ -0,0 +1,47 @@
using System.Reflection;
using UnityEngine;
namespace UnityEditor.ShaderGraph
{
[Title("Procedural", "Checkerboard")]
class CheckerboardNode : CodeFunctionNode
{
public CheckerboardNode()
{
name = "Checkerboard";
}
protected override MethodInfo GetFunctionToConvert()
{
return GetType().GetMethod("Unity_Checkerboard", BindingFlags.Static | BindingFlags.NonPublic);
}
static string Unity_Checkerboard(
[Slot(0, Binding.MeshUV0)] Vector2 UV,
[Slot(1, Binding.None, 0.2f, 0.2f, 0.2f, 1f)] ColorRGB ColorA,
[Slot(2, Binding.None, 0.7f, 0.7f, 0.7f, 1f)] ColorRGB ColorB,
[Slot(3, Binding.None, 1f, 1f, 1f, 1f)] Vector2 Frequency,
[Slot(4, Binding.None, ShaderStageCapability.Fragment)] out Vector3 Out)
{
Out = Vector2.zero;
return
@"
{
#if defined(SHADER_STAGE_RAY_TRACING)
int2 checker = frac(Frequency * UV) > 0.5;
Out = checker.x ^ checker.y ? ColorA : ColorB;
#else
UV = (UV.xy + 0.5) * Frequency;
$precision2 distance3 = 4.0 * abs(frac(UV + 0.25) - 0.5) - 1.0;
$precision4 derivatives = $precision4(ddx(UV), ddy(UV));
$precision2 duv_length = sqrt($precision2(dot(derivatives.xz, derivatives.xz), dot(derivatives.yw, derivatives.yw)));
$precision2 scale = 0.35 / duv_length.xy;
$precision freqLimiter = sqrt(clamp(1.1f - max(duv_length.x, duv_length.y), 0.0, 1.0));
$precision2 vector_alpha = clamp(distance3 * scale.xy, -1.0, 1.0);
$precision alpha = saturate(0.5f + 0.5f * vector_alpha.x * vector_alpha.y * freqLimiter);
Out = lerp(ColorA, ColorB, alpha.xxx);
#endif
}";
}
}
}

View File

@@ -0,0 +1,12 @@
fileFormatVersion: 2
guid: 7c311b074714f8d468c9bfbf8f91619d
timeCreated: 1495731579
licenseType: Pro
MonoImporter:
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@@ -0,0 +1,8 @@
fileFormatVersion: 2
guid: 3f074934a38b41c4ea34460933733201
folderAsset: yes
DefaultImporter:
externalObjects: {}
userData:
assetBundleName:
assetBundleVariant:

View File

@@ -0,0 +1,154 @@
using System.Collections.Generic;
using UnityEditor.Graphing;
using UnityEditor.ShaderGraph.Drawing.Controls;
using UnityEditor.ShaderGraph.Internal;
using UnityEngine;
namespace UnityEditor.ShaderGraph
{
[Title("Procedural", "Noise", "Gradient Noise")]
class GradientNoiseNode : AbstractMaterialNode, IGeneratesBodyCode, IGeneratesFunction, IMayRequireMeshUV
{
// 0 original version
// 1 add deterministic noise option
public override int latestVersion => 1;
public override IEnumerable<int> allowedNodeVersions => new int[] { 1 };
public const int UVSlotId = 0;
public const int ScaleSlotId = 1;
public const int OutSlotId = 2;
const string kUVSlotName = "UV";
const string kScaleSlotName = "Scale";
const string kOutSlotName = "Out";
public GradientNoiseNode()
{
name = "Gradient Noise";
synonyms = new string[] { "perlin noise" };
UpdateNodeAfterDeserialization();
}
public enum HashType
{
Deterministic,
LegacyMod,
};
static readonly string[] kHashFunctionPrefix =
{
"Hash_Tchou_2_1_",
"Hash_LegacyMod_2_1_",
};
public override bool hasPreview => true;
public sealed override void UpdateNodeAfterDeserialization()
{
AddSlot(new UVMaterialSlot(UVSlotId, kUVSlotName, kUVSlotName, UVChannel.UV0));
AddSlot(new Vector1MaterialSlot(ScaleSlotId, kScaleSlotName, kScaleSlotName, SlotType.Input, 10.0f));
AddSlot(new Vector1MaterialSlot(OutSlotId, kOutSlotName, kOutSlotName, SlotType.Output, 0.0f));
RemoveSlotsNameNotMatching(new[] { UVSlotId, ScaleSlotId, OutSlotId });
}
[SerializeField]
private HashType m_HashType = HashType.Deterministic;
[EnumControl("Hash Type")]
public HashType hashType
{
get
{
if (((int)m_HashType < 0) || ((int)m_HashType >= kHashFunctionPrefix.Length))
return (HashType)0;
return m_HashType;
}
set
{
if (m_HashType == value)
return;
m_HashType = value;
Dirty(ModificationScope.Graph);
}
}
void IGeneratesFunction.GenerateNodeFunction(FunctionRegistry registry, GenerationMode generationMode)
{
registry.RequiresIncludePath("Packages/com.unity.render-pipelines.core/ShaderLibrary/Hashes.hlsl");
var hashType = this.hashType;
var hashTypeString = hashType.ToString();
var HashFunction = kHashFunctionPrefix[(int)hashType];
registry.ProvideFunction($"Unity_GradientNoise_{hashTypeString}_Dir_$precision", s =>
{
s.AppendLine($"$precision2 Unity_GradientNoise_{hashTypeString}_Dir_$precision($precision2 p)");
using (s.BlockScope())
{
s.AppendLine($"$precision x; {HashFunction}$precision(p, x);");
s.AppendLine("return normalize($precision2(x - floor(x + 0.5), abs(x) - 0.5));");
}
});
registry.ProvideFunction($"Unity_GradientNoise_{hashTypeString}_$precision", s =>
{
s.AppendLine($"void Unity_GradientNoise_{hashTypeString}_$precision ($precision2 UV, $precision3 Scale, out $precision Out)");
using (s.BlockScope())
{
s.AppendLine("$precision2 p = UV * Scale.xy;");
s.AppendLine("$precision2 ip = floor(p);");
s.AppendLine("$precision2 fp = frac(p);");
s.AppendLine($"$precision d00 = dot(Unity_GradientNoise_{hashTypeString}_Dir_$precision(ip), fp);");
s.AppendLine($"$precision d01 = dot(Unity_GradientNoise_{hashTypeString}_Dir_$precision(ip + $precision2(0, 1)), fp - $precision2(0, 1));");
s.AppendLine($"$precision d10 = dot(Unity_GradientNoise_{hashTypeString}_Dir_$precision(ip + $precision2(1, 0)), fp - $precision2(1, 0));");
s.AppendLine($"$precision d11 = dot(Unity_GradientNoise_{hashTypeString}_Dir_$precision(ip + $precision2(1, 1)), fp - $precision2(1, 1));");
s.AppendLine("fp = fp * fp * fp * (fp * (fp * 6 - 15) + 10);");
s.AppendLine("Out = lerp(lerp(d00, d01, fp.y), lerp(d10, d11, fp.y), fp.x) + 0.5;");
}
});
}
public void GenerateNodeCode(ShaderStringBuilder sb, GenerationMode generationMode)
{
var hashType = this.hashType;
var hashTypeString = hashType.ToString();
string uv = GetSlotValue(UVSlotId, generationMode);
string scale = GetSlotValue(ScaleSlotId, generationMode);
string output = GetVariableNameForSlot(OutSlotId);
var outSlot = FindSlot<MaterialSlot>(OutSlotId);
sb.AppendLine($"{outSlot.concreteValueType.ToShaderString(PrecisionUtil.Token)} {output};");
sb.AppendLine($"Unity_GradientNoise_{hashTypeString}_$precision({uv}, {scale}, {output});");
}
public bool RequiresMeshUV(UVChannel channel, ShaderStageCapability stageCapability)
{
using (var tempSlots = PooledList<MaterialSlot>.Get())
{
GetInputSlots(tempSlots);
var result = false;
foreach (var slot in tempSlots)
{
if (slot.RequiresMeshUV(channel))
{
result = true;
break;
}
}
tempSlots.Clear();
return result;
}
}
public override void OnAfterMultiDeserialize(string json)
{
if (sgVersion < 1)
{
// old nodes should select "LegacyMod" to replicate old behavior
hashType = HashType.LegacyMod;
ChangeVersion(1);
}
}
}
}

View File

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

View File

@@ -0,0 +1,167 @@
using System.Collections.Generic;
using UnityEditor.Graphing;
using UnityEditor.ShaderGraph.Drawing.Controls;
using UnityEditor.ShaderGraph.Internal;
using UnityEngine;
namespace UnityEditor.ShaderGraph
{
[Title("Procedural", "Noise", "Simple Noise")]
class NoiseNode : AbstractMaterialNode, IGeneratesBodyCode, IGeneratesFunction, IMayRequireMeshUV
{
// 0 original version
// 1 add deterministic noise option
public override int latestVersion => 1;
public override IEnumerable<int> allowedNodeVersions => new int[] { 1 };
public const int UVSlotId = 0;
public const int ScaleSlotId = 1;
public const int OutSlotId = 2;
const string kUVSlotName = "UV";
const string kScaleSlotName = "Scale";
const string kOutSlotName = "Out";
public NoiseNode()
{
name = "Simple Noise";
synonyms = new string[] { "value noise" };
UpdateNodeAfterDeserialization();
}
public enum HashType
{
Deterministic,
LegacySine,
};
static readonly string[] kHashFunctionPrefix =
{
"Hash_Tchou_2_1_",
"Hash_LegacySine_2_1_",
};
public override bool hasPreview => true;
public sealed override void UpdateNodeAfterDeserialization()
{
AddSlot(new UVMaterialSlot(UVSlotId, kUVSlotName, kUVSlotName, UVChannel.UV0));
AddSlot(new Vector1MaterialSlot(ScaleSlotId, kScaleSlotName, kScaleSlotName, SlotType.Input, 500.0f));
AddSlot(new Vector1MaterialSlot(OutSlotId, kOutSlotName, kOutSlotName, SlotType.Output, 0.0f));
RemoveSlotsNameNotMatching(new[] { UVSlotId, ScaleSlotId, OutSlotId });
}
[SerializeField]
private HashType m_HashType = HashType.Deterministic;
[EnumControl("Hash Type")]
public HashType hashType
{
get
{
if (((int)m_HashType < 0) || ((int)m_HashType >= kHashFunctionPrefix.Length))
return (HashType)0;
return m_HashType;
}
set
{
if (m_HashType == value)
return;
m_HashType = value;
Dirty(ModificationScope.Graph);
}
}
void IGeneratesFunction.GenerateNodeFunction(FunctionRegistry registry, GenerationMode generationMode)
{
registry.RequiresIncludePath("Packages/com.unity.render-pipelines.core/ShaderLibrary/Hashes.hlsl");
var hashType = this.hashType;
var hashTypeString = hashType.ToString();
var HashFunction = kHashFunctionPrefix[(int)hashType];
registry.ProvideFunction($"Unity_SimpleNoise_ValueNoise_{hashTypeString}_$precision", s =>
{
s.AppendLine($"$precision Unity_SimpleNoise_ValueNoise_{hashTypeString}_$precision ($precision2 uv)");
using (s.BlockScope())
{
s.AppendLine("$precision2 i = floor(uv);");
s.AppendLine("$precision2 f = frac(uv);");
s.AppendLine("f = f * f * (3.0 - 2.0 * f);");
s.AppendLine("uv = abs(frac(uv) - 0.5);");
s.AppendLine("$precision2 c0 = i + $precision2(0.0, 0.0);");
s.AppendLine("$precision2 c1 = i + $precision2(1.0, 0.0);");
s.AppendLine("$precision2 c2 = i + $precision2(0.0, 1.0);");
s.AppendLine("$precision2 c3 = i + $precision2(1.0, 1.0);");
s.AppendLine($"$precision r0; {HashFunction}$precision(c0, r0);");
s.AppendLine($"$precision r1; {HashFunction}$precision(c1, r1);");
s.AppendLine($"$precision r2; {HashFunction}$precision(c2, r2);");
s.AppendLine($"$precision r3; {HashFunction}$precision(c3, r3);");
s.AppendLine("$precision bottomOfGrid = lerp(r0, r1, f.x);");
s.AppendLine("$precision topOfGrid = lerp(r2, r3, f.x);");
s.AppendLine("$precision t = lerp(bottomOfGrid, topOfGrid, f.y);");
s.AppendLine("return t;");
}
});
registry.ProvideFunction($"Unity_SimpleNoise_" + hashTypeString + "_$precision", s =>
{
s.AppendLine($"void Unity_SimpleNoise_{hashTypeString}_$precision($precision2 UV, $precision Scale, out $precision Out)");
using (s.BlockScope())
{
s.AppendLine("$precision freq, amp;");
s.AppendLine("Out = 0.0f;");
for (int octave = 0; octave < 3; octave++)
{
s.AppendLine($"freq = pow(2.0, $precision({octave}));");
s.AppendLine($"amp = pow(0.5, $precision(3-{octave}));");
s.AppendLine($"Out += Unity_SimpleNoise_ValueNoise_{hashTypeString}_$precision($precision2(UV.xy*(Scale/freq)))*amp;");
}
}
});
}
public void GenerateNodeCode(ShaderStringBuilder sb, GenerationMode generationMode)
{
var hashType = this.hashType;
var hashTypeString = hashType.ToString();
string uv = GetSlotValue(UVSlotId, generationMode);
string scale = GetSlotValue(ScaleSlotId, generationMode);
string output = GetVariableNameForSlot(OutSlotId);
var outSlot = FindSlot<MaterialSlot>(OutSlotId);
sb.AppendLine($"{outSlot.concreteValueType.ToShaderString(PrecisionUtil.Token)} {output};");
sb.AppendLine($"Unity_SimpleNoise_{hashTypeString}_$precision({uv}, {scale}, {output});");
}
public bool RequiresMeshUV(UVChannel channel, ShaderStageCapability stageCapability)
{
using (var tempSlots = PooledList<MaterialSlot>.Get())
{
GetInputSlots(tempSlots);
var result = false;
foreach (var slot in tempSlots)
{
if (slot.RequiresMeshUV(channel))
{
result = true;
break;
}
}
tempSlots.Clear();
return result;
}
}
public override void OnAfterMultiDeserialize(string json)
{
if (sgVersion < 1)
{
// old nodes should select "LegacySine" to replicate old behavior
hashType = HashType.LegacySine;
ChangeVersion(1);
}
}
}
}

View File

@@ -0,0 +1,12 @@
fileFormatVersion: 2
guid: 3b0333da20fc0bf48a4d9b09a9d8d9db
timeCreated: 1495718308
licenseType: Pro
MonoImporter:
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@@ -0,0 +1,177 @@
using System.Collections.Generic;
using UnityEditor.Graphing;
using UnityEditor.ShaderGraph.Drawing.Controls;
using UnityEditor.ShaderGraph.Internal;
using UnityEngine;
namespace UnityEditor.ShaderGraph
{
[FormerName("UnityEditor.ShaderGraph.VoronoAbstractMaterialNode")]
[Title("Procedural", "Noise", "Voronoi")]
class VoronoiNode : AbstractMaterialNode, IGeneratesBodyCode, IGeneratesFunction, IMayRequireMeshUV
{
// 0 original version
// 1 add deterministic noise option
public override int latestVersion => 1;
public override IEnumerable<int> allowedNodeVersions => new int[] { 1 };
public const int UVSlotId = 0;
public const int AngleOffsetSlotId = 1;
public const int CellDensitySlotId = 2;
public const int OutSlotId = 3;
public const int CellsSlotId = 4;
const string kUVSlotName = "UV";
const string kAngleOffsetSlotName = "AngleOffset";
const string kCellDensitySlotName = "CellDensity";
const string kOutSlotName = "Out";
const string kCellsSlotName = "Cells";
public VoronoiNode()
{
name = "Voronoi";
synonyms = new string[] { "worley noise" };
UpdateNodeAfterDeserialization();
}
public enum HashType
{
Deterministic,
LegacySine,
};
static readonly string[] kHashFunctionPrefix =
{
"Hash_Tchou_2_2_",
"Hash_LegacySine_2_2_",
};
public override bool hasPreview => true;
public sealed override void UpdateNodeAfterDeserialization()
{
AddSlot(new UVMaterialSlot(UVSlotId, kUVSlotName, kUVSlotName, UVChannel.UV0));
AddSlot(new Vector1MaterialSlot(AngleOffsetSlotId, kAngleOffsetSlotName, kAngleOffsetSlotName, SlotType.Input, 2.0f));
AddSlot(new Vector1MaterialSlot(CellDensitySlotId, kCellDensitySlotName, kCellDensitySlotName, SlotType.Input, 5.0f));
AddSlot(new Vector1MaterialSlot(OutSlotId, kOutSlotName, kOutSlotName, SlotType.Output, 0.0f));
AddSlot(new Vector1MaterialSlot(CellsSlotId, kCellsSlotName, kCellsSlotName, SlotType.Output, 0.0f));
RemoveSlotsNameNotMatching(new[] { UVSlotId, AngleOffsetSlotId, CellDensitySlotId, OutSlotId, CellsSlotId });
}
[SerializeField]
private HashType m_HashType = HashType.Deterministic;
[EnumControl("Hash Type")]
public HashType hashType
{
get
{
if (((int)m_HashType < 0) || ((int)m_HashType >= kHashFunctionPrefix.Length))
return (HashType)0;
return m_HashType;
}
set
{
if (m_HashType == value)
return;
m_HashType = value;
Dirty(ModificationScope.Graph);
}
}
void IGeneratesFunction.GenerateNodeFunction(FunctionRegistry registry, GenerationMode generationMode)
{
registry.RequiresIncludePath("Packages/com.unity.render-pipelines.core/ShaderLibrary/Hashes.hlsl");
var hashType = this.hashType;
var hashTypeString = hashType.ToString();
var HashFunction = kHashFunctionPrefix[(int)hashType];
registry.ProvideFunction($"Unity_Voronoi_RandomVector_{hashTypeString}_$precision", s =>
{
s.AppendLine($"$precision2 Unity_Voronoi_RandomVector_{hashTypeString}_$precision ($precision2 UV, $precision offset)");
using (s.BlockScope())
{
s.AppendLine($"{HashFunction}$precision(UV, UV);");
s.AppendLine("return $precision2(sin(UV.y * offset), cos(UV.x * offset)) * 0.5 + 0.5;");
}
});
registry.ProvideFunction($"Unity_Voronoi_{hashTypeString}_$precision", s =>
{
s.AppendLine($"void Unity_Voronoi_{hashTypeString}_$precision($precision2 UV, $precision AngleOffset, $precision CellDensity, out $precision Out, out $precision Cells)");
using (s.BlockScope())
{
s.AppendLine("$precision2 g = floor(UV * CellDensity);");
s.AppendLine("$precision2 f = frac(UV * CellDensity);");
s.AppendLine("$precision t = 8.0;");
s.AppendLine("$precision3 res = $precision3(8.0, 0.0, 0.0);");
s.AppendLine("for (int y = -1; y <= 1; y++)");
using (s.BlockScope())
{
s.AppendLine("for (int x = -1; x <= 1; x++)");
using (s.BlockScope())
{
s.AppendLine("$precision2 lattice = $precision2(x, y);");
s.AppendLine($"$precision2 offset = Unity_Voronoi_RandomVector_{hashTypeString}_$precision(lattice + g, AngleOffset);");
s.AppendLine("$precision d = distance(lattice + offset, f);");
s.AppendLine("if (d < res.x)");
using (s.BlockScope())
{
s.AppendLine("res = $precision3(d, offset.x, offset.y);");
s.AppendLine("Out = res.x;");
s.AppendLine("Cells = res.y;");
}
}
}
}
});
}
public void GenerateNodeCode(ShaderStringBuilder sb, GenerationMode generationMode)
{
var hashType = this.hashType;
var hashTypeString = hashType.ToString();
string uv = GetSlotValue(UVSlotId, generationMode);
string angleOffset = GetSlotValue(AngleOffsetSlotId, generationMode);
string cellDensity = GetSlotValue(CellDensitySlotId, generationMode);
string output = GetVariableNameForSlot(OutSlotId);
string cells = GetVariableNameForSlot(CellsSlotId);
sb.AppendLine($"{FindSlot<MaterialSlot>(OutSlotId).concreteValueType.ToShaderString(PrecisionUtil.Token)} {output};");
sb.AppendLine($"{FindSlot<MaterialSlot>(CellsSlotId).concreteValueType.ToShaderString(PrecisionUtil.Token)} {cells};");
sb.AppendLine($"Unity_Voronoi_{hashTypeString}_$precision({uv}, {angleOffset}, {cellDensity}, {output}, {cells});");
}
public bool RequiresMeshUV(UVChannel channel, ShaderStageCapability stageCapability)
{
using (var tempSlots = PooledList<MaterialSlot>.Get())
{
GetInputSlots(tempSlots);
var result = false;
foreach (var slot in tempSlots)
{
if (slot.RequiresMeshUV(channel))
{
result = true;
break;
}
}
tempSlots.Clear();
return result;
}
}
public override void OnAfterMultiDeserialize(string json)
{
if (sgVersion < 1)
{
// old nodes should select "LegacySine" to replicate old behavior
hashType = HashType.LegacySine;
ChangeVersion(1);
}
}
}
}

View File

@@ -0,0 +1,12 @@
fileFormatVersion: 2
guid: 38694b8d93b01e049ad6dafebebb60ba
timeCreated: 1495535565
licenseType: Pro
MonoImporter:
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@@ -0,0 +1,8 @@
fileFormatVersion: 2
guid: 74f319061d92c11439d4f7e87c991e89
folderAsset: yes
DefaultImporter:
externalObjects: {}
userData:
assetBundleName:
assetBundleVariant:

View File

@@ -0,0 +1,38 @@
using System.Reflection;
using UnityEngine;
namespace UnityEditor.ShaderGraph
{
[Title("Procedural", "Shape", "Ellipse")]
class EllipseNode : CodeFunctionNode
{
public EllipseNode()
{
name = "Ellipse";
synonyms = new string[] { "circle" };
}
protected override MethodInfo GetFunctionToConvert()
{
return GetType().GetMethod("Unity_Ellipse", BindingFlags.Static | BindingFlags.NonPublic);
}
static string Unity_Ellipse(
[Slot(0, Binding.MeshUV0)] Vector2 UV,
[Slot(2, Binding.None, 0.5f, 0, 0, 0)] Vector1 Width,
[Slot(3, Binding.None, 0.5f, 0, 0, 0)] Vector1 Height,
[Slot(4, Binding.None, ShaderStageCapability.Fragment)] out Vector1 Out)
{
return
@"
{
#if defined(SHADER_STAGE_RAY_TRACING)
Out = saturate((1.0 - length((UV * 2 - 1) / $precision2(Width, Height))) * 1e7);
#else
$precision d = length((UV * 2 - 1) / $precision2(Width, Height));
Out = saturate((1 - d) / fwidth(d));
#endif
}";
}
}
}

View File

@@ -0,0 +1,12 @@
fileFormatVersion: 2
guid: c65ff5e45f92e3b498f3ec3aa63b9598
timeCreated: 1495550756
licenseType: Pro
MonoImporter:
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@@ -0,0 +1,48 @@
using System.Reflection;
using UnityEngine;
namespace UnityEditor.ShaderGraph
{
[Title("Procedural", "Shape", "Polygon")]
class PolygonNode : CodeFunctionNode
{
public PolygonNode()
{
name = "Polygon";
synonyms = new string[] { "shape" };
}
protected override MethodInfo GetFunctionToConvert()
{
return GetType().GetMethod("Unity_Polygon", BindingFlags.Static | BindingFlags.NonPublic);
}
static string Unity_Polygon(
[Slot(0, Binding.MeshUV0)] Vector2 UV,
[Slot(1, Binding.None, 6, 0, 0, 0)] Vector1 Sides,
[Slot(2, Binding.None, 0.5f, 0, 0, 0)] Vector1 Width,
[Slot(3, Binding.None, 0.5f, 0, 0, 0)] Vector1 Height,
[Slot(4, Binding.None, ShaderStageCapability.Fragment)] out DynamicDimensionVector Out)
{
return
@"
{
$precision pi = 3.14159265359;
$precision aWidth = Width * cos(pi / Sides);
$precision aHeight = Height * cos(pi / Sides);
$precision2 uv = (UV * 2 - 1) / $precision2(aWidth, aHeight);
uv.y *= -1;
$precision pCoord = atan2(uv.x, uv.y);
$precision r = 2 * pi / Sides;
$precision distance = cos(floor(0.5 + pCoord / r) * r - pCoord) * length(uv);
#if defined(SHADER_STAGE_RAY_TRACING)
Out = saturate((1.0 - distance) * 1e7);
#else
Out = saturate((1 - distance) / fwidth(distance));
#endif
}
";
}
}
}

View File

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

View File

@@ -0,0 +1,95 @@
using System.Reflection;
using UnityEngine;
using UnityEditor.Graphing;
using UnityEditor.ShaderGraph.Drawing.Controls;
namespace UnityEditor.ShaderGraph
{
enum ClampType
{
Fastest,
Nicest
};
[Title("Procedural", "Shape", "Rectangle")]
class RectangleNode : CodeFunctionNode
{
public RectangleNode()
{
name = "Rectangle";
synonyms = new string[] { "square" };
}
[SerializeField]
private ClampType m_ClampType = ClampType.Fastest;
[EnumControl("")]
public ClampType clampType
{
get { return m_ClampType; }
set
{
if (m_ClampType == value)
return;
m_ClampType = value;
Dirty(ModificationScope.Graph);
}
}
protected override MethodInfo GetFunctionToConvert()
{
switch (clampType)
{
case ClampType.Nicest:
return GetType().GetMethod("Unity_Rectangle_Nicest", BindingFlags.Static | BindingFlags.NonPublic);
case ClampType.Fastest:
default:
return GetType().GetMethod("Unity_Rectangle_Fastest", BindingFlags.Static | BindingFlags.NonPublic);
}
}
static string Unity_Rectangle_Fastest(
[Slot(0, Binding.MeshUV0)] Vector2 UV,
[Slot(1, Binding.None, 0.5f, 0, 0, 0)] Vector1 Width,
[Slot(2, Binding.None, 0.5f, 0, 0, 0)] Vector1 Height,
[Slot(3, Binding.None, ShaderStageCapability.Fragment)] out Vector1 Out)
{
return
@"
{
$precision2 d = abs(UV * 2 - 1) - $precision2(Width, Height);
#if defined(SHADER_STAGE_RAY_TRACING)
d = saturate((1 - saturate(d * 1e7)));
#else
d = saturate(1 - d / fwidth(d));
#endif
Out = min(d.x, d.y);
}";
}
static string Unity_Rectangle_Nicest(
[Slot(0, Binding.MeshUV0)] Vector2 UV,
[Slot(1, Binding.None, 0.5f, 0, 0, 0)] Vector1 Width,
[Slot(2, Binding.None, 0.5f, 0, 0, 0)] Vector1 Height,
[Slot(3, Binding.None, ShaderStageCapability.Fragment)] out Vector1 Out)
{
return
@"
{
UV = UV * 2.0 - 1.0;
$precision2 w = $precision2(Width, Height); // rectangle width/height
#if defined(SHADER_STAGE_RAY_TRACING)
$precision2 o = saturate(0.5f + 1e7 * (w - abs(UV)));
o = min(o, 1e7 * w * 2.0f);
#else
$precision2 f = min(fwidth(UV), 0.5f);
$precision2 k = 1.0f / f;
$precision2 o = saturate(0.5f + k * (w - abs(UV)));
o = min(o, k * w * 2.0f);
#endif
Out = o.x * o.y;
}";
}
}
}

View File

@@ -0,0 +1,12 @@
fileFormatVersion: 2
guid: 0f23589566e318c408af431d95bb492c
timeCreated: 1495565599
licenseType: Pro
MonoImporter:
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@@ -0,0 +1,91 @@
using System.Reflection;
using UnityEngine;
namespace UnityEditor.ShaderGraph
{
[Title("Procedural", "Shape", "Rounded Polygon")]
class RoundedPolygonNode : CodeFunctionNode
{
public RoundedPolygonNode()
{
name = "Rounded Polygon";
synonyms = new string[] { "shape" };
}
protected override MethodInfo GetFunctionToConvert()
{
return GetType().GetMethod("RoundedPolygon_Func", BindingFlags.Static | BindingFlags.NonPublic);
}
static string RoundedPolygon_Func(
[Slot(0, Binding.MeshUV0)] Vector2 UV,
[Slot(1, Binding.None, 0.5f, 0, 0, 0)] Vector1 Width,
[Slot(2, Binding.None, 0.5f, 0, 0, 0)] Vector1 Height,
[Slot(3, Binding.None, 5f, 0, 0, 0)] Vector1 Sides,
[Slot(4, Binding.None, 0.3f, 0, 0, 0)] Vector1 Roundness,
[Slot(5, Binding.None)] out Vector1 Out)
{
return
@"
{
UV = UV * 2. + $precision2(-1.,-1.);
$precision epsilon = 1e-6;
UV.x = UV.x / ( Width + (Width==0)*epsilon);
UV.y = UV.y / ( Height + (Height==0)*epsilon);
Roundness = clamp(Roundness, 1e-6, 1.);
$precision i_sides = floor( abs( Sides ) );
$precision fullAngle = 2. * PI / i_sides;
$precision halfAngle = fullAngle / 2.;
$precision opositeAngle = HALF_PI - halfAngle;
$precision diagonal = 1. / cos( halfAngle );
// Chamfer values
$precision chamferAngle = Roundness * halfAngle; // Angle taken by the chamfer
$precision remainingAngle = halfAngle - chamferAngle; // Angle that remains
$precision ratio = tan(remainingAngle) / tan(halfAngle); // This is the ratio between the length of the polygon's triangle and the distance of the chamfer center to the polygon center
// Center of the chamfer arc
$precision2 chamferCenter = $precision2(
cos(halfAngle) ,
sin(halfAngle)
)* ratio * diagonal;
// starting of the chamfer arc
$precision2 chamferOrigin = $precision2(
1.,
tan(remainingAngle)
);
// Using Al Kashi algebra, we determine:
// The distance distance of the center of the chamfer to the center of the polygon (side A)
$precision distA = length(chamferCenter);
// The radius of the chamfer (side B)
$precision distB = 1. - chamferCenter.x;
// The refence length of side C, which is the distance to the chamfer start
$precision distCref = length(chamferOrigin);
// This will rescale the chamfered polygon to fit the uv space
// diagonal = length(chamferCenter) + distB;
$precision uvScale = diagonal;
UV *= uvScale;
$precision2 polaruv = $precision2 (
atan2( UV.y, UV.x ),
length(UV)
);
polaruv.x += HALF_PI + 2*PI;
polaruv.x = fmod( polaruv.x + halfAngle, fullAngle );
polaruv.x = abs(polaruv.x - halfAngle);
UV = $precision2( cos(polaruv.x), sin(polaruv.x) ) * polaruv.y;
// Calculate the angle needed for the Al Kashi algebra
$precision angleRatio = 1. - (polaruv.x-remainingAngle) / chamferAngle;
// Calculate the distance of the polygon center to the chamfer extremity
$precision distC = sqrt( distA*distA + distB*distB - 2.*distA*distB*cos( PI - halfAngle * angleRatio ) );
Out = UV.x;
float chamferZone = ( halfAngle - polaruv.x ) < chamferAngle;
Out = lerp( UV.x, polaruv.y / distC, chamferZone );
// Output this to have the shape mask instead of the distance field
#if defined(SHADER_STAGE_RAY_TRACING)
Out = saturate((1 - Out) * 1e7);
#else
Out = saturate((1 - Out) / fwidth(Out));
#endif
}
";
}
}
}

View File

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

View File

@@ -0,0 +1,42 @@
using System.Reflection;
using UnityEngine;
namespace UnityEditor.ShaderGraph
{
[Title("Procedural", "Shape", "Rounded Rectangle")]
class RoundedRectangleNode : CodeFunctionNode
{
public RoundedRectangleNode()
{
name = "Rounded Rectangle";
synonyms = new string[] { "square" };
}
protected override MethodInfo GetFunctionToConvert()
{
return GetType().GetMethod("Unity_RoundedRectangle", BindingFlags.Static | BindingFlags.NonPublic);
}
static string Unity_RoundedRectangle(
[Slot(0, Binding.MeshUV0)] Vector2 UV,
[Slot(1, Binding.None, 0.5f, 0, 0, 0)] Vector1 Width,
[Slot(2, Binding.None, 0.5f, 0, 0, 0)] Vector1 Height,
[Slot(3, Binding.None, 0.1f, 0, 0, 0)] Vector1 Radius,
[Slot(4, Binding.None, ShaderStageCapability.Fragment)] out Vector1 Out)
{
return
@"
{
Radius = max(min(min(abs(Radius * 2), abs(Width)), abs(Height)), 1e-5);
$precision2 uv = abs(UV * 2 - 1) - $precision2(Width, Height) + Radius;
$precision d = length(max(0, uv)) / Radius;
#if defined(SHADER_STAGE_RAY_TRACING)
Out = saturate((1 - d) * 1e7);
#else
$precision fwd = max(fwidth(d), 1e-5);
Out = saturate((1 - d) / fwd);
#endif
}";
}
}
}

View File

@@ -0,0 +1,12 @@
fileFormatVersion: 2
guid: faba19d28a5fc1d4187c0124658f1822
timeCreated: 1495565599
licenseType: Pro
MonoImporter:
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant: