Ok, I am new to shaders but I cant find a way to animate or even change a color value in my custom shader that is attached to a material.
Im using the material for my skybox and it is a gradient w/ 3 properties declared like this:
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
}
I looked at Unitys docs and and am trying to change the bottom color (eventually lerping so it is animated, although I don't know how to do this) like this:
skybox.SetColor ("_Color3", afterColor);
where the material was a public var. I have tried writing "Bottom Color" as the parameter as well, but when it runs nothing happens. I don't understand why.
How can I change the color in my custom shader? Moreover, how can I animate this change once I do?
EDIT: @Programmer The shader is a .shader file attached to a material object. Shader code ( I did not write it as Im new to shaders):
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
}
}
}
Trying to change this with public variables like this from a script within an empty:
public Material skybox;
public Color afterColor;
void Start () {
chars = GameObject.FindGameObjectsWithTag("BeginChar");
balls = GameObject.FindGameObjectsWithTag ("Ball");
StartCoroutine (waitForTheRun ());
skybox.SetColor ("_Color3", afterColor); //here
}
Don't see anything wrong with your code.
Things to try:
1.You must plug your skybox3 material to the skybox slot. After that you can change the Material. This ensures that you are modifying the right material.
2.Direct SkyBox change
You don't have to do #1 with this. It will modify whatever material is attached to the SkyBox.
public Material skybox;
public Color afterColor = Color.white;
// Use this for initialization
public void Start()
{
skybox = RenderSettings.skybox;
skybox.SetColor("_Color3", afterColor); //here
}