- Home /
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!
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.
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.
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.
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.
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
};
Your answer
Follow this Question
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