OVRMeshRenderer.cs 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  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. public class OVRMeshRenderer : MonoBehaviour
  17. {
  18. public interface IOVRMeshRendererDataProvider
  19. {
  20. MeshRendererData GetMeshRendererData();
  21. }
  22. public struct MeshRendererData
  23. {
  24. public bool IsDataValid { get; set; }
  25. public bool IsDataHighConfidence { get; set; }
  26. }
  27. [SerializeField]
  28. private IOVRMeshRendererDataProvider _dataProvider;
  29. [SerializeField]
  30. private OVRMesh _ovrMesh;
  31. [SerializeField]
  32. private OVRSkeleton _ovrSkeleton;
  33. private SkinnedMeshRenderer _skinnedMeshRenderer;
  34. private bool _isInitialized;
  35. private void Awake()
  36. {
  37. if (_dataProvider == null)
  38. {
  39. _dataProvider = GetComponent<IOVRMeshRendererDataProvider>();
  40. }
  41. if (_ovrMesh == null)
  42. {
  43. _ovrMesh = GetComponent<OVRMesh>();
  44. }
  45. if (_ovrSkeleton == null)
  46. {
  47. _ovrSkeleton = GetComponent<OVRSkeleton>();
  48. }
  49. }
  50. private void Start()
  51. {
  52. if (_ovrMesh == null)
  53. {
  54. this.enabled = false;
  55. return;
  56. }
  57. Initialize();
  58. }
  59. private void Initialize()
  60. {
  61. _skinnedMeshRenderer = GetComponent<SkinnedMeshRenderer>();
  62. if (!_skinnedMeshRenderer)
  63. {
  64. _skinnedMeshRenderer = gameObject.AddComponent<SkinnedMeshRenderer>();
  65. }
  66. _skinnedMeshRenderer.sharedMesh = _ovrMesh.Mesh;
  67. if (_ovrSkeleton != null)
  68. {
  69. int numSkinnableBones = _ovrSkeleton.GetCurrentNumSkinnableBones();
  70. var bindPoses = new Matrix4x4[numSkinnableBones];
  71. var bones = new Transform[numSkinnableBones];
  72. var localToWorldMatrix = transform.localToWorldMatrix;
  73. for (int i = 0; i < numSkinnableBones && i < _ovrSkeleton.Bones.Count; ++i)
  74. {
  75. bones[i] = _ovrSkeleton.Bones[i].Transform;
  76. bindPoses[i] = _ovrSkeleton.BindPoses[i].Transform.worldToLocalMatrix * localToWorldMatrix;
  77. }
  78. _ovrMesh.Mesh.bindposes = bindPoses;
  79. _skinnedMeshRenderer.bones = bones;
  80. _skinnedMeshRenderer.updateWhenOffscreen = true;
  81. }
  82. _isInitialized = true;
  83. }
  84. private void Update()
  85. {
  86. if (_isInitialized)
  87. {
  88. bool shouldRender = false;
  89. if (_dataProvider != null)
  90. {
  91. var data = _dataProvider.GetMeshRendererData();
  92. shouldRender = data.IsDataValid && data.IsDataHighConfidence;
  93. }
  94. if (_skinnedMeshRenderer != null && _skinnedMeshRenderer.enabled != shouldRender)
  95. {
  96. _skinnedMeshRenderer.enabled = shouldRender;
  97. }
  98. }
  99. }
  100. }