浏览代码

Non-working partial implementation. Needs merging the individual passes into one uber pass on HDRP I believe

/multipleCameras
Jon Hogins 4 年前
当前提交
b178cad2
共有 16 个文件被更改,包括 115 次插入204 次删除
  1. 13
      com.unity.perception/Runtime/GroundTruth/GroundTruthCrossPipelinePass.cs
  2. 54
      com.unity.perception/Runtime/GroundTruth/GroundTruthPass.cs
  3. 8
      com.unity.perception/Runtime/GroundTruth/InstanceSegmentationCrossPipelinePass.cs
  4. 16
      com.unity.perception/Runtime/GroundTruth/InstanceSegmentationPass.cs
  5. 6
      com.unity.perception/Runtime/GroundTruth/Labelers/InstanceSegmentationLabeler.cs
  6. 24
      com.unity.perception/Runtime/GroundTruth/Labelers/SemanticSegmentationLabeler.cs
  7. 57
      com.unity.perception/Runtime/GroundTruth/LensDistortionCrossPipelinePass.cs
  8. 7
      com.unity.perception/Runtime/GroundTruth/LensDistortionPass.cs
  9. 49
      com.unity.perception/Runtime/GroundTruth/PerceptionCamera_InstanceSegmentation.cs
  10. 2
      com.unity.perception/Runtime/GroundTruth/SemanticSegmentationCrossPipelinePass.cs
  11. 18
      com.unity.perception/Runtime/GroundTruth/SemanticSegmentationPass.cs
  12. 2
      com.unity.perception/Tests/Editor/PerceptionCameraEditorTests.cs
  13. 20
      com.unity.perception/Editor/GroundTruth/InstanceSegmentationPassEditor.cs
  14. 11
      com.unity.perception/Editor/GroundTruth/InstanceSegmentationPassEditor.cs.meta
  15. 11
      com.unity.perception/Editor/GroundTruth/SemanticSegmentationPassEditor.cs.meta
  16. 21
      com.unity.perception/Editor/GroundTruth/SemanticSegmentationPassEditor.cs

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


{
internal abstract class GroundTruthCrossPipelinePass : IGroundTruthGenerator
{
public Camera targetCamera;
protected GroundTruthCrossPipelinePass(Camera targetCamera)
protected GroundTruthCrossPipelinePass()
this.targetCamera = targetCamera;
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.
// CustomPasses are executed for each camera. We only want to run for the target camera
if (camera != targetCamera)
return;
ExecutePass(renderContext, cmd, camera, cullingResult);
}

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


#if HDRP_PRESENT
using System;
using System.Collections.Generic;
using Unity.Entities;
using UnityEngine.Rendering;
using UnityEngine.Rendering.HighDefinition;

public abstract class GroundTruthPass : CustomPass, IGroundTruthGenerator
public abstract class GroundTruthPass : CustomPass
public Camera targetCamera;
private GroundTruthCrossPipelinePass m_GroundTruthCrossPipelinePass;
Dictionary<Camera, RenderTexture> targets = new Dictionary<Camera, RenderTexture>();
bool m_IsActivated;
public abstract void SetupMaterialProperties(MaterialPropertyBlock mpb, Renderer meshRenderer, Labeling labeling, uint instanceId);
protected GroundTruthPass()
{
}
protected GroundTruthPass(Camera targetCamera)
internal void Init(GroundTruthCrossPipelinePass groundTruthCrossPipelinePass)
this.targetCamera = targetCamera;
m_GroundTruthCrossPipelinePass = groundTruthCrossPipelinePass;
m_GroundTruthCrossPipelinePass.Setup();
protected override void Setup(ScriptableRenderContext renderContext, CommandBuffer cmd)
public void AddTarget(Camera camera, RenderTexture renderTexture)
if (targetCamera == null)
throw new InvalidOperationException("targetCamera may not be null");
if (targets == null)
targets = new Dictionary<Camera, RenderTexture>();
// If we are forced to activate here we will get zeroes in the first frame.
EnsureActivated();
targets[camera] = renderTexture;
}
protected override void Setup(ScriptableRenderContext renderContext, CommandBuffer cmd)
{
this.targetColorBuffer = TargetBuffer.Custom;
this.targetDepthBuffer = TargetBuffer.Custom;
}

// CustomPasses are executed for each camera. We only want to run for the target camera
if (hdCamera.camera != targetCamera)
RenderTexture targetTexture = null;
if (targets != null && !targets.TryGetValue(hdCamera.camera, out targetTexture))
ExecutePass(renderContext, cmd, hdCamera, cullingResult);
}
protected abstract void ExecutePass(ScriptableRenderContext renderContext, CommandBuffer cmd, HDCamera hdCamera, CullingResults cullingResult);
protected void EnsureActivated()
{
if (!m_IsActivated)
{
var labelSetupSystem = World.DefaultGameObjectInjectionWorld?.GetExistingSystem<GroundTruthLabelSetupSystem>();
labelSetupSystem?.Activate(this);
m_IsActivated = true;
}
}
protected override void Cleanup()
{
var labelSetupSystem = World.DefaultGameObjectInjectionWorld?.GetExistingSystem<GroundTruthLabelSetupSystem>();
labelSetupSystem?.Deactivate(this);
CoreUtils.SetRenderTarget(cmd, targetTexture, ClearFlag.All);
m_GroundTruthCrossPipelinePass.Execute(renderContext, cmd, hdCamera.camera, cullingResult);
}
}
}

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


/// <summary>
/// Create a new <see cref="InstanceSegmentationCrossPipelinePass"/> referencing the given
/// </summary>
/// <param name="targetCamera"></param>
public InstanceSegmentationCrossPipelinePass(Camera targetCamera)
: base(targetCamera)
public InstanceSegmentationCrossPipelinePass()
: base()
if (targetCamera == null)
throw new ArgumentNullException(nameof(targetCamera));
//Activating in the constructor allows us to get correct labeling in the first frame.
EnsureActivated();
}

16
com.unity.perception/Runtime/GroundTruth/InstanceSegmentationPass.cs


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

/// A CustomPass for creating object instance segmentation images. GameObjects containing Labeling components
/// are assigned unique IDs, which are rendered into the target texture.
/// </summary>
public class InstanceSegmentationPass : CustomPass
public class InstanceSegmentationPass : GroundTruthPass
public RenderTexture targetTexture;
public Camera targetCamera;
protected override void Execute(ScriptableRenderContext renderContext, CommandBuffer cmd, HDCamera hdCamera, CullingResults cullingResult)
{
CoreUtils.SetRenderTarget(cmd, targetTexture, ClearFlag.All);
m_InstanceSegmentationCrossPipelinePass.Execute(renderContext, cmd, hdCamera.camera, cullingResult);
}
m_InstanceSegmentationCrossPipelinePass = new InstanceSegmentationCrossPipelinePass(targetCamera);
m_InstanceSegmentationCrossPipelinePass.Setup();
m_InstanceSegmentationCrossPipelinePass = new InstanceSegmentationCrossPipelinePass();
this.Init(m_InstanceSegmentationCrossPipelinePass);
}
}

6
com.unity.perception/Runtime/GroundTruth/Labelers/InstanceSegmentationLabeler.cs


/// <inheritdoc/>
protected override bool supportsVisualization => true;
static readonly string k_Directory = "InstanceSegmentation" + Guid.NewGuid().ToString();
readonly string m_Directory = "InstanceSegmentation" + Guid.NewGuid();
const string k_FilePrefix = "Instance_";
/// <summary>

