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 pretender · Jul 01, 2010 at 04:29 PM · screenlineopenglworldprojection

Converting world point to screen point

i am trying to make labels for my objects in the scene. obviously i dont know how to properly convert world to screen coords, they seem to rotate around 3d space but they shouldnt

here is the code i use:

function OnPostRender() { var origin : Vector3=cam.camera.WorldToScreenPoint(cam.targetPosition);

     CreateLineMaterial(); 
     lineMaterial.SetPass(0); 
     GL.Begin( GL.LINES ); 
     GL.Color(Color(1,1,1,1)); 
     GL.Vertex3(cam.targetPosition.x ,cam.targetPosition.y, cam.targetPosition.z ); 
     GL.Vertex3( 1,0, 0 ); 
     GL.End(); 

}

i know this code isnt good, it was just for testing. i want line to go from the middle of the object (that is targetPosition Vector3) and other point should be, let's say 1 point long left or right from the object position, it should just project to the screen, not in 3D space

EDIT: i tried this, but it didnt get me too far (i used origin variable):

function OnPostRender(){

origin=cam.camera.WorldToViewportPoint(SceneControl.ReturnCameraTargetTransform().position);

 Debug.Log(origin.ToString());

 CreateLineMaterial(); 
 lineMaterial.SetPass(0); 

     GL.Begin(GL.LINES); 
     GL.Color(Color(1,1,1,1)); 

     GL.Vertex3(origin.x ,origin.y, 0);

     GL.Vertex3(origin.x+0.1 ,origin.y+0.1,0); 
     GL.End();

}

i simply dont get this.. :( what i am trying to do is to do labels for my objects in the scene. first step would be to get the position of the object and draw the line from that point and point that is offset a little. that worked fine if i used for example

GL.Vertex3(cam.targetPosition.x ,cam.targetPosition.y, cam.targetPosition.z);
GL.Vertex3(cam.targetPosition.x+0.5 ,cam.targetPosition.y+0.5, cam.targetPosition.z); 

but it rotated in 3d space so my guess was that i need to project the offset point to viewport of screen space, but every combination i tried didnt work...it is so frustrating...

if i use WorldToScreenPoint i get huge numbers for origin.ToString() so i am using WorldToViewportPoint... this seems to be so simple, but i cant get it to work

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

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by Eric5h5 · Jul 01, 2010 at 08:24 PM

You declared the "origin" variable but you're not doing anything with it.

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 pretender · Jul 01, 2010 at 10:12 PM 0
Share

i know, i tried it before and it didnt work out

avatar image pretender · Jul 06, 2010 at 09:25 AM 0
Share

how would you do it?

avatar image
0

Answer by gloopy · Oct 06, 2013 at 11:26 AM

I'm not entirely sure of your problem but if it is what I think it is and you are using a static camera or you always want your labels to face the camera I added this to my text labels for items in a script:

 function Update () {
 transform.rotation = Camera.main.transform.rotation;
 }

Now my text labels for items / players / anything always face the camera. I am using a Text Mesh to create labels above my items.

Hope this helps, and apologies if I have misunderstood the issue.

Comment
Add comment · 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

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

2 People are following this question.

avatar image avatar image

Related Questions

how to render 3d line on top everything else in the scene? 1 Answer

Drawing Lines with AA 1 Answer

Transform's position wherever the mouse is pointing. 2 Answers

how to calculate screen to world point (custom) 0 Answers

Is it possible to enable Screen.sleepTimeout to the screen but not the VR view? 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