Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
2 captures
12 Jun 22 - 14 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 /
  • Help Room /
avatar image
1
Question by tgaldi · Oct 27, 2015 at 07:53 PM · cameraguirender texturecoordinate-system

Screen Space from Render Texture World Space Position

Hello,

I have a scene with a two camera setup. The main camera is perspective, and displays a UI that includes a panel with a render texture (render texture does not take up the entire UI). The Render Texture is created by another camera, which is orthographic, looking down -y, and provides a god's eye view of the world. I have objects in the world and need to take their world position and translate them into screen space positions of the first camera to use for UI buttons, parented to the render texture panel (so that their positions match the world positions of the objects within the render texture).

Thanks.

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

4 Replies

· Add your reply
  • Sort: 
avatar image
3

Answer by nnikolas · Jan 28, 2018 at 09:37 PM

I hope this can help someone:

 using UnityEngine;
 using UnityEngine.UI;
 
 public class PositionObjectOnWorldImage : MonoBehaviour 
 {
     public Camera worldCamera; //world camera with RenderTexture
     public RectTransform worldImage; //RawImage which shows RenderTexture from camera
 
     RectTransform helper; //object to help with transformations
     void Start()
     {
         helper = (new GameObject ("helper", typeof(RectTransform))).GetComponent<RectTransform>();
         helper.SetParent (worldImage, false);
         helper.anchorMin = Vector2.zero;
         helper.anchorMax = Vector2.zero;
     }
 
     public Vector3 GetPositionOnUI(Vector3 worldPosition)
     {
         //first we get screnPoint in camera viewport space
         Vector2 screenPoint = RectTransformUtility.WorldToScreenPoint (worldCamera, worldPosition);
         //then transform it to position in worldImage using its rect
         Vector2 positionInImage = new Vector2 (screenPoint.x * worldImage.rect.width / worldCamera.pixelWidth, 
                                       screenPoint.y * worldImage.rect.height / worldCamera.pixelHeight);
 
         //after positioning helper to that spot
         helper.anchoredPosition = positionInImage;
         //... return 3D position of that helper for any other RectTransform.position to use
         return helper.position;
     }
 }

For improving speed: worldImage.rect.width / worldCamera.pixelWidth and worldImage.rect.height / worldCamera.pixelHeight should be calculated once, outside the method, and only recalculated if any of those dimensions change.

Incidently, if worldImage.rect is used to set size of world camera render target to always be 1:1, it can immediately be: helper.anchoredPosition = screenPoint;

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 sanchez_x · Feb 09, 2020 at 08:26 PM 0
Share

Thank you so much!

avatar image
0

Answer by tgaldi · Oct 28, 2015 at 05:19 PM

So far I have:

 Vector3 ConvertWorldToScreen( Vector3 positionIn ) {
 
 Texture tex = this.GetComponentInParent<RawImage>().mainTexture;
 RectTransform rectTrans = this.GetComponentInParent<RectTransform>();
 Rect rect = rectTrans.rect;
 
 Vector3 screenCoord = orthoCam.WorldToScreenPoint( positionIn )
 Vector2 localPoint;
 RectTransformUtility.ScreenToLocalPointInRectangle( rectTrans, screenCoord, null, out localPoint );
 int px = Mathf.Clamp( 0, (int)( ( ( localPoint.x - rect.x ) * tex.width ) / rect.width ), tex.width );
 int py = Mathf.Clamp( 0, (int)( ( ( localPoint.y - rect.y ) * tex.height ) / rect.height ), tex.height );
 
 Vector3 worldCoord = rectTrans.TransformPoint( localPoint );
 Vector2 offset = rectTrans.offsetMin;
 return new Vector3( offset.x + worldCoord.x, offset.y + worldCoord.y, 1f );
 }


The icons are slightly off, moreso in the x direction than the y (they appear above and to the right of where they should be). I have the anchor on the panel set to the lower left corner.

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
avatar image
0

Answer by tgaldi · Oct 28, 2015 at 06:32 PM

Actually this works too, although coordinates are still a bit off (looks like a scaling issue).

 Vector3 ConvertWorldToScreen( Vector3 positionIn ) {
      
     RectTransform rectTrans = this.GetComponentInParent<RectTransform>(); 
     
     Vector3 screenCoord = orthoCam.WorldToScreenPoint( positionIn );
     Vector3 worldCoord = RectTransformUtility.PixelAdjustPoint( screenCoord, rectTransform.transform, rectTrans.GetComponentInParent<Canvas>() );
     
     Vector2 offset = rectTrans.offsetMin;
     return new Vector3( offset.x + worldCoord.x, offset.y + worldCoord.y, 1f );
 }




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
avatar image
0

Answer by WarArm · Oct 14, 2016 at 09:03 AM

I have the same problem, anyone know a solution to this?

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 tgaldi · Oct 14, 2016 at 02:34 PM 0
Share

https://forum.unity3d.com/threads/convert-from-world-space-coordinate-to-pixel-of-render-texture.364586/#post-2365916

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

47 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

Related Questions

Orthographic Camera for objects but perspective for GUI? 1 Answer

Mouse click not registered correctly using CRT shader 0 Answers

(HDRP) Camera.Render() renders black, but only if Scene View is not visible 0 Answers

problem on rendertexture handling on Android 0 Answers

Why my texts on ugui disappear when the camera moving far from the gameobjects 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