Assignment for RMIT Mixed Reality in 2020
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

122 lines
3.5 KiB

  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 bool IsInitialized { get; private set; }
  35. public Mesh Mesh
  36. {
  37. get { return _mesh; }
  38. }
  39. private void Awake()
  40. {
  41. if (_dataProvider == null)
  42. {
  43. _dataProvider = GetComponent<IOVRMeshDataProvider>();
  44. }
  45. if (_dataProvider != null)
  46. {
  47. _meshType = _dataProvider.GetMeshType();
  48. }
  49. if (_meshType != MeshType.None)
  50. {
  51. Initialize(_meshType);
  52. }
  53. }
  54. private void Initialize(MeshType meshType)
  55. {
  56. _mesh = new Mesh();
  57. var ovrpMesh = new OVRPlugin.Mesh();
  58. if (OVRPlugin.GetMesh((OVRPlugin.MeshType)_meshType, out ovrpMesh))
  59. {
  60. var vertices = new Vector3[ovrpMesh.NumVertices];
  61. for (int i = 0; i < ovrpMesh.NumVertices; ++i)
  62. {
  63. vertices[i] = ovrpMesh.VertexPositions[i].FromFlippedXVector3f();
  64. }
  65. _mesh.vertices = vertices;
  66. var uv = new Vector2[ovrpMesh.NumVertices];
  67. for (int i = 0; i < ovrpMesh.NumVertices; ++i)
  68. {
  69. uv[i] = new Vector2(ovrpMesh.VertexUV0[i].x, -ovrpMesh.VertexUV0[i].y);
  70. }
  71. _mesh.uv = uv;
  72. var triangles = new int[ovrpMesh.NumIndices];
  73. for (int i = 0; i < ovrpMesh.NumIndices; ++i)
  74. {
  75. triangles[i] = ovrpMesh.Indices[ovrpMesh.NumIndices - i - 1];
  76. }
  77. _mesh.triangles = triangles;
  78. var normals = new Vector3[ovrpMesh.NumVertices];
  79. for (int i = 0; i < ovrpMesh.NumVertices; ++i)
  80. {
  81. normals[i] = ovrpMesh.VertexNormals[i].FromFlippedXVector3f();
  82. }
  83. _mesh.normals = normals;
  84. var boneWeights = new BoneWeight[ovrpMesh.NumVertices];
  85. for (int i = 0; i < ovrpMesh.NumVertices; ++i)
  86. {
  87. var currentBlendWeight = ovrpMesh.BlendWeights[i];
  88. var currentBlendIndices = ovrpMesh.BlendIndices[i];
  89. boneWeights[i].boneIndex0 = (int)currentBlendIndices.x;
  90. boneWeights[i].weight0 = currentBlendWeight.x;
  91. boneWeights[i].boneIndex1 = (int)currentBlendIndices.y;
  92. boneWeights[i].weight1 = currentBlendWeight.y;
  93. boneWeights[i].boneIndex2 = (int)currentBlendIndices.z;
  94. boneWeights[i].weight2 = currentBlendWeight.z;
  95. boneWeights[i].boneIndex3 = (int)currentBlendIndices.w;
  96. boneWeights[i].weight3 = currentBlendWeight.w;
  97. }
  98. _mesh.boneWeights = boneWeights;
  99. IsInitialized = true;
  100. }
  101. }
  102. }