您最多选择25个主题
主题必须以中文或者字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符
77 行
2.1 KiB
77 行
2.1 KiB
using System.Reflection;
|
|
using UnityEngine;
|
|
|
|
namespace UnityEditor.ShaderGraph
|
|
{
|
|
[Title("Procedural/Noise")]
|
|
public class NoiseNode : CodeFunctionNode
|
|
{
|
|
public NoiseNode()
|
|
{
|
|
name = "Noise";
|
|
}
|
|
|
|
protected override MethodInfo GetFunctionToConvert()
|
|
{
|
|
return GetType().GetMethod("Unity_Noise", BindingFlags.Static | BindingFlags.NonPublic);
|
|
}
|
|
|
|
static string Unity_Noise(
|
|
[Slot(0, Binding.MeshUV0)] Vector2 UV,
|
|
[Slot(1, Binding.None,500f,500f,500f,500f)] Vector1 Scale,
|
|
[Slot(2, Binding.None)] out Vector1 Out)
|
|
{
|
|
return
|
|
@"
|
|
{
|
|
float t = 0.0;
|
|
for(int i = 0; i < 3; i++)
|
|
{
|
|
float freq = pow(2.0, float(i));
|
|
float amp = pow(0.5, float(3-i));
|
|
t += unity_valueNoise(float2(UV.x*Scale/freq, UV.y*Scale/freq))*amp;
|
|
}
|
|
Out = t;
|
|
}
|
|
";
|
|
}
|
|
|
|
public override void GenerateNodeFunction(ShaderGenerator visitor, GenerationMode generationMode)
|
|
{
|
|
string functionPreamble = @"
|
|
inline float unity_noise_randomValue (float2 uv)
|
|
{
|
|
return frac(sin(dot(uv, float2(12.9898, 78.233)))*43758.5453);
|
|
}
|
|
|
|
inline float unity_noise_interpolate (float a, float b, float t)
|
|
{
|
|
return (1.0-t)*a + (t*b);
|
|
}
|
|
|
|
inline float unity_valueNoise (float2 uv)
|
|
{
|
|
float2 i = floor(uv);
|
|
float2 f = frac(uv);
|
|
f = f * f * (3.0 - 2.0 * f);
|
|
|
|
uv = abs(frac(uv) - 0.5);
|
|
float2 c0 = i + float2(0.0, 0.0);
|
|
float2 c1 = i + float2(1.0, 0.0);
|
|
float2 c2 = i + float2(0.0, 1.0);
|
|
float2 c3 = i + float2(1.0, 1.0);
|
|
float r0 = unity_noise_randomValue(c0);
|
|
float r1 = unity_noise_randomValue(c1);
|
|
float r2 = unity_noise_randomValue(c2);
|
|
float r3 = unity_noise_randomValue(c3);
|
|
|
|
float bottomOfGrid = unity_noise_interpolate(r0, r1, f.x);
|
|
float topOfGrid = unity_noise_interpolate(r2, r3, f.x);
|
|
float t = unity_noise_interpolate(bottomOfGrid, topOfGrid, f.y);
|
|
return t;
|
|
}";
|
|
visitor.AddShaderChunk(functionPreamble, true);
|
|
base.GenerateNodeFunction(visitor, generationMode);
|
|
}
|
|
}
|
|
}
|