- Home /
Frag shader: get value of uv in screen pixels
Hi all,
I have decided to enter the world of shaders but am finding it slightly overwhelming at the moment.
Note: I've edited my original question to try and be more specific.
I am trying to write my first shader but need some advice on where to go next.
The goal of the shader is to implement a kind of AA on the top and bottom edges of a solid color quad. I decided to try and achieve this by alpha blending the top and bottom based on a percentage of the uv coordinates. Right now I have a working shader which lets me specify the amount of blending which I store as 'threshold'.
Shader "SolidColorLineAA" {
Properties
{
_Color("Color", Color) = (1, 1, 1)
_Threshold("Threshold", Range(0, 1)) = 0.05
_ThresholdPx("ThresholdPx", float) = 2
}
SubShader
{
Tags { "RenderType" = "Transparent" }
// Render both front and back facing polygons.
Cull Off
// TODO
ZWrite Off
Blend SrcAlpha OneMinusSrcAlpha
pass
{
// Vertex & Fragment shader
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
#include "UnityCG.cginc"
// access shader property in Cg
fixed4 _Color;
half _Threshold;
half _ThresholdPx;
// vertex input
struct vertexIn
{
float4 vertex : POSITION;
float4 texcoord : TEXCOORD0;
};
// vertex output
struct vertexOut
{
float4 pos : SV_POSITION;
float4 uv : TEXCOORD0;
};
vertexOut vert (vertexIn v)
{
vertexOut o;
o.pos = mul(UNITY_MATRIX_MVP, v.vertex);
o.uv = float4(v.texcoord.xy, 0, 0);
return o;
}
half4 frag (vertexOut i) : COLOR
{
// Shader Parameters
half4 color = _Color;
half threshold = _Threshold;
half thresholdPx = _ThresholdPx;
half v = frac(i.uv)[1];
// alpha fade the lower and upper regions (regions set by threshold)
v = (v < threshold) ? (v/threshold) : (v > (1 - threshold)) ? (1-v) / threshold : 1;
return half4(color.rgb, v);
}
ENDCG
}
}
}
As mentioned this is currently working.
But what I'd really like is to specify not a threshold percentage of the uv, but an amount of pixels on the screen. The problem with my current implementation is that when you zoom in there is too much blending, but when you zoom out there is too little. I would like it to be constant, such that it always blends for example 2 screen pixels worth of the quad. For this I have declared (but am not using) _ThresholdPx.
How should I go about changing the shader code?
I think I want to calculate something like the following: given a uv.v value, calculate how many screen pixels that value is from a uv.v value of 1.
Or should I be doing this another way entirely?
For example storing the height of the geometry in a vertex color and referencing that in my shader (seems like the naive solution).
Cheers
Answer by R.Lindsay · Oct 07, 2014 at 07:42 AM
I was able to fix the shader by changing how the threshold was calculated.
In the original code threshold was a fixed amount (0.05). What I wanted was a value that scaled with the relative size of the line on screen.
I ended up calculating threshold like this:
half amount = 1; // pixels
half threshold = fwidth(i.uv.y) * amount;
This give the desired and scales perfectly (at least, with my test lines).
No Shader
Shader
Answer by drudiverse · Oct 06, 2014 at 07:49 PM
dsd hlsl... vpos and sv_position are avaliable
search google for "surf vpos" for shader example's here's one with sv_position:
http://answers.unity3d.com/questions/693132/need-to-get-lighting-to-work-on-bending-vertex-sha.html
Your answer
Follow this Question
Related Questions
Assigning UV Map to model at runtime 0 Answers
Changing shader based on graphics settings 1 Answer
Using second UV as Lightmap. Slow? 0 Answers
Simple Shader Syntax ... or so I thought 0 Answers
Unwrap a mesh with a vertex shader 0 Answers