• 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 mtalbott · Jun 18, 2014 at 01:03 PM · shadersurfaceshader

Vertex Position to Screen Space and Back Again.

Hi Shader Pros,

I wrote a complex Fragment shader recently that modified the vertex positions relative to screen space. I'm trying to see if I can do the same thing in a Surface Shader. I understand the different spaces and how to use the built-in matrices to move up the chain e.g. M*V*P. What I struggle with is moving in the other direction.

here's the example scenario:

Let's say I want to move every vertex 5 screen-space pixels to the right. In a Fragment shader since my end goal is to convert the vertex position from object space to clip space (1 to -1 on the screen), i simple get the vertex screen position bump it to the right and I'm on my way:

 float offset = 5;

 float4 scrnSpacePosit = mul( UNITY_MATRIX_MVP, currVertex );
 scrnSpacePosit.y = scrnSpacePosit.y*_ProjectionParams.x;
 scrnSpacePosit.xy = float2(scrnSpacePosit.xy/scrnSpacePosit.w);
 float4 clipSpaceOffset = float4(offset/_ScreenParams.xy, 0, 0);
 float4 vectorRight = float4(1,0,0,0);
                     
 o.pos = scrnSpacePosit + (vectorRight * clipSpaceOffset);

However, for a Surface Shader it appears that vertex modification is done in Object Space. So my question is: How can I reverse the ScreenSpace Position back into Object Space. Or alternatively, I need to pass my (vectorRight * clipSpaceOffset) back into objectSpace.

Any Help is much appreciated.

-Mike

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

1 Reply

· Add your reply
  • Sort: 
avatar image
1

Answer by mtalbott · Jun 18, 2014 at 02:45 PM

This might not be the best answer but here's my strategy for now... I figured that If I could get a value for the "MetersPerPixel" at the current Vertex then I could use that to offset the vertex by a given number of pixels.

here's my code for finding the metersPerPixel. Please correct my method if there is a more efficient way to do this.

 float4x4 projectionMatrix = UNITY_MATRIX_P;
 float d = projectionMatrix[1][1];
 float distanceFromCameraToVertex = mul( UNITY_MATRIX_MV, v.vertex ).z;
 //The check here is for wether the camera is orthographic or perspective
 float frustumHeight = projectionMatrix[3][3] == 1 ? 2/d : 2.0*-distanceFromCameraToVertex*(1/d);
 float metersPerPixel = frustumHeight/_ScreenParams.y;
Comment
Add comment · Show 2 · 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 son1cman · May 24, 2015 at 02:44 PM 0
Share

Hi man!!! can you share your fragment shader that modify the vertex pos relative to screen space? it will be a great help to understand how this work!!

avatar image chantey · Feb 13, 2018 at 10:48 PM 0
Share

$$anonymous$$any thanks this is exactly what I needed. I'm using it to render star constellations in 3D.

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

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

24 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

Related Questions

Two Color Lighting Shader Not Working 2 Answers

Surface shader: Wrong lighting when moving vertices 1 Answer

Visible seams on borders when tiling texture 1 Answer

Unlit Shader with alpha mask 2 Answers

Vertex Colors as Splat Map for Terrain Texture colors blown out 1 Answer

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges