其他分享
首页 > 其他分享> > CodeGo.net>如何使Unity中的径向渐变天空盒?

CodeGo.net>如何使Unity中的径向渐变天空盒?

作者:互联网

好的,我是空盒的新手,在尝试实现RADIAL渐变空盒时遇到一些问题.我有一个径向渐变着色器,但是当我将其作为天空盒放置时,在两种颜色之间插值的部分过于放大,这意味着您根本无法真正分辨出其渐变.

enter image description here

我认为这是因为它的着色器并不适用于天空盒.然后,我找到了用于线性渐变天空盒的着色器,该着色器可以用作线性渐变,但是我需要它是放射状的,例如从中心颜色向外.

香港专业教育学院到处看,但找不到径向渐变SKYBOX着色器.我的问题是-如何将线性渐变着色器转换为Skybox的辐射状?或者如何使现有的径向渐变适用于天空盒?

我没有写这个,但这是线性渐变天空盒的代码:

Shader "Custom/Horizontal Skybox"
{
    Properties
    {
        _Color1 ("Top Color", Color) = (1, 1, 1, 0)
        _Color2 ("Horizon Color", Color) = (1, 1, 1, 0)
        _Color3 ("Bottom Color", Color) = (1, 1, 1, 0)
        _Exponent1 ("Exponent Factor for Top Half", Float) = 1.0
        _Exponent2 ("Exponent Factor for Bottom Half", Float) = 1.0
        _Intensity ("Intensity Amplifier", Float) = 1.0
    }

    CGINCLUDE

    #include "UnityCG.cginc"

    struct appdata
    {
        float4 position : POSITION;
        float3 texcoord : TEXCOORD0;
    };

    struct v2f
    {
        float4 position : SV_POSITION;
        float3 texcoord : TEXCOORD0;
    };

    half4 _Color1;
    half4 _Color2;
    half4 _Color3;
    half _Intensity;
    half _Exponent1;
    half _Exponent2;

    v2f vert (appdata v)
    {
        v2f o;
        o.position = mul (UNITY_MATRIX_MVP, v.position);
        o.texcoord = v.texcoord;
        return o;
    }

    half4 frag (v2f i) : COLOR
    {
        float p = normalize (i.texcoord).y;
        float p1 = 1.0f - pow (min (1.0f, 1.0f - p), _Exponent1);
        float p3 = 1.0f - pow (min (1.0f, 1.0f + p), _Exponent2);
        float p2 = 1.0f - p1 - p3;
        return (_Color1 * p1 + _Color2 * p2 + _Color3 * p3) * _Intensity;
    }

    ENDCG

    SubShader
    {
        Tags { "RenderType"="Background" "Queue"="Background" }
        Pass
        {
            ZWrite Off
            Cull Off
            Fog { Mode Off }
            CGPROGRAM
            #pragma fragmentoption ARB_precision_hint_fastest
            #pragma vertex vert
            #pragma fragment frag
            ENDCG
        }
    } 
}

像这样 –

enter image description here

enter image description here

解决方法:

您提供的着色器代码确实仅沿y轴绘制了径向渐变.如果通过更改线来翻转轴

float p = normalize (i.texcoord).y;

float p = normalize (i.texcoord).x;

编辑:

由于您实际上想旋转渐变,因此可以在顶点着色器中执行以下操作(1.57为pi / 2).

v2f vert (appdata v)
{
    v2f o;
    float sinX = sin ( 1.57 );
    float cosX = cos ( 1.57 );
    float sinY = sin ( 1.57 );
    float2x2 rotationMatrix = float2x2( cosX, -sinX, sinY, cosX);
    o.position = mul (UNITY_MATRIX_MVP, v.position);
    o.texcoord.xz = mul(v.texcoord.xz, rotationMatrix);
    o.texcoord.y = v.texcoord.y;
    return o;
}

编辑2:

完整的代码:

Shader "Custom/Horizontal Skybox"
{
    Properties
    {
        _Color1 ("Top Color", Color) = (1, 1, 1, 0)
        _Color2 ("Horizon Color", Color) = (1, 1, 1, 0)
        _Color3 ("Bottom Color", Color) = (1, 1, 1, 0)
        _Exponent1 ("Exponent Factor for Top Half", Float) = 1.0
        _Exponent2 ("Exponent Factor for Bottom Half", Float) = 1.0
        _Intensity ("Intensity Amplifier", Float) = 1.0
        _Angle ("Angle", Float) = 0.0
    }

    CGINCLUDE

    #include "UnityCG.cginc"

    struct appdata
    {
        float4 position : POSITION;
        float3 texcoord : TEXCOORD0;
    };

    struct v2f
    {
        float4 position : SV_POSITION;
        float3 texcoord : TEXCOORD0;
    };

    half4 _Color1;
    half4 _Color2;
    half4 _Color3;
    half _Intensity;
    half _Exponent1;
    half _Exponent2;
    half _Angle;

    v2f vert (appdata v)
    {
        v2f o;
        float sinX = sin ( _Angle );
        float cosX = cos ( _Angle );
        float sinY = sin ( _Angle );
        float2x2 rotationMatrix = float2x2( cosX, -sinX, sinY, cosX);
        o.position = mul (UNITY_MATRIX_MVP, v.position);
        o.texcoord.xz = mul(v.texcoord.xz, rotationMatrix);
        o.texcoord.y = v.texcoord.y;
        return o;
    }

    half4 frag (v2f i) : COLOR
    {
        float p = normalize (i.texcoord).x;
        float p1 = 1.0f - pow (min (1.0f, 1.0f - p), _Exponent1);
        float p3 = 1.0f - pow (min (1.0f, 1.0f + p), _Exponent2);
        float p2 = 1.0f - p1 - p3;
        return (_Color1 * p1 + _Color2 * p2 + _Color3 * p3) * _Intensity;
    }

    ENDCG

    SubShader
    {
        Tags { "RenderType"="Background" "Queue"="Background" }
        Pass
        {
            ZWrite Off
            Cull Off
            Fog { Mode Off }
            CGPROGRAM
            #pragma fragmentoption ARB_precision_hint_fastest
            #pragma vertex vert
            #pragma fragment frag
            ENDCG
        }
    }
}

标签:radial-gradients,unity3d,c,skybox
来源: https://codeday.me/bug/20191111/2022881.html