浏览代码

Reworked animation randomization to decouple it from labeler

/main
Steven Borkman 4 年前
当前提交
52b02f18
共有 12 个文件被更改,包括 152 次插入150 次删除
  1. 51
      com.unity.perception/Runtime/GroundTruth/Labelers/KeyPointLabeler.cs
  2. 3
      com.unity.perception/Runtime/Randomization/Parameters/ParameterTypes/CategorialParameters/AnimationClipParameter.cs
  3. 80
      com.unity.perception/Runtime/Randomization/Randomizers/RandomizerExamples/Randomizers/AnimationRandomizer.cs
  4. 21
      com.unity.perception/Runtime/Randomization/Randomizers/RandomizerExamples/Tags/AnimationRandomizerTag.cs
  5. 72
      com.unity.perception/Runtime/GroundTruth/Resources/AnimationRandomizerController.controller
  6. 8
      com.unity.perception/Runtime/GroundTruth/Resources/AnimationRandomizerController.controller.meta
  7. 16
      com.unity.perception/Runtime/GroundTruth/Labelers/PoseStateGroundTruthInfo.cs
  8. 3
      com.unity.perception/Runtime/GroundTruth/Labelers/PoseStateGroundTruthInfo.cs.meta
  9. 28
      com.unity.perception/Runtime/Randomization/Randomizers/RandomizerExamples/Randomizers/ConstantTransformRandomizer.cs
  10. 3
      com.unity.perception/Runtime/Randomization/Randomizers/RandomizerExamples/Randomizers/ConstantTransformRandomizer.cs.meta
  11. 14
      com.unity.perception/Runtime/Randomization/Randomizers/RandomizerExamples/Tags/ConstantTransformRandomizerTag.cs
  12. 3
      com.unity.perception/Runtime/Randomization/Randomizers/RandomizerExamples/Tags/ConstantTransformRandomizerTag.cs.meta

51
com.unity.perception/Runtime/GroundTruth/Labelers/KeyPointLabeler.cs


this.activeTemplate = template;
}
/// <summary>
/// Array of animation pose labels which map animation clip times to ground truth pose labels.
/// </summary>
public AnimationPoseLabel[] poseStateConfigs;
/// <inheritdoc/>
protected override void Setup()
{

var pt = activeTemplate.keyPoints[i];
if (pt.associateToRig)
{
var loc = ConvertToScreenSpace(animator.GetBoneTransform(pt.rigLabel).position);
keyPoints[i].index = i;
keyPoints[i].x = loc.x;
keyPoints[i].y = loc.y;
keyPoints[i].state = 1;
var bone = animator.GetBoneTransform(pt.rigLabel);
if (bone != null)
{
var loc = ConvertToScreenSpace(bone.position);
keyPoints[i].index = i;
keyPoints[i].x = loc.x;
keyPoints[i].y = loc.y;
keyPoints[i].state = 1;
}
}
}

keyPoints[idx].state = 1;
}
// check if the model has a pose state component, if so we will add that to our model, this value
// is purposely not cached, because it could be added during runtime
var poseState = labeledEntity.GetComponent<PoseStateGroundTruthInfo>();
cachedData.keyPoints.pose = poseState != null ? poseState.poseState : "unset";
cachedData.keyPoints.pose = "unset";
if (cachedData.animator != null)
{
cachedData.keyPoints.pose = GetPose(cachedData.animator);
}
}
string GetPose(Animator animator)
{
var info = animator.GetCurrentAnimatorClipInfo(0);
var clip = info[0].clip;
var timeOffset = animator.GetCurrentAnimatorStateInfo(0).normalizedTime;
if (poseStateConfigs != null)
{
foreach (var p in poseStateConfigs)
{
if (p.animationClip == clip)
{
var time = clip.length * timeOffset;
var label = p.GetPoseAtTime(time);
return label;
}
}
}
return "unset";
}
/// <inheritdoc/>

3
com.unity.perception/Runtime/Randomization/Parameters/ParameterTypes/CategorialParameters/AnimationClipParameter.cs


using System;
using UnityEngine.Perception.GroundTruth;
namespace UnityEngine.Experimental.Perception.Randomization.Parameters
{

[Serializable]
public class AnimationClipParameter : CategoricalParameter<AnimationPoseLabel> { }
public class AnimationClipParameter : CategoricalParameter<AnimationClip> { }
}

80
com.unity.perception/Runtime/Randomization/Randomizers/RandomizerExamples/Randomizers/AnimationRandomizer.cs


using System;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Animations;
using UnityEngine.Perception.GroundTruth;
using UnityEngine.Playables;
namespace UnityEngine.Experimental.Perception.Randomization.Randomizers.SampleRandomizers
{

{
FloatParameter m_FloatParameter = new FloatParameter{ value = new UniformSampler(0, 1) };
class CachedData
{
public PlayableGraph graph;
public AnimationPlayableOutput output;
public Dictionary<string, AnimationClipPlayable> playables = new Dictionary<string, AnimationClipPlayable>();
public PoseStateGroundTruthInfo poseState;
}
Dictionary<GameObject, CachedData> m_CacheMap;
/// <inheritdoc/>
protected override void OnCreate()
{
m_CacheMap = new Dictionary<GameObject, CachedData>();
}
const string clipName = "Idle";
const string stateName = "Base Layer.RandomState";
CachedData InitializeCacheData(AnimationRandomizerTag tag)
void RandomizeAnimation(AnimationRandomizerTag tag)
var cachedData = new CachedData { graph = PlayableGraph.Create() };
cachedData.graph.SetTimeUpdateMode(DirectorUpdateMode.GameTime);
cachedData.output = AnimationPlayableOutput.Create(cachedData.graph, "Animation", animator);
for (var i = 0; i < tag.animationClips.GetCategoryCount(); i++)
{
var clip = tag.animationClips.GetCategory(i);
var playable = AnimationClipPlayable.Create(cachedData.graph, clip.animationClip);
cachedData.playables[clip.animationClip.name] = playable;
}
cachedData.poseState = tag.gameObject.GetComponent<PoseStateGroundTruthInfo>();
if (cachedData.poseState == null)
{
cachedData.poseState = tag.gameObject.AddComponent<PoseStateGroundTruthInfo>();
}
return cachedData;
}
CachedData GetGraph(AnimationRandomizerTag tag)
{
if (!m_CacheMap.ContainsKey(tag.gameObject))
var overrider = tag.animatorOverrideController;
if (overrider != null)
m_CacheMap[tag.gameObject] = InitializeCacheData(tag);
overrider[clipName] = tag.animationClips.Sample();
animator.Play(stateName, 0, m_FloatParameter.Sample());
return m_CacheMap[tag.gameObject];
}
/// <inheritdoc/>

foreach (var taggedObject in taggedObjects)
{
var tag = taggedObject.GetComponent<AnimationRandomizerTag>();
var cached = GetGraph(tag);
var clips = tag.animationClips;
var animationPoseLabel = clips.Sample();
var clip = animationPoseLabel.animationClip;
var playable = cached.playables[clip.name];
cached.output.SetSourcePlayable(cached.playables[clip.name]);
var t = m_FloatParameter.Sample() * clip.length;
playable.SetTime(t);
playable.SetSpeed(0);
cached.graph.Play();
cached.poseState.poseState = animationPoseLabel.GetPoseAtTime(t);
}
}
protected override void OnScenarioComplete()
{
foreach (var cache in m_CacheMap.Values)
{
foreach (var p in cache.playables.Values)
cache.graph.DestroyPlayable(p);
cache.graph.DestroyOutput(cache.output);
cache.graph.Destroy();
RandomizeAnimation(tag);
}
}
}

21
com.unity.perception/Runtime/Randomization/Randomizers/RandomizerExamples/Tags/AnimationRandomizerTag.cs


/// </summary>
public bool applyRootMotion = false;
/// <summary>
/// Gets the animation override controller for an animation randomization. The controller is loaded from
/// resources.
/// </summary>
public AnimatorOverrideController animatorOverrideController
{
get
{
if (m_Controller == null)
{
var animator = gameObject.GetComponent<Animator>();
var runtimeAnimatorController = Resources.Load<RuntimeAnimatorController>("AnimationRandomizerController");
m_Controller = new AnimatorOverrideController(runtimeAnimatorController);
animator.runtimeAnimatorController = m_Controller;
}
return m_Controller;
}
}
AnimatorOverrideController m_Controller;
}
}

72
com.unity.perception/Runtime/GroundTruth/Resources/AnimationRandomizerController.controller


