- Home /
Can't get UV coordinates into my pixel shader
Hello. I am trying to learn how to write shaders, and experimenting with an attempt to write a healthbar shader that "fills" up to the current health value (by percentage) for a given actor with a red colour, and hides the damaged portion using transparency or clips the vertex completely.
I'm learning ShaderLab and Cg/HLSL on the fly, and I'm having trouble understanding why I don't seem to be able to pass texture coordinates into the pixel shader of my vertex and fragment shader.
Here is the code:
Shader "HUD/Healthbar"
{
Properties
{
_damage("Damage Percent", Range(0,1)) = 0
}
Subshader
{
Pass
{
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
#include "UnityCG.cginc"
struct v2f
{
float4 pos : SV_POSITION;
float4 texcoord : TEXCOORD0;
fixed4 colour : COLOR;
};
float _damage;
fixed4 _healthColour = fixed4(1, 0, 0, 1);
fixed4 _damageColour = fixed4(0, 0, 0, 1);
float _alpha = 1.0;
v2f vert(appdata_base v)
{
v2f o;
o.pos = UnityObjectToClipPos(v.vertex);
if (v.texcoord.x == 0 && v.texcoord.y == 0) // why is this always 0?
o.colour = fixed4(1, 0, 0, 1);
o.texcoord = v.texcoord;
return o;
}
fixed4 frag(v2f i) : COLOR
{
fixed4 c = i.colour;
return c;
}
ENDCG
}
}
}
I've clipped out the irrelevant (read: unfinished) code to focus on the main issue: I am unable to get meaningful uv data from the vertex function into the fragment function so that I can determine the pixel's position, relative to the damage cutoff. I can't even start figuring this out until I get over this hump. I'm hoping someone out there can help with this understanding.
I've done my best to read through as many tutorials and references as I can find, especially with Unity3d's documentation on Shaders as well as NVidia's Cg documentation.
Is this something as simple as using the wrong data types or semantics?
I feel like I should also note that I've tried using appdata_full
and my own struct appdata
with the POSITION
and TEXCOORD0
semantics. I have no idea what I'm doing wrong. Is it the render queue maybe?
Answer by waller_g · Mar 27, 2019 at 02:46 PM
Answering my own question here:
I am using Blender for my meshes, and I forgot to make sure to specify a UV Map for the mesh that I am applying my vertex shader to, thus, no UV data existed for the mesh. Adding this vertex group in Blender solved my issue.
Your answer
Follow this Question
Related Questions
How to Fade out Custom Unity Shader ? 0 Answers
Header files in ShaderLab 1 Answer
How to get vertex world positions in fragment shader 1 Answer
Surface shader changing z value 0 Answers
Fog not working in my Shader~ 0 Answers