浏览代码

moved custom passes and cleaned up code

/main
sleal-unity 4 年前
当前提交
515a1a83
共有 34 个文件被更改,包括 357 次插入318 次删除
  1. 3
      com.unity.perception/Editor/GroundTruth/InstanceSegmentationPassEditor.cs
  2. 2
      com.unity.perception/Runtime/GroundTruth/IGroundTruthGenerator.cs
  3. 57
      com.unity.perception/Runtime/GroundTruth/LabeledObjectsManager.cs
  4. 2
      com.unity.perception/Runtime/GroundTruth/PerceptionUpdater.cs
  5. 11
      com.unity.perception/Runtime/Randomization/Scenarios/ScenarioBase.cs
  6. 2
      com.unity.perception/Runtime/GroundTruth/CustomPasses/GroundTruthCrossPipelinePass.cs.meta
  7. 2
      com.unity.perception/Runtime/GroundTruth/CustomPasses/SemanticSegmentationCrossPipelinePass.cs.meta
  8. 2
      com.unity.perception/Runtime/GroundTruth/CustomPasses/SemanticSegmentationPass.cs.meta
  9. 6
      com.unity.perception/Runtime/GroundTruth/CustomPasses/LensDistortionCrossPipelinePass.cs
  10. 5
      com.unity.perception/Runtime/GroundTruth/CustomPasses/LensDistortionPass.cs
  11. 10
      com.unity.perception/Runtime/GroundTruth/CustomPasses/GroundTruthPass.cs
  12. 3
      com.unity.perception/Runtime/GroundTruth/CustomPasses.meta
  13. 120
      com.unity.perception/Runtime/GroundTruth/CustomPasses/GroundTruthCrossPipelinePass.cs
  14. 3
      com.unity.perception/Runtime/GroundTruth/CustomPasses/GroundTruthPass.cs.meta
  15. 78
      com.unity.perception/Runtime/GroundTruth/CustomPasses/InstanceSegmentationCrossPipelinePass.cs
  16. 3
      com.unity.perception/Runtime/GroundTruth/CustomPasses/InstanceSegmentationCrossPipelinePass.cs.meta
  17. 83
      com.unity.perception/Runtime/GroundTruth/CustomPasses/SemanticSegmentationCrossPipelinePass.cs
  18. 11
      com.unity.perception/Runtime/GroundTruth/GroundTruthPass.cs.meta
  19. 11
      com.unity.perception/Runtime/GroundTruth/InstanceSegmentationCrossPipelinePass.cs.meta
  20. 78
      com.unity.perception/Runtime/GroundTruth/InstanceSegmentationCrossPipelinePass.cs
  21. 79
      com.unity.perception/Runtime/GroundTruth/SemanticSegmentationCrossPipelinePass.cs
  22. 104
      com.unity.perception/Runtime/GroundTruth/GroundTruthCrossPipelinePass.cs
  23. 0
      /com.unity.perception/Runtime/GroundTruth/CustomPasses/GroundTruthCrossPipelinePass.cs.meta
  24. 0
      /com.unity.perception/Runtime/GroundTruth/CustomPasses/InstanceSegmentationPass.cs
  25. 0
      /com.unity.perception/Runtime/GroundTruth/CustomPasses/InstanceSegmentationPass.cs.meta
  26. 0
      /com.unity.perception/Runtime/GroundTruth/CustomPasses/SemanticSegmentationCrossPipelinePass.cs.meta
  27. 0
      /com.unity.perception/Runtime/GroundTruth/CustomPasses/SemanticSegmentationPass.cs
  28. 0
      /com.unity.perception/Runtime/GroundTruth/CustomPasses/SemanticSegmentationPass.cs.meta
  29. 0
      /com.unity.perception/Runtime/GroundTruth/CustomPasses/LensDistortionCrossPipelinePass.cs
  30. 0
      /com.unity.perception/Runtime/GroundTruth/CustomPasses/LensDistortionCrossPipelinePass.cs.meta
  31. 0
      /com.unity.perception/Runtime/GroundTruth/CustomPasses/LensDistortionPass.cs
  32. 0
      /com.unity.perception/Runtime/GroundTruth/CustomPasses/LensDistortionPass.cs.meta
  33. 0
      /com.unity.perception/Runtime/GroundTruth/CustomPasses/GroundTruthPass.cs

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);

