Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 /
avatar image
3
Question by HppyDedPxl · Dec 26, 2016 at 09:27 PM · shaderverticesprojectiongrabrefraction

What do these lines in the refraction shader actually do? (UV calculation)

Hello. I've been studying the refraction shader and I am not exactly sure what the following lines do :

 o.uvgrab.xy = (float2(o.vertex.x, o.vertex.y) + o.vertex.w) * 0.5;
 o.uvgrab.zw = o.vertex.zw;
 

this value is later used in

     half4 col = tex2Dproj(_GrabTexture, UNITY_PROJ_COORD(i.uvgrab));

I am assuming that those lines calculate the position of the vertex on the Grab Texture, transforming it back into projection (or view? not sure) space. So getting the x and y position, then using the w property, which stores projection displacement, to get the vertex back to the near clipping plane, then multiplying by 0.5 to convert it from a coordinate system with it's origin in the center to the screen space coordinate system?

It would be amazing if somebody could clear this up for me, since everyone seems to just use this and never comment on what exactly it does. (probably because it's really basic...)

Thanks in advance!

Comment
Add comment · Show 2
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 Socapex · Dec 28, 2016 at 08:50 PM 0
Share

Ok so you've picked my interest. Please chime in if you figure it out, this is killing me lol.

To get the projected space, you would divide by w. Simply adding the w component is very strange, it does not seem to have any mathematical sense what-so-ever. I'm guessing this is a "trick" to achieve a visual effect at low cost.

Thats all for now, I can't wrap my head around that manipulation -___-

avatar image HppyDedPxl Socapex · Dec 28, 2016 at 10:42 PM 0
Share

I've looked into the UnityCG.cginc file after finding out that you can substitute that line for ComputeGrabScreenPos(float4 pos)

This is defined as follows (together with ComputeScreenPos, sort of relevant)

 // Projected screen position helpers
 #define V2F_SCREEN_TYPE float4
 inline float4 ComputeScreenPos (float4 pos) {
     float4 o = pos * 0.5f;
     #if defined(UNITY_HALF_TEXEL_OFFSET)
     o.xy = float2(o.x, o.y*_ProjectionParams.x) + o.w * _ScreenParams.zw;
     #else
     o.xy = float2(o.x, o.y*_ProjectionParams.x) + o.w;
     #endif
     
     o.zw = pos.zw;
     return o;
 }
 
 inline float4 ComputeGrabScreenPos (float4 pos) {
     #if UNITY_UV_STARTS_AT_TOP
     float scale = -1.0;
     #else
     float scale = 1.0;
     #endif
     float4 o = pos * 0.5f;
     o.xy = float2(o.x, o.y*scale) + o.w;
     o.zw = pos.zw;
     return o;
 }

Note that ComputeScreenPos does pretty much the same thing (apart from taking into account the texel offset define and not flipping,the image) So appearently it is really a world position to screen position calculation, but I still have no clue why it works the way it does.

0 Replies

· Add your reply
  • Sort: 

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

7 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Reuse Shader Pass output as input for next Pass 0 Answers

Distort shader not showing sprites 2 Answers

how to change progection matrix for some objects 0 Answers

Painted Vertices are Blockly, Looking to smooth them Out 1 Answer

Cubemap Projection 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