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 Soviut · Nov 25, 2010 at 08:58 AM · resolutionpixelmeasure

How to determine the number of pixels per unit?

I'm using the LineRenderer to produce a detailed 2D curve. I want to determine the number of pixels per unit based on the distance of the object from the camera and the output resolution of the display. This would allow me to accurately control the resolution of the curve so no matter what display resolution is used, it always appears smooth.

I'd like to do this instead of simply guessing and setting a very high resolution to begin with since on smaller mobile displays this is overkill.

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

1 Reply

· Add your reply
  • Sort: 
avatar image
2
Best Answer

Answer by skovacs1 · Nov 25, 2010 at 03:47 PM

To figure it out with the brute force approach, you would need to convert at least two points at the same distance. Something like:

//Get the screen coordinate of some point
var screenPos : Vector3 = Camera.main.WorldPointToScreen(transform.position);
//Offset by 1 pixel
if(screenPos .x > 0) = screenPos - Vector3.right;
else screenPos + Vector3.right;
if(screenPos .y > 0) = screenPos - Vector3.up;
else screenPos + Vector3.up;
//Get the world coordinate once offset.
var worldOffsetPos : Vector3 = Camera.main.ScreenPointToWorld(screenPos);
//Get the number of world units difference.
var Ratio : Vector3 =
    Camera.main.transform.InverseTransformPoint(transform.position)
    - Camera.main.transform.InverseTransformPoint(worldOffsetPos);

This calculates using screen positions along a plane parallel to the camera's clipping plane which I assume is what you imagine you are looking for.

If you wanted to get an answer that is not transform-relative as this one is, you would pick two pixels diagonally adjacent to each other and get their world positions and do pretty much the same, like so:

//Get the screen coordinate of some point
var screenPos : Vector3 = Vector3(Screen.width/2,Screen.height/2,distance);
//Get the world position of that point
var worldPos : Vector3 = Vector3 = Camera.main.ScreenPointToWorld(screenPos);
//Get the world position of some offset point
var worldOffsetPos : Vector3 = Camera.main.ScreenPointToWorld(screenPos
                                   + Vector3(1.0f,1.0f,0.0f));
//Get the number of world units difference.
var ratio : Vector3 =
    Camera.main.transform.InverseTransformPoint(worldPos)
    - Camera.main.transform.InverseTransformPoint(worldOffsetPos);

Using the projection matrix or screenPointToRay would actually perform perspective projection and your points would curve about the camera, making the question of how many world units go into a pixel a bit more complicated.

Comment
Add comment · Show 1 · 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 Soviut · Nov 25, 2010 at 11:39 PM 0
Share

I see your last point. However, my goal is to make the curve smooth regardless of resolution, so just finding an ideal ratio is all I really need; not necessarily pixel accuracy.

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

Pixel Perfect Camera Consistent World Size 0 Answers

World space to pixel space accuracy 1 Answer

Problem graphics 2d Pixel art Android build game. 1 Answer

pixel in unit in unity3d and have a full screen texture 1 Answer

How to import pixel perfect clear textures? 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