Hello, I am trying to get the world position of a pixel by reconstructing it from depth. I have tried different approaches, but the latest one that I found is the following (which doesn't seem to work): Code (csharp): [vert shader] o.pos = mul(UNITY_MATRIX_MVP, v.vertex); o.uv = v.texcoord; o.projPos = ComputeScreenPos(o.pos); o.ray = mul(UNITY_MATRIX_MV, v.vertex).xyz * float3(-1,-1,1); o.ray = lerp(o.ray, v.texcoord, v.texcoord.z != 0); [frag shader] i.ray = i.ray * (_ProjectionParams.z / i.ray.z); float2 uv = i.projPos.xy / i.projPos.w; float depth = tex2D(_CameraDepthTexture, uv).r; depth = Linear01Depth(depth); float4 vpos = float4(i.ray * depth, 1); float3 wpos = mul(_CameraToWorld, vpos).xyz; has anybody done it before? Thanks a lot
I've searched, that is how I got the above solution. Unfortunately it seems to return view-space position, or simply the _CameraToWorld does not work as expected.
If you get it figured out please do post the solution, I tried a year or so back to get it working and failed, and everyone who posted in response to my pleas for help couldn't get it to work either, we need Aras to step in and show us the light on this one.
The funny thing is that I did the exact thing for my deferred renderer some months ago. I never experienced such an issue, which is why I cannot solve the problem, the math seem correct Spooky, I also read your threads about it. Let's hope we find a solution
_ViewProjectInverse calculated in a script: If you have very far camera distance, you will have precision problems. EDIT: For opengl, you must invert the uvCoord.y
Thanks aubergine, however I do not get the same result as with getting the world position from the vertex shader. The colours seem correct, however they move around with the camera (I do not see the world-space "color cross" that I get with the vertex shader approach). Maybe it is my uvs? I am using ComputeScreenPos(o.pos) to get the sampling texcoords.
You are using this for an image effect right? If so, uvcoords is just the v.texcoord.xy of a screen quad.
Fully working solution with scene on GitHub: https://github.com/zezba9000/UnityMathReference/tree/master/Assets/Demos/Shaders/DepthBuffToWorldPos Lots of credits to this: https://gamedev.stackexchange.com/q...in-vr-through-projection-matrix/140924#140924
Thanks, this helped me a lot. I made gist with key parts for myself, maybe this will help someone on a similar journey. Video of the final result: Making a projector brush for playtime painter.
Thanks) I used a camera. It renders the depth when I call for it. Also it sets it's projection matrix and some other parameters as global parameters. Here is a link to that script: https://github.com/quizcanners/Tool...inter/Texture Scripts/DepthProjectorCamera.cs And here is the test shader: https://github.com/quizcanners/Tool...Shaders/PlaytimePainter_Test_Projector.shader It was a bit less confusing before I started to optimize. I moved parts of the code to .cginclude file to share between a multiple shaders, which makes it harder to follow. Not sure if everything is 100% correct though, made it with lots of trial and error. Also it didn't work for orthogonal projection as I remember.
@quizcanners all your links are now dead. Did you delete them? It's a shame you didn't copy/paste them into the forum, instead of linking off site.
@a436t4ataf Look at the links I posted. They're not broken and should never be. https://github.com/zezba9000/UnityMathReference/tree/master/Assets/Demos/Shaders/DepthBuffToWorldPos
I think I replied to a similar question a few days ago - you can find discussions about world position reconstruction from depth in this thread: https://forum.unity.com/threads/sol...epth-texture-in-single-pass-stereo-vr.480003/ and Keijiro's GitHub repo: https://github.com/keijiro/DepthInverseProjection Have you seen these two? There's been a few other threads too but I can't pull those from my head atm. I can check my notes later and maybe add links if possible.
That Keijiro sample needs a hard sticky or something. The way he calculates the view Ray is great, all in the shader, perspective and orthographic alike. Thanks a lot for the share.
Hi. I changed the folder structure at some point. The link to repository is: https://github.com/quizcanners/Playtime-Painter Link to the camera (currently): https://github.com/quizcanners/Playtime-Painter/tree/master/Painter Core/Components The Shader code you need is in: https://github.com/quizcanners/Playtime-Painter/blob/master/Shaders/PlaytimePainter_cg.cginc Can't promise that I will not change the structure again, but repository will not be deleted. By the way, I do feel like there are simpler and more correct ways to do this. I can't remamber the exact problem, but for some reason I couldn't get the right result with recommended functions (GetEyeDepth or something like that). So consider other posts before consider mine.