using System;
using System.Collections.Generic;
using System.Linq;
using UnityEditor;
using UnityEngine;
using UnityEngine.Serialization;
namespace UnityEngine.Perception.GroundTruth
{
public enum SelfOcclusionDistanceSource
{
JointLabel,
KeypointLabeler
}
///
/// Label to designate a custom joint/keypoint. These are needed to add body
/// parts to a humanoid model that are not contained in its
///
[AddComponentMenu("Perception/Labeling/Joint Label")]
[Serializable]
public class JointLabel : MonoBehaviour, ISerializationCallbackReceiver
{
private static PerceptionCamera singlePerceptionCamera = null;
///
/// 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]
private 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 (singlePerceptionCamera == null)
{
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 (singlePerceptionCamera == null)
{
occlusionDistance = KeypointDefinition.defaultSelfOcclusionDistance;
}
else
{
var keypointLabeler = (KeypointLabeler) 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);
}
}
}