Implementation of custom blend logic should be as follows (using doubles as an example). 更多...
Implementation of custom blend logic should be as follows (using doubles as an example).
~ Generic multi-channel blending support for sequencer.
Specializing TBlendableTokenTraits for a particular input data type causes WorkingDataType to be used during the blending operation. Where WorkingDataType belongs to a namespace, ADL will be employed to discover any relevant overloads for BlendValue that match the necessary types. This allows blending of any arbitrary type into the WorkingDataType.
namespace MovieScene { Define a custom namespaced type that will be used to calculate blends between doubles struct FBlendableDouble { FBlendableDouble() : AbsoluteTotal(0.0), AdditiveTotal(0.0) {}
double AbsoluteTotal; double AdditiveTotal;
TOptional<float> TotalWeight;
double Resolve(TMovieSceneInitialValueStore<int32>& InitialValueStore) { if (TotalWeight.IsSet()) { if (TotalWeight.GetValue() == 0.f) { AbsoluteTotal = InitialValueStore.GetInitialValue(); } else { AbsoluteTotal /= TotalWeight.GetValue(); } }
return AbsoluteTotal + AdditiveTotal; } };
void BlendValue(FBlendableDouble& OutBlend, double InValue, float Weight, EMovieSceneBlendType BlendType, TMovieSceneInitialValueStore<double>& InitialValueStore) { if (BlendType == EMovieSceneBlendType::Absolute || BlendType == EMovieSceneBlendType::Relative) { if (BlendType == EMovieSceneBlendType::Relative) { OutBlend.AbsoluteTotal += (InitialValueStore.GetInitialValue() + InValue) * Weight; } else { OutBlend.AbsoluteTotal += InValue * Weight; }
OutBlend.TotalWeight = OutBlend.TotalWeight.Get(0.f) + Weight; } else if (BlendType == EMovieSceneBlendType::Additive) { OutBlend.AdditiveTotal += InValue * Weight; } } } template<> struct TBlendableTokenTraits<double> { typedef UE::MovieScene::FBlendableDouble WorkingDataType; };
Works in conjunction with TMovieSceneBlendingActuator<Type> where Type has been set up with the correct traits to allow multi-channel blending: Example:
// My struct contains 3 floats, so is represented as a TMultiChannelValue<float, 3> struct FMyStruct { float X, Y, Z; ... };
namespace MovieScene { // Marshall my struct into a multi-channel value inline void MultiChannelFromData(FMyStruct In, TMultiChannelValue<float, 3>& Out) { Out = { In.X, In.Y, In.Z }; } // Marshall my struct out of a static float array inline void ResolveChannelsToData(float (&Channels)[3], FMyStruct& Out) { Out = FMyStruct(Channels[0], Channels[1], Channels[2]); } }
// Access a unique runtime type identifier for FMyStruct. Implemented in cpp to ensure there is only ever 1. This is required to support actuators operating on FMyStructs. template<> MYMODULE_API FMovieSceneAnimTypeID GetBlendingDataType<FMyStruct>();
// Inform the blending code to use a maksed blendable of 3 floats for my struct template<> struct TBlendableTokenTraits<FMyStruct> { typedef UE::MovieScene::TMaskedBlendable<float, 3> WorkingDataType; };
// To inject a blendable token into the accumulator for an FMyStruct: UE::MovieScene::TMultiChannelValue<float, 3> AnimatedData;
// Set the X and Z channels AnimatedData.Set(0, 100.f); AnimatedData.Set(2, 250.f);
// Ensure the accumulator knows how to apply an FMyStruct (usually this will be on a property, and happen through EnsureActuator<>) FMovieSceneBlendingActuatorID ActuatorTypeID = FMyActuatorType::GetActuatorTypeID(); if (!ExecutionTokens.GetAccumulator().FindActuator<FMyStruct>(ActuatorTypeID)) { ExecutionTokens.GetAccumulator().DefineActuator(ActuatorTypeID, MakeShared<FMyActuatorType>()); }
// Add the blendable to the accumulator float Weight = EvaluateEasing(Context.GetTime()); // Constructing a TBlendableToken from any type other than TMultiChannelValue<float, 3> requires a supporting BlendValue function ExecutionTokens.BlendToken(ActuatorTypeID, TBlendableToken<FMyStruct>(AnimatedData, EMovieSceneBlendType::Absolute, Weight));