Files
UnrealEngineUWP/Engine/Source/Runtime/AnimGraphRuntime/Private/AnimNodes/AnimNode_RandomPlayer.cpp
Marc Audy 3c0e962858 Copying //UE4/Dev-Frame to //UE4/Main
#lockdown Nick.Penwarden

==========================
MAJOR FEATURES + CHANGES
==========================

Change 2775736 on 2015/11/20 by Richard.Hinckley

	Fix for Paper2D issue with repeated imports in one edutor session. Paper2D import process now creates a new importer at the end. This prevents the sprite sheet import process from leaving frame data around, causing subsequent imports (including imports of different sprite sheets) to include this data inappropriately.

	#codereview michael.noland

Change 2776352 on 2015/11/20 by Zak.Middleton

	#ue4 - Avoid useless DetachFromParent() for the same pending AttachParent during registration. Added missing UpdateOverlaps() when detaching from object simulating physics.

	#rb Marc.Audy, Ori.Cohen
	#codereview James.Golding

Change 2776401 on 2015/11/20 by Mieszko.Zielinski

	Implemented a way to do batched points projection to navmesh, where every point can declare a custom projection box #UE4

	The biggest advantage here is that projection box is independent from projected point - no more manual offsetting of projected point to achieve "100uu up and 500uu down"-like functionality

	#jira UE-23705
	#rb Lukasz.Furman

Change 2777450 on 2015/11/23 by Martin.Wilson

	Bake additive data into animations during cooking to avoid doing additive calculations and extra pose extraction and blending at runtime

	#rb Thomas.Sarkanen

Change 2777698 on 2015/11/23 by Mieszko.Zielinski

	Gameplay debugging tools fixes #UE4

	Fixes:
	- made newly added logs respect Log Visualizer's filters
	- added handling of invalid data when trying to draw EGameplayDebuggerShapeElement::Cylinder in AGameplayDebuggingHUDComponent::DrawPerception. This is a patch, root cause to be found.
	- fixed Log Visualizer resetting it's data while trying to serialize invalid objects. This is a patch, root cause to be addressed.

	In addition
	- while at it removed bunch of 'auto' and 'class' keywords from the files I've touched

	#rb Lukasz.Furman

Change 2777762 on 2015/11/23 by Mieszko.Zielinski

	Removed BlackboardComponent's functionality deprecated since 4.7 #UE4

	#rb Lukasz.Furman

Change 2777839 on 2015/11/23 by Zak.Middleton

	#ue4 - Wrap all vector library calls to math functions through our FMath versions, so they benefit from fixes or improvements therein. Added Exp2() function.

	#rb Laurent.Delayen

Change 2777840 on 2015/11/23 by Zak.Middleton

	#ue4 - Fix up uses of library math functions to go through our FMath namespace.

	#rb Laurent.Delayen

Change 2778287 on 2015/11/23 by Stan.Melax

	deprecation of  FCollisionQueryParams(bool)

	See 2774707 description for the whole story

	#OR-9936

	#codereview marc.audy

	Changes to kite will have to be in a separate check-in

	I couldn't submit to all files from the framework branch   addition fixes have their files are shelved in cl 2778299

Change 2778507 on 2015/11/23 by Marc.Audy

	Eliminate spurious cook warnings for known missing packages
	#rb Michael.Noland

Change 2778546 on 2015/11/23 by Aaron.McLeran

	Moving occlusion feature settings from audio component to sound attenuation settings struct.

	- Sound attenuation setting struct is used for all sounds that do 3d spatialization so it make sense for the occlusion feature settings to be there.
	- Kept old low-pass frequency filter setting values on audio component (where HighFrequencyAttenuation used to be)

	#rb Zak.Middleton

Change 2778664 on 2015/11/23 by Zak.Middleton

	#ue4 - Clarify some comparison functions (IsZero, IsNearlyZero, Equals) in FRotator to explain that they compare as orientations, not other interpretations such as rotational speed, winding, etc.

	#rb Aaron.Mcleran
	#codereview Frank.Gigliotti

Change 2779335 on 2015/11/24 by Mieszko.Zielinski

	Another VisualLog patch to avoid crashing due to a core bug that remains to be investigated #UE4

	Again, the core bug here is related visual log trying to serialize invalid objects on a regular basis.

	#rb Lukasz.Furman

