How to get 3D world space from render texture

I use Render Texture to create a Side Map , this works just fine, and I did it with these steps.

  • One main camera displaying the scene.
  • NGUI camera displaying a graphical interface.
  • This GUI contains a Render Texture (minimap) that renders a scene on a texture using a GUI camera.

But now I want to improve the map further and I want it to become insoluble. I want the world space to occupy the click point of the Render Texture. Is there any accessible way for me to get the world space position from the texture click point?

If I clicked somewhere in the rendering texture, then how can I get a point in the 3D space of the world.

For example: I clicked on a car object on a Render Texture. Now, how can I highlight or get it in the 3D space of the world? How to convert the 2D rendering of the texture to the position "Insert into the world"?

+8
c # unity3d camera
source share
1 answer

This suggests some things.

First I use EventSystems to get the mouse clicks. This is not a requirement, it can be easily changed (by you, though ^^). To properly configure it, you will need an EventSystem in your scene (if you have a user interface, you probably already have it).

Secondly, you need to attach the PhysicsRaycaster component to the main camera (the camera that the player sees).

Finally, in a GameObject that contains a renderer for the rendering texture (I used a simple quad), you apply the script below and assign the appropriate camera.

 using UnityEngine; using UnityEngine.EventSystems; //i chose box collider because its cheap [RequireComponent(typeof(BoxCollider))] public class RenderTextureRaycaster : MonoBehaviour, IPointerDownHandler { //assign in inspector public Camera portalExit; BoxCollider portal; Vector3 portalExitSize; void Start() { portal = GetComponent<BoxCollider>(); //this is the target camera resolution, idk if there is another way to get it. portalExitSize = new Vector3(portalExit.targetTexture.width, portalExit.targetTexture.height, 0); } public void OnPointerDown(PointerEventData eventData) { //the click in world space Vector3 worldClick = eventData.pointerCurrentRaycast.worldPosition; //transformed into local space Vector3 localClick = transform.InverseTransformPoint(worldClick); //since the origin of the collider is in its center, we need to offset it by half its size to get it realtive to bottom left Vector3 textureClick = localClick + portal.size / 2; //now we scale it up by the actual texture size which equals to the "camera resoution" Vector3 rayOriginInCameraSpace = Vector3.Scale(textureClick, portalExitSize); //with this knowledge we can creata a ray. Ray portaledRay = portalExit.ScreenPointToRay(rayOriginInCameraSpace ); RaycastHit raycastHit; //and cast it. if (Physics.Raycast(portaledRay, out raycastHit)) { Debug.DrawLine(portaledRay.origin, raycastHit.point, Color.blue, 4); } else { Debug.DrawRay(portaledRay.origin, portaledRay.direction * 100, Color.red, 4); } } } 

edit: the above is probably a bit verbose, you can easily save it if you like some liners, just to show how it works. also please, consider this only as a proof of concept, it has not been fully tested.

+4
source share

All Articles