Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 Spaniel · Feb 27, 2013 at 06:08 AM · shaderfragment

How do I get the texture pixel location in a fragment shader?

I am writing a shader where based on time, a progress bar fills up. The way I want to do this is calculate if the pixel is in a specific range. For instance, I want to see if the pixel for the texture sampled is between x (50 and 100). How do I get the coordinates of the pixel where the sample is taken from in the fragment shader?

Thanks!

Comment
Add comment
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

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by Owen-Reynolds · Feb 27, 2013 at 03:17 PM

For a non-rotating plane, simpler to use UV.x (assuming the unwrap goes that way.) Greater than 50% full would be something like i.uv.x>0.5 (a standard plane unwrap has coords from 0 to 1, no matter how large/stretched the actual plane is.)

If you have to get the actual (world? model?) coords, I think you can't(*). What you can do is add something like float3 pos2 to your v2f structure (vertex output, fragment input,) and assign to it in the vertex shader (copy it first thing if you want model coords.) Then can use it in the frag shader. Like anything else, it will be auto-interpolated to give correct numbers for each pixel.

(*)The "standard" v2f does have a "pos SV_POSITION" variable. This is the viewport(?) coords, so not useful, plus it's normally used and eaten by the hardware, so the frag shader can't read it anyway.

Comment
Add comment · Show 3 · 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
avatar image 1337GameDev · Apr 15, 2013 at 04:40 PM 0
Share

I think screen position is what I want. The bar is just stretched to grow BEHIND my object that has the shader. The bar has light pixel and the background behind that has black, so I want to basically do something with color when a font pixel is in front of the bar.

I just need pixel coordinates as they are rendered on screen. I plan to use these coordinates to access the pixel at the same location in grabpass and check the color of that pixel.

Can you demonstrate code for cg (not a surface shader) of this? I can't find it anywhere on the web and am stuck.

avatar image Owen-Reynolds · Apr 15, 2013 at 09:02 PM 0
Share

Almost everything with a life/progress bar can be done using UV.x as "percent from L to R." You don't see examples using screen position since it's good for so little (maybe not drawing over the $$anonymous$$i-map, but it's easier to just draw the $$anonymous$$i-map last.)

The GPU system isn't really set-up to manipulate pixels directly. I'd guess if you started with the effect you want, you could find/ask-for the standard way of approaching it.

avatar image 1337GameDev · Apr 15, 2013 at 09:48 PM 0
Share

http://stackoverflow.com/questions/4421261/vertex-shader-vs-fragment-shader

A fragment shader handles rendering of pixels. I know its possible and not out of the range of uses.

I don't want to use a shader to render the status bar. I went to render font that changes color based on the status bar. So i need to know if the status bar is behind the pixel of the font ik trying to render.

avatar image
0

Answer by antislash · May 04, 2015 at 10:38 AM

hey, i also wanted to have the space coordinates of a pixel in the fragment shader (that only manages a x,y screen position).

i used aditionnal TEXCOORDS to store the vertex position in the v2f , then could reuse it the frag function.

      struct v2f {
           float4 pos : SV_POSITION;
           fixed4 color : COLOR;
           float2 uv : TEXCOORD0; // stores uv
           float3 pos1 : TEXCOORD1; // vertex position
       };
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

12 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

Related Questions

Prevent ColorMask obscuring parts of an object's mesh 0 Answers

Why doesn't this vertex+fragment shader work? 0 Answers

Local position in vertex shader? 1 Answer

Simple cut shader: determine if fragment is inside another sphere? 3 Answers

Unexpected behavior in Vertex/Fragment shader when using saturate method (CG) 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