Change 2779338 on 2015/11/24 by Benn.Gallagher

	Fixed crash in Persona when focus is taken from a different window
	#jira UE-22516
	#rb Ben.Cosh

Change 2779375 on 2015/11/24 by Benn.Gallagher

	Fix for deadlock in destructibles. Aquiring actor buffer without releasing causes an infinite wait on next aquire.
	#rb Ori.Cohen

Change 2779753 on 2015/11/24 by Zak.Middleton

	#ue4 - FMath::Atan2() no longer calls atan2f() because of some compiler or library bugs causing it to randomly return NaN for valid input. It now uses a high-precision minimax approximation instead, measured to be 2x faster than the stock C version.

	#rb Brian.Karis

Change 2779853 on 2015/11/24 by Marc.Audy
2015-12-02 16:42:06 -05:00

318 lines
9.8 KiB
C++

// Copyright 1998-2015 Epic Games, Inc. All Rights Reserved.
#include "AnimGraphRuntimePrivatePCH.h"
#include "AnimNode_RandomPlayer.h"
#include "AnimInstanceProxy.h"
FAnimNode_RandomPlayer::FAnimNode_RandomPlayer()
: CurrentEntry(INDEX_NONE)
, NextEntry(INDEX_NONE)
, CurrentDataIndex(0)
{
}
void FAnimNode_RandomPlayer::Initialize(const FAnimationInitializeContext& Context)
{
FAnimNode_Base::Initialize(Context);
EvaluateGraphExposedInputs.Execute(Context);
const int32 NumEntries = Entries.Num();
if(NumEntries == 0)
{
// early out here, no need to do anything at all if we're not playing anything
return;
}
NormalizedPlayChances.Empty(NormalizedPlayChances.Num());
NormalizedPlayChances.AddUninitialized(NumEntries);
// Initialize normalized play chance for each entry and validate entry data
float SumChances = 0.0f;
for(FRandomPlayerSequenceEntry& Entry : Entries)
{
SumChances += Entry.ChanceToPlay;
if(Entry.MaxLoopCount < Entry.MinLoopCount)
{
Swap(Entry.MaxLoopCount, Entry.MinLoopCount);
}
if(Entry.MaxPlayRate < Entry.MinPlayRate)
{
Swap(Entry.MaxLoopCount, Entry.MinLoopCount);
}
}
for(int32 Idx = 0 ; Idx < NumEntries ; ++Idx)
{
NormalizedPlayChances[Idx] = Entries[Idx].ChanceToPlay / SumChances;
}
// Initialize random stream and pick first entry
RandomStream.Initialize(FPlatformTime::Cycles());
CurrentEntry = GetNextEntryIndex();
NextEntry = GetNextEntryIndex();
PlayData.Empty(2);
PlayData.AddDefaulted(2);
FRandomAnimPlayData& CurrentData = PlayData[GetDataIndex(ERandomDataIndexType::Current)];
FRandomAnimPlayData& NextData = PlayData[GetDataIndex(ERandomDataIndexType::Next)];
// Init play data
CurrentData.BlendWeight = 1.0f;
CurrentData.PlayRate = RandomStream.FRandRange(Entries[CurrentEntry].MinPlayRate, Entries[CurrentEntry].MaxPlayRate);
CurrentData.RemainingLoops = FMath::Clamp(RandomStream.RandRange(Entries[CurrentEntry].MinLoopCount, Entries[CurrentEntry].MaxLoopCount), 0, MAX_int32);
NextData.BlendWeight = 0.0f;
NextData.PlayRate = RandomStream.FRandRange(Entries[NextEntry].MinPlayRate, Entries[NextEntry].MaxPlayRate);
NextData.RemainingLoops = FMath::Clamp(RandomStream.RandRange(Entries[NextEntry].MinLoopCount, Entries[NextEntry].MaxLoopCount), 0, MAX_int32);
}
void FAnimNode_RandomPlayer::Update(const FAnimationUpdateContext& Context)
{
EvaluateGraphExposedInputs.Execute(Context);
FRandomAnimPlayData* CurrentData = &PlayData[GetDataIndex(ERandomDataIndexType::Current)];
FRandomAnimPlayData* NextData = &PlayData[GetDataIndex(ERandomDataIndexType::Next)];
if(UAnimSequence* CurrentSequence = Entries[CurrentEntry].Sequence)
{
float TimeRemaining = CurrentSequence->SequenceLength - CurrentData->InternalTimeAccumulator;
if(CurrentData->InternalTimeAccumulator < CurrentData->PreviousTimeAccumulator)
{
// We've looped, update remaining
--CurrentData->RemainingLoops;
// Special case for same entry - we don't do any blending so need to switch the data over here
if(CurrentData->RemainingLoops < 0 && CurrentEntry == NextEntry)
{
// Need to switch to the next anim, but first put our accumulator in the next data we're about to switch
// to so we don't see a pop
NextData->InternalTimeAccumulator = CurrentData->InternalTimeAccumulator;
SwitchNextToCurrent();
// Re-get data as we've switched over
CurrentData = &PlayData[GetDataIndex(ERandomDataIndexType::Current)];
NextData = &PlayData[GetDataIndex(ERandomDataIndexType::Next)];
}
}
// Cache time to detect loops
CurrentData->PreviousTimeAccumulator = CurrentData->InternalTimeAccumulator;
NextData->PreviousTimeAccumulator = NextData->InternalTimeAccumulator;
// If we're in the blend window start blending, but only if we're moving to a new animation,
// otherwise just keep looping.
const bool bInCrossfadeTime = TimeRemaining <= Entries[NextEntry].BlendIn.GetBlendTime();
const bool bNextAnimIsDifferent = NextEntry != CurrentEntry;
const bool bNeedMoreLoops = CurrentData->RemainingLoops > 0;
if(bInCrossfadeTime && !bNeedMoreLoops)
{
if(bNextAnimIsDifferent)
{
// Blending to next
Entries[NextEntry].BlendIn.Update(Context.GetDeltaTime());
float BlendedAlpha = Entries[NextEntry].BlendIn.GetBlendedValue();
if(BlendedAlpha < 1.0f)
{
NextData->BlendWeight = BlendedAlpha;
CurrentData->BlendWeight = 1.0f - BlendedAlpha;
}
}
}
// If we were blending but now we're done, switch play data
if(Entries[NextEntry].BlendIn.IsComplete())
{
SwitchNextToCurrent();
// Re-get data as we've switched over
CurrentData = &PlayData[GetDataIndex(ERandomDataIndexType::Current)];
NextData = &PlayData[GetDataIndex(ERandomDataIndexType::Next)];
}
if(FAnimInstanceProxy* AnimProxy = Context.AnimInstanceProxy)
{
FAnimGroupInstance* SyncGroup;
FAnimTickRecord& TickRecord = AnimProxy->CreateUninitializedTickRecord(INDEX_NONE, SyncGroup);
AnimProxy->MakeSequenceTickRecord(TickRecord, Entries[CurrentEntry].Sequence, true, CurrentData->PlayRate, CurrentData->BlendWeight, CurrentData->InternalTimeAccumulator, CurrentData->MarkerTickRecord);
if(NextData->BlendWeight > 0.0f)
{
FAnimTickRecord& NextTickRecord = AnimProxy->CreateUninitializedTickRecord(INDEX_NONE, SyncGroup);
AnimProxy->MakeSequenceTickRecord(NextTickRecord, Entries[NextEntry].Sequence, true, NextData->PlayRate, NextData->BlendWeight, NextData->InternalTimeAccumulator, NextData->MarkerTickRecord);
}
}
}
}
void FAnimNode_RandomPlayer::Evaluate(FPoseContext& Output)
{
if(Entries.Num() > 0)
{
UAnimSequence* CurrentSequence = Entries[CurrentEntry].Sequence;
if(CurrentSequence)
{
FRandomAnimPlayData& CurrentData = PlayData[GetDataIndex(ERandomDataIndexType::Current)];
FRandomAnimPlayData& NextData = PlayData[GetDataIndex(ERandomDataIndexType::Next)];
if(CurrentData.BlendWeight != 1.0f)
{
if(FAnimInstanceProxy* AnimProxy = Output.AnimInstanceProxy)
{
// Start Blending
FCompactPose Poses[2];
FBlendedCurve Curves[2];
float Weights[2];
Poses[0].SetBoneContainer(&AnimProxy->GetRequiredBones());
Poses[1].SetBoneContainer(&AnimProxy->GetRequiredBones());
Curves[0].InitFrom(AnimProxy->GetSkelMeshComponent()->GetCachedAnimCurveMappingNameUids());
Curves[1].InitFrom(AnimProxy->GetSkelMeshComponent()->GetCachedAnimCurveMappingNameUids());
Weights[0] = CurrentData.BlendWeight;
Weights[1] = NextData.BlendWeight;
UAnimSequence* NextSequence = Entries[NextEntry].Sequence;
CurrentSequence->GetAnimationPose(Poses[0], Curves[0], FAnimExtractContext(CurrentData.InternalTimeAccumulator, AnimProxy->ShouldExtractRootMotion()));
NextSequence->GetAnimationPose(Poses[1], Curves[1], FAnimExtractContext(NextData.InternalTimeAccumulator, AnimProxy->ShouldExtractRootMotion()));
FAnimationRuntime::BlendPosesTogether(TFixedSizeArrayView<FCompactPose>(Poses, 2), TFixedSizeArrayView<FBlendedCurve>(Curves, 2), TFixedSizeArrayView<float>(Weights, 2), Output.Pose, Output.Curve);
}
else
{
Output.ResetToRefPose();
}
}
else
{
// Single anim
CurrentSequence->GetAnimationPose(Output.Pose, Output.Curve, FAnimExtractContext(CurrentData.InternalTimeAccumulator, Output.AnimInstanceProxy->ShouldExtractRootMotion()));
}
}
else
{
Output.ResetToRefPose();
}
}
else
{
Output.ResetToRefPose();
}
}
void FAnimNode_RandomPlayer::GatherDebugData(FNodeDebugData& DebugData)
{
FString DebugLine = DebugData.GetNodeName(this);
DebugData.AddDebugItem(DebugLine, true);
}
int32 FAnimNode_RandomPlayer::GetNextEntryIndex()
{
if(Entries.Num() > 0)
{
if(bShuffleMode)
{
if(ShuffleList.Num() == 0)
{
// Need a new list
BuildShuffleList();
}
// Get the top value, don't allow realloc
return ShuffleList.Pop(false);
}
else
{
float RandomVal = RandomStream.GetFraction();
const int32 NumEntries = Entries.Num();
// Grab the entry index corresponding to the value
for(int32 Idx = 0 ; Idx < NumEntries ; ++Idx)
{
RandomVal -= NormalizedPlayChances[Idx];
if(RandomVal <= 0.0f)
{
return Idx;
}
}
}
}
return INDEX_NONE;
}
int32 FAnimNode_RandomPlayer::GetDataIndex(const ERandomDataIndexType& Type)
{
if(Type == ERandomDataIndexType::Current)
{
return CurrentDataIndex;
}
else
{
// Next Accumulator
return (CurrentDataIndex + 1) % 2;
}
}
void FAnimNode_RandomPlayer::SwitchNextToCurrent()
{
// reset alpha blend we've possibly just taken
Entries[NextEntry].BlendIn.Reset();
// Switch which entry to get sequences and parameters from, and pre-generate the next entry index
CurrentEntry = NextEntry;
NextEntry = GetNextEntryIndex();
// Switch play data
CurrentDataIndex = (CurrentDataIndex + 1) %2;
// Get our play data
FRandomAnimPlayData& CurrentData = PlayData[GetDataIndex(ERandomDataIndexType::Current)];
FRandomAnimPlayData& NextData = PlayData[GetDataIndex(ERandomDataIndexType::Next)];
// Reset blendweights
CurrentData.BlendWeight = 1.0f;
NextData.BlendWeight = 0.0f;
// Set up data for next switch
NextData.InternalTimeAccumulator = 0.0f;
NextData.PreviousTimeAccumulator = 0.0f;
NextData.PlayRate = RandomStream.FRandRange(Entries[NextEntry].MinPlayRate, Entries[NextEntry].MaxPlayRate);
NextData.RemainingLoops = FMath::Clamp(RandomStream.RandRange(Entries[NextEntry].MinLoopCount, Entries[NextEntry].MaxLoopCount), 0, MAX_int32);
NextData.MarkerTickRecord.Reset();
}
void FAnimNode_RandomPlayer::BuildShuffleList()
{
ShuffleList.Reset(Entries.Num());
// Build entry index list
const int32 NumEntries = Entries.Num();
for(int32 i = 0 ; i < NumEntries ; ++i)
{
ShuffleList.Add(i);
}
// Shuffle the list
const int32 NumShuffles = ShuffleList.Num() - 1;
for(int32 i = 0 ; i < NumShuffles ; ++i)
{
int32 SwapIdx = RandomStream.RandRange(i, NumShuffles);
ShuffleList.Swap(i, SwapIdx);
}
}