You've already forked UnrealEngineUWP
mirror of
https://github.com/izzy2lost/UnrealEngineUWP.git
synced 2026-03-26 18:15:20 -07:00
#lockdown Nick.Penwarden #rb none ========================== MAJOR FEATURES + CHANGES ========================== Change 3209340 on 2016/11/23 by Ben.Marsh Convert UE4 codebase to an "include what you use" model - where every header just includes the dependencies it needs, rather than every source file including large monolithic headers like Engine.h and UnrealEd.h. Measured full rebuild times around 2x faster using XGE on Windows, and improvements of 25% or more for incremental builds and full rebuilds on most other platforms. * Every header now includes everything it needs to compile. * There's a CoreMinimal.h header that gets you a set of ubiquitous types from Core (eg. FString, FName, TArray, FVector, etc...). Most headers now include this first. * There's a CoreTypes.h header that sets up primitive UE4 types and build macros (int32, PLATFORM_WIN64, etc...). All headers in Core include this first, as does CoreMinimal.h. * Every .cpp file includes its matching .h file first. * This helps validate that each header is including everything it needs to compile. * No engine code includes a monolithic header such as Engine.h or UnrealEd.h any more. * You will get a warning if you try to include one of these from the engine. They still exist for compatibility with game projects and do not produce warnings when included there. * There have only been minor changes to our internal games down to accommodate these changes. The intent is for this to be as seamless as possible. * No engine code explicitly includes a precompiled header any more. * We still use PCHs, but they're force-included on the compiler command line by UnrealBuildTool instead. This lets us tune what they contain without breaking any existing include dependencies. * PCHs are generated by a tool to get a statistical amount of coverage for the source files using it, and I've seeded the new shared PCHs to contain any header included by > 15% of source files. Tool used to generate this transform is at Engine\Source\Programs\IncludeTool. [CL 3209342 by Ben Marsh in Main branch]
482 lines
13 KiB
C++
482 lines
13 KiB
C++
// Copyright 1998-2016 Epic Games, Inc. All Rights Reserved.
|
|
|
|
#include "GeometryCacheComponent.h"
|
|
#include "GeometryCache.h"
|
|
#include "Logging/MessageLog.h"
|
|
#include "ContentStreaming.h"
|
|
|
|
#include "GeometryCacheTrack.h"
|
|
#include "GeometryCacheMeshData.h"
|
|
|
|
#define LOCTEXT_NAMESPACE "GeometryCacheComponent"
|
|
|
|
DECLARE_CYCLE_STAT(TEXT("GeometryCacheTick"), STAT_GeometryCacheComponent_TickComponent, STATGROUP_GeometryCache);
|
|
|
|
UGeometryCacheComponent::UGeometryCacheComponent(const FObjectInitializer& ObjectInitializer)
|
|
: Super(ObjectInitializer)
|
|
{
|
|
PrimaryComponentTick.bCanEverTick = true;
|
|
ElapsedTime = 0.0f;
|
|
bRunning = false;
|
|
bLooping = true;
|
|
PlayDirection = 1.0f;
|
|
StartTimeOffset = 0.0f;
|
|
PlaybackSpeed = 1.0f;
|
|
Duration = 0.0f;
|
|
}
|
|
|
|
void UGeometryCacheComponent::BeginDestroy()
|
|
{
|
|
Super::BeginDestroy();
|
|
ReleaseResources();
|
|
}
|
|
|
|
void UGeometryCacheComponent::OnRegister()
|
|
{
|
|
ClearTrackData();
|
|
SetupTrackData();
|
|
Super::OnRegister();
|
|
}
|
|
|
|
void UGeometryCacheComponent::ClearTrackData()
|
|
{
|
|
NumTracks = 0;
|
|
TrackMatrixSampleIndices.Empty();
|
|
TrackMatrixSampleIndices.Empty();
|
|
TrackSections.Empty();
|
|
SceneProxy = nullptr;
|
|
}
|
|
|
|
void UGeometryCacheComponent::SetupTrackData()
|
|
{
|
|
if (GeometryCache != nullptr)
|
|
{
|
|
// Refresh NumTracks and clear Index Arrays
|
|
NumTracks = GeometryCache->Tracks.Num();
|
|
TrackMeshSampleIndices.Empty(GeometryCache->Tracks.Num());
|
|
TrackMatrixSampleIndices.Empty(GeometryCache->Tracks.Num());
|
|
|
|
Duration = 0.0f;
|
|
// Create mesh sections for each GeometryCacheTrack
|
|
for (int32 TrackIndex = 0; TrackIndex < NumTracks; ++TrackIndex)
|
|
{
|
|
UGeometryCacheTrack* Track = GeometryCache->Tracks[TrackIndex];
|
|
FMatrix WorldMatrix;
|
|
int32 MeshSampleIndex = -1;
|
|
int32 MatrixSampleIndex = -1;
|
|
FGeometryCacheMeshData* MeshData = nullptr;
|
|
|
|
// Retrieve the matrix/mesh data and the appropriate sample indices
|
|
const float ClampedStartTimeOffset = FMath::Clamp(StartTimeOffset, -14400.0f, 14400.0f);
|
|
Track->UpdateMatrixData(ElapsedTime + ClampedStartTimeOffset, bLooping, MatrixSampleIndex, WorldMatrix);
|
|
Track->UpdateMeshData(ElapsedTime + ClampedStartTimeOffset, bLooping, MeshSampleIndex, MeshData);
|
|
|
|
// First time so create rather than update the mesh sections
|
|
CreateTrackSection(TrackIndex, WorldMatrix, MeshData);
|
|
|
|
// Store the sample indices for both the mesh and matrix data
|
|
TrackMeshSampleIndices.Add(MeshSampleIndex);
|
|
TrackMatrixSampleIndices.Add(MatrixSampleIndex);
|
|
|
|
const float TrackMaxSampleTime = Track->GetMaxSampleTime();
|
|
Duration = (Duration > TrackMaxSampleTime) ? Duration : TrackMaxSampleTime;
|
|
}
|
|
}
|
|
}
|
|
|
|
void UGeometryCacheComponent::OnUnregister()
|
|
{
|
|
Super::OnUnregister();
|
|
|
|
NumTracks = 0;
|
|
TrackMatrixSampleIndices.Empty();
|
|
TrackMatrixSampleIndices.Empty();
|
|
TrackSections.Empty();
|
|
}
|
|
|
|
void UGeometryCacheComponent::TickComponent(float DeltaTime, enum ELevelTick TickType, FActorComponentTickFunction *ThisTickFunction)
|
|
{
|
|
SCOPE_CYCLE_COUNTER(STAT_GeometryCacheComponent_TickComponent);
|
|
if (GeometryCache && bRunning)
|
|
{
|
|
// Increase total elapsed time since BeginPlay according to PlayDirection and speed
|
|
ElapsedTime += (DeltaTime * PlayDirection * GetPlaybackSpeed());
|
|
|
|
if (ElapsedTime < 0.0f && bLooping)
|
|
{
|
|
ElapsedTime += Duration;
|
|
}
|
|
|
|
for (int32 TrackIndex = 0; TrackIndex < NumTracks; ++TrackIndex)
|
|
{
|
|
// Determine if and which part of the section we have to update
|
|
UGeometryCacheTrack* Track = GeometryCache->Tracks[TrackIndex];
|
|
FMatrix WorldMatrix;
|
|
FGeometryCacheMeshData* MeshData = nullptr;
|
|
const float ClampedStartTimeOffset = FMath::Clamp(StartTimeOffset, -14400.0f, 14400.0f);
|
|
const bool bUpdateMatrix = Track->UpdateMatrixData(ElapsedTime + ClampedStartTimeOffset, bLooping, TrackMatrixSampleIndices[TrackIndex], WorldMatrix);
|
|
const bool bUpdateMesh = Track->UpdateMeshData(ElapsedTime + ClampedStartTimeOffset, bLooping, TrackMeshSampleIndices[TrackIndex], MeshData);
|
|
|
|
// Update sections according what is required
|
|
if (bUpdateMatrix)
|
|
{
|
|
UpdateTrackSectionMatrixData(TrackIndex, WorldMatrix);
|
|
}
|
|
|
|
if (bUpdateMesh)
|
|
{
|
|
UpdateTrackSectionMeshData(TrackIndex, MeshData);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
FBoxSphereBounds UGeometryCacheComponent::CalcBounds(const FTransform& LocalToWorld) const
|
|
{
|
|
return LocalBounds.TransformBy(LocalToWorld);
|
|
}
|
|
void UGeometryCacheComponent::UpdateLocalBounds()
|
|
{
|
|
FBox LocalBox(0);
|
|
|
|
for (const FTrackRenderData& Section : TrackSections)
|
|
{
|
|
// Use World matrix per section for correct bounding box
|
|
LocalBox += (Section.MeshData->BoundingBox.TransformBy(Section.WorldMatrix));
|
|
}
|
|
|
|
LocalBounds = LocalBox.IsValid ? FBoxSphereBounds(LocalBox) : FBoxSphereBounds(FVector(0, 0, 0), FVector(0, 0, 0), 0); // fallback to reset box sphere bounds
|
|
|
|
UpdateBounds();
|
|
}
|
|
|
|
FPrimitiveSceneProxy* UGeometryCacheComponent::CreateSceneProxy()
|
|
{
|
|
SceneProxy = new FGeometryCacheSceneProxy(this);
|
|
return SceneProxy;
|
|
}
|
|
|
|
#if WITH_EDITOR
|
|
void UGeometryCacheComponent::PostEditChangeProperty(FPropertyChangedEvent& PropertyChangedEvent)
|
|
{
|
|
InvalidateTrackSampleIndices();
|
|
MarkRenderStateDirty();
|
|
Super::PostEditChangeProperty(PropertyChangedEvent);
|
|
}
|
|
#endif
|
|
|
|
int32 UGeometryCacheComponent::GetNumMaterials() const
|
|
{
|
|
return GeometryCache ? GeometryCache->Materials.Num() : 0;
|
|
}
|
|
|
|
UMaterialInterface* UGeometryCacheComponent::GetMaterial(int32 MaterialIndex) const
|
|
{
|
|
// If we have a base materials array, use that
|
|
if (MaterialIndex < OverrideMaterials.Num() && OverrideMaterials[MaterialIndex])
|
|
{
|
|
return OverrideMaterials[MaterialIndex];
|
|
}
|
|
// Otherwise get from static mesh
|
|
else
|
|
{
|
|
return GeometryCache ? ( GeometryCache->Materials.IsValidIndex(MaterialIndex) ? GeometryCache->Materials[MaterialIndex] : nullptr) : nullptr;
|
|
}
|
|
}
|
|
|
|
void UGeometryCacheComponent::CreateTrackSection(int32 SectionIndex, const FMatrix& WorldMatrix, FGeometryCacheMeshData* MeshData)
|
|
{
|
|
// Ensure sections array is long enough
|
|
if (TrackSections.Num() <= SectionIndex)
|
|
{
|
|
TrackSections.SetNum(SectionIndex + 1, false);
|
|
}
|
|
|
|
// Reset this section (in case it already existed)
|
|
FTrackRenderData& NewSection = TrackSections[SectionIndex];
|
|
NewSection.Reset();
|
|
|
|
// Number of Vertices
|
|
const int32 NumVerts = MeshData->Vertices.Num();
|
|
|
|
// Store Matrix and MeshData-pointer
|
|
NewSection.WorldMatrix = WorldMatrix;
|
|
NewSection.MeshData = MeshData;
|
|
|
|
UpdateLocalBounds(); // Update overall bounds
|
|
MarkRenderStateDirty(); // Recreate scene proxy
|
|
}
|
|
|
|
void UGeometryCacheComponent::UpdateTrackSectionMeshData(int32 SectionIndex, FGeometryCacheMeshData* MeshData)
|
|
{
|
|
// Check if the index is in range and Vertices contains any data
|
|
check(SectionIndex < TrackSections.Num() && MeshData != nullptr && "Invalid SectionIndex or Mesh Data");
|
|
// Reset this section (in case it already existed)
|
|
FTrackRenderData& UpdateSection = TrackSections[SectionIndex];
|
|
|
|
// Number of Vertices
|
|
const int32 NumVerts = MeshData->Vertices.Num();
|
|
|
|
// Update MeshDataPointer
|
|
UpdateSection.MeshData = MeshData;
|
|
|
|
//if (SceneProxy)
|
|
//{
|
|
// UpdateTrackSectionVertexbuffer(SectionIndex, MeshData);
|
|
// UpdateTrackSectionIndexbuffer(SectionIndex, MeshData->Indices);
|
|
// // Will update the bounds (used for frustum culling)
|
|
// MarkRenderTransformDirty();
|
|
// MarkRenderDynamicDataDirty();
|
|
//}
|
|
//else
|
|
//{
|
|
// // Recreate scene proxy
|
|
// MarkRenderStateDirty();
|
|
//}
|
|
|
|
// Update overall bounds
|
|
UpdateLocalBounds();
|
|
|
|
MarkRenderStateDirty();
|
|
}
|
|
|
|
void UGeometryCacheComponent::UpdateTrackSectionMatrixData(int32 SectionIndex, const FMatrix& WorldMatrix)
|
|
{
|
|
check(SectionIndex < TrackSections.Num() && "Invalid SectionIndex");
|
|
// Reset this section (in case it already existed)
|
|
FTrackRenderData& UpdateSection = TrackSections[SectionIndex];
|
|
|
|
// Update the WorldMatrix only
|
|
UpdateSection.WorldMatrix = WorldMatrix;
|
|
|
|
if (!IsRenderStateDirty() && SceneProxy != nullptr)
|
|
{
|
|
// Update it within the scene proxy
|
|
SceneProxy->UpdateSectionWorldMatrix(SectionIndex, WorldMatrix);
|
|
}
|
|
|
|
// Update local bounds
|
|
UpdateLocalBounds();
|
|
|
|
// Mark transform Dirty
|
|
MarkRenderTransformDirty();
|
|
}
|
|
|
|
void UGeometryCacheComponent::UpdateTrackSectionVertexbuffer(int32 SectionIndex, FGeometryCacheMeshData* MeshData)
|
|
{
|
|
ENQUEUE_UNIQUE_RENDER_COMMAND_THREEPARAMETER(
|
|
FUpdateVertexBufferCommand,
|
|
FGeometryCacheSceneProxy*, SceneProxy, SceneProxy,
|
|
const int32, Index, SectionIndex,
|
|
FGeometryCacheMeshData*, Data, MeshData,
|
|
{
|
|
SceneProxy->UpdateSectionVertexBuffer(Index, Data);
|
|
});
|
|
}
|
|
|
|
void UGeometryCacheComponent::UpdateTrackSectionIndexbuffer(int32 SectionIndex, const TArray<uint32>& Indices)
|
|
{
|
|
ENQUEUE_UNIQUE_RENDER_COMMAND_THREEPARAMETER(
|
|
FUpdateVertexBufferCommand,
|
|
FGeometryCacheSceneProxy*, SceneProxy, SceneProxy,
|
|
const int32, Index, SectionIndex,
|
|
const TArray<uint32>&, Data, Indices,
|
|
{
|
|
SceneProxy->UpdateSectionIndexBuffer(Index, Data);
|
|
});
|
|
}
|
|
|
|
void UGeometryCacheComponent::OnObjectReimported(UGeometryCache* ImportedGeometryCache)
|
|
{
|
|
if (GeometryCache == ImportedGeometryCache)
|
|
{
|
|
ReleaseResources();
|
|
GeometryCache = ImportedGeometryCache;
|
|
|
|
if (GeometryCache != nullptr)
|
|
{
|
|
// Refresh NumTracks and clear Index Arrays
|
|
NumTracks = GeometryCache->Tracks.Num();
|
|
TrackMeshSampleIndices.Empty(GeometryCache->Tracks.Num());
|
|
TrackMatrixSampleIndices.Empty(GeometryCache->Tracks.Num());
|
|
|
|
Duration = 0.0f;
|
|
// Create mesh sections for each GeometryCacheTrack
|
|
for (int32 TrackIndex = 0; TrackIndex < NumTracks; ++TrackIndex)
|
|
{
|
|
UGeometryCacheTrack* Track = GeometryCache->Tracks[TrackIndex];
|
|
FMatrix WorldMatrix;
|
|
int32 MeshSampleIndex = -1;
|
|
int32 MatrixSampleIndex = -1;
|
|
FGeometryCacheMeshData* MeshData = nullptr;
|
|
|
|
// Retrieve the matrix/mesh data and the appropriate sample indices
|
|
const float ClampedStartTimeOffset = FMath::Clamp(StartTimeOffset, -14400.0f, 14400.0f);
|
|
Track->UpdateMatrixData(ElapsedTime + ClampedStartTimeOffset, bLooping, MatrixSampleIndex, WorldMatrix);
|
|
Track->UpdateMeshData(ElapsedTime + ClampedStartTimeOffset, bLooping, MeshSampleIndex, MeshData);
|
|
|
|
// First time so create rather than update the mesh sections
|
|
CreateTrackSection(TrackIndex, WorldMatrix, MeshData);
|
|
|
|
// Store the sample indices for both the mesh and matrix data
|
|
TrackMeshSampleIndices.Add(MeshSampleIndex);
|
|
TrackMatrixSampleIndices.Add(MatrixSampleIndex);
|
|
|
|
const float TrackMaxSampleTime = Track->GetMaxSampleTime();
|
|
Duration = (Duration > TrackMaxSampleTime) ? Duration : TrackMaxSampleTime;
|
|
}
|
|
}
|
|
|
|
MarkRenderStateDirty();
|
|
}
|
|
}
|
|
|
|
void UGeometryCacheComponent::Play()
|
|
{
|
|
bRunning = true;
|
|
PlayDirection = 1.0f;
|
|
}
|
|
|
|
void UGeometryCacheComponent::PlayFromStart()
|
|
{
|
|
ElapsedTime = 0.0f;
|
|
bRunning = true;
|
|
PlayDirection = 1.0f;
|
|
}
|
|
|
|
void UGeometryCacheComponent::Pause()
|
|
{
|
|
bRunning = !bRunning;
|
|
}
|
|
|
|
void UGeometryCacheComponent::Stop()
|
|
{
|
|
bRunning = false;
|
|
}
|
|
|
|
bool UGeometryCacheComponent::IsPlaying() const
|
|
{
|
|
return bRunning;
|
|
}
|
|
|
|
bool UGeometryCacheComponent::IsLooping() const
|
|
{
|
|
return bLooping;
|
|
}
|
|
|
|
void UGeometryCacheComponent::SetLooping(const bool bNewLooping)
|
|
{
|
|
bLooping = bNewLooping;
|
|
}
|
|
|
|
bool UGeometryCacheComponent::IsPlayingReversed() const
|
|
{
|
|
return FMath::IsNearlyEqual( PlayDirection, -1.0f );
|
|
}
|
|
|
|
float UGeometryCacheComponent::GetPlaybackSpeed() const
|
|
{
|
|
return FMath::Clamp(PlaybackSpeed, -512.0f, 512.0f);
|
|
}
|
|
|
|
void UGeometryCacheComponent::SetPlaybackSpeed(const float NewPlaybackSpeed)
|
|
{
|
|
// Currently only positive play back speeds are supported
|
|
PlaybackSpeed = FMath::Clamp( NewPlaybackSpeed, -512.0f, 512.0f );
|
|
}
|
|
|
|
bool UGeometryCacheComponent::SetGeometryCache(UGeometryCache* NewGeomCache)
|
|
{
|
|
// Do nothing if we are already using the supplied static mesh
|
|
if (NewGeomCache == GeometryCache)
|
|
{
|
|
return false;
|
|
}
|
|
|
|
// Don't allow changing static meshes if "static" and registered
|
|
AActor* Owner = GetOwner();
|
|
if (!AreDynamicDataChangesAllowed() && Owner != nullptr)
|
|
{
|
|
FMessageLog("PIE").Warning(FText::Format(LOCTEXT("SetGeometryCache", "Calling SetGeometryCache on '{0}' but Mobility is Static."),
|
|
FText::FromString(GetPathName())));
|
|
return false;
|
|
}
|
|
|
|
|
|
GeometryCache = NewGeomCache;
|
|
|
|
ClearTrackData();
|
|
SetupTrackData();
|
|
|
|
// Need to send this to render thread at some point
|
|
MarkRenderStateDirty();
|
|
|
|
// Update physics representation right away
|
|
RecreatePhysicsState();
|
|
|
|
// Notify the streaming system. Don't use Update(), because this may be the first time the geometry cache has been set
|
|
// and the component may have to be added to the streaming system for the first time.
|
|
IStreamingManager::Get().NotifyPrimitiveAttached(this, DPT_Spawned);
|
|
|
|
// Since we have new tracks, we need to update bounds
|
|
UpdateBounds();
|
|
return true;
|
|
}
|
|
|
|
UGeometryCache* UGeometryCacheComponent::GetGeometryCache() const
|
|
{
|
|
return GeometryCache;
|
|
}
|
|
|
|
void UGeometryCacheComponent::PlayReversedFromEnd()
|
|
{
|
|
ElapsedTime = Duration;
|
|
PlayDirection = -1.0f;
|
|
bRunning = true;
|
|
}
|
|
|
|
void UGeometryCacheComponent::PlayReversed()
|
|
{
|
|
PlayDirection = -1.0f;
|
|
bRunning = true;
|
|
}
|
|
|
|
void UGeometryCacheComponent::InvalidateTrackSampleIndices()
|
|
{
|
|
for (int32& Index : TrackMeshSampleIndices)
|
|
{
|
|
Index = -1;
|
|
}
|
|
|
|
for (int32& Index : TrackMatrixSampleIndices)
|
|
{
|
|
Index = -1;
|
|
}
|
|
}
|
|
|
|
void UGeometryCacheComponent::ReleaseResources()
|
|
{
|
|
GeometryCache = nullptr;
|
|
NumTracks = 0;
|
|
TrackMatrixSampleIndices.Empty();
|
|
TrackMatrixSampleIndices.Empty();
|
|
TrackSections.Empty();
|
|
DetachFence.BeginFence();
|
|
}
|
|
|
|
#if WITH_EDITOR
|
|
void UGeometryCacheComponent::PreEditUndo()
|
|
{
|
|
InvalidateTrackSampleIndices();
|
|
MarkRenderStateDirty();
|
|
}
|
|
|
|
void UGeometryCacheComponent::PostEditUndo()
|
|
{
|
|
InvalidateTrackSampleIndices();
|
|
MarkRenderStateDirty();
|
|
}
|
|
#endif
|
|
|
|
#undef LOCTEXT_NAMESPACE
|