basic gradient shader, float4 color alpha not showing
I'm starting practicing shaders, and I have this shader that suppose to just have gradient color, base on x position. But the alpha doesn't seem to affect:
Shader "Custom/GradShader" { Properties { _Color ("Color", Color) = (1,1,1,1) _MainTex ("Main Texture", 2D) = "white" {} } SubShader{ pass { CGPROGRAM #pragma vertex vert #pragma fragment frag uniform half4 _Color; uniform sampler2D _MainTex; struct vertexInput { float4 vertex : POSITION; float4 texcoord : TEXCOORD0; }; struct vertexOutput { float4 pos : SV_POSITION; float4 texcoord : TEXCOORD0; }; vertexOutput vert(vertexInput v) { vertexOutput o; o.pos = mul(UNITY_MATRIX_MVP, v.vertex); o.texcoord = v.texcoord; return o; } half4 frag(vertexOutput vo) : COLOR { float4 col = tex2D(_MainTex, vo.texcoord) * _Color; col.a = vo.texcoord.x; return col; } ENDCG } } //FallBack "Diffuse" }
(sorry about the looks, I couldn't seem to have new line in that :/.. looks good in edit mode) what is wrong here?
Answer by Namey5 · Oct 08, 2016 at 09:12 AM
Shader "Custom/GradShader" {
Properties {
_Color ("Color", Color) = (1,1,1,1)
_MainTex ("Main Texture", 2D) = "white" {}
}
SubShader {
Pass {
Blend SrcAlpha OneMinusSrcAlpha
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
uniform half4 _Color;
uniform sampler2D _MainTex;
struct vertexInput {
float4 vertex : POSITION;
float4 texcoord : TEXCOORD0;
};
struct vertexOutput {
float4 pos : SV_POSITION;
float4 texcoord : TEXCOORD0;
};
vertexOutput vert (vertexInput v) {
vertexOutput o;
o.pos = mul(UNITY_MATRIX_MVP, v.vertex);
o.texcoord = v.texcoord; return o;
}
half4 frag(vertexOutput vo) : COLOR
{
float4 col = tex2D (_MainTex, vo.texcoord) * _Color;
col.a = vo.texcoord.x;
return col;
}
ENDCG
}
}
//FallBack "Diffuse"
}
If by that you mean you want it to have transparency, then you have use blending.