- Home /
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!
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
}
}
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?
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.
After playing around with my code a lot, I got it to work! Thanks!
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.
@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
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.
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.
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
Your answer
Follow this Question
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