BlueGit

blue-star ggj-2020

Files

ggj-2020 / Assets / Oculus / VR / Scripts / Util / OVRPhysicsRaycaster.cs

OVRPhysicsRaycaster.cs

Project Import
timfa Tim Falken · pushed by timfa · 6 years ago
b77834a1 History
OVRPhysicsRaycaster.cs 6.3 KiB Download Raw
1/************************************************************************************
2Copyright : Copyright (c) Facebook Technologies, LLC and its affiliates. All rights reserved.
3
4Licensed under the Oculus Utilities SDK License Version 1.31 (the "License"); you may not use
5the Utilities SDK except in compliance with the License, which is provided at the time of installation
6or download, or which otherwise accompanies this software in either electronic or hard copy form.
7
8You may obtain a copy of the License at
9https://developer.oculus.com/licenses/utilities-1.31
10
11Unless required by applicable law or agreed to in writing, the Utilities SDK distributed
12under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF
13ANY KIND, either express or implied. See the License for the specific language governing
14permissions and limitations under the License.
15************************************************************************************/
16
17using System.Collections.Generic;
18
19namespace UnityEngine.EventSystems
20{
21 /// <summary>
22 /// Simple event system using physics raycasts. Very closely based on UnityEngine.EventSystems.PhysicsRaycaster
23 /// </summary>
24 [RequireComponent(typeof(OVRCameraRig))]
25 public class OVRPhysicsRaycaster : BaseRaycaster
26 {
27 /// <summary>
28 /// Const to use for clarity when no event mask is set
29 /// </summary>
30 protected const int kNoEventMaskSet = -1;
31
32
33 /// <summary>
34 /// Layer mask used to filter events. Always combined with the camera's culling mask if a camera is used.
35 /// </summary>
36 [SerializeField]
37 protected LayerMask m_EventMask = kNoEventMaskSet;
38
39 protected OVRPhysicsRaycaster()
40 { }
41
42 public override Camera eventCamera
43 {
44 get
45 {
46 return GetComponent<OVRCameraRig>().leftEyeCamera;
47 }
48 }
49
50 /// <summary>
51 /// Depth used to determine the order of event processing.
52 /// </summary>
53 public virtual int depth
54 {
55 get { return (eventCamera != null) ? (int)eventCamera.depth : 0xFFFFFF; }
56 }
57
58 public int sortOrder = 20;
59 public override int sortOrderPriority
60 {
61 get
62 {
63 return sortOrder;
64 }
65 }
66
67 /// <summary>
68 /// Event mask used to determine which objects will receive events.
69 /// </summary>
70 public int finalEventMask
71 {
72 get { return (eventCamera != null) ? eventCamera.cullingMask & m_EventMask : kNoEventMaskSet; }
73 }
74
75 /// <summary>
76 /// Layer mask used to filter events. Always combined with the camera's culling mask if a camera is used.
77 /// </summary>
78 public LayerMask eventMask
79 {
80 get { return m_EventMask; }
81 set { m_EventMask = value; }
82 }
83
84
85 /// <summary>
86 /// Perform a raycast using the worldSpaceRay in eventData.
87 /// </summary>
88 /// <param name="eventData"></param>
89 /// <param name="resultAppendList"></param>
90 public override void Raycast(PointerEventData eventData, List<RaycastResult> resultAppendList)
91 {
92 // This function is closely based on PhysicsRaycaster.Raycast
93
94 if (eventCamera == null)
95 return;
96
97 if (!eventData.IsVRPointer())
98 return;
99
100 var ray = eventData.GetRay();
101
102 float dist = eventCamera.farClipPlane - eventCamera.nearClipPlane;
103
104 var hits = Physics.RaycastAll(ray, dist, finalEventMask);
105
106 if (hits.Length > 1)
107 System.Array.Sort(hits, (r1, r2) => r1.distance.CompareTo(r2.distance));
108
109 if (hits.Length != 0)
110 {
111 for (int b = 0, bmax = hits.Length; b < bmax; ++b)
112 {
113 var result = new RaycastResult
114 {
115 gameObject = hits[b].collider.gameObject,
116 module = this,
117 distance = hits[b].distance,
118 index = resultAppendList.Count,
119 worldPosition = hits[0].point,
120 worldNormal = hits[0].normal,
121 };
122 resultAppendList.Add(result);
123 }
124 }
125 }
126
127 /// <summary>
128 /// Perform a Spherecast using the worldSpaceRay in eventData.
129 /// </summary>
130 /// <param name="eventData"></param>
131 /// <param name="resultAppendList"></param>
132 /// <param name="radius">Radius of the sphere</param>
133 public void Spherecast(PointerEventData eventData, List<RaycastResult> resultAppendList, float radius)
134 {
135 if (eventCamera == null)
136 return;
137
138 if (!eventData.IsVRPointer())
139 return;
140
141 var ray = eventData.GetRay();
142
143
144 float dist = eventCamera.farClipPlane - eventCamera.nearClipPlane;
145
146 var hits = Physics.SphereCastAll(ray, radius, dist, finalEventMask);
147
148 if (hits.Length > 1)
149 System.Array.Sort(hits, (r1, r2) => r1.distance.CompareTo(r2.distance));
150
151 if (hits.Length != 0)
152 {
153 for (int b = 0, bmax = hits.Length; b < bmax; ++b)
154 {
155 var result = new RaycastResult
156 {
157 gameObject = hits[b].collider.gameObject,
158 module = this,
159 distance = hits[b].distance,
160 index = resultAppendList.Count,
161 worldPosition = hits[0].point,
162 worldNormal = hits[0].normal,
163 };
164 resultAppendList.Add(result);
165 }
166 }
167 }
168 /// <summary>
169 /// Get screen position of this world position as seen by the event camera of this OVRPhysicsRaycaster
170 /// </summary>
171 /// <param name="worldPosition"></param>
172 /// <returns></returns>
173 public Vector2 GetScreenPos(Vector3 worldPosition)
174 {
175 // In future versions of Uinty RaycastResult will contain screenPosition so this will not be necessary
176 return eventCamera.WorldToScreenPoint(worldPosition);
177 }
178 }
179}