• 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
1
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
Add comment · 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)?

Show more comments

1 Reply

· Add your reply
  • Sort: 
avatar image
0
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
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.

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

Can a rendertexture be placed on top of an image? (Minimap help) 0 Answers

Writing from fragment shader into 3D render texture 1 Answer

Texture alignment 0 Answers

Render Texture vs 3D model in screen space - camera 1 Answer

Can I use UnityGUI to make a 3D GUI? 2 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