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
2
Question by dorpeleg · Mar 06, 2013 at 01:44 PM · cameraraycastrendertexture

Raycasting and Render Texture

Hello everyone.

I have a static camera looking at a "TV screen".

The "TV screen" is a model and a render texture showing a second camera.

An object is moving in front of the second camera (think surveillance camera)

I want to be able to click the object trough the render texture.

Any ideas of how it can be done?

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

3 Replies

· Add your reply
  • Sort: 
avatar image
5
Best Answer

Answer by Statement · Mar 06, 2013 at 02:15 PM

First perform a raycast, that if it hits the plane/mesh of your TV screens render texture, get the texture coordinate of the hit. Use this texture coordinate to create a ray from the second camera using ViewportPointToRay. Feed the texture coordinate from the first hit. The new ray should now be able to hit the object moving in front of the second camera.

So two ray casts are necessary.

http://docs.unity3d.com/Documentation/ScriptReference/Physics.Raycast.html http://docs.unity3d.com/Documentation/ScriptReference/RaycastHit.html http://docs.unity3d.com/Documentation/ScriptReference/RaycastHit-textureCoord.html http://docs.unity3d.com/Documentation/ScriptReference/Camera.ViewportPointToRay.html

Some psuedo code that should work (some tweaks might be necessary, and this code can't be copied into a source file and expect to compile)

 ray = mainCamera.ScreenPointToRay(Input.mousePosition)
 if (Physics.Raycast(ray, hit) && hit.object == theTVScreen)
 {
    ray = secondCamera.ViewportPointToRay(new Vector3(hit.textureCoord));
    if (Physics.Raycast(ray, hit)
    {
       // hit should now contain information about what was hit through secondCamera
    }
 }
Comment
Add comment · Show 6 · 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 dorpeleg · Mar 06, 2013 at 05:39 PM 0
Share

Thanks for the answer.

I have a followup question.

If I have more then one "TVscreen" and more then one camera (lets say 4 screen and 4 cameras).

Is it possible, once my first ray hits a render texture, to know what camera is connected to that render texture?

avatar image Statement · Mar 07, 2013 at 12:32 AM 0
Share

Sure, but you should probably keep a Dictionary or something mapping textures to cameras. The long way around would be to go through each camera in the scene and check if their render texture is the one that you clicked on. Perhaps the former way is slightly more performent.

avatar image dorpeleg · Mar 07, 2013 at 10:53 AM 1
Share

After playing around with my code a lot, I got it to work! Thanks!

avatar image Adam_Benko · Oct 31, 2019 at 07:12 AM 0
Share

Hi, @dorpeleg Did you solve the the issue ? Could you please share your final code ? I have the same problem as you but cant make it work. I cant get a callback from the renderTexture at all with ray.

avatar image dorpeleg Adam_Benko · Oct 31, 2019 at 09:36 PM 0
Share

@Adam_Benko this was few years ago :D I don't even remember what project I was working on at that time. If you share more information, I can try and help you

avatar image Adam_Benko dorpeleg · Nov 03, 2019 at 07:22 PM 0
Share

Hi. I made a question here in unity but nobody can really help me with my problem. I've described it here: https://answers.unity.com/questions/1675224/

Could you please help me with that ? Thank you.

avatar image
0

Answer by rizawerx · Jan 06 at 01:09 PM

I know this is an old thread, but recently I need to do exact thing, raycasting from main camera to a 'portal' that has a render texture from a that 'portal' camera and want to share how I do this based on above clue:

 void Update()
     {
         RaycastHit hit;
         Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
 
         if (Physics.Raycast(ray, out hit))
         {
             Debug.DrawRay(ray.origin, ray.direction * 20, Color.green);
             if (hit.collider.tag == "portal")
             {
                 Vector2 localPoint = hit.textureCoord;
                 Ray _ray = cameraZoom.ViewportPointToRay(localPoint);
                 RaycastHit _hit;
                 if (Physics.Raycast(_ray, out _hit))
                 {
                     Debug.DrawRay(_ray.origin, _ray.direction * 20, Color.green);
                     if (_hit.collider.tag == "dirt")
                     {
                         print("contacted ");
                     }
                 }
             }
         }

I use an 3D object from my animation package and assign UV texture to it. Export it as FBX and import to Unity, then use that as my 'portal' view and assign Render Texture as its texture. The result is very precise.

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 ina · Jan 10 at 02:42 AM

If this is projected as a render texture to a raw image, use a RectTransformUtility.ScreenPointToLocalPointInRectangle() solution instead of having to do two raycasts

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

15 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

Related Questions

Help with Raycast on Render Texture 0 Answers

Mouseclick GameObject through the view of a RenderTexture 0 Answers

How convert the coorardinates of the mouse from one camera to another? 2 Answers

Rendertexture raycast 0 Answers

Ray through camera 1 Answer


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