sleal-unity
4 年前
当前提交
fec66d94
共有 21 个文件被更改,包括 212 次插入 和 288 次删除
-
9com.unity.perception/Runtime/GroundTruth/GroundTruthCrossPipelinePass.cs
-
11com.unity.perception/Runtime/GroundTruth/GroundTruthPass.cs
-
20com.unity.perception/Runtime/GroundTruth/Labelers/BoundingBox3DLabeler.cs
-
18com.unity.perception/Runtime/GroundTruth/Labelers/KeypointLabeler.cs
-
17com.unity.perception/Runtime/GroundTruth/Labeling/LabelEntryMatchCache.cs
-
34com.unity.perception/Runtime/GroundTruth/Labeling/Labeling.cs
-
1com.unity.perception/Runtime/Randomization/Randomizers/Randomizer.cs
-
1com.unity.perception/Runtime/Randomization/Randomizers/RandomizerExamples/Utilities/PoissonDiskSampling.cs
-
1com.unity.perception/Tests/Runtime/GroundTruthTests/LabelEntryMatchCacheTests.cs
-
1com.unity.perception/Tests/Runtime/Randomization/RandomizerTests/RandomizerTagTests.cs
-
14com.unity.perception/package.json
-
141com.unity.perception/Runtime/GroundTruth/LabeledObjectsManager.cs
-
3com.unity.perception/Runtime/GroundTruth/LabeledObjectsManager.cs.meta
-
24com.unity.perception/Runtime/GroundTruth/PerceptionUpdater.cs
-
3com.unity.perception/Runtime/GroundTruth/PerceptionUpdater.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
-
13com.unity.perception/Runtime/GroundTruth/SimulationManagementComponentSystem.cs
-
11com.unity.perception/Runtime/GroundTruth/SimulationManagementComponentSystem.cs.meta
-
141com.unity.perception/Runtime/GroundTruth/GroundTruthLabelSetupSystem.cs
|
|||
using System.Collections.Generic; |
|||
using UnityEngine.Perception.Randomization.Randomizers; |
|||
|
|||
namespace UnityEngine.Perception.GroundTruth |
|||
{ |
|||
class LabeledObjectsManager |
|||
{ |
|||
public static LabeledObjectsManager singleton { get; } = new LabeledObjectsManager(); |
|||
|
|||
const uint k_StartingIndex = 1; |
|||
uint m_CurrentObjectIndex = k_StartingIndex; |
|||
List<IGroundTruthGenerator> m_ActiveGenerators = new List<IGroundTruthGenerator>(); |
|||
LinkedHashSet<Labeling> m_UnregisteredLabels = new LinkedHashSet<Labeling>(); |
|||
LinkedHashSet<Labeling> m_RegisteredLabels = new LinkedHashSet<Labeling>(); |
|||
|
|||
public IEnumerable<Labeling> registeredLabels => m_RegisteredLabels; |
|||
|
|||
public void Update() |
|||
{ |
|||
if (m_RegisteredLabels.Count == 0) |
|||
m_CurrentObjectIndex = k_StartingIndex; |
|||
|
|||
foreach (var unregisteredLabel in m_UnregisteredLabels) |
|||
{ |
|||
if (m_RegisteredLabels.Contains(unregisteredLabel)) |
|||
continue; |
|||
|
|||
var instanceId = m_CurrentObjectIndex++; |
|||
InitGameObjectRecursive( |
|||
unregisteredLabel.gameObject, |
|||
new MaterialPropertyBlock(), |
|||
unregisteredLabel, |
|||
instanceId); |
|||
|
|||
unregisteredLabel.SetInstanceId(instanceId); |
|||
m_RegisteredLabels.Add(unregisteredLabel); |
|||
} |
|||
|
|||
m_UnregisteredLabels.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) |
|||
InitGameObjectRecursive(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="labeledObject">the component to register</param>
|
|||
public void Register(Labeling labeledObject) |
|||
{ |
|||
m_UnregisteredLabels.Add(labeledObject); |
|||
} |
|||
|
|||
/// <summary>
|
|||
/// Unregisters a labeling component
|
|||
/// </summary>
|
|||
/// <param name="labeledObject">the component to unregister</param>
|
|||
public void Unregister(Labeling labeledObject) |
|||
{ |
|||
m_UnregisteredLabels.Remove(labeledObject); |
|||
m_RegisteredLabels.Remove(labeledObject); |
|||
} |
|||
|
|||
/// <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="labeledObject">the component to refresh</param>
|
|||
public void RefreshLabeling(Labeling labeledObject) |
|||
{ |
|||
m_RegisteredLabels.Remove(labeledObject); |
|||
m_UnregisteredLabels.Add(labeledObject); |
|||
} |
|||
|
|||
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 = 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; |
|||
|
|||
InitGameObjectRecursive(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>
|
|||
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() |
|||
{ |
|||
LabeledObjectsManager.singleton.Update(); |
|||
DatasetCapture.SimulationState?.Update(); |
|||
} |
|||
} |
|||
} |
|
|||
fileFormatVersion: 2 |
|||
guid: ea65959b27da42a2a364b2ae9a3dc65a |
|||
timeCreated: 1614983032 |
|
|||
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: |
|
|||
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.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); |
|||
} |
|||
} |
|||
} |
撰写
预览
正在加载...
取消
保存
Reference in new issue