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
0
Question by hrgchris · Mar 03, 2017 at 12:04 PM · shadervrdepthstereo

Computing world space pixel position in VR

Hi there

I've been wrestling with this one for a day and got no further. From within a fragment shader, towards the end of the render pipeline (but not in OnRenderImage), I'm attempting to use sample the depth texture in order to calculate the location of a given pixel at a given point on the screen. This is in order to achieve localised volumetric effects.

The very simple code, that I'm sure I've used before outside of VR, and is used by the soft particles (a bit) is as follows:

                 sampler2D _CameraDepthTexture;
 
                 struct v2f {
                     float4 pos : SV_POSITION;
                     float3 view : TEXCOORD0;
                     float4 clippos : TEXCOORD1;
                     float3 world : TEXCOORD2;
                 };
                  
                 v2f vert (appdata_base v) {
                     v2f o;
                     float4 wPos = mul(unity_ObjectToWorld, v.vertex);
                     o.pos = UnityObjectToClipPos(v.vertex);
                     o.view = wPos.xyz - _WorldSpaceCameraPos;
                     o.clippos = o.pos;
                     o.world = wPos.xyz;
                     return o;
                 }
                  
                 half4 frag (v2f i) : COLOR 
                 {
                     float4 screenpos = ComputeScreenPos(i.clippos);
                     float scenez = LinearEyeDepth(SAMPLE_DEPTH_TEXTURE_PROJ(_CameraDepthTexture, UNITY_PROJ_COORD(screenpos)));
                     float3 position = i.world + normalize(i.world - _WorldSpaceCameraPos)*scenez;
                     return half4(frac(abs(position*3)), 1);
                 }

On rendering the above, I would expect a sort of 'tiled' effect, as the fragment shader outputs a colour based on the world space position of the pixel it is rendering. However what I get is a warped output, that seems accurate in the center of the screen, but wrong towards the edges:

alt text

Rendering the scenez instead looks fairly sensible (though a bit of me thinks perhaps it should be curving out from the camera if it is correct?)

alt text

I am baffled. I've tried numerous different approaches, and placed all sorts of divide-by-w bits for the projection. I've had a go at passing different bits between vertex/fragment shader, or calculating the depth in different ways. However, all eventually end up doing the same dodgy effect.

I think it must have something to do with the fact that in VR the output is warped to match the lense, but how to compensate for this, and why a similar post-effect style approach works in OnRenderImage is beyond me.

Anybody any experience with this issue? Do I need to 'unwarp' somehow? Is a warp causing me to sample the wrong area of the depth texture perhaps?

wspixelimage.png (187.2 kB)
wsdepthimage.png (26.2 kB)
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 equalsequals · Jun 28, 2017 at 02:05 AM 0
Share

Are you attempting this in Single or $$anonymous$$ulti Pass Stereo?

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by equalsequals · Jul 13, 2017 at 05:27 PM

For posterity, I believe to have resolved the issue, at least in my case. Please see my Forum Post.

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.

Follow this Question

Answers Answers and Comments

107 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

Related Questions

Transparent objects scheduling - Unity 3D - VR 0 Answers

Camera layering : UI shader is cleared by second camera with Depth Only flag 1 Answer

Scissor test/early rejection 2 Answers

Reuse Shader Pass output as input for next Pass 0 Answers

Render scene depth to a texture 4 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