浏览代码

Fixing simulation stopping when editor is not focused. Fixing crashes and hangs on completing long simulations by adding object caching

/main
Steven Leal 4 年前
当前提交
a944e1e6
共有 5 个文件被更改,包括 114 次插入22 次删除
  1. 2
      com.unity.perception/Runtime/GroundTruth/PerceptionCamera.cs
  2. 24
      com.unity.perception/Runtime/Randomization/Randomizers/RandomizerExamples/Randomizers/BackgroundObjectPlacementRandomizer.cs
  3. 26
      com.unity.perception/Runtime/Randomization/Randomizers/RandomizerExamples/Randomizers/ForegroundObjectPlacementRandomizer.cs
  4. 81
      com.unity.perception/Runtime/Randomization/Randomizers/RandomizerExamples/Utilities/GameObjectOneWayCache.cs
  5. 3
      com.unity.perception/Runtime/Randomization/Randomizers/RandomizerExamples/Utilities/GameObjectOneWayCache.cs.meta

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


AsyncRequest.maxJobSystemParallelism = 0; // Jobs are not chained to one another in any way, maximizing parallelism
AsyncRequest.maxAsyncRequestFrameAge = 4; // Ensure that readbacks happen before Allocator.TempJob allocations get stale
Application.runInBackground = true;
SetupInstanceSegmentation();
m_AttachedCamera = GetComponent<Camera>();

24
com.unity.perception/Runtime/Randomization/Randomizers/RandomizerExamples/Randomizers/BackgroundObjectPlacementRandomizer.cs


using System;
using System.Collections.Generic;
using System.Linq;
using UnityEngine.Experimental.Perception.Randomization.Parameters;
using UnityEngine.Experimental.Perception.Randomization.Samplers;

[AddRandomizerMenu("Perception/Background Object Placement Randomizer")]
public class BackgroundObjectPlacementRandomizer : Randomizer
{
List<GameObject> m_SpawnedObjects = new List<GameObject>();
/// <summary>
/// The Z offset component applied to all generated background layers
/// </summary>

/// </summary>
public GameObjectParameter prefabs;
GameObject container;
GameObjectOneWayCache gameObjectOneWayCache;
protected override void OnCreate()
{
container = new GameObject("BackgroundContainer");
container.transform.parent = scenario.transform;
gameObjectOneWayCache = new GameObjectOneWayCache(container.transform, prefabs.categories.Select((element) => element.Item1).ToArray());
}
if (m_SpawnedObjects == null)
m_SpawnedObjects = new List<GameObject>();
for (var i = 0; i < layerCount; i++)
{
var seed = scenario.NextRandomState();

var parent = scenario.transform;
var instance = Object.Instantiate(prefabs.Sample(), parent);
var instance = gameObjectOneWayCache.GetOrInstantiate(prefabs.Sample());
m_SpawnedObjects.Add(instance);
}
placementSamples.Dispose();
}

/// </summary>
protected override void OnIterationEnd()
{
foreach (var spawnedObject in m_SpawnedObjects)
Object.Destroy(spawnedObject);
m_SpawnedObjects.Clear();
gameObjectOneWayCache.ResetAllObjects();
}
}
}

26
com.unity.perception/Runtime/Randomization/Randomizers/RandomizerExamples/Randomizers/ForegroundObjectPlacementRandomizer.cs


using System;
using System.Collections.Generic;
using System.Linq;
using UnityEngine.Experimental.Perception.Randomization.Parameters;
namespace UnityEngine.Experimental.Perception.Randomization.Randomizers.SampleRandomizers

