浏览代码

Merge pull request #240 from Unity-Technologies/remove-entities-dependency

Remove entities dependency
/main
GitHub 4 年前
当前提交
304f106b
共有 78 个文件被更改,包括 1210 次插入1317 次删除
  1. 4
      com.unity.perception/CHANGELOG.md
  2. 3
      com.unity.perception/Editor/GroundTruth/InstanceSegmentationPassEditor.cs
  3. 75
      com.unity.perception/Runtime/GroundTruth/GroundTruthRendererFeature.cs
  4. 4
      com.unity.perception/Runtime/GroundTruth/IGroundTruthGenerator.cs
  5. 20
      com.unity.perception/Runtime/GroundTruth/Labelers/BoundingBox3DLabeler.cs
  6. 18
      com.unity.perception/Runtime/GroundTruth/Labelers/KeypointLabeler.cs
  7. 25
      com.unity.perception/Runtime/GroundTruth/Labelers/SemanticSegmentationLabeler.cs
  8. 17
      com.unity.perception/Runtime/GroundTruth/Labeling/LabelEntryMatchCache.cs
  9. 34
      com.unity.perception/Runtime/GroundTruth/Labeling/Labeling.cs
  10. 476
      com.unity.perception/Runtime/GroundTruth/PerceptionCamera.cs
  11. 24
      com.unity.perception/Runtime/GroundTruth/PerceptionCamera_InstanceSegmentation.cs
  12. 11
      com.unity.perception/Runtime/GroundTruth/RenderTextureReader.cs
  13. 12
      com.unity.perception/Runtime/GroundTruth/RenderedObjectInfo.cs
  14. 2
      com.unity.perception/Runtime/GroundTruth/RenderedObjectInfoGenerator.cs
  15. 77
      com.unity.perception/Runtime/GroundTruth/SimulationState.cs
  16. 1
      com.unity.perception/Runtime/Randomization/Randomizers/Randomizer.cs
  17. 1
      com.unity.perception/Runtime/Randomization/Randomizers/RandomizerExamples/Utilities/PoissonDiskSampling.cs
  18. 11
      com.unity.perception/Runtime/Randomization/Scenarios/ScenarioBase.cs
  19. 45
      com.unity.perception/Tests/Runtime/GroundTruthTests/DatasetJsonUtilityTests.cs
  20. 1
      com.unity.perception/Tests/Runtime/GroundTruthTests/LabelEntryMatchCacheTests.cs
  21. 4
      com.unity.perception/Tests/Runtime/GroundTruthTests/PerceptionCameraIntegrationTests.cs
  22. 14
      com.unity.perception/Tests/Runtime/GroundTruthTests/SegmentationGroundTruthTests.cs
  23. 1
      com.unity.perception/Tests/Runtime/Randomization/RandomizerTests/RandomizerTagTests.cs
  24. 14
      com.unity.perception/package.json
  25. 2
      com.unity.perception/Runtime/GroundTruth/RenderPasses/CrossPipelinePasses/GroundTruthCrossPipelinePass.cs.meta
  26. 4
      com.unity.perception/Runtime/GroundTruth/RenderPasses/HdrpPasses/InstanceSegmentationPass.cs
  27. 2
      com.unity.perception/Runtime/GroundTruth/RenderPasses/CrossPipelinePasses/SemanticSegmentationCrossPipelinePass.cs.meta
  28. 5
      com.unity.perception/Runtime/GroundTruth/RenderPasses/HdrpPasses/SemanticSegmentationPass.cs
  29. 2
      com.unity.perception/Runtime/GroundTruth/RenderPasses/HdrpPasses/SemanticSegmentationPass.cs.meta
  30. 23
      com.unity.perception/Runtime/GroundTruth/RenderPasses/HdrpPasses/GroundTruthPass.cs
  31. 168
      com.unity.perception/Runtime/GroundTruth/Labeling/LabelManager.cs
  32. 3
      com.unity.perception/Runtime/GroundTruth/Labeling/LabelManager.cs.meta
  33. 25
      com.unity.perception/Runtime/GroundTruth/PerceptionUpdater.cs
  34. 3
      com.unity.perception/Runtime/GroundTruth/PerceptionUpdater.cs.meta
  35. 3
      com.unity.perception/Runtime/GroundTruth/RenderPasses.meta
  36. 3
      com.unity.perception/Runtime/GroundTruth/RenderPasses/CrossPipelinePasses.meta
  37. 120
      com.unity.perception/Runtime/GroundTruth/RenderPasses/CrossPipelinePasses/GroundTruthCrossPipelinePass.cs
  38. 78
      com.unity.perception/Runtime/GroundTruth/RenderPasses/CrossPipelinePasses/InstanceSegmentationCrossPipelinePass.cs
  39. 11
      com.unity.perception/Runtime/GroundTruth/RenderPasses/CrossPipelinePasses/InstanceSegmentationCrossPipelinePass.cs.meta
  40. 191
      com.unity.perception/Runtime/GroundTruth/RenderPasses/CrossPipelinePasses/LensDistortionCrossPipelinePass.cs
  41. 3
      com.unity.perception/Runtime/GroundTruth/RenderPasses/CrossPipelinePasses/LensDistortionCrossPipelinePass.cs.meta
  42. 83
      com.unity.perception/Runtime/GroundTruth/RenderPasses/CrossPipelinePasses/SemanticSegmentationCrossPipelinePass.cs
  43. 3
      com.unity.perception/Runtime/GroundTruth/RenderPasses/HdrpPasses.meta
  44. 3
      com.unity.perception/Runtime/GroundTruth/RenderPasses/HdrpPasses/GroundTruthPass.cs.meta
  45. 48
      com.unity.perception/Runtime/GroundTruth/RenderPasses/HdrpPasses/LensDistortionPass.cs
  46. 3
      com.unity.perception/Runtime/GroundTruth/RenderPasses/UrpPasses.meta
  47. 31
      com.unity.perception/Runtime/GroundTruth/RenderPasses/UrpPasses/InstanceSegmentationUrpPass.cs
  48. 3
      com.unity.perception/Runtime/GroundTruth/RenderPasses/UrpPasses/InstanceSegmentationUrpPass.cs.meta
  49. 31
      com.unity.perception/Runtime/GroundTruth/RenderPasses/UrpPasses/LensDistortionUrpPass.cs
  50. 3
      com.unity.perception/Runtime/GroundTruth/RenderPasses/UrpPasses/LensDistortionUrpPass.cs.meta
  51. 31
      com.unity.perception/Runtime/GroundTruth/RenderPasses/UrpPasses/SemanticSegmentationUrpPass.cs
  52. 3
      com.unity.perception/Runtime/GroundTruth/RenderPasses/UrpPasses/SemanticSegmentationUrpPass.cs.meta
  53. 15
      com.unity.perception/Runtime/GroundTruth/GroundTruthInfo.cs
  54. 11
      com.unity.perception/Runtime/GroundTruth/GroundTruthInfo.cs.meta
  55. 11
      com.unity.perception/Runtime/GroundTruth/GroundTruthLabelSetupSystem.cs.meta
  56. 11
      com.unity.perception/Runtime/GroundTruth/GroundTruthPass.cs.meta
  57. 11
      com.unity.perception/Runtime/GroundTruth/InstanceSegmentationCrossPipelinePass.cs.meta
  58. 13
      com.unity.perception/Runtime/GroundTruth/SimulationManagementComponentSystem.cs
  59. 11
      com.unity.perception/Runtime/GroundTruth/SimulationManagementComponentSystem.cs.meta
  60. 107
      com.unity.perception/Runtime/GroundTruth/GroundTruthCrossPipelinePass.cs
  61. 141
      com.unity.perception/Runtime/GroundTruth/GroundTruthLabelSetupSystem.cs
  62. 78
      com.unity.perception/Runtime/GroundTruth/InstanceSegmentationCrossPipelinePass.cs
  63. 79
      com.unity.perception/Runtime/GroundTruth/SemanticSegmentationCrossPipelinePass.cs
  64. 200
      com.unity.perception/Runtime/GroundTruth/LensDistortionCrossPipelinePass.cs
  65. 3
      com.unity.perception/Runtime/GroundTruth/LensDistortionCrossPipelinePass.cs.meta
  66. 54
      com.unity.perception/Runtime/GroundTruth/LensDistortionPass.cs
  67. 0
      /com.unity.perception/Runtime/GroundTruth/RenderPasses/CrossPipelinePasses/GroundTruthCrossPipelinePass.cs.meta
  68. 0
      /com.unity.perception/Runtime/GroundTruth/RenderPasses/HdrpPasses/InstanceSegmentationPass.cs
  69. 0
      /com.unity.perception/Runtime/GroundTruth/RenderPasses/HdrpPasses/InstanceSegmentationPass.cs.meta
  70. 0
      /com.unity.perception/Runtime/GroundTruth/RenderPasses/CrossPipelinePasses/SemanticSegmentationCrossPipelinePass.cs.meta
  71. 0
      /com.unity.perception/Runtime/GroundTruth/RenderPasses/HdrpPasses/SemanticSegmentationPass.cs
  72. 0
      /com.unity.perception/Runtime/GroundTruth/RenderPasses/HdrpPasses/SemanticSegmentationPass.cs.meta
  73. 0
      /com.unity.perception/Runtime/GroundTruth/RenderPasses/HdrpPasses/GroundTruthPass.cs
  74. 0
      /com.unity.perception/Runtime/GroundTruth/RenderPasses/HdrpPasses/LensDistortionPass.cs.meta

4
com.unity.perception/CHANGELOG.md


The PerceptionScenario abstract class has been added to abstract perception data capture specific functionality from the vanilla scenario lifecycle.
The newly added LabelManager class now enables custom Labelers to access the list of registered Labeling Components present in the scene.
Improved UI for KeypointTemplate and added useful default colors for keypoint and skeleton definitions.
### Changed

### Deprecated
### Removed
Removed the entities package dependency
### Fixed

3
com.unity.perception/Editor/GroundTruth/InstanceSegmentationPassEditor.cs


#if HDRP_PRESENT
using UnityEditor.Rendering.HighDefinition;
using UnityEngine.Perception.GroundTruth;