2
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="GroundTruthLabelSetupSystem"/>.
/// </summary>
public interface IGroundTruthGenerator
{

57
com.unity.perception/Runtime/GroundTruth/LabeledObjectsManager.cs


namespace UnityEngine.Perception.GroundTruth
{
class LabeledObjectsManager
/// <summary>
///
/// </summary>
public class LabeledObjectsManager
/// <summary>
/// Returns the active LabeledObjectsManager instance
/// </summary>
LinkedHashSet<Labeling> m_UnregisteredLabels = new LinkedHashSet<Labeling>();
LinkedHashSet<Labeling> m_LabelsPendingRegistration = new LinkedHashSet<Labeling>();
/// <summary>
/// Returns the set of registered Labeling components
/// </summary>
public void Update()
/// <summary>
/// Registers all pending labels. Called once per frame during LateUpdate by the PerceptionUpdater.
/// </summary>
public void RegisterPendingLabels()
foreach (var unregisteredLabel in m_UnregisteredLabels)
foreach (var unregisteredLabel in m_LabelsPendingRegistration)
InitGameObjectRecursive(
RecursivelyInitializeGameObjects(
unregisteredLabel.gameObject,
new MaterialPropertyBlock(),
unregisteredLabel,

m_RegisteredLabels.Add(unregisteredLabel);
}
m_UnregisteredLabels.Clear();
m_LabelsPendingRegistration.Clear();
/// 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.
/// 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)

InitGameObjectRecursive(label.gameObject, new MaterialPropertyBlock(), label, label.instanceId);
RecursivelyInitializeGameObjects(label.gameObject, new MaterialPropertyBlock(), label, label.instanceId);
/// Deactivates the given <see cref="IGroundTruthGenerator"/>. It will no longer receive calls when <see cref="Labeling"/> instances are created.
/// Deactivates the given <see cref="IGroundTruthGenerator"/>.
/// It will no longer receive calls when <see cref="Labeling"/> instances are created.
/// <returns>True if the <see cref="generator"/> was successfully removed. False if <see cref="generator"/> was not active.</returns>
/// <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);

/// <param name="labeledObject">the component to register</param>
public void Register(Labeling labeledObject)
{
m_UnregisteredLabels.Add(labeledObject);
m_LabelsPendingRegistration.Add(labeledObject);
}
/// <summary>

public void Unregister(Labeling labeledObject)
{
m_UnregisteredLabels.Remove(labeledObject);
m_LabelsPendingRegistration.Remove(labeledObject);
m_RegisteredLabels.Remove(labeledObject);
}

public void RefreshLabeling(Labeling labeledObject)
{
m_RegisteredLabels.Remove(labeledObject);
m_UnregisteredLabels.Add(labeledObject);
m_LabelsPendingRegistration.Add(labeledObject);
void InitGameObjectRecursive(
/// <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 (child.GetComponent<Labeling>() != null)
continue;
InitGameObjectRecursive(child, mpb, labeling, instanceId);
RecursivelyInitializeGameObjects(child, mpb, labeling, instanceId);
}
}
}

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


void LateUpdate()
{
LabeledObjectsManager.singleton.Update();
LabeledObjectsManager.singleton.RegisterPendingLabels();
DatasetCapture.SimulationState?.Update();
}
}

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
}
}

2
com.unity.perception/Runtime/GroundTruth/CustomPasses/GroundTruthCrossPipelinePass.cs.meta


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

2
com.unity.perception/Runtime/GroundTruth/CustomPasses/SemanticSegmentationCrossPipelinePass.cs.meta


fileFormatVersion: 2
guid: a6740ca70f44ece45901dc26511f9aa0
guid: 8a3f9f9754caf9c41a31f452c1426a2a
MonoImporter:
externalObjects: {}
serializedVersion: 2

2
com.unity.perception/Runtime/GroundTruth/CustomPasses/SemanticSegmentationPass.cs.meta


fileFormatVersion: 2
guid: 6d4bfcc857cac4d4ab70b19c87508947
guid: 892ef4062d56ed24e95ef548b42be343
MonoImporter:
externalObjects: {}
serializedVersion: 2

6
com.unity.perception/Runtime/GroundTruth/CustomPasses/LensDistortionCrossPipelinePass.cs


