using UnityEngine.Experimental.Rendering ;
using UnityEngine.Profiling ;
using UnityEngine.Rendering ;
using UnityEngine.Serialization ;
using UnityEngine.Perception.GroundTruth ;
namespace UnityEngine.Perception.GroundTruth
{
/// <summary>
/// The ID to use for object count annotations in the resulting dataset
/// </summary>
public string m_ObjectCountID = "51DA3C27-369D-4929-AEA6-D01614635CE2" ;
[FormerlySerializedAs("m_ObjectCountID")]
public string objectCountId = "51DA3C27-369D-4929-AEA6-D01614635CE2" ;
/// <summary>
/// Whether object bounding boxes should be computed
/// </summary>
/// </summary>
public string m_BoundingBoxID = "F9F22E05-443F-4602-A422-EBE4EA9B55CB" ;
[FormerlySerializedAs("m_BoundingBoxID")]
public string boundingBoxId = "F9F22E05-443F-4602-A422-EBE4EA9B55CB" ;
/// <summary>
/// Whether visible pixels should be computed for each labeled object
/// </summary>
/// </summary>
public string m_VisiblePixelsID = "5BA92024-B3B7-41A7-9D3F-C03A6A8DDD01" ;
[FormerlySerializedAs("m_VisiblePixelsID")]
public string visiblePixelsId = "5BA92024-B3B7-41A7-9D3F-C03A6A8DDD01" ;
/// <summary>
/// The corner of the image to use as the origin for bounding boxs.
/// </summary>
internal event Action < NativeSlice < uint > , IReadOnlyList < LabelingConfigurationEntry > , int > classCountsReceived ;
[NonSerialized]
internal RenderTexture m_L abelingTexture;
internal RenderTexture l abelingTexture;
internal RenderTexture m_S egmentationTexture;
internal RenderTexture s egmentationTexture;
RenderTextureReader < short > m_ClassLabelingTextureReader ;
RenderTextureReader < uint > m_SegmentationReader ;
produceBoundingBoxAnnotations = false ;
}
m_S egmentationTexture = new RenderTexture ( new RenderTextureDescriptor ( width , height , GraphicsFormat . R8G8B8A8_UNorm , 8 ) ) ;
m_S egmentationTexture. name = "Segmentation" ;
m_L abelingTexture = new RenderTexture ( new RenderTextureDescriptor ( width , height , GraphicsFormat . R8G8B8A8_UNorm , 8 ) ) ;
m_L abelingTexture. name = "Labeling" ;
s egmentationTexture = new RenderTexture ( new RenderTextureDescriptor ( width , height , GraphicsFormat . R8G8B8A8_UNorm , 8 ) ) ;
s egmentationTexture. name = "Segmentation" ;
l abelingTexture = new RenderTexture ( new RenderTextureDescriptor ( width , height , GraphicsFormat . R8G8B8A8_UNorm , 8 ) ) ;
l abelingTexture. name = "Labeling" ;
#if HDRP_PRESENT
var customPassVolume = this . GetComponent < CustomPassVolume > ( ) ? ? gameObject . AddComponent < CustomPassVolume > ( ) ;
{
name = "Segmentation Pass" ,
targetCamera = myCamera ,
targetTexture = m_S egmentationTexture
targetTexture = s egmentationTexture
m_SemanticSegmentationPass = new SemanticSegmentationPass ( myCamera , m_L abelingTexture, LabelingConfiguration )
m_SemanticSegmentationPass = new SemanticSegmentationPass ( myCamera , l abelingTexture, LabelingConfiguration )
{
name = "Labeling Pass"
} ;
#if URP_PRESENT
instanceSegmentationUrpPass = new InstanceSegmentationUrpPass ( myCamera , m_S egmentationTexture) ;
semanticSegmentationUrpPass = new SemanticSegmentationUrpPass ( myCamera , m_L abelingTexture, LabelingConfiguration ) ;
instanceSegmentationUrpPass = new InstanceSegmentationUrpPass ( myCamera , s egmentationTexture) ;
semanticSegmentationUrpPass = new SemanticSegmentationUrpPass ( myCamera , l abelingTexture, LabelingConfiguration ) ;
#endif
if ( produceSegmentationImages )
m_SegmentationAnnotationDefinition = SimulationManager . RegisterAnnotationDefinition ( "semantic segmentation" , specs , "pixel-wise semantic segmentation label" , "PNG" ) ;
m_ClassLabelingTextureReader = new RenderTextureReader < short > ( m_L abelingTexture, myCamera ,
m_ClassLabelingTextureReader = new RenderTextureReader < short > ( l abelingTexture, myCamera ,
( frameCount , data , tex ) = > OnSemanticSegmentationImageRead ( frameCount , data ) ) ;
}
if ( produceObjectCountAnnotations )
{
m_ObjectCountMetricDefinition = SimulationManager . RegisterMetricDefinition ( "object count" , labelingMetricSpec , "Counts of objects for each label in the sensor's view" , id : new Guid ( m_ObjectCountID ) ) ;
m_ObjectCountMetricDefinition = SimulationManager . RegisterMetricDefinition ( "object count" , labelingMetricSpec , "Counts of objects for each label in the sensor's view" , id : new Guid ( objectCountId ) ) ;
m_BoundingBoxAnnotationDefinition = SimulationManager . RegisterAnnotationDefinition ( "bounding box" , labelingMetricSpec , "Bounding boxe for each labeled object visible to the sensor" , id : new Guid ( m_BoundingBoxID ) ) ;
m_BoundingBoxAnnotationDefinition = SimulationManager . RegisterAnnotationDefinition ( "bounding box" , labelingMetricSpec , "Bounding boxe for each labeled object visible to the sensor" , id : new Guid ( boundingBoxId ) ) ;
m_VisiblePixelsMetricDefinition = SimulationManager . RegisterMetricDefinition ( "visible pixels" , labelingMetricSpec , "Visible pixels for each visible object" , id : new Guid ( m_VisiblePixelsID ) ) ;
m_VisiblePixelsMetricDefinition = SimulationManager . RegisterMetricDefinition ( "visible pixels" , labelingMetricSpec , "Visible pixels for each visible object" , id : new Guid ( visiblePixelsId ) ) ;
m_SegmentationReader = new RenderTextureReader < uint > ( m_S egmentationTexture, myCamera , ( frameCount , data , tex ) = >
m_SegmentationReader = new RenderTextureReader < uint > ( s egmentationTexture, myCamera , ( frameCount , data , tex ) = >
{
if ( segmentationImageReceived ! = null )
segmentationImageReceived ( frameCount , data ) ;
SimulationManager . SimulationEnding + = OnSimulationEnding ;
}
// ReSharper disable InconsistentNaming
[UsedImplicitly]
[UsedImplicitly]
[UsedImplicitly]
// ReSharper restore InconsistentNaming
void ProduceVisiblePixelsMetric ( NativeArray < RenderedObjectInfo > renderedObjectInfos , int frameCount )
{
if ( ! SensorHandle . IsValid )
return ;
var camera = this . GetComponent < Camera > ( ) ;
camera . enabled = SensorHandle . ShouldCaptureThisFrame ;
var cam = GetComponent < Camera > ( ) ;
cam . enabled = SensorHandle . ShouldCaptureThisFrame ;
m_AsyncCaptureInfos . RemoveSwapBack ( i = >
! i . SegmentationAsyncAnnotation . IsPending & &
}
}
void CaptureRgbData ( Camera camera )
void CaptureRgbData ( Camera cam )
{
Profiler . BeginSample ( "CaptureDataFromLastFrame" ) ;
if ( ! captureRgbImages )
SensorHandle . ReportCapture ( dxRootPath , SensorSpatialData . FromGameObjects ( m_EgoMarker = = null ? null : m_EgoMarker . gameObject , gameObject ) , m_PersistentSensorData . Select ( kvp = > ( kvp . Key , kvp . Value ) ) . ToArray ( ) ) ;
Func < AsyncRequest < CaptureCamera . CaptureState > , AsyncRequest . Result > colorFunctor = null ;
var width = camera . pixelWidth ;
var height = camera . pixelHeight ;
var flipY = ShouldFlipY ( camera ) ;
var width = cam . pixelWidth ;
var height = cam . pixelHeight ;
var flipY = ShouldFlipY ( cam ) ;
colorFunctor = r = >
{
}
} ;
CaptureCamera . Capture ( camera , colorFunctor ) ;
CaptureCamera . Capture ( cam , colorFunctor ) ;
Profiler . EndSample ( ) ;
}
RenderPipelineManager . beginCameraRendering - = OnBeginCameraRendering ;
}
void OnBeginCameraRendering ( ScriptableRenderContext _ , Camera camera )
void OnBeginCameraRendering ( ScriptableRenderContext _ , Camera cam )
if ( camera ! = GetComponent < Camera > ( ) )
if ( cam ! = GetComponent < Camera > ( ) )
return ;
#if UNITY_EDITOR
if ( UnityEditor . EditorApplication . isPaused )
CaptureRgbData ( camera ) ;
CaptureRgbData ( cam ) ;
}
void OnDisable ( )
m_ClassLabelingTextureReader ? . Dispose ( ) ;
m_ClassLabelingTextureReader = null ;
if ( m_S egmentationTexture ! = null )
m_S egmentationTexture. Release ( ) ;
if ( s egmentationTexture ! = null )
s egmentationTexture. Release ( ) ;
m_S egmentationTexture = null ;
if ( m_L abelingTexture ! = null )
m_L abelingTexture. Release ( ) ;
s egmentationTexture = null ;
if ( l abelingTexture ! = null )
l abelingTexture. Release ( ) ;
if ( m_RenderedObjectInfoGenerator ! = null )
{
SensorHandle = default ;
m_L abelingTexture = null ;
l abelingTexture = null ;
}
void OnSemanticSegmentationImageRead ( int frameCount , NativeArray < short > data )
asyncRequest . data = new AsyncSemanticSegmentationWrite ( )
{
dataArray = data . ToArray ( ) ,
width = m_L abelingTexture. width ,
height = m_L abelingTexture. height ,
width = l abelingTexture. width ,
height = l abelingTexture. height ,
path = path
} ;
asyncRequest . Start ( ( r ) = >