浏览代码

RandomzierTagManager.Query<T>() now returns tags directly instead of GameObjects (#166)

* RandomzierTagManager.Query<T>() now returns tags instead of GameObjects

* updated docs

* fixed tag type acquisition bug
/main
GitHub 4 年前
当前提交
fbc1af5f
共有 12 个文件被更改,包括 57 次插入64 次删除
  1. 3
      com.unity.perception/CHANGELOG.md
  2. 20
      com.unity.perception/Documentation~/Tutorial/Phase2.md
  3. 6
      com.unity.perception/Runtime/Randomization/Randomizers/RandomizerExamples/Randomizers/ColorRandomizer.cs
  4. 6
      com.unity.perception/Runtime/Randomization/Randomizers/RandomizerExamples/Randomizers/HueOffsetRandomizer.cs
  5. 6
      com.unity.perception/Runtime/Randomization/Randomizers/RandomizerExamples/Randomizers/RotationRandomizer.cs
  6. 6
      com.unity.perception/Runtime/Randomization/Randomizers/RandomizerExamples/Randomizers/SunAngleRandomizer.cs
  7. 6
      com.unity.perception/Runtime/Randomization/Randomizers/RandomizerExamples/Randomizers/TextureRandomizer.cs
  8. 4
      com.unity.perception/Runtime/Randomization/Randomizers/RandomizerTag.cs
  9. 39
      com.unity.perception/Runtime/Randomization/Randomizers/RandomizerTagManager.cs
  10. 14
      com.unity.perception/Tests/Runtime/Randomization/RandomizerTests/RandomizerTagTests.cs
  11. 8
      com.unity.perception/Tests/Runtime/Randomization/RandomizerTests/ExampleTag.cs
  12. 3
      com.unity.perception/Tests/Runtime/Randomization/RandomizerTests/ExampleTag.cs.meta

3
com.unity.perception/CHANGELOG.md


ScenarioBase.Serialize() now not only serializes scenario constants, but also all sampler member fields on randomizers attached to the scenario
RandomizerTagManager.Query<T>() now returns RandomizerTags directly instead of the GameObjects attached to said tags
### Deprecated

20
com.unity.perception/Documentation~/Tutorial/Phase2.md


protected override void OnIterationStart()
{
var taggedObjects = tagManager.Query<MyLightRandomizerTag>();
var tags = tagManager.Query<MyLightRandomizerTag>();
foreach (var taggedObject in taggedObjects)
foreach (var tag in tags)
var light = taggedObject.GetComponent<Light>();
var light = tag.GetComponent<Light>();
light.intensity = lightIntensityParameter.Sample();
}
}

