浏览代码

tested to check if parameter targets are applied correctly

/usim-randomization
Steven Leal 4 年前
当前提交
c7ddc095
共有 6 个文件被更改,包括 67 次插入64 次删除
  1. 2
      com.unity.perception/Editor/Randomization/ParameterElement.cs
  2. 2
      com.unity.perception/Editor/Randomization/Uxml/ParameterElement.uxml
  3. 8
      com.unity.perception/Runtime/Randomization/Parameters/Parameter.cs
  4. 40
      com.unity.perception/Runtime/Randomization/Parameters/ParameterTarget.cs
  5. 29
      com.unity.perception/Runtime/Randomization/Scenarios/ScenarioBase.cs
  6. 50
      com.unity.perception/Tests/Runtime/Randomization/ScenarioTests.cs

2
com.unity.perception/Editor/Randomization/ParameterElement.cs


m_TargetGameObjectProperty = m_SerializedProperty.FindPropertyRelative("target.gameObject");
ToggleTargetContainer();
var frequencyField = this.Q<PropertyField>("application-frequency");
var frequencyField = this.Q<EnumField>("application-frequency");
frequencyField.BindProperty(m_SerializedProperty.FindPropertyRelative("target.applicationFrequency"));
var targetField = this.Q<PropertyField>("target");

2
com.unity.perception/Editor/Randomization/Uxml/ParameterElement.uxml


<Label text="Target Property" class="unity-base-field__label"/>
<editor:ToolbarMenu text="Select A Property" name="property-select-menu" class="property-select-menu"/>
</VisualElement>
<editor:PropertyField name="application-frequency"/>
<editor:EnumField label="Application Frequency" name="application-frequency"/>
</VisualElement>
</Box>
<Box name="extra-properties" style="padding-left: 4px; border-top-width: 0px;"/>

8
com.unity.perception/Runtime/Randomization/Parameters/Parameter.cs


/// <summary>
/// The target this parameter apply a sample to
/// </summary>
[HideInInspector, SerializeField] internal ParameterTarget target = new ParameterTarget();
[HideInInspector, SerializeField] public ParameterTarget target = new ParameterTarget();
// /// <summary>
// /// Returns meta information regarding this type of parameter
// /// </summary>
// internal ParameterDisplayName displayName =>
// (ParameterDisplayName)Attribute.GetCustomAttribute(GetType(), typeof(ParameterDisplayName));
/// <summary>
/// The sample type generated by this parameter

40
com.unity.perception/Runtime/Randomization/Parameters/ParameterTarget.cs


/// Typically managed by a parameter configuration.
/// </summary>
[Serializable]
class ParameterTarget
public class ParameterTarget
public GameObject gameObject;
public Component component;
public string propertyName = "";
public FieldOrProperty fieldOrProperty;
public ParameterApplicationFrequency applicationFrequency;
[SerializeField] internal GameObject gameObject;
[SerializeField] internal Component component;
[SerializeField] internal string propertyName = "";
[SerializeField] internal FieldOrProperty fieldOrProperty;
[SerializeField] internal ParameterApplicationFrequency applicationFrequency;
/// <summary>
/// Assigns a new target
/// </summary>
/// <param name="targetObject">The target GameObject</param>
/// <param name="targetComponent">The target component on the target GameObject</param>
/// <param name="fieldOrPropertyName">The name of the property to apply the parameter to</param>
/// <param name="frequency">How often to apply the parameter to its target</param>
GameObject obj, Component comp, string fieldOrPropertyName, ParameterApplicationFrequency frequency)
GameObject targetObject,
Component targetComponent,
string fieldOrPropertyName,
ParameterApplicationFrequency frequency)
gameObject = obj;
component = comp;
gameObject = targetObject;
component = targetComponent;
propertyName = fieldOrPropertyName;
applicationFrequency = frequency;
var componentType = component.GetType();

}
public void Clear()
internal void Clear()
{
gameObject = null;
component = null;

/// <summary>
/// Writes a sampled value to the target GameObject and property
/// </summary>
public void ApplyValueToTarget(object value)
internal void ApplyValueToTarget(object value)
{
var componentType = component.GetType();
if (fieldOrProperty == FieldOrProperty.Field)

}
}
enum ParameterApplicationFrequency
/// <summary>
/// How often to apply a new sample to a parameter's target
/// </summary>
public enum ParameterApplicationFrequency
{
OnIterationSetup,
EveryFrame

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


public abstract class ScenarioBase : MonoBehaviour
{
static ScenarioBase s_ActiveScenario;
bool m_FirstFrame = true;
/// <summary>
/// If true, this scenario will quit the Unity application when it's finished executing

public virtual void OnInitialize() { }
/// <summary>
/// Called when each scenario iteration starts
/// Called at the beginning of every scenario iteration
/// </summary>
public virtual void OnIterationSetup() { }

public virtual void OnFrameStart() { }
/// <summary>
/// Called right before the scenario iterates
/// Called the frame after an iteration ends
/// </summary>
public virtual void OnIterationTeardown() { }

s_ActiveScenario = null;
}
IEnumerator Start()
void Start()
{
if (deserializeOnStart)
Deserialize();

// TODO: remove this yield when the perception camera no longer skips the first frame of the simulation
yield return null;
if (currentIterationFrame == 0 && !m_FirstFrame)
OnIterationTeardown();
if (isScenarioComplete)
{
OnComplete();

return;
}
if (isIterationComplete)
{
currentIteration++;
currentIterationFrame = 0;
OnIterationTeardown();
}
if (currentIterationFrame == 0)
{
DatasetCapture.StartNewSequence();

foreach (var config in ParameterConfiguration.configurations)
config.ApplyParameters(framesSinceInitialization, ParameterApplicationFrequency.EveryFrame);
OnFrameStart();
}
void LateUpdate()
{
if (isIterationComplete)
{
currentIteration++;
currentIterationFrame = 0;
}
m_FirstFrame = false;
}
}
}

