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,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: