无法在Unity中自定义着色器中设置动画或更改颜色?

时间:2017-01-06 22:09:05

标签: c# unity3d colors shader material

好的,我是着色器的新手,但是我无法找到一种方法来动画甚至更改附加到材质的自定义着色器中的颜色值。

我使用我的天空盒的材料,它是一个渐变w / 3属性声明如下:

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
    }

我查看了Unitys的文档,并且我正在尝试更改底部颜色(最终是lerping,所以它是动画的,虽然我不知道该怎么做),像这样:

skybox.SetColor ("_Color3", afterColor);

材料是公共变量。我也试过写“底色”作为参数,但是当它运行时没有任何反应。我不明白为什么。

如何更改自定义着色器中的颜色?而且,一旦我这样做,我该怎样才能为这一变化做好准备?

编辑:@Programmer着色器是附加到材质对象的.shader文件。着色器代码(我没有把它写成着色器的新手):

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
        }
    } 
}

尝试使用空的脚本中的公共变量来更改它:

public Material skybox;
public Color afterColor;

void Start () {

        chars = GameObject.FindGameObjectsWithTag("BeginChar");
        balls = GameObject.FindGameObjectsWithTag ("Ball");
        StartCoroutine (waitForTheRun ());

        skybox.SetColor ("_Color3", afterColor); //here

    }

enter image description here

1 个答案:

答案 0 :(得分:1)

您的代码没有任何问题。

要尝试的事情:

1 。您必须将 skybox3 材料插入天空盒插槽。之后,您可以更改材料。这可确保您修改正确的材料。

enter image description here

2 。直接SkyBox更改

您不必使用此#1 。它将修改附加到SkyBox的任何材料。

public Material skybox;
public Color afterColor = Color.white;

// Use this for initialization
public void Start()
{
    skybox = RenderSettings.skybox;
    skybox.SetColor("_Color3", afterColor); //here
}