public override void GenerateNodeFunction(FunctionRegistry registry, GraphContext graphContext, GenerationMode generationMode)
    {
        registry.ProvideFunction("bounceOut", s => s.Append(@"
inline float bounceOut(float p)
{
    if(p < 4.0/11.0)
	{
		return (121.0 * p * p)/16.0;
	}
	else if(p < 8.0/11.0)
	{
		return (363.0/40.0 * p * p) - (99.0/10.0 * p) + 17.0/5.0;
	}
	else if(p < 9.0/10.0)
	{
		return (4356.0/361.0 * p * p) - (35442.0/1805.0 * p) + 16061.0/1805.0;
	}
	else
	{
		return (54.0/5.0 * p * p) - (513.0/25.0 * p) + 268.0/25.0;
    }
}"));
        registry.ProvideFunction("bounceIn", s => s.Append(@"
inline float bounceIn(float p)
{
    return 1.0 - bounceOut(1.0 - p);
}"));

        base.GenerateNodeFunction(registry, graphContext, generationMode);
    }
Esempio n. 2
0
    public override void GenerateNodeFunction(FunctionRegistry registry, GraphContext graphContext, GenerationMode generationMode)
    {
        registry.ProvideFunction("distance_func", s => s.Append(@"
            // 距離関数: 点pからオブジェクトまでの距離を求める
            #define INTERVAL interval
            float distance_func(float3 p, float size, float interval) {
                p = frac(p / INTERVAL) * INTERVAL - INTERVAL / 2.0; // -INTERVAL/2.0 ~ +INTERVAL/2.0 の繰り返しを作る
                return length(p) - size;
            }
        "));

        registry.ProvideFunction("getNormal", s => s.Append(@"
            // 法線の計算
            float3 getNormal(float3 p, float size, float interval) {
                float2 e = float2(0.0001, 0.0);
                return normalize(float3(
                    distance_func(p + e.xyy, size, interval) - distance_func(p - e.xyy, size, interval),
                    distance_func(p + e.yxy, size, interval) - distance_func(p - e.yxy, size, interval),
                    distance_func(p + e.yyx, size, interval) - distance_func(p - e.yyx, size, interval)
                ));
            }
        "));


        base.GenerateNodeFunction(registry, graphContext, generationMode);
    }
Esempio n. 3
0
    public override void GenerateNodeFunction(FunctionRegistry registry, GraphContext graphContext, GenerationMode generationMode)
    {
        registry.ProvideFunction("include_instancing_pragmas", s => s.Append(@"

    #pragma instancing_options renderinglayer procedural:setupVSPro

    #ifdef UNITY_PROCEDURAL_INSTANCING_ENABLED
        struct IndirectShaderData
        {
	        float4x4 PositionMatrix;
	        float4x4 InversePositionMatrix;
	        float4 ControlData;
        };

        #if defined(SHADER_API_GLCORE) || defined(SHADER_API_D3D11) || defined(SHADER_API_GLES3) || defined(SHADER_API_METAL) || defined(SHADER_API_VULKAN) || defined(SHADER_API_PSSL) || defined(SHADER_API_XBOXONE)
            uniform StructuredBuffer<IndirectShaderData> VisibleShaderDataBuffer;
	    #endif	
    #endif

    void setupVSPro()
    {    

    #ifdef UNITY_PROCEDURAL_INSTANCING_ENABLED
	        unity_LODFade = VisibleShaderDataBuffer[unity_InstanceID].ControlData;
	        unity_ObjectToWorld = VisibleShaderDataBuffer[unity_InstanceID].PositionMatrix;
		    unity_WorldToObject = VisibleShaderDataBuffer[unity_InstanceID].InversePositionMatrix;
    #endif
    }

    "));

        base.GenerateNodeFunction(registry, graphContext, generationMode);
    }
Esempio n. 4
0
    public override void GenerateNodeFunction(FunctionRegistry registry, GenerationMode generationMode)
    {
        registry.ProvideFunction("easing_custom_bounce_out", s => s.Append(@"
            float easing_custom_bounce_out(float t)
            {
                const float a = 4.0 / 11.0;
                const float b = 8.0 / 11.0;
                const float c = 9.0 / 10.0;

                const float ca = 4356.0 / 361.0;
                const float cb = 35442.0 / 1805.0;
                const float cc = 16061.0 / 1805.0;
            
                float t2 = t * t;

                return t < a
                ? 7.5625 * t2
                : t < b
                    ? 9.075 * t2 - 9.9 * t + 3.4
                    : t < c
                    ? ca * t2 - cb * t + cc
                    : 10.8 * t * t - 20.52 * t + 10.72;
            }
        "));

        base.GenerateNodeFunction(registry, generationMode);
    }
        public static void PeriodicRidgeFunction(FunctionRegistry registry, string noiseFunctionName, string dimensionTypeName = "float2")
        {
            registry.ProvideFunction(GetRidgeFunctionName(noiseFunctionName), s => s.Append(@"
inline float " + GetRidgeFunctionName(noiseFunctionName) + @"(" + dimensionTypeName + @" uv, int Period, float persistence, float lacunarity, float sharpness)
{
    float currentPersistence = persistence;
    " + dimensionTypeName + @" currentUV = uv; 
    float ret = 0.0;
    int p = pow(2, Period);
    for (uint i = 0; i < 4; i++)
    {
        // create creases
        float n = saturate(abs(" + noiseFunctionName + @"(currentUV, p)));
        // invert so creases are at top
        n = 1.0 - n + 0.0001f;
        // sharpen creases
        n = pow(n, sharpness);
        ret += currentPersistence * n;
        currentPersistence *= persistence;
        currentUV *= lacunarity;
        p *= 2;
    }

    return ret;
}"));
        }
Esempio n. 6
0
        public void GenerateNodeFunction(FunctionRegistry registry, GenerationMode generationMode)
        {
            // We may need ConvertEvToLuminance() so we include CommonLighting.hlsl
            registry.RequiresIncludePath("Packages/com.unity.render-pipelines.core/ShaderLibrary/CommonLighting.hlsl");

            registry.ProvideFunction(GetFunctionName(), s =>
            {
                s.AppendLine("$precision3 {0}($precision3 ldrColor, {1} luminanceIntensity, {1} exposureWeight)",
                             GetFunctionName(),
                             intensitySlot.concreteValueType.ToShaderString());
                using (s.BlockScope())
                {
                    if (normalizeColor.isOn)
                    {
                        s.AppendLine("ldrColor = ldrColor * rcp(max(Luminance(ldrColor), 1e-6));");
                    }
                    s.AppendLine("$precision3 hdrColor = ldrColor * luminanceIntensity;");
                    s.AppendNewLine();
                    s.AppendLine("#ifdef SHADERGRAPH_PREVIEW");
                    s.AppendLine($"$precision inverseExposureMultiplier = 1.0;");
                    s.AppendLine("#else");
                    s.AppendLine($"$precision inverseExposureMultiplier = GetInverseCurrentExposureMultiplier();");
                    s.AppendLine("#endif");
                    s.AppendNewLine();

                    s.AppendLine("// Inverse pre-expose using _EmissiveExposureWeight weight");
                    s.AppendLine("hdrColor = lerp(hdrColor * inverseExposureMultiplier, hdrColor, exposureWeight);");
                    s.AppendLine("return hdrColor;");
                }
            });
        }
Esempio n. 7
0
        public void GenerateNodeFunction(FunctionRegistry registry, GraphContext graphContext, GenerationMode generationMode)
        {
            registry.ProvideFunction(GetFunctionName(), s =>
            {
                // We may need ConvertEvToLuminance() so we include CommonLighting.hlsl
                s.AppendLine("#include \"Assets/HDRP/com.unity.render-pipelines.core/ShaderLibrary/CommonLighting.hlsl\"");

                s.AppendLine("{1}3 {0}({1}3 ldrColor, {2} luminanceIntensity, {2} exposureWeight, {2} inverseCurrentExposureMultiplier)",
                             GetFunctionName(),
                             precision,
                             intensitySlot.concreteValueType.ToString(precision));
                using (s.BlockScope())
                {
                    if (normalizeColor.isOn)
                    {
                        s.AppendLine("ldrColor = ldrColor * rcp(max(Luminance(ldrColor), 1e-6));");
                    }
                    s.AppendLine("{0}3 hdrColor = ldrColor * luminanceIntensity;", precision);
                    s.AppendNewLine();
                    s.AppendLine("// Inverse pre-expose using _EmissiveExposureWeight weight");
                    s.AppendLine("hdrColor = lerp(hdrColor * inverseCurrentExposureMultiplier, hdrColor, exposureWeight);", precision);
                    s.AppendLine("return hdrColor;");
                }
            });
        }
        public void GenerateNodeFunction(FunctionRegistry registry, GenerationMode generationMode)
        {
            registry.ProvideFunction(GetFunctionName(), s =>
            {
                s.AppendLine("#ifndef LIGHTWEIGHT_SHADOWS_INCLUDED");
                s.Append(@"
float4 ComputeShadowCoord(float4 clipPos)
{
    return (0.0).xxxx;
}

half RealtimeShadowAttenuation(float4 shadowCoord)
{
    return 1.0h;
}
");
                s.AppendLine("#endif");
                s.AppendLine("void {0}({1}3 WorldSpacePosition, out {2} Out)",
                             GetFunctionName(),
                             precision,
                             FindOutputSlot <MaterialSlot>(OutputSlotId).concreteValueType.ToString(precision));
                using (s.BlockScope())
                {
                    s.AppendLine("Out = RealtimeShadowAttenuation(ComputeShadowCoord(TransformWorldToHClip(WorldSpacePosition)));");
                }
            });
        }
        public static void RandomValue2dTo1dFunction(FunctionRegistry registry)
        {
            registry.ProvideFunction("SGE_RandomValue2dTo1d", s => s.Append(@"
inline float SGE_RandomValue2dTo1d(float2 uv)
{
    return frac(sin(dot(uv, float2(12.9898, 78.233)))*43758.5453) * 2.0 - 1.0;
}"));
        }
Esempio n. 10
0
    public override void GenerateNodeFunction(FunctionRegistry registry, GraphContext graphContext, GenerationMode generationMode)
    {
        registry.ProvideFunction("fmod", s => s.Append(@"
float fmod(float a, float b)
{
    return a - floor(a / b) * b;
}
        "));

        base.GenerateNodeFunction(registry, graphContext, generationMode);
    }
        //
        // Noise Shader Library for Unity - https://github.com/keijiro/NoiseShader
        //
        // Original work (webgl-noise) Copyright (C) 2011 Stefan Gustavson
        // Translation and modification was made by Keijiro Takahashi.
        //
        // This shader is based on the webgl-noise GLSL shader. For further details
        // of the original shader, please see the following description from the
        // original source code.
        //

        //
        // GLSL textureless classic 2D noise "cnoise",
        // with an RSL-style periodic variant "pnoise".
        // Author:  Stefan Gustavson ([email protected])
        // Version: 2011-08-22
        //
        // Many thanks to Ian McEwan of Ashima Arts for the
        // ideas for permutation and gradient selection.
        //
        // Copyright (c) 2011 Stefan Gustavson. All rights reserved.
        // Distributed under the MIT license. See LICENSE file.
        // https://github.com/ashima/webgl-noise

        public static void PerlinNoiseHelperFunction(FunctionRegistry registry)
        {
            registry.ProvideFunction("SGE_Mod_F4", s => s.Append(@"
inline float4 SGE_Mod(float4 x, float4 y)
{
    return x - y * floor(x / y);
}"));

            registry.ProvideFunction("SGE_Mod_F3", s => s.Append(@"
inline float3 SGE_Mod(float3 x, float3 y)
{
    return x - y * floor(x / y);
}"));

            registry.ProvideFunction("SGE_Mod289_F4", s => s.Append(@"
inline float4 SGE_Mod289(float4 x)
{
    return x - floor(x / 289.0) * 289.0;
}"));

            registry.ProvideFunction("SGE_Mod289_F3", s => s.Append(@"
inline float3 SGE_Mod289(float3 x)
{
    return x - floor(x / 289.0) * 289.0;
}"));

            registry.ProvideFunction("SGE_Permute", s => s.Append(@"
inline float4 SGE_Permute(float4 x)
{
    return SGE_Mod289(((x*34.0)+1.0)*x);
}"));

            registry.ProvideFunction("SGE_TaylorInvSqrt", s => s.Append(@"
inline float4 SGE_TaylorInvSqrt(float4 r)
{
    return (float4)1.79284291400159 - r * 0.85373472095314;
}"));

            registry.ProvideFunction("SGE_Fade_F2", s => s.Append(@"
inline float2 SGE_Fade(float2 t)
{
    return t*t*t*(t*(t*6.0-15.0)+10.0);
}"));

            registry.ProvideFunction("SGE_Fade_F3", s => s.Append(@"
inline float3 SGE_Fade(float3 t)
{ 
    return t*t*t*(t*(t*6.0-15.0)+10.0);
}"));
        }
        public static void RandomValue2dTo2dFunction(FunctionRegistry registry)
        {
            registry.ProvideFunction("SGE_RandomValue2dTo2d", s => s.Append(@"
inline float2 SGE_RandomValue2dTo2d(float2 p)
{
    // Permutation and hashing used in webgl-nosie goo.gl/pX7HtC
    p = p % 289;
    float x = (34 * p.x + 1) * p.x % 289 + p.y;
    x = (34 * x + 1) * x % 289;
    x = frac(x / 41) * 2 - 1;
    return normalize(float2(x - floor(x + 0.5), abs(x) - 0.5));
}"));
        }
Esempio n. 13
0
        public override void GenerateNodeFunction(FunctionRegistry registry, GraphContext graphContext, GenerationMode generationMode)
        {
            registry.ProvideFunction("jick_dither", s => s.Append(@"
half jick_dither( half a, half2 screenPos, bool jitter )
{
	half2 uv = screenPos.xy * _ScreenParams.xy;
	if ( jitter ) uv += uint( _FrameCount ) % 4;
	half DITHER_THRESHOLDS[16] = {1.0/17.0,9.0/17.0,3.0/17.0,11.0/17.0,13.0/17.0,5.0/17.0,15.0/17.0,7.0/17.0,4.0/17.0,12.0/17.0,2.0/17.0,10.0/17.0,16.0/17.0,8.0/17.0,14.0/17.0,6.0/17.0};
	return a - DITHER_THRESHOLDS[ ( ( uint( uv.x ) % 4 ) * 4 + uint( uv.y ) % 4 ) ];
}
"));

            base.GenerateNodeFunction(registry, graphContext, generationMode);
        }
        public static void GradientNoiseFunction(FunctionRegistry registry)
        {
            registry.ProvideFunction(GradientNoiseFunctionName, s => s.Append(@"
inline float " + GradientNoiseFunctionName + @"(float2 uv)
{
    float2 i = floor(uv);
    float2 f = frac(uv);
    float2 u = f*f*(3.0-2.0*f);

    return lerp(lerp(dot(SGE_RandomValue2dTo2d(i), f),
                     dot(SGE_RandomValue2dTo2d(i + float2(1.0, 0.0)), f - float2(1.0,0.0)), u.x),
                lerp(dot(SGE_RandomValue2dTo2d(i + float2(0.0, 1.0)), f - float2(0.0,1.0)),
                     dot(SGE_RandomValue2dTo2d(i + float2(1.0, 1.0)), f - float2(1.0,1.0)), u.x), u.y);
}"));
        }
        public static void PeriodicValueNoiseFunction(FunctionRegistry registry)
        {
            registry.ProvideFunction(PeriodicValueNoiseFunctionName, s => s.Append(@"
inline float " + PeriodicValueNoiseFunctionName + @"(float2 uv, int p)
{
    float2 rescaledUv = uv * p;
    int2 i = floor(rescaledUv);
    float2 f = frac(rescaledUv);
    float2 u = f*f*(3.0-2.0*f);

    return lerp(lerp(SGE_RandomValue2dTo1d(i &(p - 1)),
                     SGE_RandomValue2dTo1d((i + int2(1, 0)) &(p - 1)), u.x),
                lerp(SGE_RandomValue2dTo1d((i + int2(0, 1)) &(p - 1)),
                     SGE_RandomValue2dTo1d((i + int2(1, 1)) &(p - 1)), u.x), u.y);
}"));
        }
Esempio n. 16
0
        public void GenerateNodeFunction(FunctionRegistry registry, GenerationMode generationMode)
        {
            registry.RequiresIncludePath("Packages/com.unity.render-pipelines.core/ShaderLibrary/BSDF.hlsl");

            string resultType = GetResultType();

            registry.ProvideFunction(GetFunctionName(), s =>
            {
                if (m_FresnelEquationMode == FresnelEquationMode.Schlick)
                {
                    s.AppendLine("void {0}(out {1} FresnelValue, $precision cos0, {1} f0)", GetFunctionName(), resultType, resultType);
                }
                else if (m_FresnelEquationMode == FresnelEquationMode.Dielectric)
                {
                    s.AppendLine("void {0}(out {1} FresnelValue, $precision cos0, {2} iorSource, {3} iorMedium)",
                                 GetFunctionName(),
                                 resultType,
                                 iorSourceInputSlot.concreteValueType.ToShaderString(),
                                 iorMediumInputSlot.concreteValueType.ToShaderString());
                }
                else //if (m_FresnelEquationMode == FresnelEquationMode.DielectricGeneric)
                {
                    s.AppendLine("void {0}(out {1} FresnelValue, $precision cos0, {2} iorSource, {3} iorMedium, {4} iorMediumK)",
                                 GetFunctionName(),
                                 resultType,
                                 iorSourceInputSlot.concreteValueType.ToShaderString(),
                                 iorMediumInputSlot.concreteValueType.ToShaderString(),
                                 iorMediumKInputSlot.concreteValueType.ToShaderString());
                }
                using (s.BlockScope())
                {
                    if (m_FresnelEquationMode == FresnelEquationMode.Schlick)
                    {
                        s.AppendLine("FresnelValue = F_Schlick(f0, cos0);");
                    }
                    else if (m_FresnelEquationMode == FresnelEquationMode.Dielectric)
                    {
                        s.AppendLine("FresnelValue = F_FresnelDielectric(iorMedium/iorSource, cos0);");
                    }
                    else //if (m_FresnelEquationMode == FresnelEquationMode.DielectricGeneric)
                    {
                        s.AppendLine("FresnelValue = F_FresnelConductor(iorMedium/iorSource, iorMediumK/iorSource, cos0);");
                    }
                }
            });
        }
        public static void PeriodicPerlinNoiseFunction(FunctionRegistry registry)
        {
            PerlinNoiseHelperFunction(registry);

            registry.ProvideFunction(PeriodicPerlinNoiseFunctionName, s => s.Append(@"
inline float " + PeriodicPerlinNoiseFunctionName + @"(float2 uv, float2 period)
{
      float2 rescaledUv = uv * period;
      float4 Pi = floor(rescaledUv.xyxy) + float4(0.0, 0.0, 1.0, 1.0);
      float4 Pf = frac (rescaledUv.xyxy) - float4(0.0, 0.0, 1.0, 1.0);
      Pi = SGE_Mod(Pi, period.xyxy); // To create noise with explicit period
      Pi = SGE_Mod289(Pi);        // To avoid truncation effects in permutation
      float4 ix = Pi.xzxz;
      float4 iy = Pi.yyww;
      float4 fx = Pf.xzxz;
      float4 fy = Pf.yyww;

      float4 i = SGE_Permute(SGE_Permute(ix) + iy);

      float4 gx = frac(i / 41.0) * 2.0 - 1.0 ;
      float4 gy = abs(gx) - 0.5 ;
      float4 tx = floor(gx + 0.5);
      gx = gx - tx;

      float2 g00 = float2(gx.x,gy.x);
      float2 g10 = float2(gx.y,gy.y);
      float2 g01 = float2(gx.z,gy.z);
      float2 g11 = float2(gx.w,gy.w);

      float4 norm = SGE_TaylorInvSqrt(float4(dot(g00, g00), dot(g01, g01), dot(g10, g10), dot(g11, g11)));
      g00 *= norm.x;
      g01 *= norm.y;
      g10 *= norm.z;
      g11 *= norm.w;

      float n00 = dot(g00, float2(fx.x, fy.x));
      float n10 = dot(g10, float2(fx.y, fy.y));
      float n01 = dot(g01, float2(fx.z, fy.z));
      float n11 = dot(g11, float2(fx.w, fy.w));

      float2 fade_xy = SGE_Fade(Pf.xy);
      float2 n_x = lerp(float2(n00, n01), float2(n10, n11), fade_xy.x);
      float n_xy = lerp(n_x.x, n_x.y, fade_xy.y);
      return 2.3 * n_xy;
}"));
        }
        public static void TurbulenceFunction(FunctionRegistry registry, string noiseFunctionName, string dimensionTypeName = "float2")
        {
            registry.ProvideFunction(GetTurbulenceFunctionName(noiseFunctionName), s => s.Append(@"
inline float " + GetTurbulenceFunctionName(noiseFunctionName) + @"(" + dimensionTypeName + @" uv, float persistence, float lacunarity)
{
    float currentPersistence = persistence;
    " + dimensionTypeName + @" currentUV = uv; 
    float ret = 0.0;
    for (uint i = 0; i < 4; i++)
    {
        ret += currentPersistence * abs(" + noiseFunctionName + @"(currentUV));
        currentPersistence *= persistence;
        currentUV *= lacunarity;
    }

    return ret;
}"));
        }
Esempio n. 19
0
 public void GenerateNodeFunction(FunctionRegistry registry, GraphContext graphContext, GenerationMode generationMode)
 {
     registry.ProvideFunction(GetFunctionName(), s =>
     {
         s.AppendLine("{1}3 {0}({1}3 ldrColor, {2} luminanceIntensity, {2} exposureWeight, {2} inverseCurrentExposureMultiplier)",
                      GetFunctionName(),
                      precision,
                      intensitySlot.concreteValueType.ToString(precision));
         using (s.BlockScope())
         {
             s.AppendLine("{0}3 hdrColor = ldrColor * luminanceIntensity;", precision);
             s.AppendNewLine();
             s.AppendLine("// Inverse pre-expose using _EmissiveExposureWeight weight");
             s.AppendLine("hdrColor = lerp(hdrColor * inverseCurrentExposureMultiplier, hdrColor, exposureWeight);", precision);
             s.AppendLine("return hdrColor;");
         }
     });
 }
        public static void PeriodicFractalFunction(FunctionRegistry registry, string noiseFunctionName, string dimensionTypeName = "float2")
        {
            registry.ProvideFunction(GetFractalFunctionName(noiseFunctionName), s => s.Append(@"
inline float " + GetFractalFunctionName(noiseFunctionName) + @"(" + dimensionTypeName + @" uv, int Period, float persistence, float lacunarity)
{
    float currentPersistence = persistence;
    " + dimensionTypeName + @" currentUV = uv; 
    float ret = 0.0;
    int p = pow(2, Period);
    for (uint i = 0; i < 4; i++)
    {
        ret += currentPersistence * " + noiseFunctionName + @"(currentUV, p);
        currentPersistence *= persistence;
        currentUV *= lacunarity;
        p *= 2;
    }

    return ret;
}"));
        }
        public void GenerateNodeFunction(FunctionRegistry registry, GraphContext graphContext, GenerationMode generationMode)
        {
            string perPixelDisplacementInclude = @"#include ""Packages/com.unity.render-pipelines.core/ShaderLibrary/PerPixelDisplacement.hlsl""";

            // Texture sample inputs
            var samplerSlot  = FindInputSlot <MaterialSlot>(kHeightmapSamplerSlotId);
            var edgesSampler = owner.GetEdges(samplerSlot.slotReference);
            var heightmap    = GetSlotValue(kHeightmapSlotId, generationMode);

            registry.ProvideFunction(GetFunctionName(), s =>
            {
                s.AppendLine("$precision3 GetDisplacementObjectScale()");
                using (s.BlockScope())
                {
                    s.AppendLines(@"
float3 objectScale = float3(1.0, 1.0, 1.0);
float4x4 worldTransform = GetWorldToObjectMatrix();

objectScale.x = length(float3(worldTransform._m00, worldTransform._m01, worldTransform._m02));
objectScale.z = length(float3(worldTransform._m20, worldTransform._m21, worldTransform._m22));

return objectScale;");
                }

                s.AppendLine("// Required struct and function for the ParallaxOcclusionMapping function:");
                s.AppendLine("struct PerPixelHeightDisplacementParam");
                using (s.BlockSemicolonScope())
                {
                    s.AppendLine("$precision2 uv;");
                }
                s.AppendLine("$precision ComputePerPixelHeightDisplacement($precision2 texOffsetCurrent, $precision lod, PerPixelHeightDisplacementParam param)");
                using (s.BlockScope())
                {
                    s.AppendLine("return SAMPLE_TEXTURE2D_LOD({0}, {1}, param.uv + texOffsetCurrent, lod).r;",
                                 heightmap,
                                 edgesSampler.Any() ? GetSlotValue(kHeightmapSamplerSlotId, generationMode) : "sampler" + heightmap);
                }
                s.Append(perPixelDisplacementInclude);
            });
        }
Esempio n. 22
0
        public void GenerateNodeFunction(FunctionRegistry registry, GenerationMode generationMode)
        {
            registry.RequiresIncludePath("Packages/com.unity.render-pipelines.core/ShaderLibrary/BSDF.hlsl");

            registry.ProvideFunction(GetFunctionName(), s =>
            {
                s.AppendLine("void {0}(out $precision3 Refracted, out $precision3 Intensity, $precision3 Incident, $precision3 Normal, $precision IORSource, $precision IORMedium)", GetFunctionName());
                using (s.BlockScope())
                {
                    s.AppendLine("$precision internalIORSource = max(IORSource, 1.0);");
                    s.AppendLine("$precision internalIORMedium = max(IORMedium, 1.0);");
                    s.AppendLine("$precision eta = internalIORSource/internalIORMedium;");
                    s.AppendLine("$precision cos0 = dot(Incident, Normal);");
                    s.AppendLine("$precision k = 1.0 - eta*eta*(1.0 - cos0*cos0);");
                    if (m_RefractMode == RefractMode.Safe)
                    {
                        s.AppendLine("Refracted = eta*Incident - (eta*cos0 + sqrt(max(k, 0.0)))*Normal;");
                    }
                    else
                    {
                        s.AppendLine("Refracted = k >= 0.0 ? eta*Incident - (eta*cos0 + sqrt(k))*Normal : reflect(Incident, Normal);");
                    }
                    s.AppendLine("Intensity = internalIORSource <= internalIORMedium ?");
                    s.AppendLine("    saturate(F_Transm_Schlick(IorToFresnel0(internalIORMedium, internalIORSource), -cos0)) :");
                    s.AppendLine("    (k >= 0.0 ? F_FresnelDielectric(internalIORMedium/internalIORSource, -cos0) : ");
                    if (m_RefractMode == RefractMode.Safe)
                    {
                        s.Append("0.0);");
                    }
                    else
                    {
                        s.Append("1.0);");
                    }
                }
            });
        }
        // from https://www.shadertoy.com/view/Msf3WH
        // The MIT License
        // Copyright © 2013 Inigo Quilez
        // Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
        public static void SimplexNoiseFunction(FunctionRegistry registry)
        {
            registry.ProvideFunction(SimplexNoiseFunctionName, s => s.Append(@"
inline float " + SimplexNoiseFunctionName + @"(float2 uv)
{
    const float K1 = 0.366025404; // (sqrt(3) - 1)/ 2;
    const float K2 = 0.211324865; // (3 - sqrt(3)) / 6;

	float2 i = floor(uv + (uv.x + uv.y) * K1);
	
    float2 a = uv - i + (i.x + i.y) * K2;
    float2 o = step(a.yx, a.xy);    
    float2 b = a - o + K2;
	float2 c = a - 1.0 + 2.0 * K2;

    float3 h = max(0.5 - float3(dot(a, a), dot(b, b), dot(c, c)), 0.0);

	float3 n = h * h * h * h * float3(dot(a, SGE_RandomValue2dTo2d(i + 0.0)),
        dot(b, SGE_RandomValue2dTo2d(i + o)),
        dot(c, SGE_RandomValue2dTo2d(i + 1.0)));

    return dot(n, float3(70.0, 70.0, 70.0));
}"));
        }
Esempio n. 24
0
        // generate the node's function
        public void GenerateNodeFunction(FunctionRegistry registry, GenerationMode generationMode)
        {
            bool   isNoiseSupported = IsNoiseSupported();
            string functionName     = GetFunctionName();

            if (isNoiseSupported)
            {
                Noise.RegisterFunctions(registry);
            }

            registry.ProvideFunction(functionName, s =>
            {
                var uvSlot     = FindInputSlot <MaterialSlot>(UvSlotId);
                var outputSlot = FindOutputSlot <MaterialSlot>(OutputSlotId);

                if (uvSlot == null)
                {
                    throw new NullReferenceException("UvSlot null, how is it possible ?");
                }
                if (outputSlot == null)
                {
                    throw new NullReferenceException("outputSlot null, how is it possible ?");
                }

                s.Append("void {0}({1} {2}, out {3} {4}",
                         functionName,
                         uvSlot.concreteValueType.ToShaderString(),
                         kUvSlotName,
                         outputSlot.concreteValueType.ToShaderString(),
                         kOutputSlotName);
                if (isNoiseSupported)
                {
                    if (m_noisePeriodicity == NoisePeriodicity.Periodic)
                    {
                        var periodSlot = FindInputSlot <MaterialSlot>(PeriodSlotId);

                        if (periodSlot == null)
                        {
                            throw new NullReferenceException("periodSlot null, how is it possible ?");
                        }

                        s.Append(", {0} {1}",
                                 periodSlot.concreteValueType.ToShaderString(),
                                 kPeriodSlotName);
                    }
                    if (m_noiseCombine == NoiseCombine.Fractal || m_noiseCombine == NoiseCombine.Turbulence || m_noiseCombine == NoiseCombine.Ridge)
                    {
                        var persistenceSlot = FindInputSlot <MaterialSlot>(PersistenceSlotId);
                        var lacunaritySlot  = FindInputSlot <MaterialSlot>(LacunaritySlotId);

                        if (persistenceSlot == null)
                        {
                            throw new NullReferenceException("persistenceSlot null, how is it possible ?");
                        }
                        if (lacunaritySlot == null)
                        {
                            throw new NullReferenceException("lacunaritySlot null, how is it possible ?");
                        }

                        s.Append(", {0} {1}, {2} {3}",
                                 persistenceSlot.concreteValueType.ToShaderString(),
                                 kPersistenceSlotName,
                                 lacunaritySlot.concreteValueType.ToShaderString(),
                                 kLacunaritySlotName);
                    }

                    if (m_noiseCombine == NoiseCombine.Ridge)
                    {
                        var sharpnessSlot = FindInputSlot <MaterialSlot>(SharpnessSlotId);

                        if (sharpnessSlot == null)
                        {
                            throw new NullReferenceException("sharpnessSlot null, how is it possible ?");
                        }

                        s.Append(", {0} {1}",
                                 sharpnessSlot.concreteValueType.ToShaderString(),
                                 kSharpnessSlotName);
                    }
                }

                s.AppendLine(")");

                using (s.BlockScope())
                {
                    if (isNoiseSupported)
                    {
                        s.Append("{0} = {1}({2}", kOutputSlotName, Noise.GetNoiseFunctionName(), kUvSlotName);

                        if (m_noisePeriodicity == NoisePeriodicity.Periodic)
                        {
                            s.Append(", pow(2, floor({0}))", kPeriodSlotName);
                        }

                        if (m_noiseCombine == NoiseCombine.Fractal || m_noiseCombine == NoiseCombine.Turbulence || m_noiseCombine == NoiseCombine.Ridge)
                        {
                            s.Append(", {0}, {1}", kPersistenceSlotName, kLacunaritySlotName);
                        }

                        if (m_noiseCombine == NoiseCombine.Ridge)
                        {
                            s.Append(", {0}", kSharpnessSlotName);
                        }

                        s.Append(")");

                        if (Noise.NeedRemapTo0_1())
                        {
                            s.Append(" * 0.5 + 0.5");
                        }
                        s.AppendLine(";");
                    }
                    else
                    {
                        s.AppendLine("{0} = {1};", kOutputSlotName, kUvSlotName);
                    }
                }
            });
        }
Esempio n. 25
0
    public override void GenerateNodeFunction(FunctionRegistry registry, GraphContext graphContext, GenerationMode generationMode)
    {
        registry.ProvideFunction("random", s => s.Append(@"
            float random(float2 st) {
                return frac(sin(dot(st.xy,
                                    float2(12.9898,78.233)))*
                            43758.5453123);
            }
        "));
        registry.ProvideFunction("noise", s => s.Append(@"
            // Based on Morgan McGuire @morgan3d
            // https://www.shadertoy.com/view/4dS3Wd
            float noise (float2 st) {
                float2 i = floor(st);
                float2 f = frac(st);

                // Four corners in 2D of a tile
                float a = random(i);
                float b = random(i + float2(1.0, 0.0));
                float c = random(i + float2(0.0, 1.0));
                float d = random(i + float2(1.0, 1.0));

                float2 u = f * f * (3.0 - 2.0 * f);

                return lerp(a, b, u.x) +
                        (c - a)* u.y * (1.0 - u.x) +
                        (d - b) * u.x * u.y;
            }
        "));
        registry.ProvideFunction("fbm", s => s.Append(@"
            #define OCTAVES 6
            // based on : https://thebookofshaders.com/13/?lan=jp
            float fbm (float2 st) {
                // Initial values
                float value = 0.0;
                float amplitude = .5;
                float frequency = 0.;
                // Loop of octaves
                for (int i = 0; i < OCTAVES; i++) {
                    value += amplitude * noise(st);
                    st *= 2.;
                    amplitude *= .5;
                }
                return value;
            }
        "));

        registry.ProvideFunction("pattern_1", s => s.Append(@"
            float pattern_1 (float2 p) {
                return fbm(p);
            }
        "));

        registry.ProvideFunction("pattern_2", s => s.Append(@"
            float pattern_2 (float2 p) {
                float2 q = float2( 
                                fbm( p + float2(0.0,0.0) ),
                                fbm( p + float2(5.2,1.3) ) 
                                );

                return fbm( p + 4.0*q );
            }
        "));

        registry.ProvideFunction("pattern_3", s => s.Append(@"
            float pattern_3 (float2 p) {
                // first domain warping
                float2 q = float2( 
                                fbm( p + float2(0.0,0.0) ),
                                fbm( p + float2(5.2,1.3) ) 
                                );
                            
                // second domain warping
                float2 r = float2( 
                                fbm( p + 4.0*q + float2(1.7,9.2) ),
                                fbm( p + 4.0*q + float2(8.3,2.8) ) 
                                );

                return fbm( p + 4.0*r );
            }
        "));

        registry.ProvideFunction("pattern", s => s.Append(@"
            float pattern (float2 p, float4 scale_1, float scale_2, float4 add_1, float4 add_2) {
                // first domain warping
                float2 q = float2( 
                                fbm( p + scale_1.x * add_1.xy ),
                                fbm( p + scale_1.y * add_1.zw ) 
                                );
                            
                // second domain warping
                float2 r = float2( 
                                fbm( p + scale_1.z * q + add_2.xy ),
                                fbm( p + scale_1.w * q + add_2.zw ) 
                                );

                return fbm( p + scale_2 * r );
            }
        "));

        base.GenerateNodeFunction(registry, graphContext, generationMode);
    }
Esempio n. 26
0
    public override void GenerateNodeFunction(FunctionRegistry registry, GraphContext graphContext, GenerationMode generationMode)
    {
        registry.ProvideFunction("mod", s => s.Append(@"
            float3 mod(float3 x, float y) { return x - y * floor(x/y); }
        "));

        registry.ProvideFunction("Permutation", s => s.Append(@"
            float3 Permutation(float3 x) 
            {
              return mod((34.0 * x + 1.0) * x, 289.0);
            }
        "));

        registry.ProvideFunction("inoise", s => s.Append(@"
            float2 inoise(float3 P, float jitter)
            {			
             float3 Pi = mod(floor(P), 289.0);
              float3 Pf = frac(P);
             float3 oi = float3(-1.0, 0.0, 1.0);
             float3 of = float3(-0.5, 0.5, 1.5);
             float3 px = Permutation(Pi.x + oi);
             float3 py = Permutation(Pi.y + oi);

             float3 p, ox, oy, oz, dx, dy, dz;
             float2 F = 1e6;

             for(int i = 0; i < 3; i++)
             {
              for(int j = 0; j < 3; j++)
              {
               p = Permutation(px[i] + py[j] + Pi.z + oi); // pij1, pij2, pij3

               ox = frac(p*0.142857142857) - 0.428571428571;
               oy = mod(floor(p*0.142857142857),7.0)*0.142857142857 - 0.428571428571;

               p = Permutation(p);

               oz = frac(p*0.142857142857) - 0.428571428571;

               dx = Pf.x - of[i] + jitter*ox;
               dy = Pf.y - of[j] + jitter*oy;
               dz = Pf.z - of + jitter*oz;

               float3 d = dx * dx + dy * dy + dz * dz; // dij1, dij2 and dij3, squared

               //Find lowest and second lowest distances
               for(int n = 0; n < 3; n++)
               {
                if(d[n] < F[0])
                {
                 F[1] = F[0];
                 F[0] = d[n];
                }
                else if(d[n] < F[1])
                {
                 F[1] = d[n];
                }
               }
              }
             }

             return F;
            }
        "));

        registry.ProvideFunction("fBm_F1_F0", s => s.Append(@"
            float fBm_F1_F0(float3 p, int octaves, float _Frequency, float _Lacunarity, float _Gain, float _Jitter)
            {
             float freq = _Frequency, amp = 0.5;
             float sum = 0;	
             for(int i = 0; i < octaves; i++) 
             {
              float2 F = inoise(p * freq, _Jitter) * amp;

              sum += 0.1 + sqrt(F[1]) - sqrt(F[0]);

              freq *= _Lacunarity;
              amp *= _Gain;
             }
             return sum;
            }
        "));

        //registry.ProvideFunction("fBm_F0", s => s.Append(@"
        //    float fBm_F0(float3 p, int octaves, float _Frequency, float _Lacunarity, float _Gain, float _Jitter)
        //    {
        //     float freq = _Frequency, amp = 0.5;
        //     float sum = 0;
        //     for(int i = 0; i < octaves; i++)
        //     {
        //      float2 F = inoise(p * freq, _Jitter) * amp;

        //      sum += 0.1 + sqrt(F[0]);

        //      freq *= _Lacunarity;
        //      amp *= _Gain;
        //     }
        //     return sum;
        //    }
        //"));

        base.GenerateNodeFunction(registry, graphContext, generationMode);
    }
Esempio n. 27
0
    public override void GenerateNodeFunction(FunctionRegistry registry, GraphContext graphContext, GenerationMode generationMode)
    {
        registry.ProvideFunction("mod289_3", s => s.Append(@"
			float3 mod289_3(float3 x)
			{
				return x - floor(x / 289.0) * 289.0;
			}
		"        ));

        registry.ProvideFunction("mod289_4", s => s.Append(@"
			float4 mod289_4(float4 x)
			{
				return x - floor(x / 289.0) * 289.0;
			}
		"        ));

        registry.ProvideFunction("permute", s => s.Append(@"
			float4 permute(float4 x)
			{
				return mod289_4((x * 34.0 + 1.0) * x);
			}
		"        ));

        registry.ProvideFunction("taylorInvSqrt", s => s.Append(@"
			float4 taylorInvSqrt(float4 r)
			{
				return 1.79284291400159 - r * 0.85373472095314;
			}
		"        ));

        registry.ProvideFunction("snoise_grad", s => s.Append(@"
			float4 snoise_grad(float3 v)
			{
				const float2 C = float2(1.0 / 6.0, 1.0 / 3.0);

				// First corner
				float3 i  = floor(v + dot(v, C.yyy));
				float3 x0 = v   - i + dot(i, C.xxx);

				// Other corners
				float3 g = step(x0.yzx, x0.xyz);
				float3 l = 1.0 - g;
				float3 i1 = min(g.xyz, l.zxy);
				float3 i2 = max(g.xyz, l.zxy);

				// x1 = x0 - i1  + 1.0 * C.xxx;
				// x2 = x0 - i2  + 2.0 * C.xxx;
				// x3 = x0 - 1.0 + 3.0 * C.xxx;
				float3 x1 = x0 - i1 + C.xxx;
				float3 x2 = x0 - i2 + C.yyy;
				float3 x3 = x0 - 0.5;

				// Permutations
				i = mod289_3(i); // Avoid truncation effects in permutation
				float4 p =
				  permute(permute(permute(i.z + float4(0.0, i1.z, i2.z, 1.0))
										+ i.y + float4(0.0, i1.y, i2.y, 1.0))
										+ i.x + float4(0.0, i1.x, i2.x, 1.0));

				// Gradients: 7x7 points over a square, mapped onto an octahedron.
				// The ring size 17*17 = 289 is close to a multiple of 49 (49*6 = 294)
				float4 j = p - 49.0 * floor(p / 49.0);  // mod(p,7*7)

				float4 x_ = floor(j / 7.0);
				float4 y_ = floor(j - 7.0 * x_);  // mod(j,N)

				float4 x = (x_ * 2.0 + 0.5) / 7.0 - 1.0;
				float4 y = (y_ * 2.0 + 0.5) / 7.0 - 1.0;

				float4 h = 1.0 - abs(x) - abs(y);

				float4 b0 = float4(x.xy, y.xy);
				float4 b1 = float4(x.zw, y.zw);

				//float4 s0 = float4(lessThan(b0, 0.0)) * 2.0 - 1.0;
				//float4 s1 = float4(lessThan(b1, 0.0)) * 2.0 - 1.0;
				float4 s0 = floor(b0) * 2.0 + 1.0;
				float4 s1 = floor(b1) * 2.0 + 1.0;
				float4 sh = -step(h, 0.0);

				float4 a0 = b0.xzyw + s0.xzyw * sh.xxyy;
				float4 a1 = b1.xzyw + s1.xzyw * sh.zzww;

				float3 g0 = float3(a0.xy, h.x);
				float3 g1 = float3(a0.zw, h.y);
				float3 g2 = float3(a1.xy, h.z);
				float3 g3 = float3(a1.zw, h.w);

				// Normalise gradients
				float4 norm = taylorInvSqrt(float4(dot(g0, g0), dot(g1, g1), dot(g2, g2), dot(g3, g3)));
				g0 *= norm.x;
				g1 *= norm.y;
				g2 *= norm.z;
				g3 *= norm.w;

				// Compute noise and gradient at P
				float4 m = max(0.6 - float4(dot(x0, x0), dot(x1, x1), dot(x2, x2), dot(x3, x3)), 0.0);
				float4 m2 = m * m;
				float4 m3 = m2 * m;
				float4 m4 = m2 * m2;
				float3 grad =
					-6.0 * m3.x * x0 * dot(x0, g0) + m4.x * g0 +
					-6.0 * m3.y * x1 * dot(x1, g1) + m4.y * g1 +
					-6.0 * m3.z * x2 * dot(x2, g2) + m4.z * g2 +
					-6.0 * m3.w * x3 * dot(x3, g3) + m4.w * g3;
				float4 px = float4(dot(x0, g0), dot(x1, g1), dot(x2, g2), dot(x3, g3));
				return 42.0 * float4(grad, dot(m4, px));
			}
			"            ));

        base.GenerateNodeFunction(registry, graphContext, generationMode);
    }
Esempio n. 28
0
        public override void GenerateNodeFunction(FunctionRegistry registry, GenerationMode generationMode)
        {
            registry.ProvideFunction("dm_SimplexNoise", s => s.Append(@"

float _mod289(float x) {
    return x - floor(x * (1.0 / 289.0)) * 289.0;
}

float4 _mod289(float4 x) {
    return x - floor(x * (1.0 / 289.0)) * 289.0;
}

float3 _mod289(float3 x) {
    return x - floor(x * (1.0 / 289.0)) * 289.0;
}

float3 _perm(float3 x) {
    return _mod289(((x * 34.0) + 1.0) * x);
}

float4 _perm(float4 x) {
    return _mod289(((x * 34.0) + 1.0) * x);
}

float4 _taylorInvSqrt(float4 r){return 1.79284291400159 - 0.85373472095314 * r;}

float dm_SimplexNoise(float3 v){ 
    const float2  C = float2(1.0/6.0, 1.0/3.0) ;
    const float4  D = float4(0.0, 0.5, 1.0, 2.0);

    // First corner
    float3 i  = floor(v + dot(v, C.yyy) );
    float3 x0 =   v - i + dot(i, C.xxx) ;

    // Other corners
    float3 g = step(x0.yzx, x0.xyz);
    float3 l = 1.0 - g;
    float3 i1 = min( g.xyz, l.zxy );
    float3 i2 = max( g.xyz, l.zxy );

    //  x0 = x0 - 0. + 0.0 * C 
    float3 x1 = x0 - i1 + 1.0 * C.xxx;
    float3 x2 = x0 - i2 + 2.0 * C.xxx;
    float3 x3 = x0 - 1. + 3.0 * C.xxx;

    // Permutations
    i = _mod289(i); 
    float4 p = _perm( _perm( _perm( 
                i.z + float4(0.0, i1.z, i2.z, 1.0 ))
            + i.y + float4(0.0, i1.y, i2.y, 1.0 )) 
            + i.x + float4(0.0, i1.x, i2.x, 1.0 ));

    // Gradients
    // ( N*N points uniformly over a square, mapped onto an octahedron.)
    float n_ = 1.0/7.0; // N=7
    float3  ns = n_ * D.wyz - D.xzx;

    float4 j = p - 49.0 * floor(p * ns.z *ns.z);  //  mod(p,N*N)

    float4 x_ = floor(j * ns.z);
    float4 y_ = floor(j - 7.0 * x_ );    // mod(j,N)

    float4 x = x_ *ns.x + ns.yyyy;
    float4 y = y_ *ns.x + ns.yyyy;
    float4 h = 1.0 - abs(x) - abs(y);

    float4 b0 = float4( x.xy, y.xy );
    float4 b1 = float4( x.zw, y.zw );

    float4 s0 = floor(b0)*2.0 + 1.0;
    float4 s1 = floor(b1)*2.0 + 1.0;
    float4 sh = -step(h, 0.0);

    float4 a0 = b0.xzyw + s0.xzyw*sh.xxyy ;
    float4 a1 = b1.xzyw + s1.xzyw*sh.zzww ;

    float3 p0 = float3(a0.xy,h.x);
    float3 p1 = float3(a0.zw,h.y);
    float3 p2 = float3(a1.xy,h.z);
    float3 p3 = float3(a1.zw,h.w);

    //Normalise gradients
    float4 norm = _taylorInvSqrt(float4(dot(p0,p0), dot(p1,p1), dot(p2, p2), dot(p3,p3)));
    p0 *= norm.x;
    p1 *= norm.y;
    p2 *= norm.z;
    p3 *= norm.w;

    // Mix final noise value
    float4 m = max(0.6 - float4(dot(x0,x0), dot(x1,x1), dot(x2,x2), dot(x3,x3)), 0.0);
    m = m * m;
    return 42.0 * dot( m*m, float4( dot(p0,x0), dot(p1,x1), dot(p2,x2), dot(p3,x3) ) );
}

float3 dm_sampleSimplexNoise(in float3 uvw)
{
    float3 ddx_uvw = ddx(uvw);
    float3 ddy_uvw = ddy(uvw);
    
    float samples = 8;
    float step = 1.0 / samples;


    float n = 0.0f;

    for( int j=0; j < samples; j++ )
    for( int i=0; i < samples; i++ )
    {
        float3 pos = uvw + (i * step * ddx_uvw) + (j * step * ddy_uvw);
        n += dm_SimplexNoise(pos);
    }
    return n / (samples*samples);
}

float2 dm_Cellular(float3 P) {
    #define K 0.142857142857 // 1/7
    #define Ko 0.428571428571 // 1/2-K/2
    #define K2 0.020408163265306 // 1/(7*7)
    #define Kz 0.166666666667 // 1/6
    #define Kzo 0.416666666667 // 1/2-1/6*2
    #define jitter 1.0 // smaller jitter gives more regular pattern

	float3 Pi = _mod289(floor(P));
    float3 Pf = frac(P) - 0.5;

	float3 Pfx = Pf.x + float3(1.0, 0.0, -1.0);
	float3 Pfy = Pf.y + float3(1.0, 0.0, -1.0);
	float3 Pfz = Pf.z + float3(1.0, 0.0, -1.0);

	float3 p = _perm(Pi.x + float3(-1.0, 0.0, 1.0));
	float3 p1 = _perm(p + Pi.y - 1.0);
	float3 p2 = _perm(p + Pi.y);
	float3 p3 = _perm(p + Pi.y + 1.0);

	float3 p11 = _perm(p1 + Pi.z - 1.0);
	float3 p12 = _perm(p1 + Pi.z);
	float3 p13 = _perm(p1 + Pi.z + 1.0);

	float3 p21 = _perm(p2 + Pi.z - 1.0);
	float3 p22 = _perm(p2 + Pi.z);
	float3 p23 = _perm(p2 + Pi.z + 1.0);

	float3 p31 = _perm(p3 + Pi.z - 1.0);
	float3 p32 = _perm(p3 + Pi.z);
	float3 p33 = _perm(p3 + Pi.z + 1.0);

	float3 ox11 = frac(p11*K) - Ko;
	float3 oy11 = fmod(floor(p11*K), 7.0)*K - Ko;
	float3 oz11 = floor(p11*K2)*Kz - Kzo; // p11 < 289 guaranteed

	float3 ox12 = frac(p12*K) - Ko;
	float3 oy12 = fmod(floor(p12*K), 7.0)*K - Ko;
	float3 oz12 = floor(p12*K2)*Kz - Kzo;

	float3 ox13 = frac(p13*K) - Ko;
	float3 oy13 = fmod(floor(p13*K), 7.0)*K - Ko;
	float3 oz13 = floor(p13*K2)*Kz - Kzo;

	float3 ox21 = frac(p21*K) - Ko;
	float3 oy21 = fmod(floor(p21*K), 7.0)*K - Ko;
	float3 oz21 = floor(p21*K2)*Kz - Kzo;

	float3 ox22 = frac(p22*K) - Ko;
	float3 oy22 = fmod(floor(p22*K), 7.0)*K - Ko;
	float3 oz22 = floor(p22*K2)*Kz - Kzo;

	float3 ox23 = frac(p23*K) - Ko;
	float3 oy23 = fmod(floor(p23*K), 7.0)*K - Ko;
	float3 oz23 = floor(p23*K2)*Kz - Kzo;

	float3 ox31 = frac(p31*K) - Ko;
	float3 oy31 = fmod(floor(p31*K), 7.0)*K - Ko;
	float3 oz31 = floor(p31*K2)*Kz - Kzo;

	float3 ox32 = frac(p32*K) - Ko;
	float3 oy32 = fmod(floor(p32*K), 7.0)*K - Ko;
	float3 oz32 = floor(p32*K2)*Kz - Kzo;

	float3 ox33 = frac(p33*K) - Ko;
	float3 oy33 = fmod(floor(p33*K), 7.0)*K - Ko;
	float3 oz33 = floor(p33*K2)*Kz - Kzo;

	float3 dx11 = Pfx + jitter*ox11;
	float3 dy11 = Pfy.x + jitter*oy11;
	float3 dz11 = Pfz.x + jitter*oz11;

	float3 dx12 = Pfx + jitter*ox12;
	float3 dy12 = Pfy.x + jitter*oy12;
	float3 dz12 = Pfz.y + jitter*oz12;

	float3 dx13 = Pfx + jitter*ox13;
	float3 dy13 = Pfy.x + jitter*oy13;
	float3 dz13 = Pfz.z + jitter*oz13;

	float3 dx21 = Pfx + jitter*ox21;
	float3 dy21 = Pfy.y + jitter*oy21;
	float3 dz21 = Pfz.x + jitter*oz21;

	float3 dx22 = Pfx + jitter*ox22;
	float3 dy22 = Pfy.y + jitter*oy22;
	float3 dz22 = Pfz.y + jitter*oz22;

	float3 dx23 = Pfx + jitter*ox23;
	float3 dy23 = Pfy.y + jitter*oy23;
	float3 dz23 = Pfz.z + jitter*oz23;

	float3 dx31 = Pfx + jitter*ox31;
	float3 dy31 = Pfy.z + jitter*oy31;
	float3 dz31 = Pfz.x + jitter*oz31;

	float3 dx32 = Pfx + jitter*ox32;
	float3 dy32 = Pfy.z + jitter*oy32;
	float3 dz32 = Pfz.y + jitter*oz32;

	float3 dx33 = Pfx + jitter*ox33;
	float3 dy33 = Pfy.z + jitter*oy33;
	float3 dz33 = Pfz.z + jitter*oz33;

	float3 d11 = dx11 * dx11 + dy11 * dy11 + dz11 * dz11;
	float3 d12 = dx12 * dx12 + dy12 * dy12 + dz12 * dz12;
	float3 d13 = dx13 * dx13 + dy13 * dy13 + dz13 * dz13;
	float3 d21 = dx21 * dx21 + dy21 * dy21 + dz21 * dz21;
	float3 d22 = dx22 * dx22 + dy22 * dy22 + dz22 * dz22;
	float3 d23 = dx23 * dx23 + dy23 * dy23 + dz23 * dz23;
	float3 d31 = dx31 * dx31 + dy31 * dy31 + dz31 * dz31;
	float3 d32 = dx32 * dx32 + dy32 * dy32 + dz32 * dz32;
	float3 d33 = dx33 * dx33 + dy33 * dy33 + dz33 * dz33;

	// Sort out the two smallest distances (F1, F2)
#if 0
	// Cheat and sort out only F1
	float3 d1 = min(min(d11,d12), d13);
	float3 d2 = min(min(d21,d22), d23);
	float3 d3 = min(min(d31,d32), d33);
	float3 d = min(min(d1,d2), d3);
	d.x = min(min(d.x,d.y),d.z);
	return sqrt(d.xx); // F1 duplicated, no F2 computed
#else
	// Do it right and sort out both F1 and F2
	float3 d1a = min(d11, d12);
	d12 = max(d11, d12);
	d11 = min(d1a, d13); // Smallest now not in d12 or d13
	d13 = max(d1a, d13);
	d12 = min(d12, d13); // 2nd smallest now not in d13
	float3 d2a = min(d21, d22);
	d22 = max(d21, d22);
	d21 = min(d2a, d23); // Smallest now not in d22 or d23
	d23 = max(d2a, d23);
	d22 = min(d22, d23); // 2nd smallest now not in d23
	float3 d3a = min(d31, d32);
	d32 = max(d31, d32);
	d31 = min(d3a, d33); // Smallest now not in d32 or d33
	d33 = max(d3a, d33);
	d32 = min(d32, d33); // 2nd smallest now not in d33
	float3 da = min(d11, d21);
	d21 = max(d11, d21);
	d11 = min(da, d31); // Smallest now in d11
	d31 = max(da, d31); // 2nd smallest now not in d31
	d11.xy = (d11.x < d11.y) ? d11.xy : d11.yx;
	d11.xz = (d11.x < d11.z) ? d11.xz : d11.zx; // d11.x now smallest
	d12 = min(d12, d21); // 2nd smallest now not in d21
	d12 = min(d12, d22); // nor in d22
	d12 = min(d12, d31); // nor in d31
	d12 = min(d12, d32); // nor in d32
	d11.yz = min(d11.yz,d12.xy); // nor in d12.yz
	d11.y = min(d11.y,d12.z); // Only two more to go
	d11.y = min(d11.y,d11.z); // Done! (Phew!)
	return sqrt(d11.xy); // F1, F2
#endif
}

"));
            registry.ProvideFunction("dm_FractalNoise", s => s.Append(@"
float3 dm_FractalNoise(in float3 Pos, in float Frequency, in float Gain, in float Lacunarity, in float Persistence, int Octaves)
{
    float n = 0;
    for (int i = 1; i < Octaves; i++)
    {
        float s = dm_SimplexNoise(Pos * Frequency);
        n += s * Gain;
        Frequency *= Lacunarity;
        Gain *= Persistence;
    }
    return n;
}"));
            base.GenerateNodeFunction(registry, generationMode);
        }
Esempio n. 29
0
    public override void GenerateNodeFunction(FunctionRegistry registry, GraphContext graphContext, GenerationMode generationMode)
    {
        registry.ProvideFunction("mod", s => s.Append(@"
            float3 mod(float3 x, float3 y)
            {
              return x - y * floor(x / y);
            }
        "));

        registry.ProvideFunction("mod289", s => s.Append(@"
            float3 mod289(float3 x)
            {
                return x - floor(x / 289.0) * 289.0;
            }
        "));

        registry.ProvideFunction("mod2894", s => s.Append(@"
            float4 mod2894(float4 x)
            {
                return x - floor(x / 289.0) * 289.0;
            }
        "));

        registry.ProvideFunction("permute", s => s.Append(@"
            float4 permute(float4 x)
            {
                return mod2894((x * 34.0 + 1.0) * x);
            }
        "));

        registry.ProvideFunction("taylorInvSqrt", s => s.Append(@"
            float4 taylorInvSqrt(float4 r)
            {
              return (float4)1.79284291400159 - r * 0.85373472095314;
            }
        "));

        registry.ProvideFunction("fade", s => s.Append(@"
            float3 fade(float3 t) {
              return t*t*t*(t*(t*6.0-15.0)+10.0);
            }
        "));

        registry.ProvideFunction("cnoise", s => s.Append(@"
            float cnoise(float3 P)
            {
              float3 Pi0 = floor(P); // Integer part for indexing
              float3 Pi1 = Pi0 + (float3)1.0; // Integer part + 1
              Pi0 = mod289(Pi0);
              Pi1 = mod289(Pi1);
              float3 Pf0 = frac(P); // Fractional part for interpolation
              float3 Pf1 = Pf0 - (float3)1.0; // Fractional part - 1.0
              float4 ix = float4(Pi0.x, Pi1.x, Pi0.x, Pi1.x);
              float4 iy = float4(Pi0.y, Pi0.y, Pi1.y, Pi1.y);
              float4 iz0 = (float4)Pi0.z;
              float4 iz1 = (float4)Pi1.z;

              float4 ixy = permute(permute(ix) + iy);
              float4 ixy0 = permute(ixy + iz0);
              float4 ixy1 = permute(ixy + iz1);

              float4 gx0 = ixy0 / 7.0;
              float4 gy0 = frac(floor(gx0) / 7.0) - 0.5;
              gx0 = frac(gx0);
              float4 gz0 = (float4)0.5 - abs(gx0) - abs(gy0);
              float4 sz0 = step(gz0, (float4)0.0);
              gx0 -= sz0 * (step((float4)0.0, gx0) - 0.5);
              gy0 -= sz0 * (step((float4)0.0, gy0) - 0.5);

              float4 gx1 = ixy1 / 7.0;
              float4 gy1 = frac(floor(gx1) / 7.0) - 0.5;
              gx1 = frac(gx1);
              float4 gz1 = (float4)0.5 - abs(gx1) - abs(gy1);
              float4 sz1 = step(gz1, (float4)0.0);
              gx1 -= sz1 * (step((float4)0.0, gx1) - 0.5);
              gy1 -= sz1 * (step((float4)0.0, gy1) - 0.5);

              float3 g000 = float3(gx0.x,gy0.x,gz0.x);
              float3 g100 = float3(gx0.y,gy0.y,gz0.y);
              float3 g010 = float3(gx0.z,gy0.z,gz0.z);
              float3 g110 = float3(gx0.w,gy0.w,gz0.w);
              float3 g001 = float3(gx1.x,gy1.x,gz1.x);
              float3 g101 = float3(gx1.y,gy1.y,gz1.y);
              float3 g011 = float3(gx1.z,gy1.z,gz1.z);
              float3 g111 = float3(gx1.w,gy1.w,gz1.w);

              float4 norm0 = taylorInvSqrt(float4(dot(g000, g000), dot(g010, g010), dot(g100, g100), dot(g110, g110)));
              g000 *= norm0.x;
              g010 *= norm0.y;
              g100 *= norm0.z;
              g110 *= norm0.w;

              float4 norm1 = taylorInvSqrt(float4(dot(g001, g001), dot(g011, g011), dot(g101, g101), dot(g111, g111)));
              g001 *= norm1.x;
              g011 *= norm1.y;
              g101 *= norm1.z;
              g111 *= norm1.w;

              float n000 = dot(g000, Pf0);
              float n100 = dot(g100, float3(Pf1.x, Pf0.y, Pf0.z));
              float n010 = dot(g010, float3(Pf0.x, Pf1.y, Pf0.z));
              float n110 = dot(g110, float3(Pf1.x, Pf1.y, Pf0.z));
              float n001 = dot(g001, float3(Pf0.x, Pf0.y, Pf1.z));
              float n101 = dot(g101, float3(Pf1.x, Pf0.y, Pf1.z));
              float n011 = dot(g011, float3(Pf0.x, Pf1.y, Pf1.z));
              float n111 = dot(g111, Pf1);

              float3 fade_xyz = fade(Pf0);
              float4 n_z = lerp(float4(n000, n100, n010, n110), float4(n001, n101, n011, n111), fade_xyz.z);
              float2 n_yz = lerp(n_z.xy, n_z.zw, fade_xyz.y);
              float n_xyz = lerp(n_yz.x, n_yz.y, fade_xyz.x);
              return 2.2 * n_xyz;
            }
        "));

        base.GenerateNodeFunction(registry, graphContext, generationMode);
    }
Esempio n. 30
0
        public override void GenerateNodeFunction(FunctionRegistry registry, GenerationMode generationMode)
        {
            registry.ProvideFunction("sge_mod289", s => s.Append(@"
float3 sge_mod289(float3 x)
{
    return x - floor(x / 289.0) * 289.0;
}

float4 sge_mod289(float4 x)
{
    return x - floor(x / 289.0) * 289.0;
}
"));

            registry.ProvideFunction("sge_permute", s => s.Append(@"
float4 sge_permute(float4 x)
{
    return sge_mod289((x * 34.0 + 1.0) * x);
}
"));

            registry.ProvideFunction("sge_simplexNoise3D", s => s.Append(@"
float sge_simplexNoise3D(float3 v)
{
    const float2 C = float2(1.0 / 6.0, 1.0 / 3.0);

    // First corner
    float3 i  = floor(v + dot(v, C.yyy));
    float3 x0 = v   - i + dot(i, C.xxx);

    // Other corners
    float3 g = step(x0.yzx, x0.xyz);
    float3 l = 1.0 - g;
    float3 i1 = min(g.xyz, l.zxy);
    float3 i2 = max(g.xyz, l.zxy);

    // x1 = x0 - i1  + 1.0 * C.xxx;
    // x2 = x0 - i2  + 2.0 * C.xxx;
    // x3 = x0 - 1.0 + 3.0 * C.xxx;
    float3 x1 = x0 - i1 + C.xxx;
    float3 x2 = x0 - i2 + C.yyy;
    float3 x3 = x0 - 0.5;

    // Permutations
    i = sge_mod289(i); // Avoid truncation effects in permutation
    float4 p =
      sge_permute(sge_permute(sge_permute(i.z + float4(0.0, i1.z, i2.z, 1.0))
                                        + i.y + float4(0.0, i1.y, i2.y, 1.0))
                                        + i.x + float4(0.0, i1.x, i2.x, 1.0));

    // Gradients: 7x7 points over a square, mapped onto an octahedron.
    // The ring size 17*17 = 289 is close to a multiple of 49 (49*6 = 294)
    float4 j = p - 49.0 * floor(p / 49.0);  // mod(p,7*7)

    float4 x_ = floor(j / 7.0);
    float4 y_ = floor(j - 7.0 * x_);  // mod(j,N)

    float4 x = (x_ * 2.0 + 0.5) / 7.0 - 1.0;
    float4 y = (y_ * 2.0 + 0.5) / 7.0 - 1.0;

    float4 h = 1.0 - abs(x) - abs(y);

    float4 b0 = float4(x.xy, y.xy);
    float4 b1 = float4(x.zw, y.zw);

    //float4 s0 = float4(lessThan(b0, 0.0)) * 2.0 - 1.0;
    //float4 s1 = float4(lessThan(b1, 0.0)) * 2.0 - 1.0;
    float4 s0 = floor(b0) * 2.0 + 1.0;
    float4 s1 = floor(b1) * 2.0 + 1.0;
    float4 sh = -step(h, 0.0);

    float4 a0 = b0.xzyw + s0.xzyw * sh.xxyy;
    float4 a1 = b1.xzyw + s1.xzyw * sh.zzww;

    float3 g0 = float3(a0.xy, h.x);
    float3 g1 = float3(a0.zw, h.y);
    float3 g2 = float3(a1.xy, h.z);
    float3 g3 = float3(a1.zw, h.w);

    // Normalise gradients
    float4 norm = 1.79284291400159 - (float4(dot(g0, g0), dot(g1, g1), dot(g2, g2), dot(g3, g3))) * 0.85373472095314;
    g0 *= norm.x;
    g1 *= norm.y;
    g2 *= norm.z;
    g3 *= norm.w;

    // Mix final noise value
    float4 m = max(0.6 - float4(dot(x0, x0), dot(x1, x1), dot(x2, x2), dot(x3, x3)), 0.0);
    m = m * m;
    m = m * m;

    float4 px = float4(dot(x0, g0), dot(x1, g1), dot(x2, g2), dot(x3, g3));
    return 42.0 * dot(m, px);
}
"));

            base.GenerateNodeFunction(registry, generationMode);
        }