public override Playable CreatePlayable(PlayableGraph graph, GameObject owner)
    {
        var playable = ScriptPlayable <TransformTweenBehaviour> .Create(graph, template);

        TransformTweenBehaviour clone = playable.GetBehaviour();

        clone.startLocation = startLocation.Resolve(graph.GetResolver());
        clone.endLocation   = endLocation.Resolve(graph.GetResolver());
        return(playable);
    }
    public override void ProcessFrame(Playable playable, FrameData info, object playerData)
    {
        Transform trackBinding = playerData as Transform;

        if (trackBinding == null)
        {
            return;
        }

        Vector3    defaultPosition = trackBinding.position;
        Quaternion defaultRotation = trackBinding.rotation;

        int inputCount = playable.GetInputCount();

        float positionTotalWeight = 0f;
        float rotationTotalWeight = 0f;

        Vector3    blendedPosition = Vector3.zero;
        Quaternion blendedRotation = new Quaternion(0f, 0f, 0f, 0f);

        for (int i = 0; i < inputCount; i++)
        {
            ScriptPlayable <TransformTweenBehaviour> playableInput = (ScriptPlayable <TransformTweenBehaviour>)playable.GetInput(i);
            TransformTweenBehaviour input = playableInput.GetBehaviour();

            if (input.endLocation == null)
            {
                continue;
            }

            float inputWeight = playable.GetInputWeight(i);

            if (!m_FirstFrameHappened && !input.startLocation)
            {
                input.startingPosition = defaultPosition;
                input.startingRotation = defaultRotation;
            }

            float normalisedTime = (float)(playableInput.GetTime() / playableInput.GetDuration());
            float tweenProgress  = input.EvaluateCurrentCurve(normalisedTime);

            if (input.tweenPosition)
            {
                positionTotalWeight += inputWeight;

                blendedPosition += Vector3.Lerp(input.startingPosition, input.endLocation.position, tweenProgress) * inputWeight;
            }

            if (input.tweenRotation)
            {
                rotationTotalWeight += inputWeight;

                Quaternion desiredRotation = Quaternion.Lerp(input.startingRotation, input.endLocation.rotation, tweenProgress);
                desiredRotation = NormalizeQuaternion(desiredRotation);

                if (Quaternion.Dot(blendedRotation, desiredRotation) < 0f)
                {
                    desiredRotation = ScaleQuaternion(desiredRotation, -1f);
                }

                desiredRotation = ScaleQuaternion(desiredRotation, inputWeight);

                blendedRotation = AddQuaternions(blendedRotation, desiredRotation);
            }
        }

        blendedPosition += defaultPosition * (1f - positionTotalWeight);
        Quaternion weightedDefaultRotation = ScaleQuaternion(defaultRotation, 1f - rotationTotalWeight);

        blendedRotation = AddQuaternions(blendedRotation, weightedDefaultRotation);

        trackBinding.position = blendedPosition;
        trackBinding.rotation = blendedRotation;

        m_FirstFrameHappened = true;
    }
Пример #3
0
 private void Start()
 {
     tweenBehaviour = GetComponent <TransformTweenBehaviour>();
 }