Пример #1
0
    private IEnumerator Sit()
    {
        Controller.Signal signal      = Controller.GetSignal("Sit");
        Interaction       interaction = Controller.ProjectionInteraction != null ? Controller.ProjectionInteraction : Controller.GetClosestInteraction(transform);

        float threshold = 0.25f;

        if (interaction != null)
        {
            Controller.ActiveInteraction = interaction;
            IsInteracting = true;
            while (signal.Query())
            {
                ApplyStaticGoal(interaction.GetContact("Hips").GetPosition(), interaction.GetContact("Hips").GetForward(), Signals);
                Geometry.Setup(Geometry.Resolution);
                Geometry.Sense(interaction.GetCenter(), LayerMask.GetMask("Interaction"), interaction.GetExtents(), InteractionSmoothing);
                yield return(new WaitForSeconds(0f));
            }
            while (StyleSeries.GetStyle(TimeSeries.Pivot, "Sit") > threshold)
            {
                ApplyDynamicGoal(
                    RootSeries.Transformations[TimeSeries.Pivot],
                    Controller.QueryMove(KeyCode.W, KeyCode.S, KeyCode.A, KeyCode.D, Signals),
                    Controller.QueryTurn(KeyCode.Q, KeyCode.E, 90f),
                    Signals
                    );
                Geometry.Setup(Geometry.Resolution);
                Geometry.Sense(interaction.GetCenter(), LayerMask.GetMask("Interaction"), interaction.GetExtents(), InteractionSmoothing);
                yield return(new WaitForSeconds(0f));
            }
            IsInteracting = false;
            Controller.ActiveInteraction = null;
        }
    }
Пример #2
0
    private IEnumerator Open()
    {
        Controller.Signal signal      = Controller.GetSignal("Open");
        Interaction       interaction = Controller.ProjectionInteraction != null ? Controller.ProjectionInteraction : Controller.GetClosestInteraction(transform);

        if (interaction != null)
        {
            Controller.ActiveInteraction = interaction;
            IsInteracting = true;
            while (signal.Query())
            {
                ApplyStaticGoal(interaction.GetCenter().GetPosition(), interaction.GetCenter().GetForward(), Signals);
                Geometry.Setup(Geometry.Resolution);
                Geometry.Sense(interaction.GetCenter(), LayerMask.GetMask("Interaction"), interaction.GetExtents(), InteractionSmoothing);
                yield return(new WaitForSeconds(0f));
            }
            IsInteracting = false;
            Controller.ActiveInteraction = null;
        }
    }
Пример #3
0
    protected override void Setup()
    {
        Controller = new Controller();
        Controller.Signal idle = Controller.AddSignal("Idle");
        idle.Default  = true;
        idle.Velocity = 0f;
        idle.AddKey(KeyCode.W, false);
        idle.AddKey(KeyCode.A, false);
        idle.AddKey(KeyCode.S, false);
        idle.AddKey(KeyCode.D, false);
        idle.AddKey(KeyCode.Q, false);
        idle.AddKey(KeyCode.E, false);
        idle.AddKey(KeyCode.V, true);
        idle.UserControl    = 0.25f;
        idle.NetworkControl = 0.1f;

        Controller.Signal walk = Controller.AddSignal("Walk");
        walk.AddKey(KeyCode.W, true);
        walk.AddKey(KeyCode.A, true);
        walk.AddKey(KeyCode.S, true);
        walk.AddKey(KeyCode.D, true);
        walk.AddKey(KeyCode.Q, true);
        walk.AddKey(KeyCode.E, true);
        walk.AddKey(KeyCode.LeftShift, false);
        walk.AddKey(KeyCode.C, false);
        walk.Velocity       = 1f;
        walk.UserControl    = 0.25f;
        walk.NetworkControl = 0.25f;

        Controller.Signal run = Controller.AddSignal("Run");
        run.AddKey(KeyCode.LeftShift, true);
        run.Velocity       = 3f;
        run.UserControl    = 0.25f;
        run.NetworkControl = 0.25f;

        Controller.Signal carry = Controller.AddSignal("Carry");
        carry.AddKey(KeyCode.V, true);
        carry.Velocity       = 0f;
        carry.UserControl    = 0.1f;
        carry.NetworkControl = 0f;

        Controller.Signal open = Controller.AddSignal("Open");
        open.AddKey(KeyCode.F, true);
        open.Velocity       = 0f;
        open.UserControl    = 0.1f;
        open.NetworkControl = 0f;

        Controller.Signal sit = Controller.AddSignal("Sit");
        sit.AddKey(KeyCode.C, true);
        sit.Velocity       = 0f;
        sit.UserControl    = 0.25f;
        sit.NetworkControl = 0f;

        Environment = new CylinderMap(4f, 9, 9, true);
        Geometry    = new CuboidMap(new Vector3Int(8, 8, 8));

        TimeSeries    = new TimeSeries(6, 6, 1f, 1f, 5);
        RootSeries    = new TimeSeries.Root(TimeSeries);
        StyleSeries   = new TimeSeries.Style(TimeSeries, "Idle", "Walk", "Run", "Carry", "Open", "Sit", "Climb");
        GoalSeries    = new TimeSeries.Goal(TimeSeries, Controller.GetSignalNames());
        ContactSeries = new TimeSeries.Contact(TimeSeries, "Hips", "RightWrist", "LeftWrist", "RightAnkle", "LeftAnkle");
        PhaseSeries   = new TimeSeries.Phase(TimeSeries);
        for (int i = 0; i < TimeSeries.Samples.Length; i++)
        {
            RootSeries.Transformations[i] = transform.GetWorldMatrix(true);
            if (StyleSeries.Styles.Length > 0)
            {
                StyleSeries.Values[i][0] = 1f;
            }
            if (GoalSeries.Actions.Length > 0)
            {
                GoalSeries.Values[i][0] = 1f;
            }
            GoalSeries.Transformations[i] = transform.GetWorldMatrix(true);
            PhaseSeries.Values[i]         = Mathf.Repeat((float)i / GetFramerate(), 1f);
            Geometry.Pivot         = transform.GetWorldMatrix(true);
            Geometry.References[i] = transform.position;
        }

        PosePrediction = new Vector3[Actor.Bones.Length];
        RootPrediction = new Matrix4x4[7];
        GoalPrediction = new Matrix4x4[7];

        RightFootIK = UltimateIK.BuildModel(Actor.FindTransform("RightHip"), Actor.GetBoneTransforms(ContactSeries.Bones[3]));
        LeftFootIK  = UltimateIK.BuildModel(Actor.FindTransform("LeftHip"), Actor.GetBoneTransforms(ContactSeries.Bones[4]));
    }
