GvrBasePointerRaycaster.cs
4.35 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
//-----------------------------------------------------------------------
// <copyright file="GvrBasePointerRaycaster.cs" company="Google Inc.">
// Copyright 2016 Google Inc. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// </copyright>
//-----------------------------------------------------------------------
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.EventSystems;
/// <summary>This script provides shared functionality used by all Gvr raycasters.</summary>
public abstract class GvrBasePointerRaycaster : BaseRaycaster
{
private GvrBasePointer.PointerRay lastRay;
/// <summary>
/// Initializes a new instance of the <see cref="GvrBasePointerRaycaster" /> class.
/// </summary>
protected GvrBasePointerRaycaster()
{
}
/// <summary>Gets the mode used for raycasting.</summary>
/// <value>The mode used for raycasting.</value>
protected GvrBasePointer.RaycastMode CurrentRaycastModeForHybrid { get; private set; }
/// <summary>
/// Gets the last ray created.
/// </summary>
/// <returns>The last ray created.</returns>
public GvrBasePointer.PointerRay GetLastRay()
{
return lastRay;
}
/// <summary>Raycast against the scene.</summary>
/// <param name="eventData">The pointer event data.</param>
/// <param name="resultAppendList">The result of the raycast is appended to this list.</param>
public override void Raycast(PointerEventData eventData, List<RaycastResult> resultAppendList)
{
GvrBasePointer pointer = GvrPointerInputModule.Pointer;
if (pointer == null || !pointer.IsAvailable)
{
return;
}
if (pointer.raycastMode == GvrBasePointer.RaycastMode.Hybrid)
{
RaycastHybrid(pointer, eventData, resultAppendList);
}
else
{
RaycastDefault(pointer, eventData, resultAppendList);
}
}
/// <summary>Perform raycast on the scene.</summary>
/// <param name="pointerRay">The ray to use for the operation.</param>
/// <param name="radius">The radius of the ray to use when testing for hits.</param>
/// <param name="eventData">The event data triggered by any resultant Raycast hits.</param>
/// <param name="resultAppendList">The results are appended to this list.</param>
/// <returns>Returns `true` if the Raycast has at least one hit, `false` otherwise.</returns>
protected abstract bool PerformRaycast(GvrBasePointer.PointerRay pointerRay,
float radius,
PointerEventData eventData,
List<RaycastResult> resultAppendList);
private void RaycastHybrid(GvrBasePointer pointer,
PointerEventData eventData,
List<RaycastResult> resultAppendList)
{
CurrentRaycastModeForHybrid = GvrBasePointer.RaycastMode.Direct;
lastRay = GvrBasePointer.CalculateHybridRay(pointer, CurrentRaycastModeForHybrid);
float radius = pointer.CurrentPointerRadius;
bool foundHit = PerformRaycast(lastRay, radius, eventData, resultAppendList);
if (!foundHit)
{
CurrentRaycastModeForHybrid = GvrBasePointer.RaycastMode.Camera;
lastRay = GvrBasePointer.CalculateHybridRay(pointer, CurrentRaycastModeForHybrid);
PerformRaycast(lastRay, radius, eventData, resultAppendList);
}
}
private void RaycastDefault(GvrBasePointer pointer,
PointerEventData eventData,
List<RaycastResult> resultAppendList)
{
lastRay = GvrBasePointer.CalculateRay(pointer, pointer.raycastMode);
float radius = pointer.CurrentPointerRadius;
PerformRaycast(lastRay, radius, eventData, resultAppendList);
}
}