• 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
1
Question by JonnyHilly · Jun 19, 2016 at 06:38 AM · vertex shaderscreenspace

How do I get screen space co-ordinate of a vertex in vertex shader ?

I rendered a scene with several test objects to a texture, in flat color (purple) test. Now I am trying to render the same objects again... mapping that texture directly onto them... seems like it would be easy, if I can just get the screen co-ordinate of each vertex in vertex shader, and map it as UV onto my texture from step 1 un 0-1 space. This is where the trouble begins...

using MVP transform... this does not appear to be in -1 to +1 screen space, but some other space called clip space, which seems local to each object. My texture from step 1 gets mapped wholly onto to each object... no good. almost like its mapping to to the bound of each object instead of the entire camera space.

I also tried manually calculating offset of vertex world space from camera space... transforming it to view space, then dividing by Z or by Range, for my own perspective calculation.... This gets pretty close to mapping step1 texture over entire screen, but comes with some kind of distortion/warping in a saddle shape....

Anyone know the correct way to get arbitrary vertices on any/all objects.. screen space position -1 tp 1 or 0-1 space ? help appreciated. thanks.

example code... 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 shader param, so I can experiment with it

}

Comment
Add comment · 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:51 AM 0
Share

ok, got closer... scrap the distance part of the code, and do this for the perspective code

o.locPos.xy = o.locPos.xy /-o.locPos.z; //true space perspective, ins$$anonymous$$d of /z try this if no P on the $$anonymous$$VP transform

1 Reply

· Add your reply
  • Sort: 
avatar image
1

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

If you're targeting shader model 3, you get this automatically using a variable with the VPOS semantic. See the Unlit/Screen Position example at http://docs.unity3d.com/Manual/SL-ShaderSemantics.html.

Otherwise, use the ComputeScreenPos macro. First define a new float4 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
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.

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

43 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 avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image 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

Return gameobject Vector 3 relative to Screen Space? 1 Answer

How find coordinates of ui object in screen space 1 Answer

How to calculate screen space texcoords for surface shader? 1 Answer

Most efficient way to get the current screen width of 1 world point. 1 Answer

Vertex Shader Properties Issu 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