- Home /
Shader Color
Hey, I have this waving sprite shader here and I would like to add color to wave objects and this already kind a works after I added those 3 lines there (commented ones) but after I press play button to see it in game it resets to default white. I can also edit it in game and can see the changes but again it resets as soon as I stop play mode. So, how can I get to keep that color I set? I'm new with shaders still... But I read many topics saying it shouldn't be that hard. (but seems bit too hard) Thanks in advance.
Shader "Custom/WavySprite"{
Properties{
_Color("Color", Color) = (1,1,1,1) // Edit: Added this line here
_MainTex("Texture",2D)="white"{}
_WaveDirection("Wave Direction",range(0,1))=0
_StaticSide("Static Side",range(0,4))=3
_WaveFrequency("Wave Frequency",float)=10
_WaveForce("Wave Force",float)=0.1
_WaveSpeed("Wave Speed",float)=1
}
SubShader
{
Tags{
"Queue"="Transparent"
"IgnoreProjector"="True"
"RenderType"="Transparent"
"PreviewType"="Plane"
}
ZWrite Off
Lighting Off
Cull Off
Fog { Mode Off }
Blend One Zero
Pass{
Blend SrcAlpha OneMinusSrcAlpha
CGPROGRAM
#pragma exclude_renderers xbox360 ps3 flash
#pragma vertex vert
#pragma fragment frag
#include "UnityCG.cginc"
struct appdata{
float4 pos:POSITION;
float2 uv:TEXCOORD0;
};
struct v2f{
float4 pos:SV_POSITION;
float2 uv:TEXCOORD0;
};
fixed4 _Color; // Edit: Added this line here
sampler2D _MainTex;
float _WaveDirection;
float _StaticSide;
float _WaveFrequency;
float _WaveForce;
float _WaveSpeed;
v2f vert(appdata v){
v2f o;
float3 unit=UnityObjectToClipPos(float3(1,1,0))-UnityObjectToClipPos(float3(0,0,0));
float3 wpos=mul(unity_ObjectToWorld,v.pos).xyz;
o.pos=UnityObjectToClipPos(v.pos);
//Decide a static side
float multiplier=0;
if(_StaticSide==1) multiplier=1-v.uv.y; //Top
if(_StaticSide==2) multiplier=1-v.uv.x; //Right
if(_StaticSide==3) multiplier=v.uv.y; //Bottom
if(_StaticSide==4) multiplier=v.uv.x; //Left
if(_StaticSide==0) multiplier=1; //None
//Horizontal waves
if(_WaveDirection==0) o.pos.x+=sin((wpos.y*_WaveFrequency)-(_Time.a*_WaveSpeed))*(_WaveForce*unit.x*multiplier);
//Vertical waves
else o.pos.y+=sin((wpos.x*_WaveFrequency)-(_Time.a*_WaveSpeed))*(_WaveForce*unit.y*multiplier);
o.uv=v.uv;
return o;
}
fixed4 frag (v2f i):SV_Target{
fixed4 col=tex2D(_MainTex,i.uv);
col *= _Color; // Edit: Added this line here
return col;
}
ENDCG
}
}
}
Answer by TSI25 · Oct 25, 2017 at 08:09 PM
I'm a beginner with all the shader logic but if you check out this link and scroll down to the Final Color Modifier section it has a pattern that will apply the color tint to whatever the final calculated coloring is, and might show a more reliable way of setting that color
I tried working with that but no success so far, while I continue trying it, other suggestions are appreciated.
when i bring your shader into a new project and apply it to a sprite and change the color on the material it actually works just fine. are you changing the color from the material or from the hierarchy? and are you sure you arent just changing the color of the sprite by accident? it does seem like it overrides that color
EDIT as a side note you can also go to the scene view and enable animated material previews from this little drop down to get a better idea of what that material will look like at run time.
I got it all working. The color was indeed overridden in script that makes the object wave. (I didn't provide it earlier, sorry, didn't expect it to be cause) I edited that script and now it all works fine. $$anonymous$$ade small changes to the shader too. Thanks for help.
fixed4 frag (v2f i):SV_Target{
fixed4 col =tex2D(_$$anonymous$$ainTex,i.uv);
return col * _Color;
}