Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by waller_g · Mar 26, 2019 at 11:32 PM · shader programmingshaderlab

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?

Comment
Add comment · Show 1
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image waller_g · Mar 26, 2019 at 11:43 PM 0
Share

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?

1 Reply

· Add your reply
  • Sort: 
avatar image
0
Best Answer

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.

Needed to specify a UV Map in Blender


neededuvmap.png (7.5 kB)
Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

105 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges