Esempio n. 1
0
    public override Playable CreatePlayable(PlayableGraph graph, GameObject owner)
    {
        var playable = ScriptPlayable <TransformScaleTweenBehaviour> .Create(graph, template);

        TransformScaleTweenBehaviour 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;
        Vector3    defaultScale    = trackBinding.localScale;

        int inputCount = playable.GetInputCount();

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

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

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

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

            float inputWeight = playable.GetInputWeight(i);

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

            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);
            }

            if (input.tweenScale)
            {
                scaleTotalWeight += inputWeight;

                blendedScale += Vector3.Lerp(input.startingScale, input.endLocation.localScale, tweenProgress) * inputWeight;
            }
        }

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

        blendedRotation = AddQuaternions(blendedRotation, weightedDefaultRotation);
        blendedScale   += defaultScale * (1f - scaleTotalWeight);

        trackBinding.position   = blendedPosition;
        trackBinding.rotation   = blendedRotation;
        trackBinding.localScale = blendedScale - Vector3.one;

        m_FirstFrameHappened = true;
    }