using System; using Unity.Simulation; using UnityEngine.EventSystems; using UnityEngine.Rendering; using UnityEngine.UI; using UnityEngine.UIElements; namespace UnityEngine.Perception.GroundTruth { /// /// Abstract class for defining custom annotation and metric generation to be run by . /// Instances of CameraLabeler on will be invoked each frame the camera is /// set to capture data (see ). /// [Serializable] public abstract class CameraLabeler { /// /// A human-readable description of the labeler /// public abstract string description { get; protected set; } /// /// Whether the CameraLabeler should be set up and called each frame. /// public bool enabled = true; internal bool isInitialized { get; private set; } /// /// Labelers should set this in their setup to define if they support realtime /// visualization of their data. /// protected abstract bool supportsVisualization { get; } /// /// The heads up display (HUD) panel. Generally used to add stats to the display. /// public HUDPanel hudPanel => perceptionCamera != null ? perceptionCamera.hudPanel : null; /// /// The overlay panel. Used to control which full screen image visual is displayed. /// public OverlayPanel overlayPanel => perceptionCamera != null ? perceptionCamera.overlayPanel : null; /// /// The that contains this labeler. /// protected PerceptionCamera perceptionCamera { get; private set; } /// /// The SensorHandle for the that contains this labeler. Use this to report /// annotations and metrics. /// protected SensorHandle sensorHandle { get; private set; } /// /// Called just before the first call to or . Implement this /// to initialize state. /// protected virtual void Setup() { } /// /// Called when the labeler's visualization capability is turned on or off. /// /// The current enabled state of the visualizer protected virtual void OnVisualizerEnabledChanged(bool visualizerEnabled) {} /// /// Called during the Update each frame the the labeler is enabled and is true. /// protected virtual void OnUpdate() {} /// /// Called just before the camera renders each frame the the labeler is enabled and is true. /// /// The current context from the Scriptable Render Pipeline. protected virtual void OnBeginRendering(ScriptableRenderContext scriptableRenderContext) {} /// /// Called just after the camera renders each frame the the labeler is enabled and is true. /// /// The current context from the Scriptable Render Pipeline. protected virtual void OnEndRendering(ScriptableRenderContext scriptableRenderContext) {} /// /// Labeling pass to display labeler's visualization components, if applicable. Important note, all labeler's visualizations need /// to use Unity's Immediate Mode GUI (IMGUI) system. /// This called is triggered from call. This call happens immediately before /// so that the visualization components are drawn below the UI elements. /// protected virtual void OnVisualize() {} /// /// In this pass, a labeler can add custom GUI controls to the scene. Important note, all labeler's additional /// GUIs need to use Unity's Immediate Mode GUI (IMGUI) system. /// This called is triggered from call. This call happens immediately after the /// so that the visualization components are drawn below the UI elements. /// protected virtual void OnVisualizeAdditionalUI() {} /// /// Called when the Labeler is about to be destroyed or removed from the PerceptionCamera. Use this to clean up to state. /// protected virtual void Cleanup() {} /// /// Initializes labeler with the target perception camera /// /// The target perception camera public void Init(PerceptionCamera camera) { try { perceptionCamera = camera; sensorHandle = camera.SensorHandle; Setup(); isInitialized = true; m_ShowVisualizations = supportsVisualization && perceptionCamera.showVisualizations; } catch (Exception) { enabled = false; throw; } } internal void InternalSetup() => Setup(); internal bool InternalVisualizationEnabled { get => visualizationEnabled; set => visualizationEnabled = value; } internal void InternalOnUpdate() => OnUpdate(); internal void InternalOnBeginRendering(ScriptableRenderContext context) => OnBeginRendering(context); internal void InternalOnEndRendering(ScriptableRenderContext context) => OnEndRendering(context); internal void InternalCleanup() => Cleanup(); internal void InternalVisualize() => OnVisualize(); private bool m_ShowVisualizations = false; /// /// Turns on/off the labeler's realtime visualization capability. If a labeler does not support realtime /// visualization () or visualization is not enabled on the PerceptionCamera /// this will not function. /// internal bool visualizationEnabled { get { return supportsVisualization && m_ShowVisualizations; } set { if (!supportsVisualization) return; if (value != m_ShowVisualizations) { m_ShowVisualizations = value; OnVisualizerEnabledChanged(m_ShowVisualizations); } } } internal void VisualizeUI() { if (supportsVisualization && !(this is IOverlayPanelProvider)) { GUILayout.Label(GetType().Name); GUILayout.BeginHorizontal(); GUILayout.Space(10); GUILayout.Label("Enabled"); GUILayout.FlexibleSpace(); visualizationEnabled = GUILayout.Toggle(visualizationEnabled, ""); GUILayout.EndHorizontal(); if (visualizationEnabled) OnVisualizeAdditionalUI(); } } internal void Visualize() { if (visualizationEnabled) OnVisualize(); } } }