• 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
Question by MadHacker · Oct 02, 2014 at 03:49 PM · shaderpositionscreenvertex

How to get the screen position in an unlit shader.

Hi, I'm writing a shader in unity that isn't based on "#pragma surface surf Lambert". How do I get the current pixel screen position in the fragment program? I need it to be in pixels (not between 0 and 1 or -1 and 1). Here's my shader code:

 Shader "selectedFace"
 {
     SubShader
     {
         Pass
         {
             CGPROGRAM
             #pragma vertex vert 
             #pragma fragment frag
             float4 vert(float4 vertexPosition:POSITION):POSITION
             {
                 return mul(UNITY_MATRIX_MVP,vertexPosition);
             }
             float4 frag(void):COLOR
             {
                 //GET SCREEN POSITION HERE!!!
                 return float4(0.75,1.0,0.0,1.0);
             }
             ENDCG
         }
     }
 }

Thanks :D

Comment

People who like this

0 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 JonnyHilly · Jun 19, 2016 at 06:27 AM 0
Share

I have a similar problem... MVP mens Model rotation, followed by view, then the perspective transform, converting it into "Clip space" -1 to 1 . I assumes this clip space to be the size of the screen, but it seems to be more like the size / bounds of each object.

I have got somewhat close, by subtracting vertexposition from worldcameraposition, then doing a partial transform... not the full MVP... but the result is somewhat saddle shaped... curved, and not an exact match.... not sure how to get it perfect :/

  void vert (inout appdata_full v, out Input o) {
    UNITY_INITIALIZE_OUTPUT(Input,o);
 
        o.worldPos = mul (_Object2World, v.vertex);
        float3 relPos = o.worldPos - _WorldSpaceCameraPos;
        float dist = length (relPos);
     o.locPos = mul (UNITY_MATRIX_V, o.worldPos.xyz);      //  just V
     o.locPos = o.locPos /dist;    //true space perspective,   instead of /z    try this if no P  on the MVP transform
     o.locPos *= _MetaScalar;        //scalar param, so I can experiment with it
 
  }

1 Reply

· Add your reply
  • Sort: 
avatar image

Answer by tanoshimi · Jun 19, 2016 at 07:06 AM

Define a new member in the structure passed from your vertex to fragment shader, say, scrPos. Assign it an unused TEXCOORD semantic.

Populate it in the vertex program using the ComputeScreenPos macro:

 v2f vert(appdata_full v)
  {
      o.scrPos = ComputeScreenPos(v.pos);
      ...

And then use it as follows in the fragment shader:

  fixed4 frag(v2f i) : COLOR
  {
      float2 screenPosition = (i.scrPos.xy/i.scrPos.w);
Comment
JonnyHilly

People who like this

1 Show 1 · 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 JonnyHilly · Jun 19, 2016 at 08:39 AM 0
Share

thanks Much tanoshimi !, that was almost it... just needed this to work... o.locPos=mul(UNITY_MATRIX_MVP,v.vertex); o.locPos = ComputeScreenPos(o.locPos);

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

3 People are following this question.

avatar image avatar image avatar image

Related Questions

How do you get the world position of a vertex in a shader? 1 Answer

Shader : issue with vertex positions when multiple models are on the scene 1 Answer

Shader Vertex Position During Animation 1 Answer

Shader vertex position depends on camera ? 1 Answer

A node in a childnode? 1 Answer


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