private static int ConvertSimpleDirectional2DBlendTree(BlendTree blendTree, Entity entity, EntityManager entityManager, BakeOptions bakeOptions)
        {
            if (!entityManager.HasComponent <BlendTree2DResource>(entity))
            {
                entityManager.AddBuffer <BlendTree2DResource>(entity);
            }

            if (!entityManager.HasComponent <BlendTree2DMotionData>(entity))
            {
                entityManager.AddBuffer <BlendTree2DMotionData>(entity);
            }

            var blendTreeResources  = entityManager.GetBuffer <BlendTree2DResource>(entity);
            var blendTreeMotionData = entityManager.GetBuffer <BlendTree2DMotionData>(entity);

            var blendTreeIndex = blendTreeResources.Length;

            blendTreeResources.Add(new BlendTree2DResource {
                MotionCount      = blendTree.children.Length,
                MotionStartIndex = blendTreeMotionData.Length
            });

            for (int i = 0; i < blendTree.children.Length; i++)
            {
                var motionData = new BlendTree2DMotionData {
                    MotionPosition = blendTree.children[i].position,
                    MotionSpeed    = blendTree.children[i].timeScale,
                };

                var clip = ClipBuilder.AnimationClipToDenseClip(blendTree.children[i].motion as AnimationClip);
                if (bakeOptions.NeedBaking)
                {
                    clip = UberClipNode.Bake(bakeOptions.RigDefinition, clip, bakeOptions.ClipConfiguration, bakeOptions.SampleRate);
                }

                motionData.Motion.Clip = clip;

                blendTreeMotionData.Add(motionData);
            }

            return(blendTreeIndex);
        }
