• 【Unity HDRP渲染管线下的WorleyUtilities文件,“Hash”函数】


    WorleyUtilities文件路径如下:

    D:…\Library\PackageCache\com.unity.render-pipelines.high-definition@14.0.8\Runtime\Lighting\VolumetricClouds\WorleyUtilities.hlsl
    在这里插入图片描述

    文件代码如下

    #ifndef WORLEY_UTILITIES_H
    #define WORLEY_UTILITIES_H
    
    // Implementation inspired from https://www.shadertoy.com/view/3dVXDc
    // Hash by David_Hoskins
    #define UI0 1597334673U
    #define UI1 3812015801U
    #define UI2 uint2(UI0, UI1)
    #define UI3 uint3(UI0, UI1, 2798796415U)
    #define UIF (1.0 / float(0xffffffffU))
    
    float3 hash33(float3 p)
    {
        uint3 q = uint3(int3(p)) * UI3;
        q = (q.x ^ q.y ^ q.z) * UI3;
        return -1. + 2. * float3(q) * UIF;
    }
    
    // Density remapping function
    float remap(float x, float a, float b, float c, float d)
    {
        return (((x - a) / (b - a)) * (d - c)) + c;
    }
    
    // Gradient noise by iq (modified to be tileable)
    float GradientNoise(float3 x, float freq)
    {
        // grid
        float3 p = floor(x);
        float3 w = frac(x);
    
        // quintic interpolant
        float3 u = w * w * w * (w * (w * 6. - 15.) + 10.);
    
        // gradients
        float3 ga = hash33(fmod(p + float3(0., 0., 0.), freq));
        float3 gb = hash33(fmod(p + float3(1., 0., 0.), freq));
        float3 gc = hash33(fmod(p + float3(0., 1., 0.), freq));
        float3 gd = hash33(fmod(p + float3(1., 1., 0.), freq));
        float3 ge = hash33(fmod(p + float3(0., 0., 1.), freq));
        float3 gf = hash33(fmod(p + float3(1., 0., 1.), freq));
        float3 gg = hash33(fmod(p + float3(0., 1., 1.), freq));
        float3 gh = hash33(fmod(p + float3(1., 1., 1.), freq));
    
        // projections
        float va = dot(ga, w - float3(0., 0., 0.));
        float vb = dot(gb, w - float3(1., 0., 0.));
        float vc = dot(gc, w - float3(0., 1., 0.));
        float vd = dot(gd, w - float3(1., 1., 0.));
        float ve = dot(ge, w - float3(0., 0., 1.));
        float vf = dot(gf, w - float3(1., 0., 1.));
        float vg = dot(gg, w - float3(0., 1., 1.));
        float vh = dot(gh, w - float3(1., 1., 1.));
    
        // interpolation
        return va +
            u.x * (vb - va) +
            u.y * (vc - va) +
            u.z * (ve - va) +
            u.x * u.y * (va - vb - vc + vd) +
            u.y * u.z * (va - vc - ve + vg) +
            u.z * u.x * (va - vb - ve + vf) +
            u.x * u.y * u.z * (-va + vb + vc - vd + ve - vf - vg + vh);
    }
    
    // There is a difference between the original implementation's mod and hlsl's fmod, so we mimic the glsl version for the algorithm
    #define Modulo(x,y) (x-y*floor(x/y))
    
    // Tileable 3D worley noise
    float WorleyNoise(float3 uv, float freq)
    {
        float3 id = floor(uv);
        float3 p = frac(uv);
    
        float minDist = 10000.;
        for (float x = -1.; x <= 1.; ++x)
        {
            for (float y = -1.; y <= 1.; ++y)
            {
                for (float z = -1.; z <= 1.; ++z)
                {
                    float3 offset = float3(x, y, z);
                    float3 idOffset = id + offset;
                    float3 h = hash33(Modulo(idOffset.xyz, freq)) * .5 + .5;
                    h += offset;
                    float3 d = p - h;
                    minDist = min(minDist, dot(d, d));
                }
            }
        }
        return minDist;
    }
    
    float EvaluatePerlinFractalBrownianMotion(float3 position, float initialFrequence, int numOctaves)
    {
        const float G = exp2(-0.85);
    
        // Accumulation values
        float amplitude = 1.0;
        float frequence = initialFrequence;
        float result = 0.0;
    
        for (int i = 0; i < numOctaves; ++i)
        {
            result += amplitude * GradientNoise(position * frequence, frequence);
            frequence *= 2.0;
            amplitude *= G;
        }
        return result;
    }
    
    #endif // WORLEY_UTILITIES_H
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88
    • 89
    • 90
    • 91
    • 92
    • 93
    • 94
    • 95
    • 96
    • 97
    • 98
    • 99
    • 100
    • 101
    • 102
    • 103
    • 104
    • 105
    • 106
    • 107
    • 108
    • 109
    • 110
    • 111
    • 112
    • 113

    Implementation inspired from:https://www.shadertoy.com/view/3dVXDc
    :Hash by David_Hoskins

    于是我找了一下:

    在这里插入图片描述

    然后转译到ShaderLab中:

    Shader "Unlit/Smoke"
    {
        Properties
        {
            _MainTex ("Texture", 2D) = "white" {}
        }
        SubShader
        {
            Tags { "RenderType"="Opaque" }
            LOD 100
    
            Pass
            {
                CGPROGRAM
                #pragma vertex vert
                #pragma fragment frag
                // make fog work
                #pragma multi_compile_fog
    
                #include "UnityCG.cginc"
    
                struct appdata
                {
                    float4 vertex : POSITION;
                    float2 uv : TEXCOORD0;
                };
    
                struct v2f
                {
                    float2 uv : TEXCOORD0;
                    float4 vertex : SV_POSITION;
                };
    
                sampler2D _MainTex;
                float4 _MainTex_ST;
    
                v2f vert (appdata v)
                {
                    v2f o;
                    o.vertex = UnityObjectToClipPos(v.vertex);
                    o.uv = TRANSFORM_TEX(v.uv,_MainTex);
                    return o;
                }
                float remap(float a,float b,float c,float d,float e)
                {
                    return d + (a - b) * (e - d) / (c - b);
                }
                fixed4 frag (v2f i) : SV_Target
                {
    
                    fixed2 st = i.uv;
                    fixed2 uv = i.uv;
                    st.x *= 5.; // 5 columns for different noises
                    uv -= .02 * _Time.y;
    
                    fixed3 col = fixed3(0,0,0);
                    
                    float perlinWorley = tex2D(_MainTex, uv * 0.5).x;
                    
                    // worley fbms with different frequencies
                    fixed3 worley = tex2D(_MainTex, uv ).yzw;
                    float wfbm = worley.x * .625 +
                    worley.y * .125 +
                    worley.z * .25; 
                    
                    // cloud shape modeled after the GPU Pro 7 chapter
                    float cloud = remap(perlinWorley, wfbm - 1., 1., 0., 1.);
                    cloud = remap(cloud, .85, 1., 0., 1.); // fake cloud coverage
                    
                    if (st.x < 1.)
                    col += perlinWorley;
                    else if(st.x < 2.)
                    col += worley.x;
                    else if(st.x < 3.)
                    col += worley.y;
                    else if(st.x < 4.)
                    col += worley.z;
                    else if(st.x < 5.)
                    col += cloud;
                    
                    // column dividers
                    float div = smoothstep(.01, 0., abs(st.x - 1.));
                    div += smoothstep(.01, 0., abs(st.x - 2.));
                    div += smoothstep(.01, 0., abs(st.x - 3.));
                    div += smoothstep(.01, 0., abs(st.x - 4.));
                    
                    col = lerp(col, fixed3(0., 0., .866), div);
                    
                    fixed4 fragColor = fixed4(col,1.0);
                    return fragColor;
                }
                ENDCG
            }
        }
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88
    • 89
    • 90
    • 91
    • 92
    • 93
    • 94
    • 95
    • 96

    存档:

    GLSL着色语言内“remap”函数,在ShaderLab中报错:

                float remap(float a,float b,float c,float d,float e)
                {
                    return d + (a - b) * (e - d) / (c - b);
                }
    
    • 1
    • 2
    • 3
    • 4

    在GLSL着色语言内mix函数,在Unity ShaderLab中对应的是lerp

                    col = lerp(col, fixed3(0., 0., .866), div);
    
    • 1
  • 相关阅读:
    【零基础学QT】第四章 控件学习方法,问卷调查小界面
    __main__文件学习测试如下
    PyQt5开发相关
    PhalAPI学习笔记 ——— 第二章接口服务请求
    spring02
    [附源码]java毕业设计校园共享单车系统
    Pytorch多GPU并行训练: DistributedDataParallel
    YOLOv3目标检测全过程记录
    Android Studio 创建项目不自动生成BuildConfig文件
    机器学习运用-民宿价格
  • 原文地址:https://blog.csdn.net/xukaibo111/article/details/133887952