OVRMeshRenderer.cs
5.07 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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
/************************************************************************************
Copyright : Copyright (c) Facebook Technologies, LLC and its affiliates. All rights reserved.
Your use of this SDK or tool is subject to the Oculus SDK License Agreement, available at
https://developer.oculus.com/licenses/oculussdk/
Unless required by applicable law or agreed to in writing, the Utilities SDK 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.
************************************************************************************/
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class OVRMeshRenderer : MonoBehaviour
{
public interface IOVRMeshRendererDataProvider
{
MeshRendererData GetMeshRendererData();
}
public struct MeshRendererData
{
public bool IsDataValid { get; set; }
public bool IsDataHighConfidence { get; set; }
public bool ShouldUseSystemGestureMaterial { get; set; }
}
public enum ConfidenceBehavior
{
None,
ToggleRenderer,
}
public enum SystemGestureBehavior
{
None,
SwapMaterial,
}
[SerializeField]
private IOVRMeshRendererDataProvider _dataProvider;
[SerializeField]
private OVRMesh _ovrMesh;
[SerializeField]
private OVRSkeleton _ovrSkeleton;
[SerializeField]
private ConfidenceBehavior _confidenceBehavior = ConfidenceBehavior.ToggleRenderer;
[SerializeField]
private SystemGestureBehavior _systemGestureBehavior = SystemGestureBehavior.SwapMaterial;
[SerializeField]
private Material _systemGestureMaterial = null;
private Material _originalMaterial = null;
private SkinnedMeshRenderer _skinnedMeshRenderer;
public bool IsInitialized { get; private set; }
public bool IsDataValid { get; private set; }
public bool IsDataHighConfidence { get; private set; }
public bool ShouldUseSystemGestureMaterial { get; private set; }
private void Awake()
{
if (_dataProvider == null)
{
_dataProvider = GetComponent<IOVRMeshRendererDataProvider>();
}
if (_ovrMesh == null)
{
_ovrMesh = GetComponent<OVRMesh>();
}
if (_ovrSkeleton == null)
{
_ovrSkeleton = GetComponent<OVRSkeleton>();
}
}
private void Start()
{
if (_ovrMesh == null)
{
// disable if no mesh configured
this.enabled = false;
return;
}
if (ShouldInitialize())
{
Initialize();
}
}
private bool ShouldInitialize()
{
if (IsInitialized)
{
return false;
}
if ((_ovrMesh == null) || ((_ovrMesh != null) && !_ovrMesh.IsInitialized) || ((_ovrSkeleton != null) && !_ovrSkeleton.IsInitialized))
{
// do not initialize if mesh or optional skeleton are not initialized
return false;
}
return true;
}
private void Initialize()
{
_skinnedMeshRenderer = GetComponent<SkinnedMeshRenderer>();
if (!_skinnedMeshRenderer)
{
_skinnedMeshRenderer = gameObject.AddComponent<SkinnedMeshRenderer>();
}
_skinnedMeshRenderer.sharedMesh = _ovrMesh.Mesh;
_originalMaterial = _skinnedMeshRenderer.sharedMaterial;
if ((_ovrSkeleton != null))
{
int numSkinnableBones = _ovrSkeleton.GetCurrentNumSkinnableBones();
var bindPoses = new Matrix4x4[numSkinnableBones];
var bones = new Transform[numSkinnableBones];
var localToWorldMatrix = transform.localToWorldMatrix;
for (int i = 0; i < numSkinnableBones && i < _ovrSkeleton.Bones.Count; ++i)
{
bones[i] = _ovrSkeleton.Bones[i].Transform;
bindPoses[i] = _ovrSkeleton.BindPoses[i].Transform.worldToLocalMatrix * localToWorldMatrix;
}
_ovrMesh.Mesh.bindposes = bindPoses;
_skinnedMeshRenderer.bones = bones;
_skinnedMeshRenderer.updateWhenOffscreen = true;
}
IsInitialized = true;
}
private void Update()
{
#if UNITY_EDITOR
if (ShouldInitialize())
{
Initialize();
}
#endif
IsDataValid = false;
IsDataHighConfidence = false;
ShouldUseSystemGestureMaterial = false;
if (IsInitialized)
{
bool shouldRender = false;
if (_dataProvider != null)
{
var data = _dataProvider.GetMeshRendererData();
IsDataValid = data.IsDataValid;
IsDataHighConfidence = data.IsDataHighConfidence;
ShouldUseSystemGestureMaterial = data.ShouldUseSystemGestureMaterial;
shouldRender = data.IsDataValid && data.IsDataHighConfidence;
}
if (_confidenceBehavior == ConfidenceBehavior.ToggleRenderer)
{
if (_skinnedMeshRenderer != null && _skinnedMeshRenderer.enabled != shouldRender)
{
_skinnedMeshRenderer.enabled = shouldRender;
}
}
if (_systemGestureBehavior == SystemGestureBehavior.SwapMaterial)
{
if (_skinnedMeshRenderer != null)
{
if (ShouldUseSystemGestureMaterial && _systemGestureMaterial != null && _skinnedMeshRenderer.sharedMaterial != _systemGestureMaterial)
{
_skinnedMeshRenderer.sharedMaterial = _systemGestureMaterial;
}
else if (!ShouldUseSystemGestureMaterial && _originalMaterial != null && _skinnedMeshRenderer.sharedMaterial != _originalMaterial)
{
_skinnedMeshRenderer.sharedMaterial = _originalMaterial;
}
}
}
}
}
}