using System.Reflection; namespace UnityEngine.MaterialGraph { [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)] out Vector1 noise) { 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/freq, uv.y/freq))*amp; } noise = 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); } } }