DemoObjectController6DoF.cs
6.93 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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
//-----------------------------------------------------------------------
// <copyright file="DemoObjectController6DoF.cs" company="Google LLC">
// Copyright 2018 Google LLC. 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>
//-----------------------------------------------------------------------
namespace GoogleVR.Beta.Demos
{
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.EventSystems;
/// <summary>A demo manager object for demos involving two 6DoF controllers.</summary>
public class DemoObjectController6DoF : MonoBehaviour
{
/// <summary>
/// The material to use when a controller-interactable object is inactive.
/// </summary>
public Material inactiveMaterial;
/// <summary>
/// The material to use when a controller-interactable object is being gazed at.
/// </summary>
public Material gazedAtMaterial;
private Vector3 startingPosition;
private Vector3 startingScale;
private bool isLockedToController;
private Renderer myRenderer;
private GvrTrackedController grabController;
/// <summary>
/// Sets the `startingPosition` and `startingScale` to the current position and scale,
/// respectively.
/// </summary>
public void UpdateStartPosition()
{
startingPosition = transform.position;
startingScale = transform.localScale;
}
/// <summary>
/// Hook this up to Event Trigger `PointerEnter` with the checkbox checked and `PointerExit`
/// with the checkbox unchecked.
/// </summary>
/// <param name="gazedAt">
/// Value `true` if the object is being gazed at, `false` otherwise.
/// </param>
public void SetGazedAt(bool gazedAt)
{
if (inactiveMaterial != null && gazedAtMaterial != null)
{
myRenderer.material = gazedAt ? gazedAtMaterial : inactiveMaterial;
}
}
/// <summary>Hook this up to Event Trigger `PointerDown`.</summary>
/// <param name="eventData">The PointerData of the triggering Grip Start event.</param>
public void GripStartGrab(BaseEventData eventData)
{
PointerEventData ptrEventData = eventData as PointerEventData;
if (ptrEventData != null &&
(ptrEventData.GvrGetButtonsDown() & GvrControllerButton.Grip) != 0)
{
grabController =
GvrPointerInputModule.Pointer.GetComponentInParent<GvrTrackedController>();
isLockedToController = false;
}
}
/// <summary>Hook this up to Event Trigger `PointerUp`.</summary>
/// <param name="eventData">The `PointerData` of the triggering Grip End event.</param>
public void GripEndGrab(BaseEventData eventData)
{
PointerEventData ptrEventData = eventData as PointerEventData;
if (ptrEventData != null &&
(ptrEventData.GvrGetButtonsDown() & GvrControllerButton.Grip) != 0)
{
grabController = null;
isLockedToController = false;
}
}
/// <summary>Hook this up to Event Trigger `PointerClick`.</summary>
/// <param name="eventData">The `PointerData` of the triggering Click event.</param>
public void ClickTeleport(BaseEventData eventData)
{
PointerEventData ptrEventData = eventData as PointerEventData;
if (ptrEventData != null &&
ptrEventData.button == PointerEventData.InputButton.Left)
{
TeleportRandomly();
}
}
private void Start()
{
startingPosition = transform.position;
startingScale = transform.localScale;
myRenderer = GetComponent<Renderer>();
SetGazedAt(false);
}
private void Update()
{
Vector3 targetPos = startingPosition;
Quaternion targetRotation = Quaternion.identity;
Vector3 targetScale = startingScale;
if (grabController != null)
{
targetRotation = grabController.transform.rotation;
targetPos = grabController.transform.position;
// Offset the object 15cm down the pointing axis of the controller
// to place it in front of the controller.
targetPos += targetRotation * Vector3.forward * 0.20f;
// Shrink the object down a bit while "gripped".
targetScale *= 0.5f;
if (Vector3.Distance(targetPos, transform.position) < 0.01f)
{
isLockedToController = true;
}
}
float interpAmount = 1;
if (!isLockedToController)
{
interpAmount = 1 - Mathf.Pow(0.01f, 4 * Time.deltaTime);
}
transform.position = Vector3.Lerp(transform.position, targetPos, interpAmount);
transform.rotation = Quaternion.Slerp(transform.rotation, targetRotation, interpAmount);
transform.localScale = Vector3.Lerp(transform.localScale, targetScale, interpAmount);
}
private void TeleportRandomly()
{
// Pick a random sibling, move them somewhere random, activate them,
// deactivate ourself.
int sibIdx = transform.GetSiblingIndex();
int numSibs = transform.parent.childCount;
sibIdx = (sibIdx + Random.Range(1, numSibs)) % numSibs;
GameObject randomSib = transform.parent.GetChild(sibIdx).gameObject;
// Move to random new location ±90˚ horzontal.
Vector3 direction = Quaternion.Euler(
0,
Random.Range(-90, 90),
0) * Vector3.forward;
// New location between 1m and 2m.
float distance = Random.Range(1, 2);
Vector3 newPos = direction * distance;
// Limit vertical position to be fully in the room.
newPos.y = Mathf.Clamp(newPos.y, -1.2f, 4f);
randomSib.transform.localPosition = newPos;
randomSib.GetComponent<DemoObjectController6DoF>().UpdateStartPosition();
randomSib.SetActive(true);
gameObject.SetActive(false);
SetGazedAt(false);
}
}
}