OVRSkeletonRenderer.cs 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  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(-70)]
  17. public class OVRSkeletonRenderer : MonoBehaviour
  18. {
  19. public interface IOVRSkeletonRendererDataProvider
  20. {
  21. SkeletonRendererData GetSkeletonRendererData();
  22. }
  23. public struct SkeletonRendererData
  24. {
  25. public float RootScale { get; set; }
  26. public bool IsDataValid { get; set; }
  27. public bool IsDataHighConfidence { get; set; }
  28. }
  29. [SerializeField]
  30. private IOVRSkeletonRendererDataProvider _dataProvider;
  31. [SerializeField]
  32. private Material _skeletonMaterial;
  33. private const float LINE_RENDERER_WIDTH = 0.005f;
  34. private List<BoneVisualization> _boneVisualizations;
  35. private OVRSkeleton _ovrSkeleton;
  36. private GameObject _skeletonGO;
  37. private float _scale;
  38. private bool _isInitialized;
  39. private class BoneVisualization
  40. {
  41. public GameObject BoneGO;
  42. public Transform BoneBegin;
  43. public Transform BoneEnd;
  44. public LineRenderer Line;
  45. public BoneVisualization(GameObject rootGO, Material mat, float scale, Transform begin, Transform end)
  46. {
  47. BoneBegin = begin;
  48. BoneEnd = end;
  49. BoneGO = new GameObject(begin.name);
  50. BoneGO.transform.SetParent(rootGO.transform, false);
  51. Line = BoneGO.AddComponent<LineRenderer>();
  52. Line.sharedMaterial = mat;
  53. Line.useWorldSpace = true;
  54. Line.positionCount = 2;
  55. Line.SetPosition(0, BoneBegin.position);
  56. Line.SetPosition(1, BoneEnd.position);
  57. Line.startWidth = LINE_RENDERER_WIDTH * scale;
  58. Line.endWidth = LINE_RENDERER_WIDTH * scale;
  59. }
  60. public void Update(float scale, bool shouldRender)
  61. {
  62. Line.enabled = shouldRender;
  63. Line.SetPosition(0, BoneBegin.position);
  64. Line.SetPosition(1, BoneEnd.position);
  65. Line.startWidth = LINE_RENDERER_WIDTH * scale;
  66. Line.endWidth = LINE_RENDERER_WIDTH * scale;
  67. }
  68. }
  69. private void Awake()
  70. {
  71. if (_dataProvider == null)
  72. {
  73. _dataProvider = GetComponent<IOVRSkeletonRendererDataProvider>();
  74. }
  75. if (_ovrSkeleton == null)
  76. {
  77. _ovrSkeleton = GetComponent<OVRSkeleton>();
  78. }
  79. }
  80. private void Start()
  81. {
  82. if (_ovrSkeleton == null)
  83. {
  84. this.enabled = false;
  85. return;
  86. }
  87. Initialize();
  88. }
  89. private void Initialize()
  90. {
  91. _boneVisualizations = new List<BoneVisualization>();
  92. _ovrSkeleton = GetComponent<OVRSkeleton>();
  93. _skeletonGO = new GameObject("SkeletonRenderer");
  94. _skeletonGO.transform.SetParent(transform, false);
  95. if (_skeletonMaterial == null)
  96. {
  97. _skeletonMaterial = new Material(Shader.Find("Diffuse"));
  98. }
  99. for (int i = 0; i < _ovrSkeleton.Bones.Count; i++)
  100. {
  101. var boneVis = new BoneVisualization(
  102. _skeletonGO,
  103. _skeletonMaterial,
  104. _scale,
  105. _ovrSkeleton.Bones[i].Transform,
  106. _ovrSkeleton.Bones[i].Transform.parent);
  107. _boneVisualizations.Add(boneVis);
  108. }
  109. _isInitialized = true;
  110. }
  111. public void Update()
  112. {
  113. if (_isInitialized)
  114. {
  115. bool shouldRender = false;
  116. if (_dataProvider != null)
  117. {
  118. var data = _dataProvider.GetSkeletonRendererData();
  119. shouldRender = data.IsDataValid && data.IsDataHighConfidence;
  120. if (data.IsDataValid)
  121. {
  122. _scale = data.RootScale;
  123. }
  124. }
  125. for (int i = 0; i < _boneVisualizations.Count; i++)
  126. {
  127. _boneVisualizations[i].Update(_scale, shouldRender);
  128. }
  129. }
  130. }
  131. }