using System; using System.Collections.Generic; using System.Linq; using UnityEditor; using UnityEngine; using UnityEngine.Serialization; namespace UnityEngine.Perception.GroundTruth { /// /// Label to designate a custom joint/keypoint. These are needed to add body /// parts to a humanoid model that are not contained in its /// /// These label's can also be applied to the keypoints found in the to override the self /// occlusion tolerance values defined in the file. The /// defines the typical tolerances for a model, but certain models may need to have specific overrides on a keypoint /// for the self occlusion to work properly. /// [AddComponentMenu("Perception/Labeling/Joint Label")] [Serializable] public class JointLabel : MonoBehaviour, ISerializationCallbackReceiver { static PerceptionCamera s_SinglePerceptionCamera; /// /// Maps this joint to a joint in a /// [Serializable] class TemplateData { /// /// The name of the joint. /// public string label; }; /// /// List of all of the templates that this joint can be mapped to. /// [SerializeField] [HideInInspector] List templateInformation; /// /// List of all of the templates that this joint can be mapped to. /// [SerializeField] public List labels = new List(); /// /// Whether should be used instead of the one specified in the . /// public bool overrideSelfOcclusionDistance = false; /// /// Whether should be used instead of the one specified in the . /// public float selfOcclusionDistance = .15f; /// /// Internal method for serialization. /// void ISerializationCallbackReceiver.OnBeforeSerialize() { } /// /// Internal method for serialization. /// void ISerializationCallbackReceiver.OnAfterDeserialize() { if (templateInformation != null) { foreach (var data in templateInformation) { labels.Add(data.label); } templateInformation = null; } } private void OnDrawGizmos() { Gizmos.DrawIcon(transform.position, "Packages/com.unity.perception/Editor/Icons/Keypoint.png", false); } private void OnDrawGizmosSelected() { if (s_SinglePerceptionCamera == null) { s_SinglePerceptionCamera = FindObjectOfType(); } Mesh sphereMesh = null; #if UNITY_EDITOR var defaultAssets = UnityEditor.AssetDatabase.LoadAllAssetsAtPath("Library/unity default resources"); sphereMesh = (Mesh) defaultAssets.FirstOrDefault(a => a.name == "Sphere"); #endif float occlusionDistance; if (this.overrideSelfOcclusionDistance) { occlusionDistance = selfOcclusionDistance; } else { if (s_SinglePerceptionCamera == null) { occlusionDistance = KeypointDefinition.defaultSelfOcclusionDistance; } else { var keypointLabeler = (KeypointLabeler) s_SinglePerceptionCamera.labelers.FirstOrDefault(l => l is KeypointLabeler); var template = keypointLabeler?.activeTemplate; if (template == null) occlusionDistance = KeypointDefinition.defaultSelfOcclusionDistance; else { KeypointDefinition matchingKeypoint = null; foreach (var k in template.keypoints) { if (this.labels.Contains(k.label)) { matchingKeypoint = k; break; } } if (matchingKeypoint == null) occlusionDistance = KeypointDefinition.defaultSelfOcclusionDistance; else occlusionDistance = matchingKeypoint.selfOcclusionDistance; } } } var occlusionDistanceScale = transform.lossyScale * occlusionDistance; Gizmos.color = new Color(1, 1, 1, .25f); Gizmos.DrawMesh(sphereMesh, 0, transform.position, transform.rotation, occlusionDistanceScale * 2); } } }