{
m_CurrentTexture = renderTexture;
m_InstancePath = $"{k_Directory}/{k_FilePrefix}{frameCount}.png";
var localPath = $"{Manager.Instance.GetDirectoryFor(k_Directory)}/{k_FilePrefix}{frameCount}.png";
m_InstancePath = $"{m_Directory}/{k_FilePrefix}{frameCount}.png";
var localPath = $"{Manager.Instance.GetDirectoryFor(m_Directory)}/{k_FilePrefix}{frameCount}.png";
var colors = new NativeArray<Color32>(data, Allocator.TempJob);

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


internal bool m_fLensDistortionEnabled = false;
#if HDRP_PRESENT
SemanticSegmentationPass m_SemanticSegmentationPass;
LensDistortionPass m_LensDistortionPass;
#elif URP_PRESENT
SemanticSegmentationUrpPass m_SemanticSegmentationPass;
LensDistortionUrpPass m_LensDistortionPass;

m_SemanticSegmentationDirectory = k_SemanticSegmentationDirectory + Guid.NewGuid();
#if HDRP_PRESENT
var gameObject = perceptionCamera.gameObject;
var customPassVolume = gameObject.GetComponent<CustomPassVolume>() ?? gameObject.AddComponent<CustomPassVolume>();
customPassVolume.injectionPoint = CustomPassInjectionPoint.BeforeRendering;
customPassVolume.isGlobal = true;
m_SemanticSegmentationPass = new SemanticSegmentationPass(myCamera, targetTexture, labelConfig)
var customPassVolumeObject = PerceptionCamera.s_CustomPassVolumeObject;
var customPassVolume = customPassVolumeObject.GetComponent<CustomPassVolume>();
var semanticSegmentationPass = (SemanticSegmentationPass)
customPassVolume.customPasses
.FirstOrDefault(p => p is SemanticSegmentationPass);
if (semanticSegmentationPass == null)
name = "Labeling Pass"
};
customPassVolume.customPasses.Add(m_SemanticSegmentationPass);
semanticSegmentationPass = new SemanticSegmentationPass(labelConfig)
{
name = "Semantic segmentation pass"
};
customPassVolume.customPasses.Add(semanticSegmentationPass);
}
semanticSegmentationPass.AddTarget(myCamera, targetTexture);
m_LensDistortionPass = new LensDistortionPass(myCamera, targetTexture)
{

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


public static readonly int _Distortion_Params1 = Shader.PropertyToID("_Distortion_Params1");
public static readonly int _Distortion_Params2 = Shader.PropertyToID("_Distortion_Params2");
LensDistortion m_lensDistortion;
//private LensDistortion m_LensDistortion;
public LensDistortionCrossPipelinePass(Camera targetCamera, RenderTexture targetTexture)
: base(targetCamera)
public LensDistortionCrossPipelinePass(RenderTexture targetTexture)
{
m_TargetTexture = targetTexture;
}

m_distortedTexture.filterMode = FilterMode.Point;
m_distortedTexture.Create();
}
m_fInitialized = true;
}
protected override void ExecutePass(ScriptableRenderContext renderContext, CommandBuffer cmd, Camera camera, CullingResults cullingResult)
{
if (m_fInitialized == false)
return;
LensDistortion lensDistortion;
var hdCamera = HDCamera.GetOrCreate(targetCamera);
var hdCamera = HDCamera.GetOrCreate(camera);
m_lensDistortion = stack.GetComponent<LensDistortion>();
lensDistortion = stack.GetComponent<LensDistortion>();
m_lensDistortion = stack.GetComponent<LensDistortion>();
lensDistortion = stack.GetComponent<LensDistortion>();
m_fInitialized = true;
}
protected override void ExecutePass(ScriptableRenderContext renderContext, CommandBuffer cmd, Camera camera, CullingResults cullingResult)
{
if (m_fInitialized == false)
if (SetLensDistortionShaderParameters(lensDistortion, camera) == false)
if (SetLensDistortionShaderParameters() == false)
return;
// Blitmayhem
// Blitmayhem
cmd.Blit(m_TargetTexture, m_distortedTexture, m_LensDistortionMaterial);
cmd.Blit(m_distortedTexture, m_TargetTexture);

public bool SetLensDistortionShaderParameters()
public bool SetLensDistortionShaderParameters(LensDistortion lensDistortion, Camera camera)
{
if (m_fInitialized == false)
return false;

var mult = new Vector2(1.0f, 1.0f);
#if HDRP_PRESENT
if(m_lensDistortion == null)
if(lensDistortion == null)
if(targetCamera == null)
return false;
var UACD = targetCamera.GetUniversalAdditionalCameraData();
var UACD = camera.GetUniversalAdditionalCameraData();
if (m_lensDistortion.active == false)
if (lensDistortion.active == false)
return false;
#else

{
intensity = lensDistortionOverride.Value;
}
else if (m_lensDistortion != null)
else if (lensDistortion != null)
if (m_lensDistortion.intensity.value != 0.0f)
if (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;
intensity = lensDistortion.intensity.value;
center = lensDistortion.center.value * 2f - Vector2.one;
mult.x = Mathf.Max(lensDistortion.xMultiplier.value, 1e-4f);
mult.y = Mathf.Max(lensDistortion.yMultiplier.value, 1e-4f);
scale = 1.0f / lensDistortion.scale.value;
}
else
{

public override void SetupMaterialProperties(MaterialPropertyBlock mpb, Renderer renderer, Labeling labeling, uint instanceId)
{
SetLensDistortionShaderParameters();
}
}
}

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


{
if (m_LensDistortionCrossPipelinePass == null)
{
m_LensDistortionCrossPipelinePass = new LensDistortionCrossPipelinePass(targetCamera, targetTexture);
m_LensDistortionCrossPipelinePass = new LensDistortionCrossPipelinePass(targetTexture);
m_LensDistortionCrossPipelinePass.EnsureActivated();
}
}

{
CoreUtils.SetRenderTarget(cmd, targetTexture);
m_LensDistortionCrossPipelinePass.Execute(renderContext, cmd, hdCamera.camera, cullingResult);
}
public void AddTarget(Camera getComponent, RenderTexture mInstanceSegmentationTexture)
{
}
}
}

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


