好的,我是着色器的新手,但是我找不到在连接到材质的自定义着色器中设置动画甚至更改颜色值的方法。
我在天空盒中使用了材质,它是一个带有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文档,并尝试更改底部颜色(最终使效果变暗,因此它是动画的,尽管我不知道该怎么做),如下所示:
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
}
您的代码没有发现任何问题。
尝试的事情:
1。您必须将skybox3素材插入天空盒插槽。之后,您可以更改材料。这样可以确保您正在修改正确的材料。
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
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句