{
protected override void Initialize(SerializedProperty customPass)
{
var targetCameraProperty = customPass.FindPropertyRelative(nameof(GroundTruthPass.targetCamera));
var targetCameraProperty = customPass.FindPropertyRelative(nameof(InstanceSegmentationPass.targetCamera));
AddProperty(targetCameraProperty);
AddProperty(customPass.FindPropertyRelative(nameof(InstanceSegmentationPass.targetTexture)));
base.Initialize(customPass);

75
com.unity.perception/Runtime/GroundTruth/GroundTruthRendererFeature.cs


using System;
using UnityEditor;
using UnityEngine;
using UnityEngine.Rendering;
class InstanceSegmentationUrpPass : ScriptableRenderPass
{
InstanceSegmentationCrossPipelinePass m_InstanceSegmentationPass;
public InstanceSegmentationUrpPass(Camera camera, RenderTexture targetTexture)
{
m_InstanceSegmentationPass = new InstanceSegmentationCrossPipelinePass(camera);
ConfigureTarget(targetTexture, targetTexture.depthBuffer);
m_InstanceSegmentationPass.Setup();
}
public override void Execute(ScriptableRenderContext context, ref RenderingData renderingData)
{
var commandBuffer = CommandBufferPool.Get(nameof(InstanceSegmentationUrpPass));
m_InstanceSegmentationPass.Execute(context, commandBuffer, renderingData.cameraData.camera, renderingData.cullResults);
CommandBufferPool.Release(commandBuffer);
}
public void Cleanup()
{
m_InstanceSegmentationPass.Cleanup();
}
}
class SemanticSegmentationUrpPass : ScriptableRenderPass
{
public SemanticSegmentationCrossPipelinePass m_SemanticSegmentationCrossPipelinePass;
public SemanticSegmentationUrpPass(Camera camera, RenderTexture targetTexture, SemanticSegmentationLabelConfig labelConfig)
{
m_SemanticSegmentationCrossPipelinePass = new SemanticSegmentationCrossPipelinePass(camera, labelConfig);
ConfigureTarget(targetTexture, targetTexture.depthBuffer);
m_SemanticSegmentationCrossPipelinePass.Setup();
}
public override void Execute(ScriptableRenderContext context, ref RenderingData renderingData)
{
var commandBuffer = CommandBufferPool.Get(nameof(SemanticSegmentationUrpPass));
m_SemanticSegmentationCrossPipelinePass.Execute(context, commandBuffer, renderingData.cameraData.camera, renderingData.cullResults);
CommandBufferPool.Release(commandBuffer);
}
public void Cleanup()
{
m_SemanticSegmentationCrossPipelinePass.Cleanup();
}
}
class LensDistortionUrpPass : ScriptableRenderPass
{
public LensDistortionCrossPipelinePass m_LensDistortionCrossPipelinePass;
public LensDistortionUrpPass(Camera camera, RenderTexture targetTexture)
{
m_LensDistortionCrossPipelinePass = new LensDistortionCrossPipelinePass(camera, targetTexture);
ConfigureTarget(targetTexture, targetTexture.depthBuffer);
m_LensDistortionCrossPipelinePass.Setup();
}
public override void Execute(ScriptableRenderContext context, ref RenderingData renderingData)
{
var commandBuffer = CommandBufferPool.Get(nameof(SemanticSegmentationUrpPass));
m_LensDistortionCrossPipelinePass.Execute(context, commandBuffer, renderingData.cameraData.camera, renderingData.cullResults);
CommandBufferPool.Release(commandBuffer);
}
public void Cleanup()
{
m_LensDistortionCrossPipelinePass.Cleanup();
}
}
public class GroundTruthRendererFeature : ScriptableRendererFeature
{
public override void Create() {}

if (!EditorApplication.isPlaying)
return;
#endif
perceptionCamera.OnGroundTruthRendererFeatureRun();
perceptionCamera.MarkGroundTruthRendererFeatureAsPresent();
foreach (var pass in perceptionCamera.passes)
renderer.EnqueuePass(pass);
}

4
com.unity.perception/Runtime/GroundTruth/IGroundTruthGenerator.cs


namespace UnityEngine.Perception.GroundTruth
{
/// <summary>
/// Interface for setting up MeshRenderers for ground truth generation via <see cref="GroundTruthLabelSetupSystem"/>.
/// Interface for setting up Renderers for ground truth generation via <see cref="LabelManager"/>.
/// Called by <see cref="GroundTruthLabelSetupSystem"/> when first registered or when a Labeling is created at runtime.
/// Called by <see cref="LabelManager"/> when first registered or when a Labeling is created at runtime.
/// </summary>
/// <param name="mpb">The MaterialPropertyBlock for the given meshRenderer. Can be used to set properties for custom rendering.</param>
/// <param name="renderer">The Renderer under the given Labeling.</param>

20
com.unity.perception/Runtime/GroundTruth/Labelers/BoundingBox3DLabeler.cs


using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using Unity.Collections;
using Unity.Entities;
using Unity.Profiling;
namespace UnityEngine.Perception.GroundTruth

/// </summary>
public class BoundingBox3DLabeler : CameraLabeler
{
EntityQuery m_EntityQuery;
///<inheritdoc/>
public override string description
{

/// <param name="labelConfig">The label config for resolving the label for each object.</param>
public BoundingBox3DLabeler(IdLabelConfig labelConfig)
{
this.idLabelConfig = labelConfig;
idLabelConfig = labelConfig;
}
/// <inheritdoc/>

"Bounding box for each labeled object visible to the sensor", id: new Guid(annotationId));
perceptionCamera.RenderedObjectInfosCalculated += OnRenderObjectInfosCalculated;
m_EntityQuery = World.DefaultGameObjectInjectionWorld.EntityManager.CreateEntityQuery(typeof(Labeling), typeof(GroundTruthInfo));
m_AsyncAnnotations = new Dictionary<int, AsyncAnnotation>();
m_BoundingBoxValues = new Dictionary<int, Dictionary<uint, BoxData>>();

m_AsyncAnnotations[m_CurrentFrame] = perceptionCamera.SensorHandle.ReportAnnotationAsync(m_AnnotationDefinition);
var entities = m_EntityQuery.ToEntityArray(Allocator.TempJob);
var entityManager = World.DefaultGameObjectInjectionWorld.EntityManager;
foreach (var entity in entities)
{
ProcessEntity(entityManager.GetComponentObject<Labeling>(entity));
}
entities.Dispose();
foreach (var label in LabelManager.singleton.registeredLabels)
ProcessLabel(label);
}
void OnRenderObjectInfosCalculated(int frameCount, NativeArray<RenderedObjectInfo> renderedObjectInfos)

}
}
void ProcessEntity(Labeling labeledEntity)
void ProcessLabel(Labeling labeledEntity)
{
using (s_BoundingBoxCallback.Auto())
{

18
com.unity.perception/Runtime/GroundTruth/Labelers/KeypointLabeler.cs


using System.Collections.Generic;
using System.Linq;
using Unity.Collections;
using Unity.Entities;
using UnityEngine.Serialization;
namespace UnityEngine.Perception.GroundTruth
{

// ReSharper restore MemberCanBePrivate.Global
AnnotationDefinition m_AnnotationDefinition;
EntityQuery m_EntityQuery;
Texture2D m_MissingTexture;
Dictionary<int, (AsyncAnnotation annotation, Dictionary<uint, KeypointEntry> keypoints)> m_AsyncAnnotations;

m_AnnotationDefinition = DatasetCapture.RegisterAnnotationDefinition("keypoints", new []{TemplateToJson(activeTemplate)},
"pixel coordinates of keypoints in a model, along with skeletal connectivity data", id: new Guid(annotationId));
m_EntityQuery = World.DefaultGameObjectInjectionWorld.EntityManager.CreateEntityQuery(typeof(Labeling), typeof(GroundTruthInfo));
// Texture to use in case the template does not contain a texture for the joints or the skeletal connections
m_MissingTexture = new Texture2D(1, 1);

m_AsyncAnnotations[m_CurrentFrame] = (annotation, keypoints);
var entities = m_EntityQuery.ToEntityArray(Allocator.TempJob);
var entityManager = World.DefaultGameObjectInjectionWorld.EntityManager;
foreach (var entity in entities)
{
ProcessEntity(entityManager.GetComponentObject<Labeling>(entity));
}
entities.Dispose();
foreach (var label in LabelManager.singleton.registeredLabels)
ProcessLabel(label);
}
// ReSharper disable InconsistentNaming

return false;
}
void ProcessEntity(Labeling labeledEntity)
void ProcessLabel(Labeling labeledEntity)
{
// Cache out the data of a labeled game object the first time we see it, this will
// save performance each frame. Also checks to see if a labeled game object can be annotated.

25
com.unity.perception/Runtime/GroundTruth/Labelers/SemanticSegmentationLabeler.cs


using Unity.Simulation;
using UnityEngine.Experimental.Rendering;
using UnityEngine.Profiling;
using UnityEngine.Rendering;
using UnityEngine.UI;
#if HDRP_PRESENT
using UnityEngine.Rendering.HighDefinition;

/// Labeler which generates a semantic segmentation image each frame. Each object is rendered to the semantic segmentation
/// image using the color associated with it based on the given <see cref="SemanticSegmentationLabelConfig"/>.
/// Semantic segmentation images are saved to the dataset in PNG format.
///
/// Only one SemanticSegmentationLabeler can render at once across all cameras.
/// </summary>
[Serializable]

{
get => "Generates a semantic segmentation image for each captured frame. Each object is rendered to the semantic segmentation image using the color associated with it based on this labeler's associated semantic segmentation label configuration. " +
"Semantic segmentation images are saved to the dataset in PNG format. " +
"Please note that only one " + this.GetType().Name + " can render at once across all cameras.";
"Please note that only one " + GetType().Name + " can render at once across all cameras.";
internal string m_SemanticSegmentationDirectory;
internal string semanticSegmentationDirectory;
/// <summary>
/// The id to associate with semantic segmentation annotations in the dataset.

AnnotationDefinition m_SemanticSegmentationAnnotationDefinition;
RenderTextureReader<Color32> m_SemanticSegmentationTextureReader;
internal bool m_fLensDistortionEnabled = false;
#if HDRP_PRESENT
#if HDRP_PRESENT
SemanticSegmentationPass m_SemanticSegmentationPass;
LensDistortionPass m_LensDistortionPass;
#elif URP_PRESENT

public SemanticSegmentationLabeler(SemanticSegmentationLabelConfig labelConfig, RenderTexture targetTextureOverride = null)
{
this.labelConfig = labelConfig;
this.m_TargetTextureOverride = targetTextureOverride;
m_TargetTextureOverride = targetTextureOverride;
}
[SuppressMessage("ReSharper", "InconsistentNaming")]

if (targetTexture.sRGB)
{
Debug.LogError("targetTexture supplied to SemanticSegmentationLabeler must be in Linear mode. Disabling labeler.");
this.enabled = false;
enabled = false;
}
var renderTextureDescriptor = new RenderTextureDescriptor(camWidth, camHeight, GraphicsFormat.R8G8B8A8_UNorm, 8);
targetTexture.descriptor = renderTextureDescriptor;

targetTexture.Create();
targetTexture.name = "Labeling";
m_SemanticSegmentationDirectory = k_SemanticSegmentationDirectory + Guid.NewGuid();
semanticSegmentationDirectory = k_SemanticSegmentationDirectory + Guid.NewGuid();
#if HDRP_PRESENT
var gameObject = perceptionCamera.gameObject;

name = "Lens Distortion Pass"
};
customPassVolume.customPasses.Add(m_LensDistortionPass);
m_fLensDistortionEnabled = true;
#elif URP_PRESENT
// Semantic Segmentation
m_SemanticSegmentationPass = new SemanticSegmentationUrpPass(myCamera, targetTexture, labelConfig);

m_LensDistortionPass = new LensDistortionUrpPass(myCamera, targetTexture);
perceptionCamera.AddScriptableRenderPass(m_LensDistortionPass);
m_fLensDistortionEnabled = true;
#endif
var specs = labelConfig.labelEntries.Select((l) => new SemanticSegmentationSpec()

if (!m_AsyncAnnotations.TryGetValue(frameCount, out var annotation))
return;
var datasetRelativePath = $"{m_SemanticSegmentationDirectory}/{k_SegmentationFilePrefix}{frameCount}.png";
var localPath = $"{Manager.Instance.GetDirectoryFor(m_SemanticSegmentationDirectory)}/{k_SegmentationFilePrefix}{frameCount}.png";
var datasetRelativePath = $"{semanticSegmentationDirectory}/{k_SegmentationFilePrefix}{frameCount}.png";
var localPath = $"{Manager.Instance.GetDirectoryFor(semanticSegmentationDirectory)}/{k_SegmentationFilePrefix}{frameCount}.png";
annotation.ReportFile(datasetRelativePath);

17
com.unity.perception/Runtime/GroundTruth/Labeling/LabelEntryMatchCache.cs


using System;
using Unity.Collections;
using Unity.Entities;
namespace UnityEngine.Perception.GroundTruth
{

const int k_StartingObjectCount = 1 << 8;
NativeList<ushort> m_InstanceIdToLabelEntryIndexLookup;
IdLabelConfig m_IdLabelConfig;
ushort m_DefaultValue = ushort.MaxValue;
const ushort k_DefaultValue = ushort.MaxValue;
World.DefaultGameObjectInjectionWorld.GetOrCreateSystem<GroundTruthLabelSetupSystem>().Activate(this);
LabelManager.singleton.Activate(this);
}
public bool TryGetLabelEntryFromInstanceId(uint instanceId, out IdLabelEntry labelEntry, out int index)

if (m_InstanceIdToLabelEntryIndexLookup.Length <= instanceId || m_InstanceIdToLabelEntryIndexLookup[(int)instanceId] == m_DefaultValue)
if (m_InstanceIdToLabelEntryIndexLookup.Length <= instanceId || m_InstanceIdToLabelEntryIndexLookup[(int)instanceId] == k_DefaultValue)
return false;
index = m_InstanceIdToLabelEntryIndexLookup[(int)instanceId];

{
if (m_IdLabelConfig.TryGetMatchingConfigurationEntry(labeling, out _, out var index))
{
Debug.Assert(index < m_DefaultValue, "Too many entries in the label config");
Debug.Assert(index < k_DefaultValue, "Too many entries in the label config");
for (int i = oldLength; i < instanceId; i++)
m_InstanceIdToLabelEntryIndexLookup[i] = m_DefaultValue;
for (var i = oldLength; i < instanceId; i++)
m_InstanceIdToLabelEntryIndexLookup[i] = k_DefaultValue;
m_InstanceIdToLabelEntryIndexLookup[(int)instanceId] = m_DefaultValue;
m_InstanceIdToLabelEntryIndexLookup[(int)instanceId] = k_DefaultValue;
World.DefaultGameObjectInjectionWorld?.GetExistingSystem<GroundTruthLabelSetupSystem>()?.Deactivate(this);
LabelManager.singleton.Deactivate(this);
m_InstanceIdToLabelEntryIndexLookup.Dispose();
}
}

34
com.unity.perception/Runtime/GroundTruth/Labeling/Labeling.cs


using System.Collections.Generic;
using Unity.Entities;
using UnityEditor;
using UnityEngine.Serialization;

[AddComponentMenu("Perception/Labeling/Labeling")]
public class Labeling : MonoBehaviour
{
static LabelManager labelManager => LabelManager.singleton;
[FormerlySerializedAs("classes")] public List<string> labels = new List<string>();
// /// <summary>
// /// A list for backing up the asset's manually added labels, so that if the user switches to auto labeling and back, the previously added labels can be revived
// /// </summary>
// public List<string> manualLabelsBackup = new List<string>();
[FormerlySerializedAs("classes")]
public List<string> labels = new List<string>();
/// <summary>
/// The specific subtype of AssetLabelingScheme that this component is using, if useAutoLabeling is enabled.

/// </summary>
public uint instanceId { get; private set; }
Entity m_Entity;
internal void SetInstanceId(uint instanceId)
{
this.instanceId = instanceId;
}
m_Entity = World.DefaultGameObjectInjectionWorld.EntityManager.CreateEntity();
World.DefaultGameObjectInjectionWorld.EntityManager.AddComponentObject(m_Entity, this);
labelManager.Register(this);
if (World.DefaultGameObjectInjectionWorld != null)
World.DefaultGameObjectInjectionWorld.EntityManager.DestroyEntity(m_Entity);
labelManager.Unregister(this);
}
void Reset()

EditorUtility.SetDirty(gameObject);
#endif
}
/// <summary>
/// Refresh ground truth generation for the labeling of the attached GameObject. This is necessary when the

{
World.DefaultGameObjectInjectionWorld.GetOrCreateSystem<GroundTruthLabelSetupSystem>()
.RefreshLabeling(m_Entity);
labelManager.RefreshLabeling(this);
}
internal void SetInstanceId(uint id)
{
instanceId = id;
}
}
}

476
com.unity.perception/Runtime/GroundTruth/PerceptionCamera.cs


[RequireComponent(typeof(Camera))]
public partial class PerceptionCamera : MonoBehaviour
{
const float k_PanelWidth = 200;
const float k_PanelHeight = 250;
const string k_RgbFilePrefix = "rgb_";
static ProfilerMarker s_WriteFrame = new ProfilerMarker("Write Frame (PerceptionCamera)");
static ProfilerMarker s_EncodeAndSave = new ProfilerMarker("Encode and save (PerceptionCamera)");
static PerceptionCamera s_VisualizedPerceptionCamera;
static string s_RgbFilePrefix = "rgb_";
internal HUDPanel hudPanel;
internal OverlayPanel overlayPanel;
[SerializeReference]
List<CameraLabeler> m_Labelers = new List<CameraLabeler>();
Dictionary<string, object> m_PersistentSensorData = new Dictionary<string, object>();
bool m_ShowingVisualizations;
bool m_GUIStylesInitialized;
int m_LastFrameCaptured = -1;
int m_LastFrameEndRendering = -1;
Ego m_EgoMarker;
SensorHandle m_SensorHandle;
Vector2 m_ScrollPosition;
#if URP_PRESENT
// only used to confirm that GroundTruthRendererFeature is present in URP
bool m_IsGroundTruthRendererFeaturePresent;
internal List<ScriptableRenderPass> passes = new List<ScriptableRenderPass>();
#endif
/// <summary>
/// A human-readable description of the camera.

/// </summary>
public bool captureRgbImages = true;
Camera m_AttachedCamera = null;
public Camera attachedCamera => m_AttachedCamera;
public Camera attachedCamera { get; private set; }
public int firstCaptureFrame = 0;
public int firstCaptureFrame;
/// <summary>
/// The method of triggering captures for this camera.

/// <summary>
/// Have this unscheduled (manual capture) camera affect simulation timings (similar to a scheduled camera) by requesting a specific frame delta time
/// Have this unscheduled (manual capture) camera affect simulation timings (similar to a scheduled camera) by
/// requesting a specific frame delta time
public bool manualSensorAffectSimulationTiming = false;
public bool manualSensorAffectSimulationTiming;
/// The simulation frame time (seconds) for this camera. E.g. 0.0166 translates to 60 frames per second. This will be used as Unity's <see cref="Time.captureDeltaTime"/>, causing a fixed number of frames to be generated for each second of elapsed simulation time regardless of the capabilities of the underlying hardware.
/// The simulation frame time (seconds) for this camera. E.g. 0.0166 translates to 60 frames per second.
/// This will be used as Unity's <see cref="Time.captureDeltaTime"/>, causing a fixed number of frames to be
/// generated for each second of elapsed simulation time regardless of the capabilities of the underlying hardware.
/// The number of frames to simulate and render between the camera's scheduled captures. Setting this to 0 makes the camera capture every frame.
/// The number of frames to simulate and render between the camera's scheduled captures.
/// Setting this to 0 makes the camera capture every frame.
public int framesBetweenCaptures = 0;
public int framesBetweenCaptures;
/// <summary>
/// Turns on/off the realtime visualization capability.
/// </summary>
[SerializeField]
public bool showVisualizations = true;
/// <summary>
/// The <see cref="CameraLabeler"/> instances which will be run for this PerceptionCamera.
/// </summary>
public IReadOnlyList<CameraLabeler> labelers => m_Labelers;
/// Requests a capture from this camera on the next rendered frame. Can only be used when using <see cref="PerceptionCamera.CaptureTriggerMode.Manual"/> capture mode.
/// Requests a capture from this camera on the next rendered frame.
/// Can only be used when using <see cref="CaptureTriggerMode.Manual"/> capture mode.
/// </summary>
public void RequestCapture()
{

}
else
{
Debug.LogError($"{nameof(RequestCapture)} can only be used if the camera is in {nameof(CaptureTriggerMode.Manual)} capture mode.");
Debug.LogError($"{nameof(RequestCapture)} can only be used if the camera is in " +
$"{nameof(CaptureTriggerMode.Manual)} capture mode.");
/// Event invoked after the camera finishes rendering during a frame.
/// </summary>
[SerializeReference]
List<CameraLabeler> m_Labelers = new List<CameraLabeler>();
Dictionary<string, object> m_PersistentSensorData = new Dictionary<string, object>();
int m_LastFrameCaptured = -1;
int m_LastFrameEndRendering = -1;
#pragma warning disable 414
//only used to confirm that GroundTruthRendererFeature is present in URP
bool m_GroundTruthRendererFeatureRun;
#pragma warning restore 414
static PerceptionCamera s_VisualizedPerceptionCamera;
/// <summary>
/// Turns on/off the realtime visualization capability.
/// </summary>
[SerializeField]
public bool showVisualizations = true;
bool m_ShowingVisualizations = false;
/// <summary>
/// The <see cref="SensorHandle"/> associated with this camera. Use this to report additional annotations and metrics at runtime.
/// The <see cref="SensorHandle"/> associated with this camera.
/// Use this to report additional annotations and metrics at runtime.
/// </summary>
public SensorHandle SensorHandle
{

private set => m_SensorHandle = value;
}
SensorHandle m_SensorHandle;
Ego m_EgoMarker;
static ProfilerMarker s_WriteFrame = new ProfilerMarker("Write Frame (PerceptionCamera)");
static ProfilerMarker s_EncodeAndSave = new ProfilerMarker("Encode and save (PerceptionCamera)");
#if URP_PRESENT
internal List<ScriptableRenderPass> passes = new List<ScriptableRenderPass>();
public void AddScriptableRenderPass(ScriptableRenderPass pass)
{
passes.Add(pass);
}
#endif
/// Add a data object which will be added to the dataset with each capture. Overrides existing sensor data associated with the given key.
/// Add a data object which will be added to the dataset with each capture.
/// Overrides existing sensor data associated with the given key.
/// </summary>
/// <param name="key">The key to associate with the data.</param>
/// <param name="data">An object containing the data. Will be serialized into json.</param>

return m_PersistentSensorData.Remove(key);
}
/// <summary>
/// Add the given <see cref="CameraLabeler"/> to the PerceptionCamera. It will be set up and executed by this
/// PerceptionCamera each frame it captures data.
/// </summary>
/// <param name="cameraLabeler">The labeler to add to this PerceptionCamera</param>
public void AddLabeler(CameraLabeler cameraLabeler) => m_Labelers.Add(cameraLabeler);
/// <summary>
/// Removes the given <see cref="CameraLabeler"/> from the list of labelers under this PerceptionCamera, if it
/// is in the list. The labeler is cleaned up in the process. Labelers removed from a PerceptionCamera should
/// not be used again.
/// </summary>
/// <param name="cameraLabeler"></param>
/// <returns></returns>
public bool RemoveLabeler(CameraLabeler cameraLabeler)
{
if (m_Labelers.Remove(cameraLabeler))
{
if (cameraLabeler.isInitialized)
cameraLabeler.InternalCleanup();
return true;
}
return false;
}
AsyncRequest.maxJobSystemParallelism = 0; // Jobs are not chained to one another in any way, maximizing parallelism
AsyncRequest.maxAsyncRequestFrameAge = 4; // Ensure that readbacks happen before Allocator.TempJob allocations get stale
// Jobs are not chained to one another in any way, maximizing parallelism
AsyncRequest.maxJobSystemParallelism = 0;
// Ensure that read-backs happen before Allocator.TempJob allocations get stale
AsyncRequest.maxAsyncRequestFrameAge = 4;
m_AttachedCamera = GetComponent<Camera>();
SetupVisualizationCamera(m_AttachedCamera);
attachedCamera = GetComponent<Camera>();
SetupVisualizationCamera();
void EnsureSensorRegistered()
{
if (m_SensorHandle.IsNil)
{
m_EgoMarker = GetComponentInParent<Ego>();
var ego = m_EgoMarker == null ? DatasetCapture.RegisterEgo("") : m_EgoMarker.EgoHandle;
SensorHandle = DatasetCapture.RegisterSensor(ego, "camera", description, firstCaptureFrame, captureTriggerMode, simulationDeltaTime, framesBetweenCaptures, manualSensorAffectSimulationTiming);
}
}
void OnEnable()
{
RenderPipelineManager.beginCameraRendering += OnBeginCameraRendering;

internal HUDPanel hudPanel = null;
internal OverlayPanel overlayPanel = null;
void SetupVisualizationCamera(Camera cam)
{
#if !UNITY_EDITOR && !DEVELOPMENT_BUILD
showVisualizations = false;
#else
var visualizationAllowed = s_VisualizedPerceptionCamera == null;
if (!visualizationAllowed && showVisualizations)
{
Debug.LogWarning($"Currently only one PerceptionCamera may be visualized at a time. Disabling visualization on {gameObject.name}.");
showVisualizations = false;
return;
}
if (!showVisualizations)
return;
m_ShowingVisualizations = true;
s_VisualizedPerceptionCamera = this;
hudPanel = gameObject.AddComponent<HUDPanel>();
overlayPanel = gameObject.AddComponent<OverlayPanel>();
overlayPanel.perceptionCamera = this;
#endif
}
void CheckForRendererFeature(ScriptableRenderContext context, Camera camera)
{
if (camera == m_AttachedCamera)
{
#if URP_PRESENT
if (!m_GroundTruthRendererFeatureRun)
{
Debug.LogError("GroundTruthRendererFeature must be present on the ScriptableRenderer associated with the camera. The ScriptableRenderer can be accessed through Edit -> Project Settings... -> Graphics -> Scriptable Render Pipeline Settings -> Renderer List.");
enabled = false;
}
#endif
RenderPipelineManager.endCameraRendering -= CheckForRendererFeature;
}
}
// Update is called once per frame
void Update()
{
EnsureSensorRegistered();

bool anyVisualizing = false;
foreach (var labeler in m_Labelers)
{
if (!labeler.enabled)

{
}
anyVisualizing |= labeler.InternalVisualizationEnabled;
// if we are utilizing async readback and we have to flip our captured image. We have created a jira
// issue for this (aisv-779) and have notified the engine team about this.
anyVisualizing = true;
// if we are utilizing async readback and we have to flip our captured image.
// We have created a jira issue for this (aisv-779) and have notified the engine team about this.
CaptureOptions.useAsyncReadbackIfSupported = !anyVisualizing;
}
private Vector2 scrollPosition;
private const float panelWidth = 200;
private const float panelHeight = 250;
private void SetUpGUIStyles()
{
GUI.skin.label.fontSize = 12;
GUI.skin.label.font = Resources.Load<Font>("Inter-Light");
GUI.skin.label.padding = new RectOffset(0, 0, 1, 1);
GUI.skin.label.margin = new RectOffset(0, 0, 1, 1);
GUI.skin.label.wordWrap = true;
GUI.skin.label.alignment = TextAnchor.MiddleLeft;
GUI.skin.box.padding = new RectOffset(5, 5, 5, 5);
GUI.skin.toggle.margin = new RectOffset(0, 0, 0, 0);
GUI.skin.horizontalSlider.margin = new RectOffset(0, 0, 0, 0);
m_GUIStylesInitialized = true;
}
private bool m_GUIStylesInitialized = false;
private void DisplayNoLabelersMessage()
{
var x = Screen.width - panelWidth - 10;
var height = Math.Min(Screen.height * 0.5f - 20, 90);
GUILayout.BeginArea(new Rect(x, 10, panelWidth, height), GUI.skin.box);
GUILayout.Label("Visualization: No labelers are currently active. Enable at least one labeler from the inspector window of your perception camera to see visualizations.");
// If a labeler has never been initialized then it was off from the
// start, it should not be called to draw on the UI
foreach (var labeler in m_Labelers.Where(labeler => labeler.isInitialized))
{
labeler.VisualizeUI();
GUILayout.Space(4);
}
GUILayout.EndArea();
CaptureOptions.useAsyncReadbackIfSupported = false;
private void OnGUI()
void OnGUI()
{
if (!m_ShowingVisualizations) return;

hudPanel.OnDrawGUI();
var x = Screen.width - panelWidth - 10;
var height = Math.Min(Screen.height * 0.5f - 20, panelHeight);
var x = Screen.width - k_PanelWidth - 10;
var height = Math.Min(Screen.height * 0.5f - 20, k_PanelHeight);
GUILayout.BeginArea(new Rect(x, 10, panelWidth, height), GUI.skin.box);
GUILayout.BeginArea(new Rect(x, 10, k_PanelWidth, height), GUI.skin.box);
scrollPosition = GUILayout.BeginScrollView(scrollPosition);
m_ScrollPosition = GUILayout.BeginScrollView(m_ScrollPosition);
// If a labeler has never been initialized then it was off from the
// start, it should not be called to draw on the UI

// This needs to happen here so that the overlay panel controls
// are placed in the controls panel
overlayPanel.OnDrawGUI(x, 10, panelWidth, height);
overlayPanel.OnDrawGUI(x, 10, k_PanelWidth, height);
}
void OnValidate()

}
// Convert the Unity 4x4 projection matrix to a 3x3 matrix
void OnDisable()
{
RenderPipelineManager.beginCameraRendering -= OnBeginCameraRendering;
RenderPipelineManager.endCameraRendering -= OnEndCameraRendering;
RenderPipelineManager.endCameraRendering -= CheckForRendererFeature;
}
void OnDestroy()
{
DatasetCapture.SimulationEnding -= OnSimulationEnding;
OnSimulationEnding();
CleanupVisualization();
if (SensorHandle.IsValid)
SensorHandle.Dispose();
SensorHandle = default;
}
void EnsureSensorRegistered()
{
if (m_SensorHandle.IsNil)
{
m_EgoMarker = GetComponentInParent<Ego>();
var ego = m_EgoMarker == null ? DatasetCapture.RegisterEgo("") : m_EgoMarker.EgoHandle;
SensorHandle = DatasetCapture.RegisterSensor(
ego, "camera", description, firstCaptureFrame, captureTriggerMode,
simulationDeltaTime, framesBetweenCaptures, manualSensorAffectSimulationTiming);
}
}
void SetupVisualizationCamera()
{
#if !UNITY_EDITOR && !DEVELOPMENT_BUILD
showVisualizations = false;
#else
var visualizationAllowed = s_VisualizedPerceptionCamera == null;
if (!visualizationAllowed && showVisualizations)
{
Debug.LogWarning("Currently only one PerceptionCamera may be visualized at a time. " +
$"Disabling visualization on {gameObject.name}.");
showVisualizations = false;
return;
}
if (!showVisualizations)
return;
m_ShowingVisualizations = true;
s_VisualizedPerceptionCamera = this;
hudPanel = gameObject.AddComponent<HUDPanel>();
overlayPanel = gameObject.AddComponent<OverlayPanel>();
overlayPanel.perceptionCamera = this;
#endif
}
void CheckForRendererFeature(ScriptableRenderContext context, Camera cam)
{
if (cam == attachedCamera)
{
#if URP_PRESENT
if (!m_IsGroundTruthRendererFeaturePresent)
{
Debug.LogError("GroundTruthRendererFeature must be present on the ScriptableRenderer associated " +
"with the camera. The ScriptableRenderer can be accessed through Edit -> Project Settings... " +
"-> Graphics -> Scriptable Render Pipeline Settings -> Renderer List.");
enabled = false;
}
#endif
RenderPipelineManager.endCameraRendering -= CheckForRendererFeature;
}
}
#if URP_PRESENT
public void AddScriptableRenderPass(ScriptableRenderPass pass)
{
passes.Add(pass);
}
#endif
void SetUpGUIStyles()
{
GUI.skin.label.fontSize = 12;
GUI.skin.label.font = Resources.Load<Font>("Inter-Light");
GUI.skin.label.padding = new RectOffset(0, 0, 1, 1);
GUI.skin.label.margin = new RectOffset(0, 0, 1, 1);
GUI.skin.label.wordWrap = true;
GUI.skin.label.alignment = TextAnchor.MiddleLeft;
GUI.skin.box.padding = new RectOffset(5, 5, 5, 5);
GUI.skin.toggle.margin = new RectOffset(0, 0, 0, 0);
GUI.skin.horizontalSlider.margin = new RectOffset(0, 0, 0, 0);
m_GUIStylesInitialized = true;
}
void DisplayNoLabelersMessage()
{
var x = Screen.width - k_PanelWidth - 10;
var height = Math.Min(Screen.height * 0.5f - 20, 90);
GUILayout.BeginArea(new Rect(x, 10, k_PanelWidth, height), GUI.skin.box);
GUILayout.Label("Visualization: No labelers are currently active. Enable at least one labeler from the " +
"inspector window of your perception camera to see visualizations.");
// If a labeler has never been initialized then it was off from the
// start, it should not be called to draw on the UI
foreach (var labeler in m_Labelers.Where(labeler => labeler.isInitialized))
{
labeler.VisualizeUI();
GUILayout.Space(4);
}
GUILayout.EndArea();
}
/// <summary>
/// Convert the Unity 4x4 projection matrix to a 3x3 matrix
/// </summary>
// ReSharper disable once InconsistentNaming
static float3x3 ToProjectionMatrix3x3(Matrix4x4 inMatrix)
{

// Record the camera's projection matrix
SetPersistentSensorData("camera_intrinsic", ToProjectionMatrix3x3(cam.projectionMatrix));
var captureFilename = $"{Manager.Instance.GetDirectoryFor(rgbDirectory)}/{s_RgbFilePrefix}{Time.frameCount}.png";
var dxRootPath = $"{rgbDirectory}/{s_RgbFilePrefix}{Time.frameCount}.png";
SensorHandle.ReportCapture(dxRootPath, SensorSpatialData.FromGameObjects(m_EgoMarker == null ? null : m_EgoMarker.gameObject, gameObject), m_PersistentSensorData.Select(kvp => (kvp.Key, kvp.Value)).ToArray());
var captureFilename = $"{Manager.Instance.GetDirectoryFor(rgbDirectory)}/{k_RgbFilePrefix}{Time.frameCount}.png";
var dxRootPath = $"{rgbDirectory}/{k_RgbFilePrefix}{Time.frameCount}.png";
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;
var width = cam.pixelWidth;

byte[] encodedData;
using (s_EncodeAndSave.Auto())
{
encodedData = ImageConversion.EncodeArrayToPNG(dataColorBuffer, GraphicsFormat.R8G8B8A8_UNorm, (uint)width, (uint)height);
encodedData = ImageConversion.EncodeArrayToPNG(
dataColorBuffer, GraphicsFormat.R8G8B8A8_UNorm, (uint)width, (uint)height);
return !FileProducer.Write(captureFilename, encodedData) ? AsyncRequest.Result.Error : AsyncRequest.Result.Completed;
return !FileProducer.Write(captureFilename, encodedData)
? AsyncRequest.Result.Error
: AsyncRequest.Result.Completed;
}
};

CallOnLabelers(l => l.InternalOnEndRendering());
}
private void CallOnLabelers(Action<CameraLabeler> action)
void CallOnLabelers(Action<CameraLabeler> action)
{
foreach (var labeler in m_Labelers)
{

}
}
private bool ShouldCallLabelers(Camera cam, int lastFrameCalledThisCallback)
bool ShouldCallLabelers(Camera cam, int lastFrameCalledThisCallback)
if (cam != m_AttachedCamera)
if (cam != attachedCamera)
//there are cases when OnBeginCameraRendering is called multiple times in the same frame. Ignore the subsequent calls.
// There are cases when OnBeginCameraRendering is called multiple times in the same frame.
// Ignore the subsequent calls.
if (lastFrameCalledThisCallback == Time.frameCount)
return false;
#if UNITY_EDITOR

return true;
}
void OnDisable()
{
RenderPipelineManager.beginCameraRendering -= OnBeginCameraRendering;
RenderPipelineManager.endCameraRendering -= OnEndCameraRendering;
RenderPipelineManager.endCameraRendering -= CheckForRendererFeature;
}
void OnDestroy()
{
DatasetCapture.SimulationEnding -= OnSimulationEnding;
OnSimulationEnding();
CleanupVisualization();
if (SensorHandle.IsValid)
SensorHandle.Dispose();
SensorHandle = default;
}
void CleanupVisualization()
{
if (s_VisualizedPerceptionCamera == this)

}
/// <summary>
/// The <see cref="CameraLabeler"/> instances which will be run for this PerceptionCamera.
/// </summary>
public IReadOnlyList<CameraLabeler> labelers => m_Labelers;
/// <summary>
/// Add the given <see cref="CameraLabeler"/> to the PerceptionCamera. It will be set up and executed by this
/// PerceptionCamera each frame it captures data.
/// </summary>
/// <param name="cameraLabeler">The labeler to add to this PerceptionCamera</param>
public void AddLabeler(CameraLabeler cameraLabeler) => m_Labelers.Add(cameraLabeler);
/// <summary>
/// Removes the given <see cref="CameraLabeler"/> from the list of labelers under this PerceptionCamera, if it
/// is in the list. The labeler is cleaned up in the process. Labelers removed from a PerceptionCamera should
/// not be used again.
/// </summary>
/// <param name="cameraLabeler"></param>
/// <returns></returns>
public bool RemoveLabeler(CameraLabeler cameraLabeler)
#if URP_PRESENT
internal void MarkGroundTruthRendererFeatureAsPresent()
if (m_Labelers.Remove(cameraLabeler))
{
if (cameraLabeler.isInitialized)
cameraLabeler.InternalCleanup();
return true;
}
return false;
// only used to confirm that GroundTruthRendererFeature is present in URP
m_IsGroundTruthRendererFeaturePresent = true;
internal void OnGroundTruthRendererFeatureRun()
{
//only used to confirm that GroundTruthRendererFeature is present in URP
m_GroundTruthRendererFeatureRun = true;
}
#endif
}
}