Пример #4
0
    private IEnumerator Carry()
    {
        Controller.Signal signal      = Controller.GetSignal("Carry");
        Interaction       interaction = Controller.ProjectionInteraction != null ? Controller.ProjectionInteraction : Controller.GetClosestInteraction(transform);

        if (interaction != null)
        {
            Controller.ActiveInteraction = interaction;
            // Debug.Log("Carrying started...");
            IsInteracting = true;

            float duration  = 0.5f;
            float threshold = 0.2f;

            Vector3    deltaPos = new Vector3(0f, -0.15f, 0.2f);
            Quaternion deltaRot = Quaternion.Euler(-30f, 0f, 0f);

            float height = 1f;

            Matrix4x4 GetObjectMatrix(float staticness)
            {
                Matrix4x4 right = Actor.GetBoneTransformation("RightWrist");
                Matrix4x4 left  = Actor.GetBoneTransformation("LeftWrist");

                Quaternion rotation = Quaternion.Slerp(
                    Quaternion.LookRotation(left.GetRight(), left.GetForward()),
                    Quaternion.LookRotation(-right.GetRight(), right.GetForward()),
                    0.5f
                    );

                rotation *= deltaRot;
                rotation  = Quaternion.Slerp(
                    rotation,
                    transform.rotation,
                    Utility.Normalise(staticness, 0f, 1f, 0.5f, 1f)
                    );

                return(Matrix4x4.TRS(
                           Vector3.Lerp(left.GetPosition(), right.GetPosition(), 0.5f) + rotation * deltaPos,
                           rotation,
                           interaction.transform.lossyScale
                           ));
            }

            bool HasContact()
            {
                float left  = ContactSeries.GetContact(TimeSeries.Pivot, "LeftWrist");
                float right = ContactSeries.GetContact(TimeSeries.Pivot, "RightWrist");

                return(right > 0.5f && left > 0.5f || (left + right) > 1f);
            }

            //Move to the target location
            // Debug.Log("Approaching to lift object...");
            while (signal.Query())
            {
                ApplyStaticGoal(interaction.GetCenter().GetPosition(), interaction.GetCenter().GetForward(), Controller.PoolSignals());
                Geometry.Setup(Geometry.Resolution);
                Geometry.Sense(interaction.GetCenter(), LayerMask.GetMask("Interaction"), interaction.GetExtents(), InteractionSmoothing);
                Geometry.Retransform(interaction.GetOrigin(GoalSeries.Transformations[TimeSeries.Pivot]));
                if (Vector3.Distance(GetObjectMatrix(0f).GetPosition(), interaction.transform.position) < threshold)
                {
                    break;
                }
                yield return(new WaitForSeconds(0f));
            }

            //Move the object from the surface to the hands
            // Debug.Log("Picking object...");
            float      tPick = Time.time;
            Vector3    pos   = interaction.transform.position;
            Quaternion rot   = interaction.transform.rotation;
            while (signal.Query() && HasContact())
            {
                float     ratio = Mathf.Clamp((Time.time - tPick) / duration, 0f, 1f);
                Matrix4x4 m     = GetObjectMatrix(1f - ratio);
                interaction.transform.position = Vector3.Lerp(pos, m.GetPosition(), ratio);
                interaction.transform.rotation = Quaternion.Slerp(rot, m.GetRotation(), ratio);
                ApplyStaticGoal(interaction.GetCenter().GetPosition(), interaction.GetCenter().GetForward(), Controller.PoolSignals());
                Geometry.Setup(Geometry.Resolution);
                Geometry.Sense(interaction.GetCenter(), LayerMask.GetMask("Interaction"), interaction.GetExtents(), InteractionSmoothing);
                Geometry.Retransform(interaction.GetOrigin(GoalSeries.Transformations[TimeSeries.Pivot]));
                if (ratio >= 1f)
                {
                    break;
                }
                yield return(new WaitForSeconds(0f));
            }

            //Move around with the object
            // Debug.Log("Carrying object and moving...");
            while (signal.Query() && HasContact())
            {
                Matrix4x4 m = GetObjectMatrix(0f);
                interaction.transform.position = m.GetPosition();
                interaction.transform.rotation = m.GetRotation();
                ApplyDynamicGoal(
                    interaction.GetCenter(),
                    Controller.QueryMove(KeyCode.W, KeyCode.S, KeyCode.A, KeyCode.D, Signals) + new Vector3(0f, height - interaction.transform.position.y, 0f),
                    Controller.QueryTurn(KeyCode.Q, KeyCode.E, 90f),
                    Signals
                    );
                Geometry.Setup(Geometry.Resolution);
                Geometry.Sense(interaction.GetCenter(), LayerMask.GetMask("Interaction"), interaction.GetExtents(), InteractionSmoothing);
                Geometry.Retransform(interaction.GetOrigin(GoalSeries.Transformations[TimeSeries.Pivot]));
                yield return(new WaitForSeconds(0f));
            }

            //Perform motions to start placing the object
            // Debug.Log("Transitioning to placing down object...");
            while (HasContact())
            {
                Matrix4x4 m = GetObjectMatrix(0f);
                interaction.transform.position = m.GetPosition();
                interaction.transform.rotation = m.GetRotation();
                Geometry.Setup(Geometry.Resolution);
                Geometry.Sense(interaction.GetCenter(), LayerMask.GetMask("Interaction"), interaction.GetExtents(), InteractionSmoothing);
                Geometry.Retransform(interaction.GetOrigin(GoalSeries.Transformations[TimeSeries.Pivot]));
                if (Controller.ProjectionActive)
                {
                    ApplyStaticGoal(Controller.Projection.point, Vector3.ProjectOnPlane(Controller.Projection.point - transform.position, Vector3.up).normalized, Signals);
                    if (Vector3.Distance(Controller.Projection.point, interaction.transform.position) < threshold)
                    {
                        break;
                    }
                }
                else
                {
                    Vector3 surface = Utility.ProjectGround(interaction.transform.position, LayerMask.GetMask("Default", "Ground"));
                    ApplyStaticGoal(surface, Vector3.ProjectOnPlane(m.GetForward(), Vector3.up).normalized, Signals);
                    if (Vector3.Distance(surface, interaction.transform.position) < threshold)
                    {
                        break;
                    }
                }
                yield return(new WaitForSeconds(0f));
            }

            //Make sure the object is again placed on the surface
            // Debug.Log("Placing object...");
            float      tPlace    = Time.time;
            Vector3    aPosPlace = interaction.transform.position;
            Vector3    bPosPlace = Utility.ProjectGround(aPosPlace, LayerMask.GetMask("Default", "Ground"));
            Quaternion aRotPlace = interaction.transform.rotation;
            Quaternion bRotPlace = Quaternion.LookRotation(Vector3.ProjectOnPlane(aRotPlace.GetForward(), Vector3.up), Vector3.up);
            while (true)
            {
                float ratio = Mathf.Clamp((Time.time - tPlace) / duration, 0f, 1f);
                interaction.transform.position = Vector3.Lerp(aPosPlace, bPosPlace, ratio);
                interaction.transform.rotation = Quaternion.Slerp(aRotPlace, bRotPlace, ratio);
                Geometry.Setup(Geometry.Resolution);
                Geometry.Sense(interaction.GetCenter(), LayerMask.GetMask("Interaction"), interaction.GetExtents(), InteractionSmoothing);
                Geometry.Retransform(interaction.GetOrigin(GoalSeries.Transformations[TimeSeries.Pivot]));
                ApplyStaticGoal(transform.position, transform.forward, Signals);
                if (ratio >= 1f)
                {
                    break;
                }
                yield return(new WaitForSeconds(0f));
            }

            while (StyleSeries.GetStyle(TimeSeries.Pivot, "Carry") > 0.1)
            {
                ApplyDynamicGoal(
                    RootSeries.Transformations[TimeSeries.Pivot],
                    Controller.QueryMove(KeyCode.W, KeyCode.S, KeyCode.A, KeyCode.D, Signals),
                    Controller.QueryTurn(KeyCode.Q, KeyCode.E, 90f),
                    Signals
                    );
                Geometry.Setup(Geometry.Resolution);
                Geometry.Sense(interaction.GetCenter(), LayerMask.GetMask("Interaction"), interaction.GetExtents(), InteractionSmoothing);
                yield return(new WaitForSeconds(0f));
            }

            IsInteracting = false;
            // Debug.Log("Carrying finished...");
            Controller.ActiveInteraction = null;
        }
    }