浏览代码
Merge pull request #240 from Unity-Technologies/remove-entities-dependency
Merge pull request #240 from Unity-Technologies/remove-entities-dependency
Remove entities dependency/main
GitHub
4 年前
当前提交
304f106b
共有 78 个文件被更改,包括 1210 次插入 和 1317 次删除
-
4com.unity.perception/CHANGELOG.md
-
3com.unity.perception/Editor/GroundTruth/InstanceSegmentationPassEditor.cs
-
75com.unity.perception/Runtime/GroundTruth/GroundTruthRendererFeature.cs
-
4com.unity.perception/Runtime/GroundTruth/IGroundTruthGenerator.cs
-
20com.unity.perception/Runtime/GroundTruth/Labelers/BoundingBox3DLabeler.cs
-
18com.unity.perception/Runtime/GroundTruth/Labelers/KeypointLabeler.cs
-
25com.unity.perception/Runtime/GroundTruth/Labelers/SemanticSegmentationLabeler.cs
-
17com.unity.perception/Runtime/GroundTruth/Labeling/LabelEntryMatchCache.cs
-
34com.unity.perception/Runtime/GroundTruth/Labeling/Labeling.cs
-
476com.unity.perception/Runtime/GroundTruth/PerceptionCamera.cs
-
24com.unity.perception/Runtime/GroundTruth/PerceptionCamera_InstanceSegmentation.cs
-
11com.unity.perception/Runtime/GroundTruth/RenderTextureReader.cs
-
12com.unity.perception/Runtime/GroundTruth/RenderedObjectInfo.cs
-
2com.unity.perception/Runtime/GroundTruth/RenderedObjectInfoGenerator.cs
-
77com.unity.perception/Runtime/GroundTruth/SimulationState.cs
-
1com.unity.perception/Runtime/Randomization/Randomizers/Randomizer.cs
-
1com.unity.perception/Runtime/Randomization/Randomizers/RandomizerExamples/Utilities/PoissonDiskSampling.cs
-
11com.unity.perception/Runtime/Randomization/Scenarios/ScenarioBase.cs
-
45com.unity.perception/Tests/Runtime/GroundTruthTests/DatasetJsonUtilityTests.cs
-
1com.unity.perception/Tests/Runtime/GroundTruthTests/LabelEntryMatchCacheTests.cs
-
4com.unity.perception/Tests/Runtime/GroundTruthTests/PerceptionCameraIntegrationTests.cs
-
14com.unity.perception/Tests/Runtime/GroundTruthTests/SegmentationGroundTruthTests.cs
-
1com.unity.perception/Tests/Runtime/Randomization/RandomizerTests/RandomizerTagTests.cs
-
14com.unity.perception/package.json
-
2com.unity.perception/Runtime/GroundTruth/RenderPasses/CrossPipelinePasses/GroundTruthCrossPipelinePass.cs.meta
-
4com.unity.perception/Runtime/GroundTruth/RenderPasses/HdrpPasses/InstanceSegmentationPass.cs
-
2com.unity.perception/Runtime/GroundTruth/RenderPasses/CrossPipelinePasses/SemanticSegmentationCrossPipelinePass.cs.meta
-
5com.unity.perception/Runtime/GroundTruth/RenderPasses/HdrpPasses/SemanticSegmentationPass.cs
-
2com.unity.perception/Runtime/GroundTruth/RenderPasses/HdrpPasses/SemanticSegmentationPass.cs.meta
-
23com.unity.perception/Runtime/GroundTruth/RenderPasses/HdrpPasses/GroundTruthPass.cs
-
168com.unity.perception/Runtime/GroundTruth/Labeling/LabelManager.cs
-
3com.unity.perception/Runtime/GroundTruth/Labeling/LabelManager.cs.meta
-
25com.unity.perception/Runtime/GroundTruth/PerceptionUpdater.cs
-
3com.unity.perception/Runtime/GroundTruth/PerceptionUpdater.cs.meta
-
3com.unity.perception/Runtime/GroundTruth/RenderPasses.meta
-
3com.unity.perception/Runtime/GroundTruth/RenderPasses/CrossPipelinePasses.meta
-
120com.unity.perception/Runtime/GroundTruth/RenderPasses/CrossPipelinePasses/GroundTruthCrossPipelinePass.cs
-
78com.unity.perception/Runtime/GroundTruth/RenderPasses/CrossPipelinePasses/InstanceSegmentationCrossPipelinePass.cs
-
11com.unity.perception/Runtime/GroundTruth/RenderPasses/CrossPipelinePasses/InstanceSegmentationCrossPipelinePass.cs.meta
-
191com.unity.perception/Runtime/GroundTruth/RenderPasses/CrossPipelinePasses/LensDistortionCrossPipelinePass.cs
-
3com.unity.perception/Runtime/GroundTruth/RenderPasses/CrossPipelinePasses/LensDistortionCrossPipelinePass.cs.meta
-
83com.unity.perception/Runtime/GroundTruth/RenderPasses/CrossPipelinePasses/SemanticSegmentationCrossPipelinePass.cs
-
3com.unity.perception/Runtime/GroundTruth/RenderPasses/HdrpPasses.meta
-
3com.unity.perception/Runtime/GroundTruth/RenderPasses/HdrpPasses/GroundTruthPass.cs.meta
-
48com.unity.perception/Runtime/GroundTruth/RenderPasses/HdrpPasses/LensDistortionPass.cs
-
3com.unity.perception/Runtime/GroundTruth/RenderPasses/UrpPasses.meta
-
31com.unity.perception/Runtime/GroundTruth/RenderPasses/UrpPasses/InstanceSegmentationUrpPass.cs
-
3com.unity.perception/Runtime/GroundTruth/RenderPasses/UrpPasses/InstanceSegmentationUrpPass.cs.meta
-
31com.unity.perception/Runtime/GroundTruth/RenderPasses/UrpPasses/LensDistortionUrpPass.cs
-
3com.unity.perception/Runtime/GroundTruth/RenderPasses/UrpPasses/LensDistortionUrpPass.cs.meta
-
31com.unity.perception/Runtime/GroundTruth/RenderPasses/UrpPasses/SemanticSegmentationUrpPass.cs
-
3com.unity.perception/Runtime/GroundTruth/RenderPasses/UrpPasses/SemanticSegmentationUrpPass.cs.meta
-
15com.unity.perception/Runtime/GroundTruth/GroundTruthInfo.cs
-
11com.unity.perception/Runtime/GroundTruth/GroundTruthInfo.cs.meta
-
11com.unity.perception/Runtime/GroundTruth/GroundTruthLabelSetupSystem.cs.meta
-
11com.unity.perception/Runtime/GroundTruth/GroundTruthPass.cs.meta
-
11com.unity.perception/Runtime/GroundTruth/InstanceSegmentationCrossPipelinePass.cs.meta
-
13com.unity.perception/Runtime/GroundTruth/SimulationManagementComponentSystem.cs
-
11com.unity.perception/Runtime/GroundTruth/SimulationManagementComponentSystem.cs.meta
-
107com.unity.perception/Runtime/GroundTruth/GroundTruthCrossPipelinePass.cs
-
141com.unity.perception/Runtime/GroundTruth/GroundTruthLabelSetupSystem.cs
-
78com.unity.perception/Runtime/GroundTruth/InstanceSegmentationCrossPipelinePass.cs
-
79com.unity.perception/Runtime/GroundTruth/SemanticSegmentationCrossPipelinePass.cs
-
200com.unity.perception/Runtime/GroundTruth/LensDistortionCrossPipelinePass.cs
-
3com.unity.perception/Runtime/GroundTruth/LensDistortionCrossPipelinePass.cs.meta
-
54com.unity.perception/Runtime/GroundTruth/LensDistortionPass.cs
-
0/com.unity.perception/Runtime/GroundTruth/RenderPasses/CrossPipelinePasses/GroundTruthCrossPipelinePass.cs.meta
-
0/com.unity.perception/Runtime/GroundTruth/RenderPasses/HdrpPasses/InstanceSegmentationPass.cs
-
0/com.unity.perception/Runtime/GroundTruth/RenderPasses/HdrpPasses/InstanceSegmentationPass.cs.meta
-
0/com.unity.perception/Runtime/GroundTruth/RenderPasses/CrossPipelinePasses/SemanticSegmentationCrossPipelinePass.cs.meta
-
0/com.unity.perception/Runtime/GroundTruth/RenderPasses/HdrpPasses/SemanticSegmentationPass.cs
-
0/com.unity.perception/Runtime/GroundTruth/RenderPasses/HdrpPasses/SemanticSegmentationPass.cs.meta
-
0/com.unity.perception/Runtime/GroundTruth/RenderPasses/HdrpPasses/GroundTruthPass.cs
-
0/com.unity.perception/Runtime/GroundTruth/RenderPasses/HdrpPasses/LensDistortionPass.cs.meta
|
|||
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); |
|||
} |
|||
} |
|||
} |
|||
} |
|
|||
fileFormatVersion: 2 |
|||
guid: d138f786c7d94325962fec6fa091edbb |
|||
timeCreated: 1614980066 |
|
|||
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(); |
|||
} |
|||
} |
|||
} |
|
|||
fileFormatVersion: 2 |
|||
guid: ea65959b27da42a2a364b2ae9a3dc65a |
|||
timeCreated: 1614983032 |
|
|||
fileFormatVersion: 2 |
|||
guid: 36c50bc1fa70491eaa8ffd85c55187d0 |
|||
timeCreated: 1615074717 |
|
|||
fileFormatVersion: 2 |
|||
guid: ab58b1700a9642998cb3a5971f6543d1 |
|||
timeCreated: 1615157471 |
|
|||
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); |
|||
} |
|||
} |
|
|||
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
|
|||
} |
|||
} |
|||
} |
|
|||
fileFormatVersion: 2 |
|||
guid: 799b5ba08ee3e6d479c2aac3a89507f0 |
|||
MonoImporter: |
|||
externalObjects: {} |
|||
serializedVersion: 2 |
|||
defaultReferences: [] |
|||
executionOrder: 0 |
|||
icon: {instanceID: 0} |
|||
userData: |
|||
assetBundleName: |
|||
assetBundleVariant: |
|
|||
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
|
|
|||
fileFormatVersion: 2 |
|||
guid: 91d542be380c442186b86e0816a0c0fb |
|||
timeCreated: 1615157478 |
|
|||
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); |
|||
} |
|||
} |
|||
} |
|
|||
fileFormatVersion: 2 |
|||
guid: 3e921ed47cbb4b4daee97efd288b537e |
|||
timeCreated: 1615157360 |
|
|||
fileFormatVersion: 2 |
|||
guid: fce7b38cf56d40358e555bb2403b23d0 |
|||
timeCreated: 1615157376 |
|
|||
#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
|
|
|||
fileFormatVersion: 2 |
|||
guid: 4cd6f632a845433cbbfc06263002654a |
|||
timeCreated: 1615157347 |
|
|||
#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
|
|
|||
fileFormatVersion: 2 |
|||
guid: f8f08e25f7cb4355aa70060081bc4200 |
|||
timeCreated: 1615156976 |
|
|||
#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
|
|
|||
fileFormatVersion: 2 |
|||
guid: db2ddd6b833f412bb68447d5f85eff4d |
|||
timeCreated: 1615157082 |
|
|||
#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
|
|
|||
fileFormatVersion: 2 |
|||
guid: a16b614c904345fa8666f31f18a703af |
|||
timeCreated: 1615157147 |
|
|||
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; |
|||
} |
|||
} |
|
|||
fileFormatVersion: 2 |
|||
guid: 013782ed5fce31d46b9849bbb3cc3da8 |
|||
MonoImporter: |
|||
externalObjects: {} |
|||
serializedVersion: 2 |
|||
defaultReferences: [] |
|||
executionOrder: 0 |
|||
icon: {instanceID: 0} |
|||
userData: |
|||
assetBundleName: |
|||
assetBundleVariant: |
|
|||
fileFormatVersion: 2 |
|||
guid: 67ff3a1956c5bb14487beccf559cdd49 |
|||
MonoImporter: |
|||
externalObjects: {} |
|||
serializedVersion: 2 |
|||
defaultReferences: [] |
|||
executionOrder: 0 |
|||
icon: {instanceID: 0} |
|||
userData: |
|||
assetBundleName: |
|||
assetBundleVariant: |
|
|||
fileFormatVersion: 2 |
|||
guid: 0d7678390ae7b4844ad760a9e39dfd7d |
|||
MonoImporter: |
|||
externalObjects: {} |
|||
serializedVersion: 2 |
|||
defaultReferences: [] |
|||
executionOrder: 0 |
|||
icon: {instanceID: 0} |
|||
userData: |
|||
assetBundleName: |
|||
assetBundleVariant: |
|
|||
fileFormatVersion: 2 |
|||
guid: b575537c5b92d6949bb233707280a614 |
|||
MonoImporter: |
|||
externalObjects: {} |
|||
serializedVersion: 2 |
|||
defaultReferences: [] |
|||
executionOrder: 0 |
|||
icon: {instanceID: 0} |
|||
userData: |
|||
assetBundleName: |
|||
assetBundleVariant: |
|
|||
using System; |
|||
using Unity.Entities; |
|||
|
|||
namespace UnityEngine.Perception.GroundTruth |
|||
{ |
|||
class SimulationManagementComponentSystem : ComponentSystem |
|||
{ |
|||
protected override void OnUpdate() |
|||
{ |
|||
DatasetCapture.SimulationState?.Update(); |
|||
} |
|||
} |
|||
} |
|
|||
fileFormatVersion: 2 |
|||
guid: 8dfdda78cfa74c6991ac2d757d8e7019 |
|||
MonoImporter: |
|||
externalObjects: {} |
|||
serializedVersion: 2 |
|||
defaultReferences: [] |
|||
executionOrder: 0 |
|||
icon: {instanceID: 0} |
|||
userData: |
|||
assetBundleName: |
|||
assetBundleVariant: |
|
|||
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); |
|||
} |
|||
} |
|
|||
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); |
|||
} |
|||
} |
|||
} |
|
|||
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
|
|||
} |
|||
} |
|||
} |
|
|||
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); |
|||
} |
|||
} |
|||
} |
|
|||
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
|
|
|||
fileFormatVersion: 2 |
|||
guid: ad37b813e25c450fbc65b2820a7d9dc2 |
|||
timeCreated: 1598041907 |
|
|||
#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
|
撰写
预览
正在加载...
取消
保存
Reference in new issue