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.

171 lines
4.8 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. 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. public bool ShouldUseSystemGestureMaterial { get; set; }
  27. }
  28. public enum ConfidenceBehavior
  29. {
  30. None,
  31. ToggleRenderer,
  32. }
  33. public enum SystemGestureBehavior
  34. {
  35. None,
  36. SwapMaterial,
  37. }
  38. [SerializeField]
  39. private IOVRMeshRendererDataProvider _dataProvider;
  40. [SerializeField]
  41. private OVRMesh _ovrMesh;
  42. [SerializeField]
  43. private OVRSkeleton _ovrSkeleton;
  44. [SerializeField]
  45. private ConfidenceBehavior _confidenceBehavior = ConfidenceBehavior.ToggleRenderer;
  46. [SerializeField]
  47. private SystemGestureBehavior _systemGestureBehavior = SystemGestureBehavior.SwapMaterial;
  48. [SerializeField]
  49. private Material _systemGestureMaterial = null;
  50. private Material _originalMaterial = null;
  51. private SkinnedMeshRenderer _skinnedMeshRenderer;
  52. public bool IsInitialized { get; private set; }
  53. public bool IsDataValid { get; private set; }
  54. public bool IsDataHighConfidence { get; private set; }
  55. public bool ShouldUseSystemGestureMaterial { get; private set; }
  56. private void Awake()
  57. {
  58. if (_dataProvider == null)
  59. {
  60. _dataProvider = GetComponent<IOVRMeshRendererDataProvider>();
  61. }
  62. if (_ovrMesh == null)
  63. {
  64. _ovrMesh = GetComponent<OVRMesh>();
  65. }
  66. if (_ovrSkeleton == null)
  67. {
  68. _ovrSkeleton = GetComponent<OVRSkeleton>();
  69. }
  70. }
  71. private void Start()
  72. {
  73. if (_ovrMesh == null)
  74. {
  75. this.enabled = false;
  76. return;
  77. }
  78. Initialize();
  79. }
  80. private void Initialize()
  81. {
  82. _skinnedMeshRenderer = GetComponent<SkinnedMeshRenderer>();
  83. if (!_skinnedMeshRenderer)
  84. {
  85. _skinnedMeshRenderer = gameObject.AddComponent<SkinnedMeshRenderer>();
  86. }
  87. _skinnedMeshRenderer.sharedMesh = _ovrMesh.Mesh;
  88. _originalMaterial = _skinnedMeshRenderer.sharedMaterial;
  89. if (_ovrSkeleton != null)
  90. {
  91. int numSkinnableBones = _ovrSkeleton.GetCurrentNumSkinnableBones();
  92. var bindPoses = new Matrix4x4[numSkinnableBones];
  93. var bones = new Transform[numSkinnableBones];
  94. var localToWorldMatrix = transform.localToWorldMatrix;
  95. for (int i = 0; i < numSkinnableBones && i < _ovrSkeleton.Bones.Count; ++i)
  96. {
  97. bones[i] = _ovrSkeleton.Bones[i].Transform;
  98. bindPoses[i] = _ovrSkeleton.BindPoses[i].Transform.worldToLocalMatrix * localToWorldMatrix;
  99. }
  100. _ovrMesh.Mesh.bindposes = bindPoses;
  101. _skinnedMeshRenderer.bones = bones;
  102. _skinnedMeshRenderer.updateWhenOffscreen = true;
  103. }
  104. IsInitialized = true;
  105. }
  106. private void Update()
  107. {
  108. IsDataValid = false;
  109. IsDataHighConfidence = false;
  110. ShouldUseSystemGestureMaterial = false;
  111. if (IsInitialized)
  112. {
  113. bool shouldRender = false;
  114. if (_dataProvider != null)
  115. {
  116. var data = _dataProvider.GetMeshRendererData();
  117. IsDataValid = data.IsDataValid;
  118. IsDataHighConfidence = data.IsDataHighConfidence;
  119. ShouldUseSystemGestureMaterial = data.ShouldUseSystemGestureMaterial;
  120. shouldRender = data.IsDataValid && data.IsDataHighConfidence;
  121. }
  122. if (_confidenceBehavior == ConfidenceBehavior.ToggleRenderer)
  123. {
  124. if (_skinnedMeshRenderer != null && _skinnedMeshRenderer.enabled != shouldRender)
  125. {
  126. _skinnedMeshRenderer.enabled = shouldRender;
  127. }
  128. }
  129. if (_systemGestureBehavior == SystemGestureBehavior.SwapMaterial)
  130. {
  131. if (_skinnedMeshRenderer != null)
  132. {
  133. if (ShouldUseSystemGestureMaterial && _systemGestureMaterial != null && _skinnedMeshRenderer.sharedMaterial != _systemGestureMaterial)
  134. {
  135. _skinnedMeshRenderer.sharedMaterial = _systemGestureMaterial;
  136. }
  137. else if (!ShouldUseSystemGestureMaterial && _originalMaterial != null && _skinnedMeshRenderer.sharedMaterial != _originalMaterial)
  138. {
  139. _skinnedMeshRenderer.sharedMaterial = _originalMaterial;
  140. }
  141. }
  142. }
  143. }
  144. }
  145. }