50
com.unity.perception/Tests/Runtime/Randomization/ScenarioTests.cs


Object.DestroyImmediate(m_TestObject);
}
IEnumerator CreateNewScenario()
void CreateNewScenario()
yield return null;
yield return CreateNewScenario();
CreateNewScenario();
m_Scenario.serializedConstantsFileName = "perception_serialization_test";
var constants = new FixedLengthScenario.Constants

[UnityTest]
public IEnumerator IterationsCanLastMultipleFrames()
{
yield return CreateNewScenario();
CreateNewScenario();
// Scenario update loop starts next frame
yield return null;
for (var i = 0; i < testIterationFrameCount; i++)
{

}
[UnityTest]
public IEnumerator CompletesWhenIsScenarioCompleteIsTrue()
public IEnumerator FinishesWhenIsScenarioCompleteIsTrue()
yield return CreateNewScenario();
CreateNewScenario();
// Scenario update loop starts next frame
yield return null;
for (var i = 0; i < testIterationTotal; i++)
{

[UnityTest]
public IEnumerator AppliesParametersEveryFrame()
{
yield return CreateNewScenario();
CreateNewScenario();
m_Scenario.constants.framesPerIteration = 5;
m_Scenario.constants.totalIterations = 1;

[UnityTest]
public IEnumerator AppliesParametersEveryIteration()
{
yield return CreateNewScenario();
m_Scenario.constants.framesPerIteration = 5;
m_Scenario.constants.totalIterations = 1;
var config = m_TestObject.AddComponent<ParameterConfiguration>();
var parameter = config.AddParameter<Vector3Parameter>();
parameter.x = new UniformSampler(1, 2);

var transform = m_Scenario.transform;
var transform = m_TestObject.transform;
transform.position = new Vector3();
var initialPosition = new Vector3();
transform.position = initialPosition;
yield return new WaitForEndOfFrame();
CreateNewScenario();
m_Scenario.constants.framesPerIteration = 2;
m_Scenario.constants.totalIterations = 2;
yield return new WaitForEndOfFrame();
var initialPosition = transform.position;
Assert.AreNotEqual(new Vector3(), initialPosition);
// The position should change when the first iteration starts
yield return null;
Assert.AreNotEqual(initialPosition, transform.position);
yield return new WaitForEndOfFrame();
initialPosition = transform.position;
var nextPosition = transform.position;
Assert.AreEqual(initialPosition, nextPosition);
// The position should stay the same since the iteration doesn't change for another 4 frames
yield return null;
yield return new WaitForEndOfFrame();
Assert.AreEqual(initialPosition, transform.position);
Assert.AreNotEqual(nextPosition, transform.position);
yield return CreateNewScenario();
CreateNewScenario();
m_Scenario.constants.framesPerIteration = 2;
m_Scenario.constants.totalIterations = 2;

正在加载...
取消
保存