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 /
This post has been wikified, any user with enough reputation can edit it.
avatar image
0
Question by R.Lindsay · Oct 06, 2014 at 08:18 AM · shaderuvantialias

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

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
1
Best Answer

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

alt text


Shader

alt text


noshader.png (19.1 kB)
shader.png (42.3 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
avatar image
0

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

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

3 People are following this question.

avatar image avatar image avatar image

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


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