OVRMesh.cs 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. /************************************************************************************
  2. Copyright : Copyright (c) Facebook Technologies, LLC and its affiliates. All rights reserved.
  3. Licensed under the Oculus Utilities SDK License Version 1.31 (the "License"); you may not use
  4. the Utilities SDK except in compliance with the License, which is provided at the time of installation
  5. or download, or which otherwise accompanies this software in either electronic or hard copy form.
  6. You may obtain a copy of the License at
  7. https://developer.oculus.com/licenses/utilities-1.31
  8. Unless required by applicable law or agreed to in writing, the Utilities SDK distributed
  9. under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF
  10. ANY KIND, either express or implied. See the License for the specific language governing
  11. permissions and limitations under the License.
  12. ************************************************************************************/
  13. using System.Collections;
  14. using System.Collections.Generic;
  15. using UnityEngine;
  16. [DefaultExecutionOrder(-80)]
  17. public class OVRMesh : MonoBehaviour
  18. {
  19. public interface IOVRMeshDataProvider
  20. {
  21. MeshType GetMeshType();
  22. }
  23. public enum MeshType
  24. {
  25. None = OVRPlugin.MeshType.None,
  26. HandLeft = OVRPlugin.MeshType.HandLeft,
  27. HandRight = OVRPlugin.MeshType.HandRight,
  28. }
  29. [SerializeField]
  30. private IOVRMeshDataProvider _dataProvider;
  31. [SerializeField]
  32. private MeshType _meshType = MeshType.None;
  33. private Mesh _mesh;
  34. public Mesh Mesh
  35. {
  36. get { return _mesh; }
  37. }
  38. private void Awake()
  39. {
  40. if (_mesh != null)
  41. {
  42. // simply act as a mesh reference if a custom mesh is specified
  43. return;
  44. }
  45. if (_dataProvider == null)
  46. {
  47. _dataProvider = GetComponent<IOVRMeshDataProvider>();
  48. }
  49. if (_dataProvider != null)
  50. {
  51. _meshType = _dataProvider.GetMeshType();
  52. }
  53. if (_meshType != MeshType.None)
  54. {
  55. Initialize(_meshType);
  56. }
  57. }
  58. private void Initialize(MeshType meshType)
  59. {
  60. _mesh = new Mesh();
  61. var ovrpMesh = new OVRPlugin.Mesh();
  62. if (OVRPlugin.GetMesh((OVRPlugin.MeshType)_meshType, out ovrpMesh))
  63. {
  64. var vertices = new Vector3[ovrpMesh.NumVertices];
  65. for (int i = 0; i < ovrpMesh.NumVertices; ++i)
  66. {
  67. vertices[i] = ovrpMesh.VertexPositions[i].FromFlippedZVector3f();
  68. }
  69. _mesh.vertices = vertices;
  70. var uv = new Vector2[ovrpMesh.NumVertices];
  71. for (int i = 0; i < ovrpMesh.NumVertices; ++i)
  72. {
  73. uv[i] = new Vector2(ovrpMesh.VertexUV0[i].x, -ovrpMesh.VertexUV0[i].y);
  74. }
  75. _mesh.uv = uv;
  76. var triangles = new int[ovrpMesh.NumIndices];
  77. for (int i = 0; i < ovrpMesh.NumIndices; ++i)
  78. {
  79. triangles[i] = ovrpMesh.Indices[ovrpMesh.NumIndices - i - 1];
  80. }
  81. _mesh.triangles = triangles;
  82. var normals = new Vector3[ovrpMesh.NumVertices];
  83. for (int i = 0; i < ovrpMesh.NumVertices; ++i)
  84. {
  85. normals[i] = ovrpMesh.VertexNormals[i].FromFlippedZVector3f();
  86. }
  87. _mesh.normals = normals;
  88. var boneWeights = new BoneWeight[ovrpMesh.NumVertices];
  89. for (int i = 0; i < ovrpMesh.NumVertices; ++i)
  90. {
  91. var currentBlendWeight = ovrpMesh.BlendWeights[i];
  92. var currentBlendIndices = ovrpMesh.BlendIndices[i];
  93. boneWeights[i].boneIndex0 = (int)currentBlendIndices.x;
  94. boneWeights[i].weight0 = currentBlendWeight.x;
  95. boneWeights[i].boneIndex1 = (int)currentBlendIndices.y;
  96. boneWeights[i].weight1 = currentBlendWeight.y;
  97. boneWeights[i].boneIndex2 = (int)currentBlendIndices.z;
  98. boneWeights[i].weight2 = currentBlendWeight.z;
  99. boneWeights[i].boneIndex3 = (int)currentBlendIndices.w;
  100. boneWeights[i].weight3 = currentBlendWeight.w;
  101. }
  102. _mesh.boneWeights = boneWeights;
  103. }
  104. }
  105. }