• 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 damagefilter · Aug 26, 2016 at 07:10 AM · rendertexturescreentoworldpointunitygui

Is rendering to RenderTexture changing output of Camera.ScreenToWorldPoint()?

Hello there!

What am I doing?

I'm attempting to draw a worldspace Canvas on a RenderTexture and have the mouse events function properly.

What happens?

The moment I make the UI camera draw on a RenderTexture it seems to confuzzle the transformation from screen to world point. That apparently causes a wrong world position within the uGUI event system and my buttons won't receive mouse-over or mouse-out events. Kind of like this: alt text I'm debugging that by fetching a world position from the camera and drawing a way from there in the forward direction like so:

 var pos = uiCanvas.worldCamera.ScreenToWorldPoint(Input.mousePosition);
 Ray r = new Ray(ev.position, Vector3.forward);
 Debug.DrawLine(r.origin, r.GetPoint(1000), Color.blue);

While the mouse, on the screen, is above a button the raycast comes out at a completely shifted-away position. The moment I make the camera render to screen, things start to work normally again.

I confirmed that the uGUI event system has the same off-set position by moving stuff around until the wrong mouse position was above the buttons and the events worked.

I also tried to convert screen to viewport point first and from there to world. But to no avail. Still wonky.

So my question is, what needs to be done to account for that offset? It seems to vary from resolution to resolution and I can't find the correct values for the life of it.

fail-description-image.png (82.2 kB)
Comment
Damjan-Mozetic

People who like this

1 Show 0
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
Best Answer

Answer by Alsio · Aug 28, 2016 at 06:50 PM

It's a bit hard to tell without seeing the whole hierarchy and scene, but this may help

RectTransformUtility.ScreenPointToLocalPointInRectangle https://docs.unity3d.com/ScriptReference/RectTransformUtility.ScreenPointToLocalPointInRectangle.html

I have used this to convert a screen point > render texture rect > its camera > ray cast > button thing Once you're in the rect you should convert to a [0,1] viewport space instead of pixels. Good luck!

Comment

People who like this

0 Show 3 · 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 damagefilter · Aug 29, 2016 at 05:14 AM 0
Share

Hey, thanks for answering :D

Lets see if I got this right.

So I'll put a UI Canvas (which has the RectTransform on it ofc) on the RT plane, then do a screen-to-local-point conversion of mouse pointer => RT rect.

Then I'll transform that into a viewport point ... and raycast through the other camera that's actually capturing the UI, does that sound about right?

I'll try that out now myself, just making sure I get it right.

avatar image damagefilter · Aug 29, 2016 at 11:16 AM 0
Share

So, as additional info: That did help and it is working now, thank you very much.

The big deal here was (and is also very important) the viewport point MUST be generated by the camera that is rendering to screen, otherwise it won't work.

avatar image Damjan-Mozetic damagefilter · Aug 25, 2017 at 11:04 AM 0
Share

Hi, I know it's an old topic, but would you mind explaining a bit more on the process behind this? I am having the same issue.

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

54 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

Related Questions

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

2D: Irregular shaped window to another world? 0 Answers

Screenshot only with specified cameras 1 Answer

Render Texture - Creating and Changing 1 Answer

RenderTexture does not render canvas image. 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