[AddRandomizerMenu("Perception/Foreground Object Placement Randomizer")]
public class ForegroundObjectPlacementRandomizer : Randomizer
{
List<GameObject> m_SpawnedObjects = new List<GameObject>();
/// <summary>
/// The Z offset component applied to the generated layer of GameObjects
/// </summary>

/// </summary>
public GameObjectParameter prefabs;
GameObject m_Container;
GameObjectOneWayCache m_GameObjectOneWayCache;
protected override void OnCreate()
{
m_Container = new GameObject("Foreground Objects");
var scenarioTransform = scenario.transform;
m_Container.transform.parent = scenarioTransform;
m_GameObjectOneWayCache = new GameObjectOneWayCache(
scenarioTransform, prefabs.categories.Select(element => element.Item1).ToArray());
}
if (m_SpawnedObjects == null)
m_SpawnedObjects = new List<GameObject>();
var parent = scenario.transform;
var instance = Object.Instantiate(prefabs.Sample(), parent);
var instance = m_GameObjectOneWayCache.GetOrInstantiate(prefabs.Sample());
m_SpawnedObjects.Add(instance);
}
placementSamples.Dispose();
}

/// </summary>
protected override void OnIterationEnd()
{
foreach (var spawnedObject in m_SpawnedObjects)
Object.Destroy(spawnedObject);
m_SpawnedObjects.Clear();
m_GameObjectOneWayCache.ResetAllObjects();
}
}
}

81
com.unity.perception/Runtime/Randomization/Randomizers/RandomizerExamples/Utilities/GameObjectOneWayCache.cs


using System;
using System.Collections.Generic;
using Unity.Profiling;
using UnityEngine;
using Object = UnityEngine.Object;
/// <summary>
/// Facilitates object pooling for a pre-specified collection of prefabs with the caveat that objects can be fetched
/// from the cache but not returned. Every frame, the cache needs to be reset, which will return all objects to the pool
/// </summary>
class GameObjectOneWayCache
{
static ProfilerMarker s_ResetAllObjectsMarker = new ProfilerMarker("ResetAllObjects");
// Objects will reset to this origin when not being used
Transform m_CacheParent;
Dictionary<int, int> m_InstanceIdToIndex;
List<GameObject>[] m_InstantiatedObjects;
int[] m_NumObjectsActive;
int NumObjectsInCache { get; set; }
public int NumObjectsActive { get; private set; }
public GameObjectOneWayCache(Transform parent, GameObject[] prefabs)
{
m_CacheParent = parent;
m_InstanceIdToIndex = new Dictionary<int, int>();
m_InstantiatedObjects = new List<GameObject>[prefabs.Length];
m_NumObjectsActive = new int[prefabs.Length];
var index = 0;
foreach (var prefab in prefabs)
{
var instanceId = prefab.GetInstanceID();
m_InstanceIdToIndex.Add(instanceId, index);
m_InstantiatedObjects[index] = new List<GameObject>();
m_NumObjectsActive[index] = 0;
++index;
}
}
public GameObject GetOrInstantiate(GameObject prefab)
{
if (!m_InstanceIdToIndex.TryGetValue(prefab.GetInstanceID(), out var index))
{
throw new ArgumentException($"Prefab {prefab.name} (ID: {prefab.GetInstanceID()}) is not in cache.");
}
++NumObjectsActive;
if (m_NumObjectsActive[index] < m_InstantiatedObjects[index].Count)
{
var nextInCache = m_InstantiatedObjects[index][m_NumObjectsActive[index]];
++m_NumObjectsActive[index];
return nextInCache;
}
else
{
++NumObjectsInCache;
var newObject = Object.Instantiate(prefab, m_CacheParent);
++m_NumObjectsActive[index];
m_InstantiatedObjects[index].Add(newObject);
return newObject;
}
}
public void ResetAllObjects()
{
using (s_ResetAllObjectsMarker.Auto())
{
NumObjectsActive = 0;
for (var i = 0; i < m_InstantiatedObjects.Length; ++i)
{
m_NumObjectsActive[i] = 0;
foreach (var obj in m_InstantiatedObjects[i])
{
// Position outside the frame
obj.transform.localPosition = new Vector3(10000, 0, 0);
}
}
}
}
}

3
com.unity.perception/Runtime/Randomization/Randomizers/RandomizerExamples/Utilities/GameObjectOneWayCache.cs.meta


fileFormatVersion: 2
guid: f71829e483594d159927c8ad5e3c8eef
timeCreated: 1583339841
正在加载...
取消
保存