%YAML 1.1
%TAG !u! tag:unity3d.com,2011:
--- !u!91 &9100000
AnimatorController:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_Name: AnimationRandomizerController
serializedVersion: 5
m_AnimatorParameters: []
m_AnimatorLayers:
- serializedVersion: 5
m_Name: Base Layer
m_StateMachine: {fileID: 1300458365308884037}
m_Mask: {fileID: 0}
m_Motions: []
m_Behaviours: []
m_BlendingMode: 0
m_SyncedLayerIndex: -1
m_DefaultWeight: 0
m_IKPass: 0
m_SyncedLayerAffectsTiming: 0
m_Controller: {fileID: 9100000}
--- !u!1107 &1300458365308884037
AnimatorStateMachine:
serializedVersion: 6
m_ObjectHideFlags: 1
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_Name: Base Layer
m_ChildStates:
- serializedVersion: 1
m_State: {fileID: 3212544235944076811}
m_Position: {x: 390, y: 320, z: 0}
m_ChildStateMachines: []
m_AnyStateTransitions: []
m_EntryTransitions: []
m_StateMachineTransitions: {}
m_StateMachineBehaviours: []
m_AnyStatePosition: {x: 50, y: 20, z: 0}
m_EntryPosition: {x: 50, y: 120, z: 0}
m_ExitPosition: {x: 800, y: 120, z: 0}
m_ParentStateMachinePosition: {x: 800, y: 20, z: 0}
m_DefaultState: {fileID: 3212544235944076811}
--- !u!1102 &3212544235944076811
AnimatorState:
serializedVersion: 6
m_ObjectHideFlags: 1
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_Name: RandomState
m_Speed: 1
m_CycleOffset: 0
m_Transitions: []
m_StateMachineBehaviours: []
m_Position: {x: 50, y: 50, z: 0}
m_IKOnFeet: 0
m_WriteDefaultValues: 1
m_Mirror: 0
m_SpeedParameterActive: 0
m_MirrorParameterActive: 0
m_CycleOffsetParameterActive: 0
m_TimeParameterActive: 0
m_Motion: {fileID: 7400000, guid: 3f8d204fb62cf440aacbef0b85ff6858, type: 2}
m_Tag:
m_SpeedParameter:
m_MirrorParameter:
m_CycleOffsetParameter:
m_TimeParameter:

8
com.unity.perception/Runtime/GroundTruth/Resources/AnimationRandomizerController.controller.meta


fileFormatVersion: 2
guid: 492c555d00e884241a46c09b49877983
NativeFormatImporter:
externalObjects: {}
mainObjectFileID: 9100000
userData:
assetBundleName:
assetBundleVariant:

16
com.unity.perception/Runtime/GroundTruth/Labelers/PoseStateGroundTruthInfo.cs


using UnityEngine;
namespace UnityEngine.Perception.GroundTruth
{
/// <summary>
/// Pose state ground truth information that can be applied to a gameobject. The typical use case
/// is that this will be added to a labeled gameobject by a randomizer.
/// </summary>
public class PoseStateGroundTruthInfo : MonoBehaviour
{
/// <summary>
/// The pose state
/// </summary>
public string poseState;
}
}

3
com.unity.perception/Runtime/GroundTruth/Labelers/PoseStateGroundTruthInfo.cs.meta


fileFormatVersion: 2
guid: 509316760ae346d1b4cac25f103fa5a7
timeCreated: 1611613163

28
com.unity.perception/Runtime/Randomization/Randomizers/RandomizerExamples/Randomizers/ConstantTransformRandomizer.cs


using System;
using UnityEngine.Experimental.Perception.Randomization.Randomizers.SampleRandomizers.Tags;
namespace UnityEngine.Experimental.Perception.Randomization.Randomizers.SampleRandomizers
{
/// <summary>
/// Sets a gameobject tagged with <see cref="ConstantTransformRandomizerTag"/> to the same transform(location, rotation, scale)
/// each frame. This randomizer is useful to reset a scene to a start state, especially when used in conjunction
/// with other randomizers that may move a gameobject.
/// </summary>
[Serializable]
[AddRandomizerMenu("Perception/Constant Transform Randomizer")]
public class ConstantTransformRandomizer : Randomizer
{
/// <inheritdoc/>
protected override void OnIterationStart()
{
var taggedObjects = tagManager.Query<ConstantTransformRandomizerTag>();
foreach (var taggedObject in taggedObjects)
{
var tag = taggedObject.GetComponent<ConstantTransformRandomizerTag>();
taggedObject.transform.position = tag.position;
taggedObject.transform.rotation = tag.rotation;
taggedObject.transform.localScale = tag.scale;
}
}
}
}

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


fileFormatVersion: 2
guid: 6a2bb7db515c4b8bb95327b2ef6f3759
timeCreated: 1611261464

14
com.unity.perception/Runtime/Randomization/Randomizers/RandomizerExamples/Tags/ConstantTransformRandomizerTag.cs


namespace UnityEngine.Experimental.Perception.Randomization.Randomizers.SampleRandomizers.Tags
{
/// <summary>
/// Tag to indicate which gameobjects should have the effects of <see cref="ConstantTransformRandomizer"/> applied
/// to them.
/// </summary>
[AddComponentMenu("Perception/RandomizerTags/Constant Transform Randomizer Tag")]
public class ConstantTransformRandomizerTag : RandomizerTag
{
public Vector3 position = Vector3.zero;
public Quaternion rotation;
public Vector3 scale = Vector3.one;
}
}

3
com.unity.perception/Runtime/Randomization/Randomizers/RandomizerExamples/Tags/ConstantTransformRandomizerTag.cs.meta


fileFormatVersion: 2
guid: 4577b7dd026b4d69b2077ac01df61342
timeCreated: 1611261493
正在加载...
取消
保存