/// <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
/// is to allow the same lens distortion being applied to the RGB image in the perception camera to be applied
internal class LensDistortionCrossPipelinePass : GroundTruthCrossPipelinePass
class LensDistortionCrossPipelinePass : GroundTruthCrossPipelinePass
{
const string k_ShaderName = "Perception/LensDistortion";

if (SetLensDistortionShaderParameters() == false)
return;
// Blitmayhem
// Blit mayhem
cmd.Blit(m_TargetTexture, m_distortedTexture, m_LensDistortionMaterial);
cmd.Blit(m_distortedTexture, m_TargetTexture);

5
com.unity.perception/Runtime/GroundTruth/CustomPasses/LensDistortionPass.cs


}
}
public LensDistortionPass()
{
//
}
protected override void Setup(ScriptableRenderContext renderContext, CommandBuffer cmd)
{
EnsureInit();

10
com.unity.perception/Runtime/GroundTruth/CustomPasses/GroundTruthPass.cs


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

public Camera targetCamera;
bool m_IsActivated;
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)
{

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);
protected void EnsureActivated()
{

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


fileFormatVersion: 2
guid: 36c50bc1fa70491eaa8ffd85c55187d0
timeCreated: 1615074717

120
com.unity.perception/Runtime/GroundTruth/CustomPasses/GroundTruthCrossPipelinePass.cs


using System;
using UnityEngine;
using UnityEngine.Experimental.Rendering;
using UnityEngine.Rendering;
namespace UnityEngine.Perception.GroundTruth
{
public 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)
{
LabeledObjectsManager.singleton.Activate(this);
m_IsActivated = true;
}
}
public void Cleanup()
{
LabeledObjectsManager.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);
}
}

3
com.unity.perception/Runtime/GroundTruth/CustomPasses/GroundTruthPass.cs.meta


fileFormatVersion: 2
guid: eb908f4c6443439287819c85e91cfea0
timeCreated: 1615084059

78
com.unity.perception/Runtime/GroundTruth/CustomPasses/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
}
}
}

3
com.unity.perception/Runtime/GroundTruth/CustomPasses/InstanceSegmentationCrossPipelinePass.cs.meta


fileFormatVersion: 2
guid: 421bd5b3d054425d89dfc7c37e8b7575
timeCreated: 1615074762

83
com.unity.perception/Runtime/GroundTruth/CustomPasses/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);
}
}
}

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:

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);
}
}
}

104
com.unity.perception/Runtime/GroundTruth/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)
{
LabeledObjectsManager.singleton.Activate(this);
m_IsActivated = true;
}
}
public void Cleanup()
{
LabeledObjectsManager.singleton.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);
}
}

/com.unity.perception/Runtime/GroundTruth/GroundTruthCrossPipelinePass.cs.meta → /com.unity.perception/Runtime/GroundTruth/CustomPasses/GroundTruthCrossPipelinePass.cs.meta

/com.unity.perception/Runtime/GroundTruth/InstanceSegmentationPass.cs → /com.unity.perception/Runtime/GroundTruth/CustomPasses/InstanceSegmentationPass.cs

/com.unity.perception/Runtime/GroundTruth/InstanceSegmentationPass.cs.meta → /com.unity.perception/Runtime/GroundTruth/CustomPasses/InstanceSegmentationPass.cs.meta

/com.unity.perception/Runtime/GroundTruth/SemanticSegmentationCrossPipelinePass.cs.meta → /com.unity.perception/Runtime/GroundTruth/CustomPasses/SemanticSegmentationCrossPipelinePass.cs.meta

/com.unity.perception/Runtime/GroundTruth/SemanticSegmentationPass.cs → /com.unity.perception/Runtime/GroundTruth/CustomPasses/SemanticSegmentationPass.cs

/com.unity.perception/Runtime/GroundTruth/SemanticSegmentationPass.cs.meta → /com.unity.perception/Runtime/GroundTruth/CustomPasses/SemanticSegmentationPass.cs.meta

/com.unity.perception/Runtime/GroundTruth/LensDistortionCrossPipelinePass.cs → /com.unity.perception/Runtime/GroundTruth/CustomPasses/LensDistortionCrossPipelinePass.cs

/com.unity.perception/Runtime/GroundTruth/LensDistortionCrossPipelinePass.cs.meta → /com.unity.perception/Runtime/GroundTruth/CustomPasses/LensDistortionCrossPipelinePass.cs.meta

/com.unity.perception/Runtime/GroundTruth/LensDistortionPass.cs → /com.unity.perception/Runtime/GroundTruth/CustomPasses/LensDistortionPass.cs

/com.unity.perception/Runtime/GroundTruth/LensDistortionPass.cs.meta → /com.unity.perception/Runtime/GroundTruth/CustomPasses/LensDistortionPass.cs.meta

/com.unity.perception/Runtime/GroundTruth/GroundTruthPass.cs → /com.unity.perception/Runtime/GroundTruth/CustomPasses/GroundTruthPass.cs

正在加载...
取消
保存