OVRMesh.cs
3.77 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
/************************************************************************************
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;
[DefaultExecutionOrder(-80)]
public class OVRMesh : MonoBehaviour
{
public interface IOVRMeshDataProvider
{
MeshType GetMeshType();
}
public enum MeshType
{
None = OVRPlugin.MeshType.None,
HandLeft = OVRPlugin.MeshType.HandLeft,
HandRight = OVRPlugin.MeshType.HandRight,
}
[SerializeField]
private IOVRMeshDataProvider _dataProvider;
[SerializeField]
private MeshType _meshType = MeshType.None;
private Mesh _mesh;
public bool IsInitialized { get; private set; }
public Mesh Mesh
{
get { return _mesh; }
}
private void Awake()
{
if (_dataProvider == null)
{
_dataProvider = GetComponent<IOVRMeshDataProvider>();
}
if (_dataProvider != null)
{
_meshType = _dataProvider.GetMeshType();
}
if (ShouldInitialize())
{
Initialize(_meshType);
}
}
private bool ShouldInitialize()
{
if (IsInitialized)
{
return false;
}
if (_meshType == MeshType.None)
{
return false;
}
else if (_meshType == MeshType.HandLeft || _meshType == MeshType.HandRight)
{
#if UNITY_EDITOR
return OVRInput.IsControllerConnected(OVRInput.Controller.Hands);
#else
return true;
#endif
}
else
{
return true;
}
}
private void Initialize(MeshType meshType)
{
_mesh = new Mesh();
var ovrpMesh = new OVRPlugin.Mesh();
if (OVRPlugin.GetMesh((OVRPlugin.MeshType)_meshType, out ovrpMesh))
{
var vertices = new Vector3[ovrpMesh.NumVertices];
for (int i = 0; i < ovrpMesh.NumVertices; ++i)
{
vertices[i] = ovrpMesh.VertexPositions[i].FromFlippedXVector3f();
}
_mesh.vertices = vertices;
var uv = new Vector2[ovrpMesh.NumVertices];
for (int i = 0; i < ovrpMesh.NumVertices; ++i)
{
uv[i] = new Vector2(ovrpMesh.VertexUV0[i].x, -ovrpMesh.VertexUV0[i].y);
}
_mesh.uv = uv;
var triangles = new int[ovrpMesh.NumIndices];
for (int i = 0; i < ovrpMesh.NumIndices; ++i)
{
triangles[i] = ovrpMesh.Indices[ovrpMesh.NumIndices - i - 1];
}
_mesh.triangles = triangles;
var normals = new Vector3[ovrpMesh.NumVertices];
for (int i = 0; i < ovrpMesh.NumVertices; ++i)
{
normals[i] = ovrpMesh.VertexNormals[i].FromFlippedXVector3f();
}
_mesh.normals = normals;
var boneWeights = new BoneWeight[ovrpMesh.NumVertices];
for (int i = 0; i < ovrpMesh.NumVertices; ++i)
{
var currentBlendWeight = ovrpMesh.BlendWeights[i];
var currentBlendIndices = ovrpMesh.BlendIndices[i];
boneWeights[i].boneIndex0 = (int)currentBlendIndices.x;
boneWeights[i].weight0 = currentBlendWeight.x;
boneWeights[i].boneIndex1 = (int)currentBlendIndices.y;
boneWeights[i].weight1 = currentBlendWeight.y;
boneWeights[i].boneIndex2 = (int)currentBlendIndices.z;
boneWeights[i].weight2 = currentBlendWeight.z;
boneWeights[i].boneIndex3 = (int)currentBlendIndices.w;
boneWeights[i].weight3 = currentBlendWeight.w;
}
_mesh.boneWeights = boneWeights;
IsInitialized = true;
}
}
#if UNITY_EDITOR
private void Update()
{
if (ShouldInitialize())
{
Initialize(_meshType);
}
}
#endif
}