浏览代码

Merge branch 'h-i' into for_vulkan_mem_test

/for_vulkan_mem_test
Mohsen Kamalzadeh 3 年前
当前提交
7dddeacb
共有 2 个文件被更改,包括 60 次插入26 次删除
  1. 66
      com.unity.perception/Runtime/Randomization/Randomizers/RandomizerExamples/Utilities/GameObjectOneWayCache.cs
  2. 20
      com.unity.perception/Runtime/Randomization/Scenarios/ScenarioBase.cs

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


{
static ProfilerMarker s_ResetAllObjectsMarker = new ProfilerMarker("ResetAllObjects");
GameObject[] m_GameObjects;
List<GameObject> m_GameObjects;
List<CachedObjectData>[] m_InstantiatedObjects;
int[] m_NumObjectsActive;
List<List<CachedObjectData>> m_InstantiatedObjects;
List<int> m_NumObjectsActive;
int NumObjectsInCache { get; set; }
/// <summary>

/// </summary>
/// <param name="parent">The parent object all cached instances will be parented under</param>
/// <param name="gameObjects">The gameObjects to cache</param>
public GameObjectOneWayCache(Transform parent, GameObject[] gameObjects)
public GameObjectOneWayCache(Transform parent, GameObject[] gameObjects = null)
if (gameObjects.Length == 0)
throw new ArgumentException(
"A non-empty array of GameObjects is required to initialize this GameObject cache");
m_GameObjects = gameObjects;
m_GameObjects = new List<GameObject>();
m_InstantiatedObjects = new List<CachedObjectData>[gameObjects.Length];
m_NumObjectsActive = new int[gameObjects.Length];
m_InstantiatedObjects = new List<List<CachedObjectData>>();
m_NumObjectsActive = new List<int>();
var index = 0;
foreach (var obj in gameObjects)
if (gameObjects != null && gameObjects.Length != 0)
if (!IsPrefab(obj))
foreach (var obj in gameObjects)
obj.transform.parent = parent;
obj.SetActive(false);
AddGameObject(obj);
var instanceId = obj.GetInstanceID();
m_InstanceIdToIndex.Add(instanceId, index);
m_InstantiatedObjects[index] = new List<CachedObjectData>();
m_NumObjectsActive[index] = 0;
++index;
/// Adds the given GameObject to the list of cached objects. An instance of the object can be immediately requested afterwards.
/// </summary>
/// <param name="gameObject"></param>
public void AddGameObject(GameObject gameObject)
{
m_GameObjects.Add(gameObject);
var index = m_InstantiatedObjects.Count;
if (!IsPrefab(gameObject))
{
gameObject.transform.parent = m_CacheParent;
gameObject.SetActive(false);
}
var instanceId = gameObject.GetInstanceID();
m_InstanceIdToIndex.Add(instanceId, index);
m_InstantiatedObjects.Add(new List<CachedObjectData>());
m_NumObjectsActive.Add(index);
}
/// <summary>
/// Retrieves an existing instance of the given gameObject from the cache if available.
/// Otherwise, instantiate a new instance of the given gameObject.
/// </summary>

public GameObject GetOrInstantiate(GameObject gameObject)
{
if (!m_InstanceIdToIndex.TryGetValue(gameObject.GetInstanceID(), out var index))
throw new ArgumentException($"GameObject {gameObject.name} (ID: {gameObject.GetInstanceID()}) is not in cache.");
{
AddGameObject(gameObject);
if (!m_InstanceIdToIndex.TryGetValue(gameObject.GetInstanceID(), out index))
Debug.LogError("Object cache is not working properly.");
}
++NumObjectsActive;
if (m_NumObjectsActive[index] < m_InstantiatedObjects[index].Count)

foreach (var tag in nextInCache.randomizerTags)
tag.Register();
var obj = nextInCache.instance;
obj.transform.localPosition = Vector3.zero;
return nextInCache.instance;
}

++m_NumObjectsActive[index];
m_InstantiatedObjects[index].Add(new CachedObjectData(newObject));
newObject.transform.localPosition = Vector3.zero;
return newObject;
}

/// <returns>A random cached GameObject</returns>
public GameObject GetOrInstantiateRandomCachedObject()
{
return GetOrInstantiate(m_GameObjects[(int)(m_Sampler.Sample() * m_GameObjects.Length)]);
return GetOrInstantiate(m_GameObjects[(int)(m_Sampler.Sample() * m_GameObjects.Count)]);
}
/// <summary>

using (s_ResetAllObjectsMarker.Auto())
{
NumObjectsActive = 0;
for (var i = 0; i < m_InstantiatedObjects.Length; ++i)
for (var i = 0; i < m_InstantiatedObjects.Count; ++i)
{
m_NumObjectsActive[i] = 0;
foreach (var cachedObjectData in m_InstantiatedObjects[i])

/// <exception cref="ArgumentException">Thrown when gameObject is not an active cached object.</exception>
public void ResetObject(GameObject gameObject)
{
for (var i = 0; i < m_InstantiatedObjects.Length; ++i)
for (var i = 0; i < m_InstantiatedObjects.Count; ++i)
{
var instantiatedObjectList = m_InstantiatedObjects[i];
int indexFound = -1;

{
// Position outside the frame
cachedObjectData.instance.transform.localPosition = new Vector3(10000, 0, 0);
cachedObjectData.instance.transform.localRotation = Quaternion.identity;
foreach (var tag in cachedObjectData.randomizerTags)
tag.Unregister();
}

20
com.unity.perception/Runtime/Randomization/Scenarios/ScenarioBase.cs


public TextAsset configuration;
private bool m_ShouldRestartIteration;
private bool m_ShouldDelayIteration;
private const int k_MaxIterationStartCount = 100;
/// <summary>

do
{
m_ShouldRestartIteration = false;
m_ShouldDelayIteration = false;
iterationStartCount++;
foreach (var randomizer in activeRandomizers)
{

if (m_ShouldDelayIteration)
{
Debug.Log($"Iteration was delayed by {randomizer.GetType().Name}");
break;
}
if (m_ShouldDelayIteration)
break;
} while (m_ShouldRestartIteration && iterationStartCount < k_MaxIterationStartCount);
if (m_ShouldRestartIteration)

randomizer.Update();
// Iterate scenario frame count
currentIterationFrame++;
if (!m_ShouldDelayIteration)
currentIterationFrame++;
framesSinceInitialization++;
}

public void RestartIteration()
{
m_ShouldRestartIteration = true;
}
public void DelayIteration()
{
m_ShouldDelayIteration = true;
}
}
}
正在加载...
取消
保存