snlehton

RenderTextureTest.cs

May 13th, 2014
936
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.30 KB | None | 0 0
  1. using UnityEngine;
  2. using System.Collections;
  3.  
  4. public class RenderTextureTest : MonoBehaviour
  5. {  
  6.     public Transform quad;
  7.     public Camera ingameCamera;
  8.  
  9.     public bool checkObstruction;
  10.    
  11.     void Start ()
  12.     {
  13.         UICamera.raycastConversion = RenderTextureRaycastConversion;
  14.     }
  15.  
  16.     public Vector3 RenderTextureRaycastConversion(Vector3 screenPoint)
  17.     {
  18.         Ray ray = ingameCamera.ScreenPointToRay(screenPoint);
  19.  
  20.         Debug.DrawRay(ray.origin, ray.direction * 100, Color.green);
  21.  
  22.         // raycast to the quad plane
  23.         Plane plane = new Plane(quad.transform.forward, quad.transform.position);
  24.        
  25.         float enter;
  26.         if (!plane.Raycast(ray, out enter))
  27.             return new Vector3(float.NaN, float.NaN, float.NaN);
  28.  
  29.         Vector3 projectionPoint = ray.origin + ray.direction * enter;
  30.  
  31.         if (checkObstruction)
  32.         {
  33.             if (Physics.Raycast(ray, enter - 0.1f))
  34.                 return new Vector3(float.NaN, float.NaN, float.NaN);
  35.         }
  36.  
  37.         Debug.DrawLine(Vector3.zero, projectionPoint, Color.red);
  38.  
  39.         // transform to quad space
  40.         Vector3 quadSpace = quad.transform.InverseTransformPoint(projectionPoint);
  41.  
  42.         Vector3 finalPoint = quadSpace + new Vector3(0.5f, 0.5f, 0);
  43.         return finalPoint;
  44.     }
  45. }
Advertisement
Add Comment
Please, Sign In to add comment