using System;
using System.Collections.Generic;
using System.Linq;
using Unity.Collections;
using UnityEngine;
using UnityEngine.Experimental.Rendering;

#if HDRP_PRESENT || URP_PRESENT
#if HDRP_PRESENT
InstanceSegmentationPass m_InstanceSegmentationPass;
internal static GameObject s_CustomPassVolumeObject;
LensDistortionPass m_LensDistortionPass;
#elif URP_PRESENT
InstanceSegmentationUrpPass m_InstanceSegmentationPass;

m_RenderedObjectInfoGenerator = new RenderedObjectInfoGenerator();
#if HDRP_PRESENT
var customPassVolume = this.GetComponent<CustomPassVolume>() ?? gameObject.AddComponent<CustomPassVolume>();
customPassVolume.injectionPoint = CustomPassInjectionPoint.BeforeRendering;
customPassVolume.isGlobal = true;
m_InstanceSegmentationPass = new InstanceSegmentationPass()
InstanceSegmentationPass instanceSegmentationPass;
if (s_CustomPassVolumeObject == null)
name = "Instance segmentation pass",
targetCamera = GetComponent<Camera>(),
targetTexture = m_InstanceSegmentationTexture
};
m_InstanceSegmentationPass.EnsureInit();
customPassVolume.customPasses.Add(m_InstanceSegmentationPass);
s_CustomPassVolumeObject = new GameObject();
var customPassVolume = s_CustomPassVolumeObject.AddComponent<CustomPassVolume>();
customPassVolume.injectionPoint = CustomPassInjectionPoint.BeforeRendering;
customPassVolume.isGlobal = true;
instanceSegmentationPass = new InstanceSegmentationPass()
{
name = "Instance segmentation pass"
};
instanceSegmentationPass.EnsureInit();
customPassVolume.customPasses.Add(instanceSegmentationPass);
m_LensDistortionPass = new LensDistortionPass(GetComponent<Camera>(), m_InstanceSegmentationTexture)
m_LensDistortionPass = new LensDistortionPass(GetComponent<Camera>(), m_InstanceSegmentationTexture)
{
name = "Instance Segmentation Lens Distortion Pass"
};
customPassVolume.customPasses.Add(m_LensDistortionPass);
m_LensDistortionPass.EnsureInit();
}
else
name = "Instance Segmentation Lens Distortion Pass"
};
m_LensDistortionPass.EnsureInit();
customPassVolume.customPasses.Add(m_LensDistortionPass);
instanceSegmentationPass = (InstanceSegmentationPass)
s_CustomPassVolumeObject.GetComponent<CustomPassVolume>().customPasses
.First(p => p is InstanceSegmentationPass);
m_LensDistortionPass = (LensDistortionPass)
s_CustomPassVolumeObject.GetComponent<CustomPassVolume>().customPasses
.First(p => p is LensDistortionPass);
}
m_LensDistortionPass.AddTarget(GetComponent<Camera>(), m_InstanceSegmentationTexture);
instanceSegmentationPass.AddTarget(GetComponent<Camera>(), m_InstanceSegmentationTexture);
m_fLensDistortionEnabled = true;
#elif URP_PRESENT

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


