浏览代码

refactored scenario end frame behavior

/addressables-test
sleal-unity 4 年前
当前提交
4df00501
共有 6 个文件被更改,包括 80 次插入54 次删除
  1. 23
      com.unity.perception/Runtime/Randomization/Scenarios/FixedLengthScenario.cs
  2. 20
      com.unity.perception/Runtime/Randomization/Scenarios/PerceptionScenario.cs
  3. 52
      com.unity.perception/Runtime/Randomization/Scenarios/ScenarioBase.cs
  4. 14
      com.unity.perception/Runtime/Randomization/Scenarios/UnitySimulationScenario.cs
  5. 22
      com.unity.perception/Runtime/Randomization/Scenarios/AssetLoadingScenario.cs
  6. 3
      com.unity.perception/Runtime/Randomization/Scenarios/AssetLoadingScenario.cs.meta

23
com.unity.perception/Runtime/Randomization/Scenarios/FixedLengthScenario.cs


using System;
using UnityEngine.Perception.GroundTruth;
namespace UnityEngine.Perception.Randomization.Scenarios
{

[AddComponentMenu("Perception/Scenarios/Fixed Length Scenario")]
public class FixedLengthScenario: UnitySimulationScenario<FixedLengthScenario.Constants>
{
PerceptionCamera m_PerceptionCamera;
/// <summary>
/// Constants describing the execution of this scenario
/// </summary>

/// Returns whether the current scenario iteration has completed
/// </summary>
protected override bool isIterationComplete => currentIterationFrame >= constants.framesPerIteration;
/// <inheritdoc/>
protected override void OnAwake()
{
base.OnAwake();
m_PerceptionCamera = FindObjectOfType<PerceptionCamera>();
if (m_PerceptionCamera != null && m_PerceptionCamera.captureTriggerMode != CaptureTriggerMode.Manual)
{
Debug.LogError("The perception camera must be set to manual capture mode", m_PerceptionCamera);
m_PerceptionCamera.enabled = false;
enabled = false;
}
}
/// <inheritdoc/>
protected override void OnIterationStart()
{
if (m_PerceptionCamera != null && currentIterationFrame == constants.framesPerIteration - 1)
m_PerceptionCamera.RequestCapture();
}
}
}

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


/// </summary>
MetricDefinition m_IterationMetricDefinition;
/// <summary>
/// The scriptable render pipeline hook used to capture perception data skips the first frame of the simulation
/// when running locally, so this flag is used to track whether the first frame has been skipped yet.
/// </summary>
protected bool m_SkippedFirstFrame;
/// <inheritdoc/>
protected override bool isScenarioReadyToStart
{
get
{
if (!m_SkippedFirstFrame)
{
m_SkippedFirstFrame = true;
return false;
}
return true;
}
}
/// <inheritdoc/>
protected override void OnAwake()
{

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


using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using UnityEditor;

/// The scenario will begin on the frame this property first returns true
/// </summary>
/// <returns>Whether the scenario should start this frame</returns>
protected abstract bool isScenarioReadyToStart { get; }
protected virtual bool isScenarioReadyToStart => true;
/// <summary>
/// Returns whether the current scenario iteration has completed

void Update()
{
// There are two known issues with the starting simulations on the first frame: rendered frames cannot be
// captured and the WaitForEndOfFrame will not function properly on the first frame.
if (Time.frameCount == 1)
return;
switch (state)
{
case State.Initializing:

void IterationLoop()
{
// Perform new iteration tasks
if (currentIterationFrame == 0)
{
ResetRandomStateOnIteration();
OnIterationStart();
foreach (var randomizer in activeRandomizers)
randomizer.IterationStart();
}
// Perform new frame tasks
OnUpdate();
foreach (var randomizer in activeRandomizers)
randomizer.Update();
StartCoroutine(EndOfFrameIterationLoop());
}
IEnumerator EndOfFrameIterationLoop()
{
yield return new WaitForEndOfFrame();
// Iterate scenario frame count
currentIterationFrame++;
framesSinceInitialization++;
// Increment iteration and cleanup last iteration
if (isIterationComplete)
{

OnComplete();
state = State.Idle;
OnIdle();
return;
// Perform new iteration tasks
if (currentIterationFrame == 0)
{
ResetRandomStateOnIteration();
OnIterationStart();
foreach (var randomizer in activeRandomizers)
randomizer.IterationStart();
}
// Perform new frame tasks
OnUpdate();
foreach (var randomizer in activeRandomizers)
randomizer.Update();
// Iterate scenario frame count
currentIterationFrame++;
framesSinceInitialization++;
}
/// <summary>

14
com.unity.perception/Runtime/Randomization/Scenarios/UnitySimulationScenario.cs


where T : UnitySimulationScenarioConstants, new()
{
/// <inheritdoc/>
protected override bool isScenarioReadyToStart
{
get
{
if (!Configuration.Instance.IsSimulationRunningInCloud() && !m_SkippedFirstFrame)
{
m_SkippedFirstFrame = true;
return false;
}
return true;
}
}
/// <inheritdoc/>
protected sealed override bool isScenarioComplete => currentIteration >= constants.totalIterations;
/// <inheritdoc/>

22
com.unity.perception/Runtime/Randomization/Scenarios/AssetLoadingScenario.cs


// using UnityEngine.AddressableAssets;
//
// namespace UnityEngine.Perception.Randomization.Scenarios
// {
// [AddComponentMenu("Perception/Scenarios/Asset Loading Scenario")]
// public class AssetLoadingScenario : FixedLengthScenario
// {
// bool m_AssetsLoaded;
//
//
// protected override bool isScenarioReadyToStart => m_AssetsLoaded;
//
// protected override void OnStart()
// {
// base.OnStart();
// Addressables.LoadAssetsAsync<GameObject>("", null).Completed += handle =>
// {
//
// };
// }
// }
// }

3
com.unity.perception/Runtime/Randomization/Scenarios/AssetLoadingScenario.cs.meta


fileFormatVersion: 2
guid: 1d7305f64b1a47dbb52fe9dbbbcdb931
timeCreated: 1616181370
正在加载...
取消
保存