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
1
Question by numberkruncher · Nov 04, 2013 at 05:58 PM · shaderrendertexturedepth

Render depth to RenderTexture and calculate world position

I have setup a simple script which renders a camera to a RenderTexture though I am having difficulty creating a replacement shader which renders depth.

Essentially I need to:

  • Render depth data into a texture.

  • Given a pixel in screen space (x, y) determine the pixel position in world space (C#).

How might I go about doing this?

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 Marino86 · Nov 04, 2013 at 07:13 PM 0
Share

To give you a hint on the second question, you can use the ScreenToWorldspace function of the camera, Unity camera reference

avatar image numberkruncher · Nov 04, 2013 at 07:25 PM 0
Share

@$$anonymous$$arino86 Somehow I need to reverse engineer the depth data into something which can be fed into a function like the one you indicated. $$anonymous$$y question is how to encode/decode this depth data so that I can feed it into the mouse ray.

1 Reply

· Add your reply
  • Sort: 
avatar image
1

Answer by Marino86 · Nov 04, 2013 at 08:49 PM

Ok you can make a script that creates a copy of your desired camera, and renders with a replacement shader This particular script is attached to the camera, else you would need to get the camera by other means, here it can be accessed directly.

 public class Testing : MonoBehaviour {
 
     public Texture2D someTex;
     public RenderTexture myDepth;
     GameObject tempCam;
 
     void Start()
     {
         someTex = new Texture2D(Screen.width, Screen.height);
         myDepth = new RenderTexture(Screen.width,Screen.height,8);
         tempCam = new GameObject("tempCam");
         tempCam.AddComponent<Camera>();
         tempCam.camera.CopyFrom(camera);
         tempCam.camera.enabled = false;
     }
 
     void Update()
     {
         tempCam.camera.SetReplacementShader(Shader.Find("Custom/ReplacementShader"), "");
         tempCam.camera.targetTexture = myDepth;
         tempCam.camera.Render();
 
         RenderTexture.active = myDepth;
         someTex.ReadPixels(new Rect(0, 0, Screen.width, Screen.height), 0, 0);
         someTex.Apply();
     }
 }
 

Somthing like that, Leaving the replacement tag renders everything with the replacement shader.

A very simple shader to render depth can be as simple as forwarding your position down from the vertex shader into the fragment shader via TEXCOORD. and the color is the z value of the position divided by the far plane value of the camera which you pass in with the script.

 Shader "Custom/ReplacementShader" {
 
     SubShader {
         Tags { "RenderType"="Opaque" }
         
 
         CGINCLUDE
 
         struct VIN {
             float4 pos : POSITION0;
         };
 
         struct V2F {
             float4 pos  : POSITION0;
             float4 p    :TEXCOORD0;
         };
 
         V2F myVert( VIN i ) {
             V2F o;
             o.pos = mul( UNITY_MATRIX_MVP, i.pos );
             o.p = mul( UNITY_MATRIX_MVP , i.pos );
             return o;
         }
 
         fixed4 myFrag(V2F i) : COLOR {
             // Here my farplane is 10, hardcoded, you can pass it like any other parameter from the unity script.
             return fixed4(i.p.z / 10 , i.p.z / 10 , i.p.z / 10 ,1);
         } 
 
         ENDCG
 
         pass {
             CGPROGRAM
             #pragma fragment myFrag
             #pragma vertex myVert
             #pragma fragmentoption ARB_precision_hint_fastest
             #pragma target 2.0
             ENDCG
         }
     } 
 }

Then you should be able to use your imagination to use the data, and or optimize this code somehow to your liking.

Hope this helps somehow.

Comment
Add comment · Show 2 · 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
avatar image numberkruncher · Nov 04, 2013 at 11:59 PM 0
Share

Thank you! this is indeed very helpful. This is so close to working, but when my camera is angled a long flat surface doesn't smoothly fade to represent varying distances from screen to camera. This has the effect of incorrectly offsetting the calculated world point on the Z axis.

Here is how I am calculating the world point:

 Color pix = _temp.GetPixel(mouseX, screenHeight - mouseY);
 mouseRay = HandleUtility.GUIPointToWorldRay(Event.current.mousePosition);
 test.worldPoint = test.mouseRay.GetPoint(pix.r * 3000);

Note: 3000 is the value for the farplane (ins$$anonymous$$d of 10).

avatar image numberkruncher · Nov 05, 2013 at 12:09 AM 0
Share

Here is what I have so far: https://gist.github.com/rotorz/4030d6e9c76f8f2e490c

And here is a video of it in action: http://www.youtube.com/watch?v=esWHkeF-ke4&feature=youtu.be

The tiny red dot is drawn onto the texture showing the pixel nearest the mouse pointer. Holding space key shows the render texture on screen. For some reason it appears offset and slightly smaller. And of course the issue which I was referring to.

Thanks again for your help :)

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

16 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

Related Questions

sampler2d object has no methods 1 Answer

Read depth buffer on the cpu 1 Answer

Complex Depth Shader 0 Answers

Render scene depth to a texture 4 Answers

Multiple RenderTexture and depth buffer 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