Shader m_ClassLabelingShader;
Material m_OverrideMaterial;
public SemanticSegmentationCrossPipelinePass(Camera targetCamera, SemanticSegmentationLabelConfig labelConfig) : base(targetCamera)
public SemanticSegmentationCrossPipelinePass(SemanticSegmentationLabelConfig labelConfig)
{
this.m_LabelConfig = labelConfig;
}

18
com.unity.perception/Runtime/GroundTruth/SemanticSegmentationPass.cs


/// 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 SemanticSegmentationPass : CustomPass
public class SemanticSegmentationPass : GroundTruthPass
public RenderTexture targetTexture;
public Camera targetCamera;
public SemanticSegmentationPass(Camera targetCamera, RenderTexture targetTexture, SemanticSegmentationLabelConfig semanticSegmentationLabelConfig)
public SemanticSegmentationPass(SemanticSegmentationLabelConfig semanticSegmentationLabelConfig)
this.targetTexture = targetTexture;
this.targetCamera = targetCamera;
EnsureInit();
}

{
m_SemanticSegmentationCrossPipelinePass = new SemanticSegmentationCrossPipelinePass(targetCamera, semanticSegmentationLabelConfig);
m_SemanticSegmentationCrossPipelinePass.EnsureActivated();
m_SemanticSegmentationCrossPipelinePass = new SemanticSegmentationCrossPipelinePass(semanticSegmentationLabelConfig);
this.Init(m_SemanticSegmentationCrossPipelinePass);
}
}

{
EnsureInit();
m_SemanticSegmentationCrossPipelinePass.Setup();
}
protected override void Execute(ScriptableRenderContext renderContext, CommandBuffer cmd, HDCamera hdCamera, CullingResults cullingResult)
{
CoreUtils.SetRenderTarget(cmd, targetTexture);
m_SemanticSegmentationCrossPipelinePass.Execute(renderContext, cmd, hdCamera.camera, cullingResult);
}
}
}

2
com.unity.perception/Tests/Editor/PerceptionCameraEditorTests.cs


[UnityTest]
public IEnumerator EditorPause_DoesNotLogErrors()
{
PerceptionCamera perceptionCamera = null;
ResetScene();
SetupCamera(p =>
{

p.AddLabeler(new RenderedObjectInfoLabeler(idLabelConfig));
perceptionCamera = p;
});
yield return new EnterPlayMode();
var expectedFirstFrame = Time.frameCount;

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


#if HDRP_PRESENT
using UnityEditor.Rendering.HighDefinition;
using UnityEngine.Perception.GroundTruth;
namespace UnityEditor.Perception.GroundTruth
{
[CustomPassDrawer(typeof(InstanceSegmentationPass))]
public class InstanceSegmentationPassEditor : BaseCustomPassDrawer
{
protected override void Initialize(SerializedProperty customPass)
{
var targetCameraProperty = customPass.FindPropertyRelative(nameof(GroundTruthPass.targetCamera));
AddProperty(targetCameraProperty);
AddProperty(customPass.FindPropertyRelative(nameof(InstanceSegmentationPass.targetTexture)));
base.Initialize(customPass);
}
}
}
#endif

11
com.unity.perception/Editor/GroundTruth/InstanceSegmentationPassEditor.cs.meta


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

11
com.unity.perception/Editor/GroundTruth/SemanticSegmentationPassEditor.cs.meta


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

21
com.unity.perception/Editor/GroundTruth/SemanticSegmentationPassEditor.cs


#if HDRP_PRESENT
using System;
using UnityEditor.Rendering.HighDefinition;
using UnityEngine.Perception.GroundTruth;
namespace UnityEditor.Perception.GroundTruth
{
[CustomPassDrawer(typeof(SemanticSegmentationPass))]
class SemanticSegmentationPassEditor : BaseCustomPassDrawer
{
protected override void Initialize(SerializedProperty customPass)
{
AddProperty(customPass.FindPropertyRelative(nameof(SemanticSegmentationPass.targetCamera)));
AddProperty(customPass.FindPropertyRelative(nameof(SemanticSegmentationPass.targetTexture)));
AddProperty(customPass.FindPropertyRelative(nameof(SemanticSegmentationPass.semanticSegmentationLabelConfig)));
base.Initialize(customPass);
}
}
}
#endif
正在加载...
取消
保存