24
com.unity.perception/Runtime/GroundTruth/PerceptionCamera_InstanceSegmentation.cs


using System;
using System.Collections.Generic;
using UnityEngine.Rendering.HighDefinition;
using UnityEngine.Rendering.HighDefinition;
using UnityEngine.Rendering.Universal;
using UnityEngine.Rendering.Universal;
#endif
namespace UnityEngine.Perception.GroundTruth

RenderTexture m_InstanceSegmentationTexture;
RenderTextureReader<Color32> m_InstanceSegmentationReader;
internal bool m_fLensDistortionEnabled = false;
private float? m_LensDistortionIntensityOverride;
#if HDRP_PRESENT
float? m_LensDistortionIntensityOverride;
#if HDRP_PRESENT
#elif URP_PRESENT
#elif URP_PRESENT
#endif
#endif
m_LensDistortionPass.m_LensDistortionCrossPipelinePass.lensDistortionOverride = intensity;
m_LensDistortionPass.lensDistortionCrossPipelinePass.lensDistortionOverride = intensity;
}
#endif

};
m_LensDistortionPass.EnsureInit();
customPassVolume.customPasses.Add(m_LensDistortionPass);
m_fLensDistortionEnabled = true;
#elif URP_PRESENT
m_InstanceSegmentationPass = new InstanceSegmentationUrpPass(myCamera, m_InstanceSegmentationTexture);
AddScriptableRenderPass(m_InstanceSegmentationPass);

