• 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 /
This question was closed Oct 27, 2017 at 05:08 PM by dkirilov for the following reason:

The question is answered, right answer was accepted

avatar image
2
Question by dkirilov · Oct 25, 2017 at 07:43 PM · shadersdepth buffer

Obtaining a depth Point Cloud

Hi,

I've been trying to extract a point cloud from Unity w$$anonymous$$ch has depth information for a w$$anonymous$$le now, but have had some trouble. I found some links online to help me get started and I feel as if I'm almost there, but there is slight problem - what should be straight lines appear to be curved. I was hoping someone could help me locate the issue / point me in the right direction.

To start off, to obtain the depth information, I used t$$anonymous$$s approach: http://answers.unity3d.com/questions/877170/render-scene-depth-to-a-texture.html

TLDR for the link: The shader and the code produce a greyscale image w$$anonymous$$ch represents the depth in the scene, where black is closest to the camera, and w$$anonymous$$te is the farthest. The code and shader used is the same as the ones in the reply of the user who posted t$$anonymous$$s.

Running the code produces t$$anonymous$$s image in Unity w$$anonymous$$ch looks proper:

alt text

From there, I obtain the render texture in the same way as the link above and store the pixel information in an array. Here is a quick reference:

         int resolutionX = Screen.width;
         int resolutionY = Screen.height;
 
         RenderTexture tempRt = new RenderTexture(resolutionX, resolutionY, 0);
         camera.targetTexture = tempRt;
         camera.Render();
         RenderTexture.active = tempRt;
         Texture2D tex2d = new Texture2D(resolutionX, resolutionY, TextureFormat.ARGB32, false);
         tex2d.ReadPixels(new Rect(0, 0, resolutionX, resolutionY), 0, 0);
 
         string[] output = new string[resolutionX * resolutionY];
         Color[] pixelInfo = tex2d.GetPixels();

From here, I run the following code on each pixel to attempt to translate it into real x, y, and z coordinates and output it into a file w$$anonymous$$ch is to be my point cloud.

             float r, g, b;
             double x, y, z, fl, range;
             double realX, realY, realZ;
 
             // Obtain X and Y Pixel Coordinates
             double pixelX = i % resolutionX;
             double pixelY = i / resolutionX;
 
             range = pixelInfo[i].r;
             x = (pixelX / resolutionX) - 0.5;
             y = (-(pixelY - resolutionY) / resolutionY) - 0.5;
 
             fl = -0.5 / (Math.Tan((Fov / 2) * Math.PI / 180));
             z = -fl;
 
             double vecLength = Math.Sqrt((x * x) + (y * y) + (z * z));
 
             // r = g = b because we are getting the value from the depth grayscale image
             r = (int)(pixelInfo[i].r * 255);
             g = (int)(pixelInfo[i].g * 255);
             b = (int)(pixelInfo[i].b * 255);
 
             // unitize the vector
             x /= vecLength;
             y /= vecLength;
             z /= vecLength;
 
             // multiply vector components by range to obtain real x, y, z
             realX = x * range;
             realY = y * range * -1;
             realZ = z * range;


When reading the file into Meshlab, all looks more or less okay, EXCEPT my straight lines are not as straight as they should be.

Image of the scene in Meshlab, rotated to show curvature.

alt text

As you can see, the rectangle w$$anonymous$$ch is a cube in the Unity world is curved when it should be straight and I have no idea why. Any help is appreciated. If anyt$$anonymous$$ng is unclear I'll gladly clarify. Thanks!

EDIT: I got the answer by posting in the forums. All I had to do was normalize the z component of vector to 1.

mlabbad.jpg (28.6 kB)
capture.jpg (23.9 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 bkupie2 · Oct 26, 2017 at 06:28 PM 1
Share

I've had a similar issue, any insight will be valuable, thanks.

0 Replies

  • Sort: 

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

127 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

Related Questions

writing _CameraDepthTexture values to file 0 Answers

Low poly artifacts in specular highlight for toon shader 0 Answers

Best way to render hundreds of textures for flooring in an in-game editor? 0 Answers

1. How many game objects(like Shpere) we can create per frame in unity? 0 Answers

How can I get the maximum texture value within a tri in a shader? 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