Advertisement
Bunny83

UnityEngine.RaycastHit

Apr 8th, 2017
330
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.30 KB | None | 0 0
  1.     [UsedByNativeCode]
  2.     public struct RaycastHit
  3.     {
  4.         private Vector3 m_Point;
  5.         private Vector3 m_Normal;
  6.         private int m_FaceID;
  7.         private float m_Distance;
  8.         private Vector2 m_UV;
  9.         private Collider m_Collider;
  10.         /// <summary>
  11.         ///   <para>The impact point in world space where the ray hit the collider.</para>
  12.         /// </summary>
  13.         public Vector3 point
  14.         {
  15.             get
  16.             {
  17.                 return this.m_Point;
  18.             }
  19.             set
  20.             {
  21.                 this.m_Point = value;
  22.             }
  23.         }
  24.         /// <summary>
  25.         ///   <para>The normal of the surface the ray hit.</para>
  26.         /// </summary>
  27.         public Vector3 normal
  28.         {
  29.             get
  30.             {
  31.                 return this.m_Normal;
  32.             }
  33.             set
  34.             {
  35.                 this.m_Normal = value;
  36.             }
  37.         }
  38.         /// <summary>
  39.         ///   <para>The barycentric coordinate of the triangle we hit.</para>
  40.         /// </summary>
  41.         public Vector3 barycentricCoordinate
  42.         {
  43.             get
  44.             {
  45.                 return new Vector3(1f - (this.m_UV.y + this.m_UV.x), this.m_UV.x, this.m_UV.y);
  46.             }
  47.             set
  48.             {
  49.                 this.m_UV = value;
  50.             }
  51.         }
  52.         /// <summary>
  53.         ///   <para>The distance from the ray's origin to the impact point.</para>
  54.         /// </summary>
  55.         public float distance
  56.         {
  57.             get
  58.             {
  59.                 return this.m_Distance;
  60.             }
  61.             set
  62.             {
  63.                 this.m_Distance = value;
  64.             }
  65.         }
  66.         /// <summary>
  67.         ///   <para>The index of the triangle that was hit.</para>
  68.         /// </summary>
  69.         public int triangleIndex
  70.         {
  71.             get
  72.             {
  73.                 return this.m_FaceID;
  74.             }
  75.         }
  76.         /// <summary>
  77.         ///   <para>The uv texture coordinate at the impact point.</para>
  78.         /// </summary>
  79.         public Vector2 textureCoord
  80.         {
  81.             get
  82.             {
  83.                 Vector2 result;
  84.                 RaycastHit.CalculateRaycastTexCoord(out result, this.collider, this.m_UV, this.m_Point, this.m_FaceID, 0);
  85.                 return result;
  86.             }
  87.         }
  88.         /// <summary>
  89.         ///   <para>The secondary uv texture coordinate at the impact point.</para>
  90.         /// </summary>
  91.         public Vector2 textureCoord2
  92.         {
  93.             get
  94.             {
  95.                 Vector2 result;
  96.                 RaycastHit.CalculateRaycastTexCoord(out result, this.collider, this.m_UV, this.m_Point, this.m_FaceID, 1);
  97.                 return result;
  98.             }
  99.         }
  100.         [Obsolete("Use textureCoord2 instead")]
  101.         public Vector2 textureCoord1
  102.         {
  103.             get
  104.             {
  105.                 Vector2 result;
  106.                 RaycastHit.CalculateRaycastTexCoord(out result, this.collider, this.m_UV, this.m_Point, this.m_FaceID, 1);
  107.                 return result;
  108.             }
  109.         }
  110.         /// <summary>
  111.         ///   <para>The uv lightmap coordinate at the impact point.</para>
  112.         /// </summary>
  113.         public Vector2 lightmapCoord
  114.         {
  115.             get
  116.             {
  117.                 Vector2 result;
  118.                 RaycastHit.CalculateRaycastTexCoord(out result, this.collider, this.m_UV, this.m_Point, this.m_FaceID, 1);
  119.                 if (this.collider.GetComponent<Renderer>() != null)
  120.                 {
  121.                     Vector4 lightmapScaleOffset = this.collider.GetComponent<Renderer>().lightmapScaleOffset;
  122.                     result.x = result.x * lightmapScaleOffset.x + lightmapScaleOffset.z;
  123.                     result.y = result.y * lightmapScaleOffset.y + lightmapScaleOffset.w;
  124.                 }
  125.                 return result;
  126.             }
  127.         }
  128.         /// <summary>
  129.         ///   <para>The Collider that was hit.</para>
  130.         /// </summary>
  131.         public Collider collider
  132.         {
  133.             get
  134.             {
  135.                 return this.m_Collider;
  136.             }
  137.         }
  138.         /// <summary>
  139.         ///   <para>The Rigidbody of the collider that was hit. If the collider is not attached to a rigidbody then it is null.</para>
  140.         /// </summary>
  141.         public Rigidbody rigidbody
  142.         {
  143.             get
  144.             {
  145.                 return (!(this.collider != null)) ? null : this.collider.attachedRigidbody;
  146.             }
  147.         }
  148.         /// <summary>
  149.         ///   <para>The Transform of the rigidbody or collider that was hit.</para>
  150.         /// </summary>
  151.         public Transform transform
  152.         {
  153.             get
  154.             {
  155.                 Rigidbody rigidbody = this.rigidbody;
  156.                 Transform result;
  157.                 if (rigidbody != null)
  158.                 {
  159.                     result = rigidbody.transform;
  160.                 }
  161.                 else
  162.                 {
  163.                     if (this.collider != null)
  164.                     {
  165.                         result = this.collider.transform;
  166.                     }
  167.                     else
  168.                     {
  169.                         result = null;
  170.                     }
  171.                 }
  172.                 return result;
  173.             }
  174.         }
  175.         private static void CalculateRaycastTexCoord(out Vector2 output, Collider col, Vector2 uv, Vector3 point, int face, int index)
  176.         {
  177.             RaycastHit.INTERNAL_CALL_CalculateRaycastTexCoord(out output, col, ref uv, ref point, face, index);
  178.         }
  179.         [MethodImpl(MethodImplOptions.InternalCall)]
  180.         private static extern void INTERNAL_CALL_CalculateRaycastTexCoord(out Vector2 output, Collider col, ref Vector2 uv, ref Vector3 point, int face, int index);
  181.     }
  182. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement