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 /
  • Help Room /
avatar image
0
Question by Birkeman · Jul 07, 2017 at 12:24 PM · shadergraphicsprojectionperspectiveprojection-matrix

How to render perspective corrected surface direction?

Hi there I'm having to solve quite an interesting problem. For a post processing effect I need to render the perceived direction of the surfaces of objects. This can be driven by artist from a flow map so in some ways it's very similar to normal mapping except for one key difference. This needs to work with the camera's perspective transformation which is where I'm running into issues. To visualize it I’ve drawn here with red arrows what I’m trying to achieve:

alt text

So given this needs to work with perspective my current approach is to transfer tangent, bitangent and vertex position into world space in the vertex shader:

 float3 normalDir = normalize(mul(unity_ObjectToWorld, float4(v.normal, 0).xyz));
 o.tangentDir = normalize(mul(unity_ObjectToWorld, float4(v.tangent.xyz, 0)).xyz);
 o.bitangentDir = normalize(cross(normalDir, o.tangentDir) * v.tangent.w);
 o.worldPosition = mul(unity_ObjectToWorld, v.vertex);

Then in the vertex shader I take the world position and world position offset by tangent and bitangent values obtained from the flow map:

 float4 dirTexel = tex2D(_DirectionTex, i.uv);
 float2 dirRG = (dirTexel.rgb.rg * 2 - 0.5);
 float3 dir = i.tangentDir * dirRG.r + i.bitangentDir * dirRG.g;

bring them into the camera frustum:

 float3 p0 = mul(UNITY_MATRIX_VP, float4(i.worldPosition.xyz, 1));
 float3 p1 = mul(UNITY_MATRIX_VP, float4(i.worldPosition.xyz + dir, 1));

Calculate the difference and render out the angle into a color channel I then read at a later stage in the post processing:

 float2 dir2D = p1.xy - p0.xy;
 
 dir.x = cos(3.14159265359 * -0.25);
 dir.y = sin(3.14159265359 * -0.25);
 dir.xy = float2(-1, );
 float angleRadians = atan2(dir2D.y, dir2D.x);
 angleRadians += 6.28318530718;
 angleRadians /= 6.28318530718;
 col.b = fmod(angleRadians, 1);

This is giving me a result I can’t really make heads or tails off. Here I have rendered out the transformed tangent directions:

alt text

Interestingly if I only multiply with UNITY_MATRIX_V and set the camera to ortographic I get the result I want so you'd think it was a problem with perspective projection but UNITY_MATRIX_VP still breaks so something goes wrong from the conversion from view space to projection space. Unity Answers only allows for 2 images to be attached to a question so I put some more screenshots here. Any help wuld be greatly appreciated!

flowdirectionperspective.png (34.4 kB)
tangentsvp.png (368.7 kB)
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

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

144 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 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 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 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

Graphics.Blit not working with my projective texture mapping shader 1 Answer

Unwanted graphical effects with metallic shaders 1 Answer

LWRP - Vertex displacement and intersection shader shows artefacts 1 Answer

Insert normal map in a shader 1 Answer

Shader can't find Unity shader .cgincs after exporting as part of a package 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