AddScriptableRenderPass(m_LensDistortionPass);
m_fLensDistortionEnabled = true;
m_LensDistortionPass.m_LensDistortionCrossPipelinePass.lensDistortionOverride =
m_LensDistortionPass.lensDistortionCrossPipelinePass.lensDistortionOverride =
m_LensDistortionIntensityOverride;
#endif

11
com.unity.perception/Runtime/GroundTruth/RenderTextureReader.cs


/// <param name="imageReadCallback">The callback to call after reading the texture</param>
public RenderTextureReader(RenderTexture source, Camera cameraRenderingToSource, Action<int, NativeArray<T>, RenderTexture> imageReadCallback)
{
this.m_Source = source;
this.m_ImageReadCallback = imageReadCallback;
this.m_CameraRenderingToSource = cameraRenderingToSource;
m_Source = source;
m_ImageReadCallback = imageReadCallback;
m_CameraRenderingToSource = cameraRenderingToSource;
m_NextFrameToCapture = Time.frameCount;
RenderPipelineManager.endFrameRendering += OnEndFrameRendering;

if (!GraphicsUtilities.SupportsAsyncReadback())
{
RenderTexture.active = m_Source;
m_CpuTexture.ReadPixels(new Rect(
Vector2.zero,
new Vector2(m_Source.width, m_Source.height)),

m_ImageReadCallback(Time.frameCount, data, m_Source);
return;
}
else
{

12
com.unity.perception/Runtime/GroundTruth/RenderedObjectInfo.cs


using System;
// ReSharper disable NonReadonlyMemberInGetHashCode
/// Describes an instance of an object in an instance segmentation frame. Generated by <see cref="RenderedObjectInfoGenerator"/>.
/// Describes an instance of an object in an instance segmentation frame.
/// Generated by <see cref="RenderedObjectInfoGenerator"/>.
/// </summary>
public struct RenderedObjectInfo : IEquatable<RenderedObjectInfo>
{

/// <inheritdoc />
public override string ToString()
{
return $"{nameof(instanceId)}: {instanceId}, {nameof(boundingBox)}: {boundingBox}, {nameof(pixelCount)}: {pixelCount}, {nameof(instanceColor)}: {instanceColor}";
return $"{nameof(instanceId)}: {instanceId}, {nameof(boundingBox)}: {boundingBox}, " +
$"{nameof(pixelCount)}: {pixelCount}, {nameof(instanceColor)}: {instanceColor}";
return instanceId == other.instanceId && boundingBox.Equals(other.boundingBox) && pixelCount == other.pixelCount;
return instanceId == other.instanceId &&
boundingBox.Equals(other.boundingBox) &&
pixelCount == other.pixelCount;
}
/// <inheritdoc />

{
unchecked
{
// ReSharper disable NonReadonlyMemberInGetHashCode
var hashCode = (int)instanceId;
hashCode = (hashCode * 397) ^ boundingBox.GetHashCode();
hashCode = (hashCode * 397) ^ pixelCount;

2
com.unity.perception/Runtime/GroundTruth/RenderedObjectInfoGenerator.cs


public int left;
public int right;
}
[BurstCompile]
struct ComputeHistogramPerRowJob : IJob
{

instanceId = 0,
row = row + rowStart
};
for (var i = 0; i < rowSlice.Length; i++)
{
var packed = InstanceIdToColorMapping.GetPackedColorFromColor(rowSlice[i]);

77
com.unity.perception/Runtime/GroundTruth/SimulationState.cs


using UnityEngine;
using UnityEngine.Profiling;
// ReSharper disable NotAccessedField.Local
// ReSharper disable NonReadonlyMemberInGetHashCode
namespace UnityEngine.Perception.GroundTruth
{
partial class SimulationState

HashSet<Guid> m_Ids = new HashSet<Guid>();
Guid m_SequenceId = Guid.NewGuid();
//Always use the property SequenceTimeMs instead
// Always use the property SequenceTimeMs instead
int m_FrameCountLastUpdatedSequenceTime;
float m_SequenceTimeDoNotUse;
float m_UnscaledSequenceTimeDoNotUse;

string m_OutputDirectoryPath;
public const string latestOutputDirectoryKey = "latestOutputDirectory";
JsonSerializer m_AnnotationSerializer;
public bool IsRunning { get; private set; }
public string OutputDirectory

public SimulationState(string outputDirectory)
{
m_AnnotationSerializer = JsonSerializer.CreateDefault();
m_AnnotationSerializer.Converters.Add(new Vector3Converter());
m_AnnotationSerializer.Converters.Add(new QuaternionConverter());
PlayerPrefs.SetString(latestOutputDirectoryKey, Manager.Instance.GetDirectoryFor("",""));
PlayerPrefs.SetString(latestOutputDirectoryKey, Manager.Instance.GetDirectoryFor());
IsRunning = true;
}

Values = values;
}
// ReSharper disable NotAccessedField.Local
public readonly SensorHandle SensorHandle;
public readonly SensorHandle SensorHandle;
public readonly Guid CaptureId;
public readonly Annotation Annotation;
public readonly Guid SequenceId;

{
unchecked
{
// ReSharper disable NonReadonlyMemberInGetHashCode
var hashCode = (name != null ? StringComparer.InvariantCulture.GetHashCode(name) : 0);
hashCode = (hashCode * 397) ^ (description != null ? StringComparer.InvariantCulture.GetHashCode(description) : 0);
hashCode = (hashCode * 397) ^ (format != null ? StringComparer.InvariantCulture.GetHashCode(format) : 0);

m_ActiveSensors.Add(sensorHandle);
}
void CheckDatasetAllowed()
static void CheckDatasetAllowed()
{
if (!Application.isPlaying)
{

if (m_Ids.Count == 0)
return;
WritePendingCaptures(true, writeCapturesFromThisFrame: true);
WritePendingCaptures(true, true);
if (m_PendingCaptures.Count > 0)
Debug.LogError($"Simulation ended with pending annotations: {string.Join(", ", m_PendingCaptures.Select(c => $"id:{c.SensorHandle.Id} frame:{c.FrameCount}"))}");

foreach (var value in values)
jArray.Add(new JRaw(DatasetJsonUtility.ToJToken(value)));
ReportAsyncAnnotationResult<T>(asyncAnnotation, filename, jArray);
ReportAsyncAnnotationResult(asyncAnnotation, filename, jArray);
[SuppressMessage("ReSharper", "PossibleInvalidOperationException")]
public class QuaternionConverter : JsonConverter<Quaternion>
{
public override void WriteJson(JsonWriter writer, Quaternion quaternion, JsonSerializer serializer)
{
writer.WriteStartArray();
writer.WriteValue(quaternion.x);
writer.WriteValue(quaternion.y);
writer.WriteValue(quaternion.z);
writer.WriteValue(quaternion.w);
writer.WriteEndArray();
}
public override Quaternion ReadJson(JsonReader reader, Type objectType, Quaternion existingValue, bool hasExistingValue, JsonSerializer serializer)
{
var q = Quaternion.identity;
reader.Read(); // open [ token
q.x = (float)reader.ReadAsDecimal();
q.y = (float)reader.ReadAsDecimal();
q.z = (float)reader.ReadAsDecimal();
q.w = (float)reader.ReadAsDecimal();
reader.Read(); // close ] token
return q;
}
}
[SuppressMessage("ReSharper", "PossibleInvalidOperationException")]
public class Vector3Converter : JsonConverter<Vector3>
{
public override void WriteJson(JsonWriter writer, Vector3 value, JsonSerializer serializer)
{
writer.WriteStartArray();
writer.WriteValue(value.x);
writer.WriteValue(value.y);
writer.WriteValue(value.z);
writer.WriteEndArray();
}
public override Vector3 ReadJson(JsonReader reader, Type objectType, Vector3 existingValue, bool hasExistingValue, JsonSerializer serializer)
{
var outVector = new Vector3();
reader.Read(); // open array token
outVector.x = (float)reader.ReadAsDecimal();
outVector.y = (float)reader.ReadAsDecimal();
outVector.z = (float)reader.ReadAsDecimal();
reader.Read(); // close array token
return outVector;
}
}
public void ReportAsyncAnnotationResult<T>(AsyncAnnotation asyncAnnotation, string filename = null, IEnumerable<T> values = null)
{
JArray jArray = null;

}
}
ReportAsyncAnnotationResult<T>(asyncAnnotation, filename, jArray);
ReportAsyncAnnotationResult(asyncAnnotation, filename, jArray);
void ReportAsyncAnnotationResult<T>(AsyncAnnotation asyncAnnotation, string filename, JArray jArray)
void ReportAsyncAnnotationResult(AsyncAnnotation asyncAnnotation, string filename, JArray jArray)
int annotationIndex = -1;
var annotationIndex = -1;
foreach (var c in m_PendingCaptures)
{
if (c.Step == asyncAnnotation.Annotation.Step && c.SensorHandle == asyncAnnotation.Annotation.SensorHandle)

1
com.unity.perception/Runtime/Randomization/Randomizers/Randomizer.cs


using System;
using System.Collections.Generic;
using Unity.Entities;
using UnityEngine.Perception.Randomization.Parameters;
using UnityEngine.Perception.Randomization.Scenarios;

1
com.unity.perception/Runtime/Randomization/Randomizers/RandomizerExamples/Utilities/PoissonDiskSampling.cs


using Unity.Collections;
using Unity.Jobs;
using Unity.Mathematics;
using UnityEngine.Scripting.APIUpdating;
namespace UnityEngine.Perception.Randomization.Randomizers.Utilities
{

11
com.unity.perception/Runtime/Randomization/Scenarios/ScenarioBase.cs


/// </summary>
public enum State
{
/// <summary>
/// The scenario has yet to start
/// </summary>
/// <summary>
/// The scenario is executing
/// </summary>
/// <summary>
/// The scenario has finished and is idle
/// </summary>
Idle
}
}

45
com.unity.perception/Tests/Runtime/GroundTruthTests/DatasetJsonUtilityTests.cs


var jsonActual = DatasetJsonUtility.ToJToken(o).ToString();
Assert.AreEqual(TestHelper.NormalizeJson(jsonExpected), TestHelper.NormalizeJson(jsonActual));
}
[Test]
[TestCase(1,2,3)]
[TestCase(0,0,0)]
[TestCase(1000, 13000,700)]
[TestCase(1.23f, 4.56f, 7.89f)]
[TestCase(-100, 27.33f, -501.78f)]
public void Vector3Converter_ConvertsProperly(float x, float y, float z)
{
var converter = new SimulationState.Vector3Converter();
var testCase = new Vector3(x,y,z);
var sb = new StringBuilder();
var sw = new StringWriter(sb);
var serializer = JsonSerializer.CreateDefault();
converter.WriteJson(new JsonTextWriter(sw), testCase, serializer);
var str = sb.ToString();
var sr = new StringReader(str);
var converted = converter.ReadJson(new JsonTextReader(sr), typeof(Vector3), Vector3.zero, false, serializer);
Assert.AreEqual(testCase, converted);
}
[Test]
[TestCase(1,2,3, 4)]
[TestCase(0,0,0, 1)]
[TestCase(0, 0.6502878f, 0, -0.7596879f)]
[TestCase(-0.3670137f, -0.1835069f, 0.1223379f, -0.9036922f)]
public void QuaternionConverter_ConvertsProperly(float x, float y, float z, float w)
{
var converter = new SimulationState.QuaternionConverter();
var testCase = new Quaternion(x, y, z, w);
var sb = new StringBuilder();
var sw = new StringWriter(sb);
var serializer = JsonSerializer.CreateDefault();
converter.WriteJson(new JsonTextWriter(sw), testCase, serializer);
var str = sb.ToString();
var sr = new StringReader(str);
var converted = converter.ReadJson(new JsonTextReader(sr), typeof(Quaternion), Quaternion.identity, false, serializer);
Assert.AreEqual(testCase, converted);
}
}
}

1
com.unity.perception/Tests/Runtime/GroundTruthTests/LabelEntryMatchCacheTests.cs


using System.Collections;
using NUnit.Framework;
using Unity.Entities;
using UnityEngine;
using UnityEngine.Perception.GroundTruth;
using UnityEngine.TestTools;

4
com.unity.perception/Tests/Runtime/GroundTruthTests/PerceptionCameraIntegrationTests.cs


{
var capturesPath = Path.Combine(DatasetCapture.OutputDirectory, "captures_000.json");
var capturesJson = File.ReadAllText(capturesPath);
var imagePath = $"{semanticSegmentationLabeler.m_SemanticSegmentationDirectory}/{expectedImageFilename}";
var imagePath = $"{semanticSegmentationLabeler.semanticSegmentationDirectory}/{expectedImageFilename}";
StringAssert.Contains(imagePath, capturesJson);
}
else

var capturesPath = Path.Combine(DatasetCapture.OutputDirectory, "captures_000.json");
var capturesJson = File.ReadAllText(capturesPath);
var imagePath = $"{semanticSegmentationLabeler.m_SemanticSegmentationDirectory}/{expectedImageFilename}";
var imagePath = $"{semanticSegmentationLabeler.semanticSegmentationDirectory}/{expectedImageFilename}";
StringAssert.Contains(imagePath, capturesJson);
}

14
com.unity.perception/Tests/Runtime/GroundTruthTests/SegmentationGroundTruthTests.cs


if (frameStart == null || frameStart > frameCount) return;
timesSegmentationImageReceived++;
CollectionAssert.AreEqual(Enumerable.Repeat(expectedPixelValue, data.Length), data);
CollectionAssert.AreEqual(Enumerable.Repeat(expectedPixelValue, data.Length), data.ToArray());
}
switch (segmentationKind)

void OnSegmentationImageReceived(NativeArray<Color32> data)
{
timesSegmentationImageReceived++;
CollectionAssert.AreEqual(Enumerable.Repeat(expectedPixelValue, data.Length), data);
CollectionAssert.AreEqual(Enumerable.Repeat(expectedPixelValue, data.Length), data.ToArray());
}
var cameraObject = SetupCameraSemanticSegmentation(a => OnSegmentationImageReceived(a.data), false);

void OnSegmentationImageReceived(NativeArray<Color32> data)
{
timesSegmentationImageReceived++;
CollectionAssert.AreEqual(Enumerable.Repeat(expectedPixelValue, data.Length), data);
CollectionAssert.AreEqual(Enumerable.Repeat(expectedPixelValue, data.Length), data.ToArray());
}
var cameraObject = SetupCameraSemanticSegmentation(a => OnSegmentationImageReceived(a.data), showVisualizations);

try
{
CollectionAssert.AreEqual(Enumerable.Repeat(expectedLabelAtFrame[frameCount], data.Length), data);
CollectionAssert.AreEqual(Enumerable.Repeat(expectedLabelAtFrame[frameCount], data.Length), data.ToArray());
}
// ReSharper disable once RedundantCatchClause

}
var cameraObject = segmentationKind == SegmentationKind.Instance ?
SetupCameraInstanceSegmentation(OnSegmentationImageReceived<Color32>) :
SetupCameraSemanticSegmentation((a) => OnSegmentationImageReceived<Color32>(a.frameCount, a.data, a.sourceTexture), false);
SetupCameraInstanceSegmentation(OnSegmentationImageReceived) :
SetupCameraSemanticSegmentation((a) => OnSegmentationImageReceived(a.frameCount, a.data, a.sourceTexture), false);
object expectedPixelValue = segmentationKind == SegmentationKind.Instance ? (object) k_InstanceSegmentationPixelValue : k_SemanticPixelValue;
var expectedPixelValue = segmentationKind == SegmentationKind.Instance ? (object) k_InstanceSegmentationPixelValue : k_SemanticPixelValue;
expectedLabelAtFrame = new Dictionary<int, object>
{

1
com.unity.perception/Tests/Runtime/Randomization/RandomizerTests/RandomizerTagTests.cs


using NUnit.Framework;
using UnityEngine;
using UnityEngine.Perception.Randomization.Randomizers;
using Assert = Unity.Assertions.Assert;
namespace RandomizationTests.RandomizerTests
{

14
com.unity.perception/package.json


{
"dependencies": {
"com.unity.burst": "1.4.6",
"com.unity.collections": "0.9.0-preview.6",
"com.unity.burst": "1.3.9",
"com.unity.entities": "0.8.0-preview.8",
"com.unity.simulation.capture": "0.0.10-preview.19",
"com.unity.simulation.capture": "0.0.10-preview.19",
"com.unity.simulation.core": "0.0.10-preview.22"
},
"description": "Tools for generating large-scale data sets for perception-based machine learning training and validation",

"version": "0.7.0-preview.2",
"samples":[
"samples": [
"path" : "Samples~/Tutorial Files"
"path": "Samples~/Tutorial Files"
"path" : "Samples~/Human Pose Labeling and Randomization"
"path": "Samples~/Human Pose Labeling and Randomization"
}
}

2
com.unity.perception/Runtime/GroundTruth/RenderPasses/CrossPipelinePasses/GroundTruthCrossPipelinePass.cs.meta


fileFormatVersion: 2
guid: a3a8d5a7497d49a2a2c8f0c1c52aae18
guid: bc2a459f27b0e93409b0ef30b7924d35
MonoImporter:
externalObjects: {}
serializedVersion: 2

4
com.unity.perception/Runtime/GroundTruth/RenderPasses/HdrpPasses/InstanceSegmentationPass.cs


#if HDRP_PRESENT
using System;
using JetBrains.Annotations;
using UnityEngine.Rendering.HighDefinition;

public Camera targetCamera;
[UsedImplicitly]
public InstanceSegmentationPass()
{}
public InstanceSegmentationPass() {}
protected override void Execute(ScriptableRenderContext renderContext, CommandBuffer cmd, HDCamera hdCamera, CullingResults cullingResult)
{

2
com.unity.perception/Runtime/GroundTruth/RenderPasses/CrossPipelinePasses/SemanticSegmentationCrossPipelinePass.cs.meta


fileFormatVersion: 2
guid: a6740ca70f44ece45901dc26511f9aa0
guid: cce1552fd4183b64f983461e6e07a317
MonoImporter:
externalObjects: {}
serializedVersion: 2

5
com.unity.perception/Runtime/GroundTruth/RenderPasses/HdrpPasses/SemanticSegmentationPass.cs


#if HDRP_PRESENT
using System;
using UnityEngine.Rendering;
using UnityEngine.Rendering.HighDefinition;

m_SemanticSegmentationCrossPipelinePass = new SemanticSegmentationCrossPipelinePass(targetCamera, semanticSegmentationLabelConfig);
m_SemanticSegmentationCrossPipelinePass.EnsureActivated();
}
}
public SemanticSegmentationPass()
{
}
protected override void Setup(ScriptableRenderContext renderContext, CommandBuffer cmd)

2
com.unity.perception/Runtime/GroundTruth/RenderPasses/HdrpPasses/SemanticSegmentationPass.cs.meta


fileFormatVersion: 2
guid: 6d4bfcc857cac4d4ab70b19c87508947
guid: 84b5f1182e5f64c49954fc264cd2e46f
MonoImporter:
externalObjects: {}
serializedVersion: 2

23
com.unity.perception/Runtime/GroundTruth/RenderPasses/HdrpPasses/GroundTruthPass.cs


#if HDRP_PRESENT
using Unity.Entities;
public abstract class GroundTruthPass : CustomPass, IGroundTruthGenerator
abstract class GroundTruthPass : CustomPass, IGroundTruthGenerator
public abstract void SetupMaterialProperties(MaterialPropertyBlock mpb, Renderer meshRenderer, Labeling labeling, uint instanceId);
public abstract void SetupMaterialProperties(
MaterialPropertyBlock mpb, Renderer meshRenderer, Labeling labeling, uint instanceId);
protected GroundTruthPass(Camera targetCamera)
{

// If we are forced to activate here we will get zeroes in the first frame.
EnsureActivated();
this.targetColorBuffer = TargetBuffer.Custom;
this.targetDepthBuffer = TargetBuffer.Custom;
targetColorBuffer = TargetBuffer.Custom;
targetDepthBuffer = TargetBuffer.Custom;
protected sealed override void Execute(ScriptableRenderContext renderContext, CommandBuffer cmd, HDCamera hdCamera, CullingResults cullingResult)
protected sealed override void Execute(
ScriptableRenderContext renderContext, CommandBuffer cmd, HDCamera hdCamera, CullingResults cullingResult)
{
// CustomPasses are executed for each camera. We only want to run for the target camera
if (hdCamera.camera != targetCamera)

}
protected abstract void ExecutePass(ScriptableRenderContext renderContext, CommandBuffer cmd, HDCamera hdCamera, CullingResults cullingResult);
protected abstract void ExecutePass(
ScriptableRenderContext renderContext, CommandBuffer cmd, HDCamera hdCamera, CullingResults cullingResult);
var labelSetupSystem = World.DefaultGameObjectInjectionWorld?.GetExistingSystem<GroundTruthLabelSetupSystem>();
labelSetupSystem?.Activate(this);
LabelManager.singleton.Activate(this);
m_IsActivated = true;
}
}

var labelSetupSystem = World.DefaultGameObjectInjectionWorld?.GetExistingSystem<GroundTruthLabelSetupSystem>();
labelSetupSystem?.Deactivate(this);
LabelManager.singleton.Deactivate(this);
}
}
}

168
com.unity.perception/Runtime/GroundTruth/Labeling/LabelManager.cs


using System.Collections.Generic;
using UnityEngine.Perception.Randomization.Randomizers;
namespace UnityEngine.Perception.GroundTruth
{
/// <summary>
/// Manages the registration of <see cref="Labeling"/> components
/// </summary>
[DefaultExecutionOrder(1)]
public class LabelManager
{
/// <summary>
/// Returns the active LabeledObjectsManager instance
/// </summary>
public static LabelManager singleton { get; } = new LabelManager();
const uint k_StartingIndex = 1;
uint m_NextObjectIndex = k_StartingIndex;
List<IGroundTruthGenerator> m_ActiveGenerators = new List<IGroundTruthGenerator>();
LinkedHashSet<Labeling> m_LabelsPendingRegistration = new LinkedHashSet<Labeling>();
LinkedHashSet<Labeling> m_RegisteredLabels = new LinkedHashSet<Labeling>();
/// <summary>
/// Returns the set of registered Labeling components
/// </summary>
public IEnumerable<Labeling> registeredLabels => m_RegisteredLabels;
/// <summary>
/// Registers all pending labels.
/// Called once per frame during LateUpdate by the <see cref="PerceptionUpdater"/>.
/// </summary>
public void RegisterPendingLabels()
{
if (m_RegisteredLabels.Count == 0)
m_NextObjectIndex = k_StartingIndex;
foreach (var unregisteredLabel in m_LabelsPendingRegistration)
{
if (m_RegisteredLabels.Contains(unregisteredLabel))
continue;
var instanceId = m_NextObjectIndex++;
RecursivelyInitializeGameObjects(
unregisteredLabel.gameObject,
new MaterialPropertyBlock(),
unregisteredLabel,
instanceId);
unregisteredLabel.SetInstanceId(instanceId);
m_RegisteredLabels.Add(unregisteredLabel);
}
m_LabelsPendingRegistration.Clear();
}
/// <summary>
/// Activates the given <see cref="IGroundTruthGenerator"/>.
/// <see cref="IGroundTruthGenerator.SetupMaterialProperties"/> will be called for all
/// <see cref="MeshRenderer"/> instances under each object containing a <see cref="Labeling"/> component.
/// </summary>
/// <param name="generator">The generator to register</param>
public void Activate(IGroundTruthGenerator generator)
{
m_ActiveGenerators.Add(generator);
foreach (var label in m_RegisteredLabels)
RecursivelyInitializeGameObjects(label.gameObject, new MaterialPropertyBlock(), label, label.instanceId);
}
/// <summary>
/// Deactivates the given <see cref="IGroundTruthGenerator"/>.
/// It will no longer receive calls when <see cref="Labeling"/> instances are created.
/// </summary>
/// <param name="generator">The generator to deactivate</param>
/// <returns>
/// True if the <see cref="generator"/> was successfully removed.
/// False if <see cref="generator"/> was not active.
/// </returns>
public bool Deactivate(IGroundTruthGenerator generator)
{
return m_ActiveGenerators.Remove(generator);
}
/// <summary>
/// Registers a labeling component
/// </summary>
/// <param name="labeling">the component to register</param>
internal void Register(Labeling labeling)
{
m_LabelsPendingRegistration.Add(labeling);
}
/// <summary>
/// Unregisters a labeling component
/// </summary>
/// <param name="labeling">the component to unregister</param>
internal void Unregister(Labeling labeling)
{
m_LabelsPendingRegistration.Remove(labeling);
m_RegisteredLabels.Remove(labeling);
}
/// <summary>
/// Refresh ground truth generation for the labeling of a particular GameObject. This is necessary when the
/// list of labels changes or when renderers or materials change on objects in the hierarchy.
/// </summary>
/// <param name="labeling">the component to refresh</param>
internal void RefreshLabeling(Labeling labeling)
{
m_RegisteredLabels.Remove(labeling);
m_LabelsPendingRegistration.Add(labeling);
}
/// <summary>
/// Recursively initializes Renderer components on GameObjects with Labeling components
/// </summary>
/// <param name="gameObject">The parent GameObject being initialized</param>
/// <param name="mpb">A reusable material property block</param>
/// <param name="labeling">The labeling component attached to the current gameObject</param>
/// <param name="instanceId">The perception specific instanceId assigned to the current gameObject</param>
void RecursivelyInitializeGameObjects(
GameObject gameObject, MaterialPropertyBlock mpb, Labeling labeling, uint instanceId)
{
var terrain = gameObject.GetComponent<Terrain>();
if (terrain != null)
{
terrain.GetSplatMaterialPropertyBlock(mpb);
foreach (var pass in m_ActiveGenerators)
pass.SetupMaterialProperties(mpb, null, labeling, instanceId);
terrain.SetSplatMaterialPropertyBlock(mpb);
}
var renderer = gameObject.GetComponent<Renderer>();
if (renderer != null)
{
renderer.GetPropertyBlock(mpb);
foreach (var pass in m_ActiveGenerators)
pass.SetupMaterialProperties(mpb, renderer, labeling, instanceId);
renderer.SetPropertyBlock(mpb);
var materialCount = renderer.materials.Length;
for (var i = 0; i < materialCount; i++)
{
renderer.GetPropertyBlock(mpb, i);
//Only apply to individual materials if there is already a MaterialPropertyBlock on it
if (!mpb.isEmpty)
{
foreach (var pass in m_ActiveGenerators)
pass.SetupMaterialProperties(mpb, renderer, labeling, instanceId);
renderer.SetPropertyBlock(mpb, i);
}
}
}
for (var i = 0; i < gameObject.transform.childCount; i++)
{
var child = gameObject.transform.GetChild(i).gameObject;
if (child.GetComponent<Labeling>() != null)
continue;
RecursivelyInitializeGameObjects(child, mpb, labeling, instanceId);
}
}
}
}

3
com.unity.perception/Runtime/GroundTruth/Labeling/LabelManager.cs.meta


fileFormatVersion: 2
guid: d138f786c7d94325962fec6fa091edbb
timeCreated: 1614980066

25
com.unity.perception/Runtime/GroundTruth/PerceptionUpdater.cs


namespace UnityEngine.Perception.GroundTruth
{
/// <summary>
/// PerceptionUpdater is automatically spawned when the player starts and is used to coordinate and maintain
/// static perception lifecycle behaviours.
/// </summary>
[DefaultExecutionOrder(5)]
class PerceptionUpdater : MonoBehaviour
{
[RuntimeInitializeOnLoadMethod(RuntimeInitializeLoadType.BeforeSceneLoad)]
static void Initialize()
{
var updaterObject = new GameObject("PerceptionUpdater");
updaterObject.AddComponent<PerceptionUpdater>();
updaterObject.hideFlags = HideFlags.HideAndDontSave;
DontDestroyOnLoad(updaterObject);
}
void LateUpdate()
{
LabelManager.singleton.RegisterPendingLabels();
DatasetCapture.SimulationState?.Update();
}
}
}

3
com.unity.perception/Runtime/GroundTruth/PerceptionUpdater.cs.meta


fileFormatVersion: 2
guid: ea65959b27da42a2a364b2ae9a3dc65a
timeCreated: 1614983032

3
com.unity.perception/Runtime/GroundTruth/RenderPasses.meta


fileFormatVersion: 2
guid: 36c50bc1fa70491eaa8ffd85c55187d0
timeCreated: 1615074717

3
com.unity.perception/Runtime/GroundTruth/RenderPasses/CrossPipelinePasses.meta


fileFormatVersion: 2
guid: ab58b1700a9642998cb3a5971f6543d1
timeCreated: 1615157471

120
com.unity.perception/Runtime/GroundTruth/RenderPasses/CrossPipelinePasses/GroundTruthCrossPipelinePass.cs


using System;
using UnityEngine;
using UnityEngine.Experimental.Rendering;
using UnityEngine.Rendering;
namespace UnityEngine.Perception.GroundTruth
{
abstract class GroundTruthCrossPipelinePass : IGroundTruthGenerator
{
public Camera targetCamera;
bool m_IsActivated;
protected GroundTruthCrossPipelinePass(Camera targetCamera)
{
this.targetCamera = targetCamera;
}
public virtual void Setup()
{
if (targetCamera == null)
throw new InvalidOperationException("targetCamera may not be null");
// If we are forced to activate here we will get zeroes in the first frame.
EnsureActivated();
}
public void Execute(
ScriptableRenderContext renderContext, CommandBuffer cmd, Camera camera, CullingResults cullingResult)
{
// CustomPasses are executed for each camera. We only want to run for the target camera
if (camera != targetCamera)
return;
ExecutePass(renderContext, cmd, camera, cullingResult);
}
protected abstract void ExecutePass(
ScriptableRenderContext renderContext, CommandBuffer cmd, Camera camera, CullingResults cullingResult);
public void EnsureActivated()
{
if (!m_IsActivated)
{
LabelManager.singleton.Activate(this);
m_IsActivated = true;
}
}
public void Cleanup()
{
LabelManager.singleton.Deactivate(this);
}
protected RendererListDesc CreateRendererListDesc(
Camera camera,
CullingResults cullingResult,
string overrideMaterialPassName,
int overrideMaterialPassIndex,
Material overrideMaterial,
LayerMask layerMask)
{
var shaderPasses = new[]
{
new ShaderTagId("Forward"), // HD Lit shader
new ShaderTagId("ForwardOnly"), // HD Unlit shader
new ShaderTagId("SRPDefaultUnlit"), // Cross SRP Unlit shader
new ShaderTagId("UniversalForward"), // URP Forward
new ShaderTagId("LightweightForward"), // LWRP Forward
new ShaderTagId(overrideMaterialPassName), // The override material shader
};
var stateBlock = new RenderStateBlock(0)
{
depthState = new DepthState(true, CompareFunction.LessEqual),
};
var result = new RendererListDesc(shaderPasses, cullingResult, camera)
{
rendererConfiguration = PerObjectData.None,
renderQueueRange = new RenderQueueRange { lowerBound = 0, upperBound = 5000 },
sortingCriteria = SortingCriteria.CommonOpaque,
excludeObjectMotionVectors = false,
overrideMaterial = overrideMaterial,
overrideMaterialPassIndex = overrideMaterialPassIndex,
stateBlock = stateBlock,
layerMask = layerMask,
};
return result;
}
protected static void DrawRendererList(
ScriptableRenderContext renderContext, CommandBuffer cmd, RendererList rendererList)
{
if (!rendererList.isValid)
throw new ArgumentException("Invalid renderer list provided to DrawRendererList");
// This is done here because DrawRenderers API lives outside command buffers so we need to call this before
// doing any DrawRenders or things will be executed out of order
renderContext.ExecuteCommandBuffer(cmd);
cmd.Clear();
if (rendererList.stateBlock == null)
renderContext.DrawRenderers(
rendererList.cullingResult, ref rendererList.drawSettings, ref rendererList.filteringSettings);
else
{
var renderStateBlock = rendererList.stateBlock.Value;
renderContext.DrawRenderers(
rendererList.cullingResult,
ref rendererList.drawSettings,
ref rendererList.filteringSettings,
ref renderStateBlock);
}
}
public abstract void SetupMaterialProperties(
MaterialPropertyBlock mpb, Renderer meshRenderer, Labeling labeling, uint instanceId);
}
}

78
com.unity.perception/Runtime/GroundTruth/RenderPasses/CrossPipelinePasses/InstanceSegmentationCrossPipelinePass.cs


using System;
using Unity.Profiling;
using UnityEngine;
using UnityEngine.Experimental.Rendering;
using UnityEngine.Rendering;
namespace UnityEngine.Perception.GroundTruth
{
class InstanceSegmentationCrossPipelinePass : GroundTruthCrossPipelinePass
{
static readonly int k_SegmentationIdProperty = Shader.PropertyToID("_SegmentationId");
const string k_SegmentationPassShaderName = "Perception/InstanceSegmentation";
static ProfilerMarker s_ExecuteMarker = new ProfilerMarker("SegmentationPass_Execute");
/// <summary>
/// The LayerMask to apply when rendering objects.
/// </summary>
LayerMask m_LayerMask = -1;
Shader m_SegmentationShader;
Material m_OverrideMaterial;
/// <summary>
/// Create a new <see cref="InstanceSegmentationCrossPipelinePass"/> referencing the given
/// </summary>
/// <param name="targetCamera"></param>
/// <exception cref="ArgumentNullException"></exception>
public InstanceSegmentationCrossPipelinePass(Camera targetCamera)
: base(targetCamera)
{
if (targetCamera == null)
throw new ArgumentNullException(nameof(targetCamera));
//Activating in the constructor allows us to get correct labeling in the first frame.
EnsureActivated();
}
public override void Setup()
{
base.Setup();
m_SegmentationShader = Shader.Find(k_SegmentationPassShaderName);
var shaderVariantCollection = new ShaderVariantCollection();
shaderVariantCollection.Add(
new ShaderVariantCollection.ShaderVariant(m_SegmentationShader, PassType.ScriptableRenderPipeline));
shaderVariantCollection.WarmUp();
m_OverrideMaterial = new Material(m_SegmentationShader);
}
protected override void ExecutePass(
ScriptableRenderContext renderContext, CommandBuffer cmd, Camera camera, CullingResults cullingResult)
{
using (s_ExecuteMarker.Auto())
{
// Render all objects to our target RenderTexture using `m_OverrideMaterial` to use our shader
cmd.ClearRenderTarget(true, true, Color.black);
var result = CreateRendererListDesc(
camera, cullingResult, "FirstPass", 0, m_OverrideMaterial, m_LayerMask);
DrawRendererList(renderContext, cmd, RendererList.Create(result));
}
}
public override void SetupMaterialProperties(
MaterialPropertyBlock mpb, Renderer renderer, Labeling labeling, uint instanceId)
{
var found = InstanceIdToColorMapping.TryGetColorFromInstanceId(instanceId, out var color);
if (!found)
Debug.LogError($"Could not get a unique color for {instanceId}");
mpb.SetVector(k_SegmentationIdProperty, (Color)color);
#if PERCEPTION_DEBUG
Debug.Log($"Assigning id. Frame {Time.frameCount} id {id}");
#endif
}
}
}

11
com.unity.perception/Runtime/GroundTruth/RenderPasses/CrossPipelinePasses/InstanceSegmentationCrossPipelinePass.cs.meta


fileFormatVersion: 2
guid: 799b5ba08ee3e6d479c2aac3a89507f0
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

191
com.unity.perception/Runtime/GroundTruth/RenderPasses/CrossPipelinePasses/LensDistortionCrossPipelinePass.cs


using System;
using UnityEngine.Rendering;
#if HDRP_PRESENT
using UnityEngine.Rendering.HighDefinition;
#elif URP_PRESENT
using UnityEngine.Rendering.Universal;
#endif
#if HDRP_PRESENT || URP_PRESENT
namespace UnityEngine.Perception.GroundTruth
{
/// <summary>
/// Custom Pass which will apply a lens distortion (per the respective volume override in URP or HDRP, or
/// through a custom override directly through the pass) to an incoming mask / texture. The purpose of this
/// is to allow the same lens distortion being applied to the RGB image in the perception camera to be applied
/// to the respective ground truths generated.
/// </summary>
class LensDistortionCrossPipelinePass : GroundTruthCrossPipelinePass
{
const string k_ShaderName = "Perception/LensDistortion";
// NOTICE: Serialize the shader so that the shader asset is included in player builds when the SemanticSegmentationPass is used.
// Currently commented out and shaders moved to Resources folder due to serialization crashes when it is enabled.
// See https://fogbugz.unity3d.com/f/cases/1187378/
// [SerializeField]
Shader m_LensDistortionShader;
Material m_LensDistortionMaterial;
bool m_Initialized;
static readonly int k_DistortionParams1Id = Shader.PropertyToID("_Distortion_Params1");
static readonly int k_DistortionParams2Id = Shader.PropertyToID("_Distortion_Params2");
LensDistortion m_LensDistortion;
internal float? lensDistortionOverride = null; // Largely for testing, but could be useful otherwise
RenderTexture m_TargetTexture;
RenderTexture m_DistortedTexture;
public LensDistortionCrossPipelinePass(Camera targetCamera, RenderTexture targetTexture)
: base(targetCamera)
{
m_TargetTexture = targetTexture;
}
public override void Setup()
{
base.Setup();
m_LensDistortionShader = Shader.Find(k_ShaderName);
var shaderVariantCollection = new ShaderVariantCollection();
if (shaderVariantCollection != null)
shaderVariantCollection.Add(new ShaderVariantCollection.ShaderVariant(m_LensDistortionShader, PassType.ScriptableRenderPipeline));
m_LensDistortionMaterial = new Material(m_LensDistortionShader);
if (shaderVariantCollection != null)
shaderVariantCollection.WarmUp();
// Set up a new texture
if (m_DistortedTexture == null || m_DistortedTexture.width != Screen.width || m_DistortedTexture.height != Screen.height)
{
if (m_DistortedTexture != null)
m_DistortedTexture.Release();
m_DistortedTexture = new RenderTexture(Screen.width, Screen.height, 0, RenderTextureFormat.ARGBFloat, RenderTextureReadWrite.Linear);
m_DistortedTexture.enableRandomWrite = true;
m_DistortedTexture.filterMode = FilterMode.Point;
m_DistortedTexture.Create();
}
// Grab the lens distortion
#if HDRP_PRESENT
// Grab the Lens Distortion from Perception Camera stack
var hdCamera = HDCamera.GetOrCreate(targetCamera);
var stack = hdCamera.volumeStack;
m_LensDistortion = stack.GetComponent<LensDistortion>();
#elif URP_PRESENT
var stack = VolumeManager.instance.stack;
m_LensDistortion = stack.GetComponent<LensDistortion>();
#endif
m_Initialized = true;
}
protected override void ExecutePass(ScriptableRenderContext renderContext, CommandBuffer cmd, Camera camera, CullingResults cullingResult)
{
if (m_Initialized == false)
return;
if (SetLensDistortionShaderParameters() == false)
return;
// Blit mayhem
cmd.Blit(m_TargetTexture, m_DistortedTexture, m_LensDistortionMaterial);
cmd.Blit(m_DistortedTexture, m_TargetTexture);
renderContext.ExecuteCommandBuffer(cmd);
cmd.Clear();
}
public bool SetLensDistortionShaderParameters()
{
if (m_Initialized == false)
return false;
// This code is lifted from the SetupLensDistortion() function in
// https://github.com/Unity-Technologies/Graphics/blob/257b08bba6c11de0f894e42e811124247a522d3c/com.unity.render-pipelines.universal/Runtime/Passes/PostProcessPass.cs
// This is in UnityEngine.Rendering.Universal.Internal.PostProcessPass::SetupLensDistortion so it's
// unclear how to re-use this code
var intensity = 0.5f;
var scale = 1.0f;
var center = new Vector2(0.0f, 0.0f);
var mult = new Vector2(1.0f, 1.0f);
#if HDRP_PRESENT
if(m_LensDistortion == null)
return false;
#elif URP_PRESENT
if (targetCamera == null)
return false;
var additionalCameraData = targetCamera.GetUniversalAdditionalCameraData();
if (additionalCameraData.renderPostProcessing == false && lensDistortionOverride.HasValue == false)
return false;
if (m_LensDistortion.active == false)
return false;
#else
return false;
#endif
if (lensDistortionOverride.HasValue)
{
intensity = lensDistortionOverride.Value;
}
else if (m_LensDistortion != null)
{
// This is a bit finicky for URP - since Lens Distortion comes off the VolumeManager stack as active
// even if post processing is not enabled. An intensity of 0.0f is untenable, so the below checks
// ensures post processing hasn't been enabled but Lens Distortion actually overriden
if (m_LensDistortion.intensity.value != 0.0f)
{
intensity = m_LensDistortion.intensity.value;
center = m_LensDistortion.center.value * 2f - Vector2.one;
mult.x = Mathf.Max(m_LensDistortion.xMultiplier.value, 1e-4f);
mult.y = Mathf.Max(m_LensDistortion.yMultiplier.value, 1e-4f);
scale = 1.0f / m_LensDistortion.scale.value;
}
else
{
return false;
}
}
var amount = 1.6f * Mathf.Max(Mathf.Abs(intensity * 100.0f), 1.0f);
var theta = Mathf.Deg2Rad * Mathf.Min(160f, amount);
var sigma = 2.0f * Mathf.Tan(theta * 0.5f);
var p1 = new Vector4(
center.x,
center.y,
mult.x,
mult.y
);
var p2 = new Vector4(
intensity >= 0f ? theta : 1f / theta,
sigma,
scale,
intensity * 100.0f
);
// Set Shader Constants
m_LensDistortionMaterial.SetVector(k_DistortionParams1Id, p1);
m_LensDistortionMaterial.SetVector(k_DistortionParams2Id, p2);
return true;
}
public override void SetupMaterialProperties(MaterialPropertyBlock mpb, Renderer renderer, Labeling labeling, uint instanceId)
{
SetLensDistortionShaderParameters();
}
}
}
#endif // ! HDRP_PRESENT || URP_PRESENT

3
com.unity.perception/Runtime/GroundTruth/RenderPasses/CrossPipelinePasses/LensDistortionCrossPipelinePass.cs.meta


fileFormatVersion: 2
guid: 91d542be380c442186b86e0816a0c0fb
timeCreated: 1615157478

83
com.unity.perception/Runtime/GroundTruth/RenderPasses/CrossPipelinePasses/SemanticSegmentationCrossPipelinePass.cs


using System;
using UnityEngine.Experimental.Rendering;
using UnityEngine.Rendering;
namespace UnityEngine.Perception.GroundTruth
{
/// <summary>
/// Custom Pass which renders labeled images where each object labeled with a Labeling component is drawn with the
/// value specified by the given LabelingConfiguration.
/// </summary>
class SemanticSegmentationCrossPipelinePass : GroundTruthCrossPipelinePass
{
const string k_ShaderName = "Perception/SemanticSegmentation";
static readonly int k_LabelingId = Shader.PropertyToID("LabelingId");
static int s_LastFrameExecuted = -1;
SemanticSegmentationLabelConfig m_LabelConfig;
// NOTICE: Serialize the shader so that the shader asset is included in player builds when the SemanticSegmentationPass is used.
// Currently commented out and shaders moved to Resources folder due to serialization crashes when it is enabled.
// See https://fogbugz.unity3d.com/f/cases/1187378/
// [SerializeField]
Shader m_ClassLabelingShader;
Material m_OverrideMaterial;
public SemanticSegmentationCrossPipelinePass(
Camera targetCamera, SemanticSegmentationLabelConfig labelConfig) : base(targetCamera)
{
m_LabelConfig = labelConfig;
}
public override void Setup()
{
base.Setup();
m_ClassLabelingShader = Shader.Find(k_ShaderName);
var shaderVariantCollection = new ShaderVariantCollection();
if (shaderVariantCollection != null)
{
shaderVariantCollection.Add(
new ShaderVariantCollection.ShaderVariant(m_ClassLabelingShader, PassType.ScriptableRenderPipeline));
}
m_OverrideMaterial = new Material(m_ClassLabelingShader);
shaderVariantCollection.WarmUp();
}
protected override void ExecutePass(
ScriptableRenderContext renderContext, CommandBuffer cmd, Camera camera, CullingResults cullingResult)
{
if (s_LastFrameExecuted == Time.frameCount)
return;
s_LastFrameExecuted = Time.frameCount;
var renderList = CreateRendererListDesc(camera, cullingResult, "FirstPass", 0, m_OverrideMaterial, -1);
cmd.ClearRenderTarget(true, true, Color.black);
DrawRendererList(renderContext, cmd, RendererList.Create(renderList));
}
public override void SetupMaterialProperties(
MaterialPropertyBlock mpb, Renderer renderer, Labeling labeling, uint instanceId)
{
var entry = new SemanticSegmentationLabelEntry();
var found = false;
foreach (var l in m_LabelConfig.labelEntries)
{
if (labeling.labels.Contains(l.label))
{
entry = l;
found = true;
break;
}
}
// Set the labeling ID so that it can be accessed in ClassSemanticSegmentationPass.shader
if (found)
mpb.SetVector(k_LabelingId, entry.color);
}
}
}

3
com.unity.perception/Runtime/GroundTruth/RenderPasses/HdrpPasses.meta


fileFormatVersion: 2
guid: 3e921ed47cbb4b4daee97efd288b537e
timeCreated: 1615157360

3
com.unity.perception/Runtime/GroundTruth/RenderPasses/HdrpPasses/GroundTruthPass.cs.meta


fileFormatVersion: 2
guid: fce7b38cf56d40358e555bb2403b23d0
timeCreated: 1615157376

48
com.unity.perception/Runtime/GroundTruth/RenderPasses/HdrpPasses/LensDistortionPass.cs


#if HDRP_PRESENT
using System;
using UnityEngine.Rendering;
using UnityEngine.Rendering.HighDefinition;
namespace UnityEngine.Perception.GroundTruth
{
/// <summary>
/// Custom Pass which renders labeled images where each object with a Labeling component is drawn with the value
/// specified by the given LabelingConfiguration.
/// </summary>
public class LensDistortionPass : CustomPass
{
public RenderTexture targetTexture;
public Camera targetCamera;
internal LensDistortionCrossPipelinePass lensDistortionCrossPipelinePass;
public LensDistortionPass(Camera targetCamera, RenderTexture targetTexture)
{
this.targetTexture = targetTexture;
this.targetCamera = targetCamera;
EnsureInit();
}
public void EnsureInit()
{
if (lensDistortionCrossPipelinePass == null)
{
lensDistortionCrossPipelinePass = new LensDistortionCrossPipelinePass(targetCamera, targetTexture);
lensDistortionCrossPipelinePass.EnsureActivated();
}
}
protected override void Setup(ScriptableRenderContext renderContext, CommandBuffer cmd)
{
EnsureInit();
lensDistortionCrossPipelinePass.Setup();
}
protected override void Execute(ScriptableRenderContext renderContext, CommandBuffer cmd, HDCamera hdCamera, CullingResults cullingResult)
{
CoreUtils.SetRenderTarget(cmd, targetTexture);
lensDistortionCrossPipelinePass.Execute(renderContext, cmd, hdCamera.camera, cullingResult);
}
}
}
#endif

3
com.unity.perception/Runtime/GroundTruth/RenderPasses/UrpPasses.meta


fileFormatVersion: 2
guid: 4cd6f632a845433cbbfc06263002654a
timeCreated: 1615157347

31
com.unity.perception/Runtime/GroundTruth/RenderPasses/UrpPasses/InstanceSegmentationUrpPass.cs


#if URP_PRESENT
using UnityEngine.Rendering;
using UnityEngine.Rendering.Universal;
namespace UnityEngine.Perception.GroundTruth
{
class InstanceSegmentationUrpPass : ScriptableRenderPass
{
InstanceSegmentationCrossPipelinePass m_InstanceSegmentationPass;
public InstanceSegmentationUrpPass(Camera camera, RenderTexture targetTexture)
{
m_InstanceSegmentationPass = new InstanceSegmentationCrossPipelinePass(camera);
ConfigureTarget(targetTexture, targetTexture.depthBuffer);
m_InstanceSegmentationPass.Setup();
}
public override void Execute(ScriptableRenderContext context, ref RenderingData renderingData)
{
var commandBuffer = CommandBufferPool.Get(nameof(InstanceSegmentationUrpPass));
m_InstanceSegmentationPass.Execute(context, commandBuffer, renderingData.cameraData.camera, renderingData.cullResults);
CommandBufferPool.Release(commandBuffer);
}
public void Cleanup()
{
m_InstanceSegmentationPass.Cleanup();
}
}
}
#endif

3
com.unity.perception/Runtime/GroundTruth/RenderPasses/UrpPasses/InstanceSegmentationUrpPass.cs.meta


fileFormatVersion: 2
guid: f8f08e25f7cb4355aa70060081bc4200
timeCreated: 1615156976

31
com.unity.perception/Runtime/GroundTruth/RenderPasses/UrpPasses/LensDistortionUrpPass.cs


#if URP_PRESENT
using UnityEngine.Rendering;
using UnityEngine.Rendering.Universal;
namespace UnityEngine.Perception.GroundTruth
{
class LensDistortionUrpPass : ScriptableRenderPass
{
public LensDistortionCrossPipelinePass lensDistortionCrossPipelinePass;
public LensDistortionUrpPass(Camera camera, RenderTexture targetTexture)
{
lensDistortionCrossPipelinePass = new LensDistortionCrossPipelinePass(camera, targetTexture);
ConfigureTarget(targetTexture, targetTexture.depthBuffer);
lensDistortionCrossPipelinePass.Setup();
}
public override void Execute(ScriptableRenderContext context, ref RenderingData renderingData)
{
var commandBuffer = CommandBufferPool.Get(nameof(SemanticSegmentationUrpPass));
lensDistortionCrossPipelinePass.Execute(context, commandBuffer, renderingData.cameraData.camera, renderingData.cullResults);
CommandBufferPool.Release(commandBuffer);
}
public void Cleanup()
{
lensDistortionCrossPipelinePass.Cleanup();
}
}
}
#endif

3
com.unity.perception/Runtime/GroundTruth/RenderPasses/UrpPasses/LensDistortionUrpPass.cs.meta


fileFormatVersion: 2
guid: db2ddd6b833f412bb68447d5f85eff4d
timeCreated: 1615157082

31
com.unity.perception/Runtime/GroundTruth/RenderPasses/UrpPasses/SemanticSegmentationUrpPass.cs


#if URP_PRESENT
using UnityEngine.Rendering;
using UnityEngine.Rendering.Universal;
namespace UnityEngine.Perception.GroundTruth
{
class SemanticSegmentationUrpPass : ScriptableRenderPass
{
public SemanticSegmentationCrossPipelinePass m_SemanticSegmentationCrossPipelinePass;
public SemanticSegmentationUrpPass(Camera camera, RenderTexture targetTexture, SemanticSegmentationLabelConfig labelConfig)
{
m_SemanticSegmentationCrossPipelinePass = new SemanticSegmentationCrossPipelinePass(camera, labelConfig);
ConfigureTarget(targetTexture, targetTexture.depthBuffer);
m_SemanticSegmentationCrossPipelinePass.Setup();
}
public override void Execute(ScriptableRenderContext context, ref RenderingData renderingData)
{
var commandBuffer = CommandBufferPool.Get(nameof(SemanticSegmentationUrpPass));
m_SemanticSegmentationCrossPipelinePass.Execute(context, commandBuffer, renderingData.cameraData.camera, renderingData.cullResults);
CommandBufferPool.Release(commandBuffer);
}
public void Cleanup()
{
m_SemanticSegmentationCrossPipelinePass.Cleanup();
}
}
}
#endif

3
com.unity.perception/Runtime/GroundTruth/RenderPasses/UrpPasses/SemanticSegmentationUrpPass.cs.meta


fileFormatVersion: 2
guid: a16b614c904345fa8666f31f18a703af
timeCreated: 1615157147

15
com.unity.perception/Runtime/GroundTruth/GroundTruthInfo.cs


using Unity.Entities;
namespace UnityEngine.Perception.GroundTruth
{
/// <summary>
/// Information regarding a Labeling instance. Generated by <see cref="GroundTruthLabelSetupSystem"/>
/// </summary>
public struct GroundTruthInfo : IComponentData
{
/// <summary>
/// The instanceId assigned to the <see cref="Labeling"/>
/// </summary>
public uint instanceId;
}
}

11
com.unity.perception/Runtime/GroundTruth/GroundTruthInfo.cs.meta


fileFormatVersion: 2
guid: 013782ed5fce31d46b9849bbb3cc3da8
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

11
com.unity.perception/Runtime/GroundTruth/GroundTruthLabelSetupSystem.cs.meta


fileFormatVersion: 2
guid: 67ff3a1956c5bb14487beccf559cdd49
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

11
com.unity.perception/Runtime/GroundTruth/GroundTruthPass.cs.meta


fileFormatVersion: 2
guid: 0d7678390ae7b4844ad760a9e39dfd7d
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

11
com.unity.perception/Runtime/GroundTruth/InstanceSegmentationCrossPipelinePass.cs.meta


fileFormatVersion: 2
guid: b575537c5b92d6949bb233707280a614
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

13
com.unity.perception/Runtime/GroundTruth/SimulationManagementComponentSystem.cs


using System;
using Unity.Entities;
namespace UnityEngine.Perception.GroundTruth
{
class SimulationManagementComponentSystem : ComponentSystem
{
protected override void OnUpdate()
{
DatasetCapture.SimulationState?.Update();
}
}
}

11
com.unity.perception/Runtime/GroundTruth/SimulationManagementComponentSystem.cs.meta


fileFormatVersion: 2
guid: 8dfdda78cfa74c6991ac2d757d8e7019
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

107
com.unity.perception/Runtime/GroundTruth/GroundTruthCrossPipelinePass.cs


using System;
using Unity.Entities;
using UnityEngine;
using UnityEngine.Experimental.Rendering;
using UnityEngine.Rendering;
namespace UnityEngine.Perception.GroundTruth
{
internal abstract class GroundTruthCrossPipelinePass : IGroundTruthGenerator
{
public Camera targetCamera;
bool m_IsActivated;
protected GroundTruthCrossPipelinePass(Camera targetCamera)
{
this.targetCamera = targetCamera;
}
public virtual void Setup()
{
if (targetCamera == null)
throw new InvalidOperationException("targetCamera may not be null");
// If we are forced to activate here we will get zeroes in the first frame.
EnsureActivated();
}
public void Execute(ScriptableRenderContext renderContext, CommandBuffer cmd, Camera camera, CullingResults cullingResult)
{
// CustomPasses are executed for each camera. We only want to run for the target camera
if (camera != targetCamera)
return;
ExecutePass(renderContext, cmd, camera, cullingResult);
}
protected abstract void ExecutePass(ScriptableRenderContext renderContext, CommandBuffer cmd, Camera camera, CullingResults cullingResult);
public void EnsureActivated()
{
if (!m_IsActivated)
{
var labelSetupSystem = World.DefaultGameObjectInjectionWorld?.GetExistingSystem<GroundTruthLabelSetupSystem>();
labelSetupSystem?.Activate(this);
m_IsActivated = true;
}
}
public void Cleanup()
{
var labelSetupSystem = World.DefaultGameObjectInjectionWorld?.GetExistingSystem<GroundTruthLabelSetupSystem>();
labelSetupSystem?.Deactivate(this);
}
protected RendererListDesc CreateRendererListDesc(Camera camera, CullingResults cullingResult, string overrideMaterialPassName, int overrideMaterialPassIndex, Material overrideMaterial, LayerMask layerMask /*, PerObjectData perObjectData*/)
{
var shaderPasses = new[]
{
new ShaderTagId("Forward"), // HD Lit shader
new ShaderTagId("ForwardOnly"), // HD Unlit shader
new ShaderTagId("SRPDefaultUnlit"), // Cross SRP Unlit shader
new ShaderTagId("UniversalForward"), // URP Forward
new ShaderTagId("LightweightForward"), // LWRP Forward
new ShaderTagId(overrideMaterialPassName), // The override material shader
};
var stateBlock = new RenderStateBlock(0)
{
depthState = new DepthState(true, CompareFunction.LessEqual),
};
var result = new RendererListDesc(shaderPasses, cullingResult, camera)
{
rendererConfiguration = PerObjectData.None,
renderQueueRange = new RenderQueueRange { lowerBound = 0, upperBound = 5000 },
sortingCriteria = SortingCriteria.CommonOpaque,
excludeObjectMotionVectors = false,
overrideMaterial = overrideMaterial,
overrideMaterialPassIndex = overrideMaterialPassIndex,
stateBlock = stateBlock,
layerMask = layerMask,
};
return result;
}
public static void DrawRendererList(ScriptableRenderContext renderContext, CommandBuffer cmd, RendererList rendererList)
{
if (!rendererList.isValid)
throw new ArgumentException("Invalid renderer list provided to DrawRendererList");
// This is done here because DrawRenderers API lives outside command buffers so we need to make call this before doing any DrawRenders or things will be executed out of order
renderContext.ExecuteCommandBuffer(cmd);
cmd.Clear();
if (rendererList.stateBlock == null)
renderContext.DrawRenderers(rendererList.cullingResult, ref rendererList.drawSettings, ref rendererList.filteringSettings);
else
{
var renderStateBlock = rendererList.stateBlock.Value;
renderContext.DrawRenderers(rendererList.cullingResult, ref rendererList.drawSettings, ref rendererList.filteringSettings, ref renderStateBlock);
}
}
public abstract void SetupMaterialProperties(MaterialPropertyBlock mpb, Renderer meshRenderer, Labeling labeling, uint instanceId);
}
}

141
com.unity.perception/Runtime/GroundTruth/GroundTruthLabelSetupSystem.cs


using System.Collections.Generic;
using System.Threading;
using Unity.Entities;
namespace UnityEngine.Perception.GroundTruth
{
struct IdAssignmentParameters : IComponentData
{
public uint idStart;
public uint idStep;
}
/// <summary>
/// System which notifies the registered <see cref="IGroundTruthGenerator"/> about <see cref="Labeling"/> additions.
/// </summary>
public class GroundTruthLabelSetupSystem : ComponentSystem
{
List<IGroundTruthGenerator> m_ActiveGenerators = new List<IGroundTruthGenerator>();
ThreadLocal<MaterialPropertyBlock> m_MaterialPropertyBlocks = new ThreadLocal<MaterialPropertyBlock>();
int m_CurrentObjectIndex = -1;
/// <inheritdoc/>
protected override void OnCreate()
{
//These are here to inform the system runner the queries we are interested in. Without these calls, OnUpdate() might not be called
GetEntityQuery(ComponentType.Exclude<GroundTruthInfo>(), ComponentType.ReadOnly<Labeling>());
GetEntityQuery(ComponentType.ReadOnly<GroundTruthInfo>(), ComponentType.ReadOnly<Labeling>());
}
/// <inheritdoc/>
protected override void OnUpdate()
{
var entityQuery = Entities.WithAll<IdAssignmentParameters>().ToEntityQuery();
IdAssignmentParameters idAssignmentParameters;
if (entityQuery.CalculateEntityCount() == 1)
idAssignmentParameters = entityQuery.GetSingleton<IdAssignmentParameters>();
else
idAssignmentParameters = new IdAssignmentParameters {idStart = 1, idStep = 1};
var entityCount = Entities.WithAll<Labeling, GroundTruthInfo>().ToEntityQuery().CalculateEntityCount();
if (entityCount == 0)
m_CurrentObjectIndex = -1;
Entities.WithNone<GroundTruthInfo>().ForEach((Entity e, Labeling labeling) =>
{
var objectIndex = (uint)Interlocked.Increment(ref m_CurrentObjectIndex);
var instanceId = idAssignmentParameters.idStart + objectIndex * idAssignmentParameters.idStep;
var gameObject = labeling.gameObject;
if (!m_MaterialPropertyBlocks.IsValueCreated)
m_MaterialPropertyBlocks.Value = new MaterialPropertyBlock();
InitGameObjectRecursive(gameObject, m_MaterialPropertyBlocks.Value, labeling, instanceId);
EntityManager.AddComponentData(e, new GroundTruthInfo
{
instanceId = instanceId
});
labeling.SetInstanceId(instanceId);
});
}
void InitGameObjectRecursive(GameObject gameObject, MaterialPropertyBlock mpb, Labeling labeling, uint instanceId)
{
var terrain = gameObject.GetComponent<Terrain>();
if (terrain != null)
{
terrain.GetSplatMaterialPropertyBlock(mpb);
foreach (var pass in m_ActiveGenerators)
pass.SetupMaterialProperties(mpb, null, labeling, instanceId);
terrain.SetSplatMaterialPropertyBlock(mpb);
}
var renderer = (Renderer)gameObject.GetComponent<MeshRenderer>();
if (renderer == null)
renderer = gameObject.GetComponent<SkinnedMeshRenderer>();
if (renderer != null)
{
renderer.GetPropertyBlock(mpb);
foreach (var pass in m_ActiveGenerators)
pass.SetupMaterialProperties(mpb, renderer, labeling, instanceId);
renderer.SetPropertyBlock(mpb);
var materialCount = renderer.materials.Length;
for (int i = 0; i < materialCount; i++)
{
renderer.GetPropertyBlock(mpb, i);
//Only apply to individual materials if there is already a MaterialPropertyBlock on it
if (!mpb.isEmpty)
{
foreach (var pass in m_ActiveGenerators)
pass.SetupMaterialProperties(mpb, renderer, labeling, instanceId);
renderer.SetPropertyBlock(mpb, i);
}
}
}
for (var i = 0; i < gameObject.transform.childCount; i++)
{
var child = gameObject.transform.GetChild(i).gameObject;
if (child.GetComponent<Labeling>() != null)
continue;
InitGameObjectRecursive(child, mpb, labeling, instanceId);
}
}
/// <summary>
/// Activates the given <see cref="IGroundTruthGenerator"/>. <see cref="IGroundTruthGenerator.SetupMaterialProperties"/>
/// will be called for all <see cref="MeshRenderer"/> instances under each object containing a <see cref="Labeling"/> component.
/// </summary>
/// <param name="generator">The generator to register</param>
public void Activate(IGroundTruthGenerator generator)
{
m_ActiveGenerators.Add(generator);
Entities.ForEach((Labeling labeling, ref GroundTruthInfo info) =>
{
var gameObject = labeling.gameObject;
InitGameObjectRecursive(gameObject, m_MaterialPropertyBlocks.Value, labeling, info.instanceId);
});
}
/// <summary>
/// Deactivates the given <see cref="IGroundTruthGenerator"/>. It will no longer receive calls when <see cref="Labeling"/> instances are created.
/// </summary>
/// <param name="generator">The generator to deactivate</param>
/// <returns>True if the <see cref="generator"/> was successfully removed. False if <see cref="generator"/> was not active.</returns>
public bool Deactivate(IGroundTruthGenerator generator)
{
return m_ActiveGenerators.Remove(generator);
}
internal void RefreshLabeling(Entity labelingEntity)
{
EntityManager.RemoveComponent<GroundTruthInfo>(labelingEntity);
}
}
}

78
com.unity.perception/Runtime/GroundTruth/InstanceSegmentationCrossPipelinePass.cs


using System;
using System.Collections.Generic;
using Unity.Profiling;
using UnityEngine;
using UnityEngine.Experimental.Rendering;
using UnityEngine.Rendering;
namespace UnityEngine.Perception.GroundTruth
{
class InstanceSegmentationCrossPipelinePass : GroundTruthCrossPipelinePass
{
static readonly int k_SegmentationIdProperty = Shader.PropertyToID("_SegmentationId");
const string k_SegmentationPassShaderName = "Perception/InstanceSegmentation";
static ProfilerMarker s_ExecuteMarker = new ProfilerMarker("SegmentationPass_Execute");
/// <summary>
/// The LayerMask to apply when rendering objects.
/// </summary>
public LayerMask layerMask = -1;
Shader m_SegmentationShader;
Material m_OverrideMaterial;
/// <summary>
/// Create a new <see cref="InstanceSegmentationCrossPipelinePass"/> referencing the given
/// </summary>
/// <param name="targetCamera"></param>
/// <exception cref="ArgumentNullException"></exception>
public InstanceSegmentationCrossPipelinePass(Camera targetCamera)
: base(targetCamera)
{
if (targetCamera == null)
throw new ArgumentNullException(nameof(targetCamera));
//Activating in the constructor allows us to get correct labeling in the first frame.
EnsureActivated();
}
public override void Setup()
{
base.Setup();
m_SegmentationShader = Shader.Find(k_SegmentationPassShaderName);
var shaderVariantCollection = new ShaderVariantCollection();
shaderVariantCollection.Add(new ShaderVariantCollection.ShaderVariant(m_SegmentationShader, PassType.ScriptableRenderPipeline));
shaderVariantCollection.WarmUp();
m_OverrideMaterial = new Material(m_SegmentationShader);
}
//Render all objects to our target RenderTexture using `overrideMaterial` to use our shader
protected override void ExecutePass(ScriptableRenderContext renderContext, CommandBuffer cmd, Camera camera, CullingResults cullingResult)
{
using (s_ExecuteMarker.Auto())
{
cmd.ClearRenderTarget(true, true, Color.black);
var result = CreateRendererListDesc(camera, cullingResult, "FirstPass", 0, m_OverrideMaterial, layerMask);
DrawRendererList(renderContext, cmd, RendererList.Create(result));
}
}
public override void SetupMaterialProperties(MaterialPropertyBlock mpb, Renderer renderer, Labeling labeling, uint instanceId)
{
var found = InstanceIdToColorMapping.TryGetColorFromInstanceId(instanceId, out var color);
if (!found)
{
Debug.LogError($"Could not get a unique color for {instanceId}");
}
mpb.SetVector(k_SegmentationIdProperty, (Color)color);
#if PERCEPTION_DEBUG
Debug.Log($"Assigning id. Frame {Time.frameCount} id {id}");
#endif
}
}
}

79
com.unity.perception/Runtime/GroundTruth/SemanticSegmentationCrossPipelinePass.cs


using System;
using UnityEngine.Experimental.Rendering;
using UnityEngine.Rendering;
namespace UnityEngine.Perception.GroundTruth
{
/// <summary>
/// Custom Pass which renders labeled images where each object labeled with a Labeling component is drawn with the
/// value specified by the given LabelingConfiguration.
/// </summary>
class SemanticSegmentationCrossPipelinePass : GroundTruthCrossPipelinePass
{
const string k_ShaderName = "Perception/SemanticSegmentation";
static readonly int k_LabelingId = Shader.PropertyToID("LabelingId");
static int s_LastFrameExecuted = -1;
SemanticSegmentationLabelConfig m_LabelConfig;
//Serialize the shader so that the shader asset is included in player builds when the SemanticSegmentationPass is used.
//Currently commented out and shaders moved to Resources folder due to serialization crashes when it is enabled.
//See https://fogbugz.unity3d.com/f/cases/1187378/
//[SerializeField]
Shader m_ClassLabelingShader;
Material m_OverrideMaterial;
public SemanticSegmentationCrossPipelinePass(Camera targetCamera, SemanticSegmentationLabelConfig labelConfig) : base(targetCamera)
{
this.m_LabelConfig = labelConfig;
}
public override void Setup()
{
base.Setup();
m_ClassLabelingShader = Shader.Find(k_ShaderName);
var shaderVariantCollection = new ShaderVariantCollection();
if (shaderVariantCollection != null)
{
shaderVariantCollection.Add(new ShaderVariantCollection.ShaderVariant(m_ClassLabelingShader, PassType.ScriptableRenderPipeline));
}
m_OverrideMaterial = new Material(m_ClassLabelingShader);
shaderVariantCollection.WarmUp();
}
protected override void ExecutePass(ScriptableRenderContext renderContext, CommandBuffer cmd, Camera camera, CullingResults cullingResult)
{
if (s_LastFrameExecuted == Time.frameCount)
return;
s_LastFrameExecuted = Time.frameCount;
var renderList = CreateRendererListDesc(camera, cullingResult, "FirstPass", 0, m_OverrideMaterial, -1);
cmd.ClearRenderTarget(true, true, Color.black);
DrawRendererList(renderContext, cmd, RendererList.Create(renderList));
}
public override void SetupMaterialProperties(MaterialPropertyBlock mpb, Renderer renderer, Labeling labeling, uint instanceId)
{
var entry = new SemanticSegmentationLabelEntry();
bool found = false;
foreach (var l in m_LabelConfig.labelEntries)
{
if (labeling.labels.Contains(l.label))
{
entry = l;
found = true;
break;
}
}
//Set the labeling ID so that it can be accessed in ClassSemanticSegmentationPass.shader
if (found)
mpb.SetVector(k_LabelingId, entry.color);
}
}
}

200
com.unity.perception/Runtime/GroundTruth/LensDistortionCrossPipelinePass.cs


using System;
using Unity.Mathematics;
using UnityEngine.Experimental.Rendering;
using UnityEngine.Rendering;
#if HDRP_PRESENT
using UnityEngine.Rendering.HighDefinition;
#elif URP_PRESENT
using UnityEngine.Rendering.Universal;
#endif
#if HDRP_PRESENT || URP_PRESENT
namespace UnityEngine.Perception.GroundTruth
{
/// <summary>
/// Custom Pass which will apply a lens distortion (per the respective volume override in URP or HDRP, or
/// through a custom override directly through the pass) to an incoming mask / texture. The purpose of this
/// is to allow the same lens distortion being applied to the RGB image ine the perception camera to be applied
/// to the respective ground truths generated.
/// </summary>
internal class LensDistortionCrossPipelinePass : GroundTruthCrossPipelinePass
{
const string k_ShaderName = "Perception/LensDistortion";
//Serialize the shader so that the shader asset is included in player builds when the SemanticSegmentationPass is used.
//Currently commented out and shaders moved to Resources folder due to serialization crashes when it is enabled.
//See https://fogbugz.unity3d.com/f/cases/1187378/
//[SerializeField]
// Lens Distortion Shader
Shader m_LensDistortionShader;
Material m_LensDistortionMaterial;
bool m_fInitialized = false;
public static readonly int _Distortion_Params1 = Shader.PropertyToID("_Distortion_Params1");
public static readonly int _Distortion_Params2 = Shader.PropertyToID("_Distortion_Params2");
LensDistortion m_lensDistortion;
internal float? lensDistortionOverride = null; // Largely for testing, but could be useful otherwise
RenderTexture m_TargetTexture;
RenderTexture m_distortedTexture;
//private LensDistortion m_LensDistortion;
public LensDistortionCrossPipelinePass(Camera targetCamera, RenderTexture targetTexture)
: base(targetCamera)
{
m_TargetTexture = targetTexture;
}
public override void Setup()
{
base.Setup();
m_LensDistortionShader = Shader.Find(k_ShaderName);
var shaderVariantCollection = new ShaderVariantCollection();
if (shaderVariantCollection != null)
shaderVariantCollection.Add(new ShaderVariantCollection.ShaderVariant(m_LensDistortionShader, PassType.ScriptableRenderPipeline));
m_LensDistortionMaterial = new Material(m_LensDistortionShader);
if(shaderVariantCollection != null)
shaderVariantCollection.WarmUp();
// Set up a new texture
if (m_distortedTexture == null || m_distortedTexture.width != Screen.width || m_distortedTexture.height != Screen.height) {
if (m_distortedTexture != null)
m_distortedTexture.Release();
m_distortedTexture = new RenderTexture(Screen.width, Screen.height, 0, RenderTextureFormat.ARGBFloat, RenderTextureReadWrite.Linear);
m_distortedTexture.enableRandomWrite = true;
m_distortedTexture.filterMode = FilterMode.Point;
m_distortedTexture.Create();
}
// Grab the lens distortion
#if HDRP_PRESENT
// Grab the Lens Distortion from Perception Camera stack
var hdCamera = HDCamera.GetOrCreate(targetCamera);
var stack = hdCamera.volumeStack;
m_lensDistortion = stack.GetComponent<LensDistortion>();
#elif URP_PRESENT
var stack = VolumeManager.instance.stack;
m_lensDistortion = stack.GetComponent<LensDistortion>();
#endif
m_fInitialized = true;
}
protected override void ExecutePass(ScriptableRenderContext renderContext, CommandBuffer cmd, Camera camera, CullingResults cullingResult)
{
if (m_fInitialized == false)
return;
if (SetLensDistortionShaderParameters() == false)
return;
// Blitmayhem
cmd.Blit(m_TargetTexture, m_distortedTexture, m_LensDistortionMaterial);
cmd.Blit(m_distortedTexture, m_TargetTexture);
renderContext.ExecuteCommandBuffer(cmd);
cmd.Clear();
}
public bool SetLensDistortionShaderParameters()
{
if (m_fInitialized == false)
return false;
// This code is lifted from the SetupLensDistortion() function in
// https://github.com/Unity-Technologies/Graphics/blob/257b08bba6c11de0f894e42e811124247a522d3c/com.unity.render-pipelines.universal/Runtime/Passes/PostProcessPass.cs
// This is in UnityEngine.Rendering.Universal.Internal.PostProcessPass::SetupLensDistortion so it's
// unclear how to re-use this code
float intensity = 0.5f;
float scale = 1.0f;
var center = new Vector2(0.0f, 0.0f);
var mult = new Vector2(1.0f, 1.0f);
#if HDRP_PRESENT
if(m_lensDistortion == null)
return false;
#elif URP_PRESENT
if(targetCamera == null)
return false;
var UACD = targetCamera.GetUniversalAdditionalCameraData();
if(UACD.renderPostProcessing == false && lensDistortionOverride.HasValue == false)
return false;
if (m_lensDistortion.active == false)
return false;
#else
return false;
#endif
if (lensDistortionOverride.HasValue)
{
intensity = lensDistortionOverride.Value;
}
else if (m_lensDistortion != null)
{
// This is a bit finicky for URP - since Lens Distortion comes off the VolumeManager stack as active
// even if post processing is not enabled. An intensity of 0.0f is untenable, so the below checks
// ensures post processing hasn't been enabled but Lens Distortion actually overriden
if (m_lensDistortion.intensity.value != 0.0f)
{
intensity = m_lensDistortion.intensity.value;
center = m_lensDistortion.center.value * 2f - Vector2.one;
mult.x = Mathf.Max(m_lensDistortion.xMultiplier.value, 1e-4f);
mult.y = Mathf.Max(m_lensDistortion.yMultiplier.value, 1e-4f);
scale = 1.0f / m_lensDistortion.scale.value;
}
else
{
return false;
}
}
float amount = 1.6f * Mathf.Max(Mathf.Abs(intensity * 100.0f), 1.0f);
float theta = Mathf.Deg2Rad * Mathf.Min(160f, amount);
float sigma = 2.0f * Mathf.Tan(theta * 0.5f);
var p1 = new Vector4(
center.x,
center.y,
mult.x,
mult.y
);
var p2 = new Vector4(
intensity >= 0f ? theta : 1f / theta,
sigma,
scale,
intensity * 100.0f
);
// Set Shader Constants
m_LensDistortionMaterial.SetVector(_Distortion_Params1, p1);
m_LensDistortionMaterial.SetVector(_Distortion_Params2, p2);
return true;
}
public override void SetupMaterialProperties(MaterialPropertyBlock mpb, Renderer renderer, Labeling labeling, uint instanceId)
{
SetLensDistortionShaderParameters();
}
}
}
#endif // ! HDRP_PRESENT || URP_PRESENT

3
com.unity.perception/Runtime/GroundTruth/LensDistortionCrossPipelinePass.cs.meta


fileFormatVersion: 2
guid: ad37b813e25c450fbc65b2820a7d9dc2
timeCreated: 1598041907

54
com.unity.perception/Runtime/GroundTruth/LensDistortionPass.cs


#if HDRP_PRESENT
using System;
using UnityEngine.Rendering;
using UnityEngine.Rendering.HighDefinition;
namespace UnityEngine.Perception.GroundTruth
{
/// <summary>
/// Custom Pass which renders labeled images where each object with a Labeling component is drawn with the value
/// specified by the given LabelingConfiguration.
/// </summary>
public class LensDistortionPass : CustomPass
{
public RenderTexture targetTexture;
public Camera targetCamera;
internal LensDistortionCrossPipelinePass m_LensDistortionCrossPipelinePass;
public LensDistortionPass(Camera targetCamera, RenderTexture targetTexture)
{
this.targetTexture = targetTexture;
this.targetCamera = targetCamera;
EnsureInit();
}
public void EnsureInit()
{
if (m_LensDistortionCrossPipelinePass == null)
{
m_LensDistortionCrossPipelinePass = new LensDistortionCrossPipelinePass(targetCamera, targetTexture);
m_LensDistortionCrossPipelinePass.EnsureActivated();
}
}
public LensDistortionPass()
{
//
}
protected override void Setup(ScriptableRenderContext renderContext, CommandBuffer cmd)
{
EnsureInit();
m_LensDistortionCrossPipelinePass.Setup();
}
protected override void Execute(ScriptableRenderContext renderContext, CommandBuffer cmd, HDCamera hdCamera, CullingResults cullingResult)
{
CoreUtils.SetRenderTarget(cmd, targetTexture);
m_LensDistortionCrossPipelinePass.Execute(renderContext, cmd, hdCamera.camera, cullingResult);
}
}
}
#endif

/com.unity.perception/Runtime/GroundTruth/GroundTruthCrossPipelinePass.cs.meta → /com.unity.perception/Runtime/GroundTruth/RenderPasses/CrossPipelinePasses/GroundTruthCrossPipelinePass.cs.meta

/com.unity.perception/Runtime/GroundTruth/InstanceSegmentationPass.cs → /com.unity.perception/Runtime/GroundTruth/RenderPasses/HdrpPasses/InstanceSegmentationPass.cs

/com.unity.perception/Runtime/GroundTruth/InstanceSegmentationPass.cs.meta → /com.unity.perception/Runtime/GroundTruth/RenderPasses/HdrpPasses/InstanceSegmentationPass.cs.meta

/com.unity.perception/Runtime/GroundTruth/SemanticSegmentationCrossPipelinePass.cs.meta → /com.unity.perception/Runtime/GroundTruth/RenderPasses/CrossPipelinePasses/SemanticSegmentationCrossPipelinePass.cs.meta

/com.unity.perception/Runtime/GroundTruth/SemanticSegmentationPass.cs → /com.unity.perception/Runtime/GroundTruth/RenderPasses/HdrpPasses/SemanticSegmentationPass.cs

/com.unity.perception/Runtime/GroundTruth/SemanticSegmentationPass.cs.meta → /com.unity.perception/Runtime/GroundTruth/RenderPasses/HdrpPasses/SemanticSegmentationPass.cs.meta

/com.unity.perception/Runtime/GroundTruth/GroundTruthPass.cs → /com.unity.perception/Runtime/GroundTruth/RenderPasses/HdrpPasses/GroundTruthPass.cs

/com.unity.perception/Runtime/GroundTruth/LensDistortionPass.cs.meta → /com.unity.perception/Runtime/GroundTruth/RenderPasses/HdrpPasses/LensDistortionPass.cs.meta

正在加载...
取消
保存