* **:green_circle: Action**: Inside the code block that intensity was previously applied, add code for sampling color from the above Parameter and applying it:
```C#
foreach (var taggedObject in taggedObjects)
foreach (var tag in tags)
var light = taggedObject.GetComponent<Light>();
var light = tag.GetComponent<Light>();
light.intensity = lightIntensityParameter.Sample();
light.color = lightColorParameter.Sample();
}

protected override void OnIterationStart()
{
var taggedObjects = tagManager.Query<MyLightRandomizerTag>();
foreach (var taggedObject in taggedObjects)
var tags = tagManager.Query<MyLightRandomizerTag>();
foreach (var tag in tags)
var light = taggedObject.GetComponent<Light>();
light.color = lightColorParameter.Sample();
var tag = taggedObject.GetComponent<MyLightRandomizerTag>();
var light = tag.GetComponent<Light>();
light.color = lightColorParameter.Sample();
tag.SetIntensity(lightIntensityParameter.Sample());
}
}

6
com.unity.perception/Runtime/Randomization/Randomizers/RandomizerExamples/Randomizers/ColorRandomizer.cs


/// </summary>
protected override void OnIterationStart()
{
var taggedObjects = tagManager.Query<ColorRandomizerTag>();
foreach (var taggedObject in taggedObjects)
var tags = tagManager.Query<ColorRandomizerTag>();
foreach (var tag in tags)
var renderer = taggedObject.GetComponent<Renderer>();
var renderer = tag.GetComponent<Renderer>();
renderer.material.SetColor(k_BaseColor, colorParameter.Sample());
}
}

6
com.unity.perception/Runtime/Randomization/Randomizers/RandomizerExamples/Randomizers/HueOffsetRandomizer.cs


/// </summary>
protected override void OnIterationStart()
{
var taggedObjects = tagManager.Query<HueOffsetRandomizerTag>();
foreach (var taggedObject in taggedObjects)
var tags = tagManager.Query<HueOffsetRandomizerTag>();
foreach (var tag in tags)
var renderer = taggedObject.GetComponent<MeshRenderer>();
var renderer = tag.GetComponent<MeshRenderer>();
renderer.material.SetFloat(k_HueOffsetShaderProperty, hueOffset.Sample());
}
}

6
com.unity.perception/Runtime/Randomization/Randomizers/RandomizerExamples/Randomizers/RotationRandomizer.cs


/// </summary>
protected override void OnIterationStart()
{
var taggedObjects = tagManager.Query<RotationRandomizerTag>();
foreach (var taggedObject in taggedObjects)
taggedObject.transform.rotation = Quaternion.Euler(rotation.Sample());
var tags = tagManager.Query<RotationRandomizerTag>();
foreach (var tag in tags)
tag.transform.rotation = Quaternion.Euler(rotation.Sample());
}
}
}

6
com.unity.perception/Runtime/Randomization/Randomizers/RandomizerExamples/Randomizers/SunAngleRandomizer.cs


/// </summary>
protected override void OnIterationStart()
{
var lightObjects = tagManager.Query<SunAngleRandomizerTag>();
foreach (var lightObject in lightObjects)
var tags = tagManager.Query<SunAngleRandomizerTag>();
foreach (var tag in tags)
{
var earthSpin = Quaternion.AngleAxis((hour.Sample() + 12f) / 24f * 360f, Vector3.down);
var timeOfYearRads = dayOfTheYear.Sample() / 365f * Mathf.PI * 2f;

lightObject.transform.rotation = Quaternion.Euler(90,0,0) * Quaternion.Inverse(lightRotation);
tag.transform.rotation = Quaternion.Euler(90,0,0) * Quaternion.Inverse(lightRotation);
}
}
}

6
com.unity.perception/Runtime/Randomization/Randomizers/RandomizerExamples/Randomizers/TextureRandomizer.cs


/// </summary>
protected override void OnIterationStart()
{
var taggedObjects = tagManager.Query<TextureRandomizerTag>();
foreach (var taggedObject in taggedObjects)
var tags = tagManager.Query<TextureRandomizerTag>();
foreach (var tag in tags)
var renderer = taggedObject.GetComponent<MeshRenderer>();
var renderer = tag.GetComponent<MeshRenderer>();
renderer.material.SetTexture(k_BaseTexture, texture.Sample());
}
}

4
com.unity.perception/Runtime/Randomization/Randomizers/RandomizerTag.cs


void Awake()
{
tagManager.AddTag(GetType(), gameObject);
tagManager.AddTag(this);
tagManager.RemoveTag(GetType(), gameObject);
tagManager.RemoveTag(this);
}
}
}

39
com.unity.perception/Runtime/Randomization/Randomizers/RandomizerTagManager.cs


namespace UnityEngine.Experimental.Perception.Randomization.Randomizers
{
/// <summary>
/// Organizes RandomizerTags attached to GameObjects in the scene
/// Organizes RandomizerTags present in the scene
/// </summary>
public class RandomizerTagManager
{

public static RandomizerTagManager singleton { get; } = new RandomizerTagManager();
Dictionary<Type, HashSet<Type>> m_TypeTree = new Dictionary<Type, HashSet<Type>>();
Dictionary<Type, HashSet<GameObject>> m_TagMap = new Dictionary<Type, HashSet<GameObject>>();
Dictionary<Type, HashSet<RandomizerTag>> m_TagMap = new Dictionary<Type, HashSet<RandomizerTag>>();
/// Enumerates all GameObjects in the scene that have a RandomizerTag of the given type
/// Enumerates over all RandomizerTags of the given type present in the scene
/// <returns>GameObjects with the given RandomizerTag</returns>
public IEnumerable<GameObject> Query<T>(bool returnSubclasses = false) where T : RandomizerTag
/// <returns>RandomizerTags of the given type</returns>
public IEnumerable<T> Query<T>(bool returnSubclasses = false) where T : RandomizerTag
{
var queriedTagType = typeof(T);
if (!m_TagMap.ContainsKey(queriedTagType))

var tagType = typeStack.Pop();
foreach (var derivedType in m_TypeTree[tagType])
typeStack.Push(derivedType);
foreach (var obj in m_TagMap[tagType])
yield return obj;
foreach (var tag in m_TagMap[tagType])
yield return (T)tag;
foreach (var obj in m_TagMap[queriedTagType])
yield return obj;
foreach (var tag in m_TagMap[queriedTagType])
yield return (T)tag;
internal void AddTag(Type tagType, GameObject obj)
internal void AddTag<T>(T tag) where T : RandomizerTag
var tagType = tag.GetType();
m_TagMap[tagType].Add(obj);
m_TagMap[tagType].Add(tag);
}
void AddTagTypeToTypeHierarchy(Type tagType)

if (tagType == null || !tagType.IsSubclassOf(typeof(RandomizerTag)))
throw new ArgumentException("Tag type is not a subclass of RandomizerTag");
m_TagMap.Add(tagType, new HashSet<GameObject>());
m_TagMap.Add(tagType, new HashSet<RandomizerTag>());
while (baseType!= null && baseType != typeof(RandomizerTag))
while (baseType != null && baseType != typeof(RandomizerTag))
m_TagMap.Add(baseType, new HashSet<GameObject>());
m_TagMap.Add(baseType, new HashSet<RandomizerTag>());
m_TypeTree[baseType] = new HashSet<Type> { tagType };
}
else

}
}
internal void RemoveTag(Type tagType, GameObject obj)
internal void RemoveTag<T>(T tag) where T : RandomizerTag
if (m_TagMap.ContainsKey(tagType) && m_TagMap[tagType].Contains(obj))
m_TagMap[tagType].Remove(obj);
var tagType = typeof(T);
if (m_TagMap.ContainsKey(tagType) && m_TagMap[tagType].Contains(tag))
m_TagMap[tagType].Remove(tag);
}
}
}

14
com.unity.perception/Tests/Runtime/Randomization/RandomizerTests/RandomizerTagTests.cs


[TestFixture]
public class RandomizerTagTests
{
public class ParentTag : RandomizerTag { }
public class ChildTag : ParentTag { }
GameObject m_TestObject;
FixedLengthScenario m_Scenario;

{
const int copyCount = 5;
var gameObject = new GameObject();
gameObject.AddComponent<ExampleTag>();
gameObject.AddComponent<ParentTag>();
gameObject2.AddComponent<ExampleTag2>();
gameObject2.AddComponent<ChildTag>();
var queriedObjects = tagManager.Query<ExampleTag>().ToArray();
var queriedObjects = tagManager.Query<ParentTag>().ToArray();
queriedObjects = tagManager.Query<ExampleTag2>().ToArray();
queriedObjects = tagManager.Query<ChildTag>().ToArray();
queriedObjects = tagManager.Query<ExampleTag>(true).ToArray();
queriedObjects = tagManager.Query<ParentTag>(true).ToArray();
Assert.AreEqual(queriedObjects.Length, copyCount * 2);
}
}

8
com.unity.perception/Tests/Runtime/Randomization/RandomizerTests/ExampleTag.cs


using UnityEngine.Experimental.Perception.Randomization.Randomizers;
namespace RandomizationTests.RandomizerTests
{
public class ExampleTag : RandomizerTag { }
public class ExampleTag2 : ExampleTag { }
}

3
com.unity.perception/Tests/Runtime/Randomization/RandomizerTests/ExampleTag.cs.meta


fileFormatVersion: 2
guid: 03b2ff8b0d794506bd887a94204d85f4
timeCreated: 1600747384
正在加载...
取消
保存