Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 SpookyCat · Dec 18, 2010 at 12:34 PM · shaderimageeffectpost-process-effect

Cant get the World Pos from Depth!

Still having trouble with the whole getting world pos from depth in a shader. Ive taken the code from Lighting.cginc and put it in my shader but just cant get the right results. In the video below you will see the red area is meant to show a world position but it moves around and distorts as the camera moves, the shader is below, can any of the expert Shader authors out there give me a clue as to how I can get this working?

Edit: It says in the docs it that _CameraToWorld is a built in matrix but I need to add it to the shader code, is this wrong? Also should I be manually be setting anything such as the camera matrix, none of the unity post effects do so so I assume not.

A couple of videos showing the issues Iam having Video 1 and Video 2

The shader code Iam using is: </p> <p>Shader "ShowPos Effect Shader" { Properties { _MainTex ("Base (RGB)", 2D) = "white" {} }</p> <pre><code>SubShader { Pass { ZTest Always Cull Off ZWrite Off Fog { Mode off } CGPROGRAM #pragma vertex vert #pragma fragment frag #pragma fragmentoption ARB_precision_hint_fastest #include "UnityCG.cginc" uniform sampler2D _MainTex; uniform float4 _MainTex_TexelSize; struct appdata { float4 vertex : POSITION; float3 texcoord : TEXCOORD0; }; struct v2f { float4 pos : SV_POSITION; float4 uv : TEXCOORD0; float3 ray : TEXCOORD1; }; v2f vert (appdata v) { v2f o; o.pos = mul(UNITY_MATRIX_MVP, v.vertex); o.uv = ComputeScreenPos (o.pos); o.ray = mul (UNITY_MATRIX_MV, v.vertex).xyz * float3(-1,-1,1); // v.texcoord is equal to 0 when we are drawing 3D light shapes and // contains a ray pointing from the camera to one of near plane's // corners in camera space when we are drawing a full screen quad. o.ray = lerp(o.ray, v.texcoord, v.texcoord.z != 0); return o; } sampler2D _CameraDepthTexture; float4x4 _CameraToWorld; float4 frag (v2f i) : COLOR { i.ray = i.ray * (_ProjectionParams.z / i.ray.z); float2 uv = i.uv.xy / i.uv.w; float depth = tex2D(_CameraDepthTexture, uv).r; depth = Linear01Depth(depth); float4 vpos = float4(i.ray * depth, 1); float3 wpos = mul(_CameraToWorld, vpos).xyz; return float4(normalize(wpos), 1); } ENDCG } } Fallback off </code></pre> <p>} And

float4 frag (v2f i) : COLOR

{ i.ray = i.ray * (_ProjectionParams.z / i.ray.z); float2 uv = i.uv.xy / i.uv.w;

float depth = tex2D(_CameraDepthTexture, uv).r; depth = Linear01Depth(depth); float4 vpos = float4(i.ray * depth, 1);

float3 wpos = mul(vpos, _CameraToWorld).xyz; float3 tocentre = wpos - _Pos;

float dist = length(tocentre);

float4 rgb = tex2D(_MainTex, uv);

if ( dist < _Radius ) rgb.r += 0.5f;

return rgb;

}

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

No one has followed this question yet.

Related Questions

Shader to a Camera 1 Answer

SSAO shader problem 0 Answers

Render Texture Screen Effect: Custom Shader Not Tiling 0 Answers

How to mask a camera using the render from another 0 Answers

Curved Screen Post Processing Effect 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