Esempio n. 2
0
    public override void AddGraphSetupComponent(Entity entity, EntityManager dstManager, GameObjectConversionSystem conversionSystem)
    {
        var walkShortLeftClip  = WalkShortLeftClip.ToDenseClip();
        var walkLongLeftClip   = WalkLongLeftClip.ToDenseClip();
        var walkStraightClip   = WalkStraightClip.ToDenseClip();
        var walkLongRightClip  = WalkLongRightClip.ToDenseClip();
        var walkShortRightClip = WalkShortRightClip.ToDenseClip();

        var jogShortLeftClip  = JogShortLeftClip.ToDenseClip();
        var jogLongLeftClip   = JogLongLeftClip.ToDenseClip();
        var jogStraightClip   = JogStraightClip.ToDenseClip();
        var jogLongRightClip  = JogLongRightClip.ToDenseClip();
        var jogShortRightClip = JogShortRightClip.ToDenseClip();

        var clipConfiguration = new ClipConfiguration();

        clipConfiguration.Mask  = ClipConfigurationMask.LoopTime | ClipConfigurationMask.CycleRootMotion | ClipConfigurationMask.DeltaRootMotion;
        clipConfiguration.Mask |= LoopValues ? ClipConfigurationMask.LoopValues : 0;
        clipConfiguration.Mask |= BankPivot ? ClipConfigurationMask.BankPivot : 0;

        clipConfiguration.MotionID = m_MotionId;

        var graphSetup = new AnimationControllerSetup
        {
            WalkShortLeftClip  = walkShortLeftClip,
            WalkLongLeftClip   = walkLongLeftClip,
            WalkStraightClip   = walkStraightClip,
            WalkLongRightClip  = walkLongRightClip,
            WalkShortRightClip = walkShortRightClip,

            JogShortLeftClip  = jogShortLeftClip,
            JogLongLeftClip   = jogLongLeftClip,
            JogStraightClip   = jogStraightClip,
            JogLongRightClip  = jogLongRightClip,
            JogShortRightClip = jogShortRightClip,
        };

        if (Bake)
        {
            var rigDefinition = dstManager.GetComponentData <Rig>(entity);

            graphSetup.WalkShortLeftClip  = UberClipNode.Bake(rigDefinition.Value, walkShortLeftClip, clipConfiguration, SampleRate);
            graphSetup.WalkLongLeftClip   = UberClipNode.Bake(rigDefinition.Value, walkLongLeftClip, clipConfiguration, SampleRate);
            graphSetup.WalkStraightClip   = UberClipNode.Bake(rigDefinition.Value, walkStraightClip, clipConfiguration, SampleRate);
            graphSetup.WalkLongRightClip  = UberClipNode.Bake(rigDefinition.Value, walkLongRightClip, clipConfiguration, SampleRate);
            graphSetup.WalkShortRightClip = UberClipNode.Bake(rigDefinition.Value, walkShortRightClip, clipConfiguration, SampleRate);

            graphSetup.JogShortLeftClip  = UberClipNode.Bake(rigDefinition.Value, jogShortLeftClip, clipConfiguration, SampleRate);
            graphSetup.JogLongLeftClip   = UberClipNode.Bake(rigDefinition.Value, jogLongLeftClip, clipConfiguration, SampleRate);
            graphSetup.JogStraightClip   = UberClipNode.Bake(rigDefinition.Value, jogStraightClip, clipConfiguration, SampleRate);
            graphSetup.JogLongRightClip  = UberClipNode.Bake(rigDefinition.Value, jogLongRightClip, clipConfiguration, SampleRate);
            graphSetup.JogShortRightClip = UberClipNode.Bake(rigDefinition.Value, jogShortRightClip, clipConfiguration, SampleRate);

            clipConfiguration.Mask     = ClipConfigurationMask.NormalizedTime | ClipConfigurationMask.LoopTime | ClipConfigurationMask.RootMotionFromVelocity;
            clipConfiguration.MotionID = 0;
        }
        else
        {
            clipConfiguration.Mask |= ClipConfigurationMask.NormalizedTime;
        }

        graphSetup.Configuration = clipConfiguration;
        dstManager.AddComponentData(entity, graphSetup);
        dstManager.AddComponent <ProcessDefaultAnimationGraph.AnimatedRootMotion>(entity);

        dstManager.AddComponent <DeltaTime>(entity);
    }
    public void Convert(Entity entity, EntityManager dstManager, GameObjectConversionSystem conversionSystem)
    {
        if (Clips == null || BlendThresholds == null)
        {
            return;
        }

        if (!dstManager.HasComponent <Rig>(entity))
        {
            throw new System.InvalidOperationException("RigComponent must be converted before this component.");
        }

        if (Clips.Length != BlendThresholds.Length)
        {
            throw new System.InvalidOperationException("You must have the same number of clips and blendthresholds.");
        }

        var        rigComponent = GetComponent <RigComponent>() as RigComponent;
        StringHash motionId     = "";

        for (var boneIter = 0; boneIter < rigComponent.Bones.Length; boneIter++)
        {
            if (MotionJointName == rigComponent.Bones[boneIter].name)
            {
                motionId = RigGenerator.ComputeRelativePath(rigComponent.Bones[boneIter], rigComponent.transform);
            }
        }

        var rigDefinition     = dstManager.GetComponentData <Rig>(entity);
        var clipConfiguration = new ClipConfiguration();

        clipConfiguration.Mask     = ClipConfigurationMask.LoopTime | ClipConfigurationMask.LoopValues | ClipConfigurationMask.CycleRootMotion | ClipConfigurationMask.DeltaRootMotion;
        clipConfiguration.MotionID = motionId;

        var denseClips = new NativeArray <SampleClip>(Clips.Length, Allocator.Temp);
        var durations  = new NativeArray <SampleClipDuration>(Clips.Length, Allocator.Temp);
        var thresholds = new NativeArray <SampleClipBlendThreshold>(Clips.Length, Allocator.Temp);

        for (int i = 0; i < Clips.Length; i++)
        {
            conversionSystem.DeclareAssetDependency(gameObject, Clips[i]);

            denseClips[i] = new SampleClip
            {
                Clip = UberClipNode.Bake(rigDefinition.Value, Clips[i].ToDenseClip(), clipConfiguration, 60.0f)
            };
            durations[i] = new SampleClipDuration {
                Value = denseClips[i].Clip.Value.Duration
            };
            thresholds[i] = new SampleClipBlendThreshold {
                Value = BlendThresholds[i]
            };
        }

        var synchronizeMotions = dstManager.AddBuffer <SampleClip>(entity);

        synchronizeMotions.CopyFrom(denseClips);

        var synchronizeMotionDurations = dstManager.AddBuffer <SampleClipDuration>(entity);

        synchronizeMotionDurations.CopyFrom(durations);

        var synchronizeMotionThresholds = dstManager.AddBuffer <SampleClipBlendThreshold>(entity);

        synchronizeMotionThresholds.CopyFrom(thresholds);

        dstManager.AddComponentData(entity, new WeightComponent
        {
            Value = 0.0f
        });

        denseClips.Dispose();
        durations.Dispose();
        thresholds.Dispose();
    }
Esempio n. 4
0
    public override void AddGraphSetupComponent(Entity entity, EntityManager dstManager, GameObjectConversionSystem conversionSystem)
    {
        var walkShortLeftClip  = ClipBuilder.AnimationClipToDenseClip(WalkShortLeftClip);
        var walkLongLeftClip   = ClipBuilder.AnimationClipToDenseClip(WalkLongLeftClip);
        var walkStraightClip   = ClipBuilder.AnimationClipToDenseClip(WalkStraightClip);
        var walkLongRightClip  = ClipBuilder.AnimationClipToDenseClip(WalkLongRightClip);
        var walkShortRightClip = ClipBuilder.AnimationClipToDenseClip(WalkShortRightClip);

        var jogShortLeftClip  = ClipBuilder.AnimationClipToDenseClip(JogShortLeftClip);
        var jogLongLeftClip   = ClipBuilder.AnimationClipToDenseClip(JogLongLeftClip);
        var jogStraightClip   = ClipBuilder.AnimationClipToDenseClip(JogStraightClip);
        var jogLongRightClip  = ClipBuilder.AnimationClipToDenseClip(JogLongRightClip);
        var jogShortRightClip = ClipBuilder.AnimationClipToDenseClip(JogShortRightClip);

        var clipConfiguration = new ClipConfiguration();

        clipConfiguration.Mask  = 0;
        clipConfiguration.Mask |= (int)ClipConfigurationMask.LoopTime;
        clipConfiguration.Mask |= LoopValues ? (int)ClipConfigurationMask.LoopValues : 0;
        clipConfiguration.Mask |= (int)ClipConfigurationMask.CycleRootMotion;
        clipConfiguration.Mask |= (int)ClipConfigurationMask.DeltaRootMotion;
        clipConfiguration.Mask |= BankPivot ? (int)ClipConfigurationMask.BankPivot : 0;

        clipConfiguration.MotionID = m_MotionId;

        var graphSetup = new AnimationControllerSetup
        {
            WalkShortLeftClip  = walkShortLeftClip,
            WalkLongLeftClip   = walkLongLeftClip,
            WalkStraightClip   = walkStraightClip,
            WalkLongRightClip  = walkLongRightClip,
            WalkShortRightClip = walkShortRightClip,

            JogShortLeftClip  = jogShortLeftClip,
            JogLongLeftClip   = jogLongLeftClip,
            JogStraightClip   = jogStraightClip,
            JogLongRightClip  = jogLongRightClip,
            JogShortRightClip = jogShortRightClip,
        };

        if (Bake)
        {
            var rigDefinition = dstManager.GetComponentData <RigDefinitionSetup>(entity);

            var walkShortLeftClipInstance  = ClipInstance.Create(rigDefinition.Value, walkShortLeftClip);
            var walkLongLeftClipInstance   = ClipInstance.Create(rigDefinition.Value, walkLongLeftClip);
            var walkStraightClipInstance   = ClipInstance.Create(rigDefinition.Value, walkStraightClip);
            var walkLongRightClipInstance  = ClipInstance.Create(rigDefinition.Value, walkLongRightClip);
            var walkShortRightClipInstance = ClipInstance.Create(rigDefinition.Value, walkShortRightClip);

            var jogShortLeftClipInstance  = ClipInstance.Create(rigDefinition.Value, jogShortLeftClip);
            var jogLongLeftClipInstance   = ClipInstance.Create(rigDefinition.Value, jogLongLeftClip);
            var jogStraightClipInstance   = ClipInstance.Create(rigDefinition.Value, jogStraightClip);
            var jogLongRightClipInstance  = ClipInstance.Create(rigDefinition.Value, jogLongRightClip);
            var jogShortRightClipInstance = ClipInstance.Create(rigDefinition.Value, jogShortRightClip);

            graphSetup.WalkShortLeftClip  = UberClipNode.Bake(walkShortLeftClipInstance, clipConfiguration, SampleRate);
            graphSetup.WalkLongLeftClip   = UberClipNode.Bake(walkLongLeftClipInstance, clipConfiguration, SampleRate);
            graphSetup.WalkStraightClip   = UberClipNode.Bake(walkStraightClipInstance, clipConfiguration, SampleRate);
            graphSetup.WalkLongRightClip  = UberClipNode.Bake(walkLongRightClipInstance, clipConfiguration, SampleRate);
            graphSetup.WalkShortRightClip = UberClipNode.Bake(walkShortRightClipInstance, clipConfiguration, SampleRate);

            graphSetup.JogShortLeftClip  = UberClipNode.Bake(jogShortLeftClipInstance, clipConfiguration, SampleRate);
            graphSetup.JogLongLeftClip   = UberClipNode.Bake(jogLongLeftClipInstance, clipConfiguration, SampleRate);
            graphSetup.JogStraightClip   = UberClipNode.Bake(jogStraightClipInstance, clipConfiguration, SampleRate);
            graphSetup.JogLongRightClip  = UberClipNode.Bake(jogLongRightClipInstance, clipConfiguration, SampleRate);
            graphSetup.JogShortRightClip = UberClipNode.Bake(jogShortRightClipInstance, clipConfiguration, SampleRate);

            walkShortLeftClipInstance.Dispose();
            walkLongLeftClipInstance.Dispose();
            walkStraightClipInstance.Dispose();
            walkLongRightClipInstance.Dispose();
            walkShortRightClipInstance.Dispose();

            jogShortLeftClipInstance.Dispose();
            jogLongLeftClipInstance.Dispose();
            jogStraightClipInstance.Dispose();
            jogLongRightClipInstance.Dispose();
            jogShortRightClipInstance.Dispose();

            clipConfiguration.Mask     = 0;
            clipConfiguration.Mask    |= (int)ClipConfigurationMask.NormalizedTime;
            clipConfiguration.Mask    |= (int)ClipConfigurationMask.LoopTime;
            clipConfiguration.Mask    |= (int)ClipConfigurationMask.RootMotionFromVelocity;
            clipConfiguration.MotionID = 0;
        }
        else
        {
            clipConfiguration.Mask |= (int)ClipConfigurationMask.NormalizedTime;
        }

        graphSetup.Configuration = clipConfiguration;

        dstManager.AddComponentData(entity, graphSetup);
    }