• 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
Question by vinvinn · Aug 10, 2020 at 03:40 AM · 3drendertextureorthographicscreentoworldpoint

Getting world coordinates for UI elements relative to a camera displaying to a render texture

I'm trying to use a GridLayoutGroup to help segment/manage an inventory system where I use a camera with a render texture attached to a UI panel to render the actual items as their icons.

I have faded circle shaped buttons setup in an evenly spaced layout group in a UI panel. The brighter circle are the positions I'm getting when trying to instantiate a sphere in the direct center of these buttons, and I can't figure out why they are offset like this.

alt text The amount they are offset changes when I change the screen size. (using maximize on play) alt text

Here is the code I'm using to instantiate these spheres.

         foreach (RectTransform btn in inventoryGrid.GetComponentInChildren<RectTransform>()) 
         {
             GameObject sphere = Instantiate(testSphere, renderCamera.transform);

             Vector3 point = rayCastCam.ScreenToWorldPoint(btn.position);
             point.z = backgroundPlane.transform.position.z;

             btn.GetComponentInChildren<Text>().text = point.ToString();

             sphere.transform.position = point;
         }

What adds to my confusion is the world points I'm getting seem to be correct. Buttons have their world coords on them, and they read the same as the transform position of the offset spheres.

Anyways I haven't been able to crack this, any help would be greatly appreciated!

annotation-2020-08-09-221606.png (137.5 kB)
annotation-2020-08-09-221954.png (155.6 kB)
Comment
valeofheart

People who like this

1 Show 4
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 Namey5 · Aug 10, 2020 at 09:32 AM 1
Share

Are you using a perspective camera to draw the previews? If so, what are you using as the input position to ScreenPointToWorld, in particular the depth component (i.e. btn.position.z)? I know they probably aren't the questions you're looking for, but I can't see anything specifically wrong with your logic and it's hard to tell what could be causing it without knowing more of the solution. Would you be able to post more of the code (including the actual camera setup, rendertexture info, etc)?

avatar image vinvinn Namey5 · Aug 10, 2020 at 11:12 PM 0
Share

Absolutely, I appreciate the response. The ortho camera view is drawn onto a UI image object. So the grid and brighter circles are 3d objects, and the buttons are drawn on top in the canvas.

So I have 2 ortho cameras, one displaying to the rendertexture, and one for getting coordinates (because apparently outputting to a rendertexture mess those up). The main camera in my scene is perspective but it (shouldn't) have any relevance to what I'm trying to do, since this is entirely for UI.

![alt text][1]

This is the entirety of my code for this thus far.

     public Camera rayCastCam, renderCamera;
     public GameObject inventoryGrid, backgroundPlane, testSphere;
     //Ordered pair of slot buttons and their corresponding world position
     private List<KeyValuePair<InventorySlot, Vector3>> slots = new List<KeyValuePair<InventorySlot, Vector3>>();
 
 
     // Update is called once per frame
     void Update()
     {
         if (Input.GetKeyDown(KeyCode.Alpha1))
         {
             if (slots.Count <= 1)
             {   //get all the world positions
                 GetInventorySlotWOrldPositions();
             }
             foreach (KeyValuePair<InventorySlot, Vector3> slot in slots)
             {   //create placeholder spheres at the world centerpoints gathered in GetInventorySlotWOrldPositions()
                 GameObject item = Instantiate(testSphere, renderCamera.transform);
                 item.transform.position = slot.Value;
             }
         }
     }
 
     //loop through all the buttons and get their world center point
     private void GetInventorySlotWOrldPositions()
     {
         foreach (RectTransform slot in inventoryGrid.GetComponentsInChildren<RectTransform>())
         {
             if (slot.gameObject.GetComponent<InventorySlot>() == null) continue;
 
             //This gets the wrold space at the center of my buttons relative to my ortho camera
             Vector3 point = rayCastCam.ScreenToWorldPoint(slot.position);
             point.z = backgroundPlane.transform.position.z;
 
             //add it to my list of Button/world space pairs
             slots.Add(new KeyValuePair<InventorySlot, Vector3>(slot.gameObject.GetComponent<InventorySlot>(), point));
 
             slot.gameObject.GetComponent<InventorySlot>().SetWorldSpacePosition(point);
         }
     }


I'm happy to provide more details if needed, I'm just not sure what else I can add.

Thanks again. [1]: /storage/temp/165214-annotation-2020-08-10-175826.png

annotation-2020-08-10-175826.png (38.4 kB)
avatar image Namey5 vinvinn · Aug 11, 2020 at 08:26 PM 0
Share

I can't investigate fully right now, but my first guess is that 'slot.position' doesn't do what you think it does. RectTransform inherits from Transform, so chances are its position component is already either in world space or canvas space (what it's actually relative to, I have no idea). I looked around and found this post for converting those positions to a screen-relative rect;

https://answers.unity.com/questions/1013011/convert-recttransform-rect-to-screen-space.html

Using that, you should be able to get the actual centre of the transform;

 Rect rect = RectTransformToScreenSpace (slot);
 Vector3 screenPos = new Vector3 (rect.center.x, rect.center.y, 1f);
 Vector3 point = rayCastCam.ScreenToWorldPoint (slot.position);

Something like that might help, but yeah my guess would be that the problem is either coming form ScreenToWorldPoint or your rendertexture/camera setup.

Show more comments

1 Reply

· Add your reply
  • Sort: 
avatar image
Best Answer

Answer by vinvinn · Aug 12, 2020 at 04:04 PM

If anyone is interested I figured this out. From what I can tell using screen coords just doesn't really work with an ortho rendertexture. So I just mapped the pixel values in the rendertexture UI image to my ortho camera world positions.

(in this case inventoryGrid is the object containing all my buttons, but is more importantly the same size and position as my rendertexture UI image)

 //map from orthocam coordinate range to inv bg image ui 
 public Vector2 RTtoOrthoWorldCoord(Vector2 coord)
 {
     Vector3 camPos = renderCamera.transform.position;
     float camSize = renderCamera.orthographicSize;
     Rect rect = inventoryGrid.GetComponent<RectTransform>().rect;
 
     float x = map(coord.x, 0f, rect.width, camPos.x - camSize, camPos.x + camSize);
     float y = map(coord.y, 0f, rect.height, camPos.y - camSize, camPos.y + camSize);
 
     return new Vector2(x, y);
 }
 float map(float s, float a1, float a2, float b1, float b2)
 {
     return b1 + (s - a1) * (b2 - b1) / (a2 - a1);
 }
Comment

People who like this

0 Show 0 · 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.

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

175 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 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

How to acquire player position on render texture? 1 Answer

3D RenderTextures with Depth Not Supported (worked in <=2017.1, broken 2018+) 1 Answer

Orthographic Camera 3D Object Rendering Issue (Clipping), What's wrong? 3 Answers

How to place a 3D object in the exact same space as 2D object on Screen? 0 Answers

How to use multiple render textures and cameras? 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