[ue4] Reworked renderer component, does not fail GC when single attachment > 1000 vertices.

This commit is contained in:
badlogic 2023-02-14 13:43:53 +01:00
parent aec3b6adaa
commit b8a2facf93
2 changed files with 115 additions and 144 deletions

View File

@ -75,74 +75,57 @@ void USpineSkeletonRendererComponent::TickComponent(float DeltaTime, ELevelTick
AActor *owner = GetOwner(); AActor *owner = GetOwner();
if (owner) { if (owner) {
UClass *skeletonClass = USpineSkeletonComponent::StaticClass(); UClass *skeletonClass = USpineSkeletonComponent::StaticClass();
USpineSkeletonComponent *skeleton = Cast<USpineSkeletonComponent>(owner->GetComponentByClass(skeletonClass)); USpineSkeletonComponent *skeletonComponent = Cast<USpineSkeletonComponent>(owner->GetComponentByClass(skeletonClass));
UpdateRenderer(skeleton); UpdateRenderer(skeletonComponent);
} }
} }
void USpineSkeletonRendererComponent::UpdateRenderer(USpineSkeletonComponent *skeleton) { void USpineSkeletonRendererComponent::UpdateRenderer(USpineSkeletonComponent *component) {
if (skeleton && !skeleton->IsBeingDestroyed() && skeleton->GetSkeleton() && skeleton->Atlas) { if (component && !component->IsBeingDestroyed() && component->GetSkeleton() && component->Atlas) {
skeleton->GetSkeleton()->getColor().set(Color.R, Color.G, Color.B, Color.A); component->GetSkeleton()->getColor().set(Color.R, Color.G, Color.B, Color.A);
if (atlasNormalBlendMaterials.Num() != skeleton->Atlas->atlasPages.Num()) { if (atlasNormalBlendMaterials.Num() != component->Atlas->atlasPages.Num()) {
atlasNormalBlendMaterials.SetNum(0); atlasNormalBlendMaterials.SetNum(0);
pageToNormalBlendMaterial.Empty();
atlasAdditiveBlendMaterials.SetNum(0); atlasAdditiveBlendMaterials.SetNum(0);
pageToAdditiveBlendMaterial.Empty();
atlasMultiplyBlendMaterials.SetNum(0); atlasMultiplyBlendMaterials.SetNum(0);
pageToMultiplyBlendMaterial.Empty();
atlasScreenBlendMaterials.SetNum(0); atlasScreenBlendMaterials.SetNum(0);
pageToScreenBlendMaterial.Empty();
for (int i = 0; i < skeleton->Atlas->atlasPages.Num(); i++) { for (int i = 0; i < component->Atlas->atlasPages.Num(); i++) {
AtlasPage *currPage = skeleton->Atlas->GetAtlas()->getPages()[i]; AtlasPage *currPage = component->Atlas->GetAtlas()->getPages()[i];
UMaterialInstanceDynamic *material = UMaterialInstanceDynamic::Create(NormalBlendMaterial, this); UMaterialInstanceDynamic *material = UMaterialInstanceDynamic::Create(NormalBlendMaterial, this);
material->SetTextureParameterValue(TextureParameterName, skeleton->Atlas->atlasPages[i]); material->SetTextureParameterValue(TextureParameterName, component->Atlas->atlasPages[i]);
atlasNormalBlendMaterials.Add(material); atlasNormalBlendMaterials.Add(material);
pageToNormalBlendMaterial.Add(currPage, material);
material = UMaterialInstanceDynamic::Create(AdditiveBlendMaterial, this); material = UMaterialInstanceDynamic::Create(AdditiveBlendMaterial, this);
material->SetTextureParameterValue(TextureParameterName, skeleton->Atlas->atlasPages[i]); material->SetTextureParameterValue(TextureParameterName, component->Atlas->atlasPages[i]);
atlasAdditiveBlendMaterials.Add(material); atlasAdditiveBlendMaterials.Add(material);
pageToAdditiveBlendMaterial.Add(currPage, material);
material = UMaterialInstanceDynamic::Create(MultiplyBlendMaterial, this); material = UMaterialInstanceDynamic::Create(MultiplyBlendMaterial, this);
material->SetTextureParameterValue(TextureParameterName, skeleton->Atlas->atlasPages[i]); material->SetTextureParameterValue(TextureParameterName, component->Atlas->atlasPages[i]);
atlasMultiplyBlendMaterials.Add(material); atlasMultiplyBlendMaterials.Add(material);
pageToMultiplyBlendMaterial.Add(currPage, material);
material = UMaterialInstanceDynamic::Create(ScreenBlendMaterial, this); material = UMaterialInstanceDynamic::Create(ScreenBlendMaterial, this);
material->SetTextureParameterValue(TextureParameterName, skeleton->Atlas->atlasPages[i]); material->SetTextureParameterValue(TextureParameterName, component->Atlas->atlasPages[i]);
atlasScreenBlendMaterials.Add(material); atlasScreenBlendMaterials.Add(material);
pageToScreenBlendMaterial.Add(currPage, material);
} }
} else { } else {
pageToNormalBlendMaterial.Empty(); for (int i = 0; i < component->Atlas->atlasPages.Num(); i++) {
pageToAdditiveBlendMaterial.Empty(); UTexture2D *texture = component->Atlas->atlasPages[i];
pageToMultiplyBlendMaterial.Empty(); UpdateMaterial(texture, atlasNormalBlendMaterials[i], NormalBlendMaterial);
pageToScreenBlendMaterial.Empty(); UpdateMaterial(texture, atlasAdditiveBlendMaterials[i], AdditiveBlendMaterial);
UpdateMaterial(texture, atlasMultiplyBlendMaterials[i], MultiplyBlendMaterial);
for (int i = 0; i < skeleton->Atlas->atlasPages.Num(); i++) { UpdateMaterial(texture, atlasScreenBlendMaterials[i], ScreenBlendMaterial);
AtlasPage *currPage = skeleton->Atlas->GetAtlas()->getPages()[i];
UTexture2D *texture = skeleton->Atlas->atlasPages[i];
UpdateRendererMaterial(currPage, texture, atlasNormalBlendMaterials[i], NormalBlendMaterial, pageToNormalBlendMaterial);
UpdateRendererMaterial(currPage, texture, atlasAdditiveBlendMaterials[i], AdditiveBlendMaterial, pageToAdditiveBlendMaterial);
UpdateRendererMaterial(currPage, texture, atlasMultiplyBlendMaterials[i], MultiplyBlendMaterial, pageToMultiplyBlendMaterial);
UpdateRendererMaterial(currPage, texture, atlasScreenBlendMaterials[i], ScreenBlendMaterial, pageToScreenBlendMaterial);
} }
} }
UpdateMesh(skeleton->GetSkeleton()); UpdateMesh(component, component->GetSkeleton());
} else { } else {
ClearAllMeshSections(); ClearAllMeshSections();
} }
} }
void USpineSkeletonRendererComponent::UpdateRendererMaterial(spine::AtlasPage *CurrentPage, UTexture2D *Texture, void USpineSkeletonRendererComponent::UpdateMaterial(UTexture2D *Texture, UMaterialInstanceDynamic *&CurrentInstance, UMaterialInterface *ParentMaterial) {
UMaterialInstanceDynamic *&CurrentInstance, UMaterialInterface *ParentMaterial,
TMap<spine::AtlasPage *, UMaterialInstanceDynamic *> &PageToBlendMaterial) {
UTexture *oldTexture = nullptr; UTexture *oldTexture = nullptr;
if (!CurrentInstance || !CurrentInstance->GetTextureParameterValue(TextureParameterName, oldTexture) || if (!CurrentInstance || !CurrentInstance->GetTextureParameterValue(TextureParameterName, oldTexture) ||
@ -152,10 +135,10 @@ void USpineSkeletonRendererComponent::UpdateRendererMaterial(spine::AtlasPage *C
material->SetTextureParameterValue(TextureParameterName, Texture); material->SetTextureParameterValue(TextureParameterName, Texture);
CurrentInstance = material; CurrentInstance = material;
} }
PageToBlendMaterial.Add(CurrentPage, CurrentInstance);
} }
void USpineSkeletonRendererComponent::Flush(int &Idx, TArray<FVector> &Vertices, TArray<int32> &Indices, TArray<FVector> &Normals, TArray<FVector2D> &Uvs, TArray<FColor> &Colors, TArray<FVector> &Colors2, UMaterialInstanceDynamic *Material) { void USpineSkeletonRendererComponent::Flush(int &Idx, TArray<FVector> &Vertices, TArray<int32> &Indices, TArray<FVector> &Normals, TArray<FVector2D> &Uvs, TArray<FColor> &Colors, UMaterialInstanceDynamic *Material) {
if (Vertices.Num() == 0) return; if (Vertices.Num() == 0) return;
SetMaterial(Idx, Material); SetMaterial(Idx, Material);
@ -175,17 +158,15 @@ void USpineSkeletonRendererComponent::Flush(int &Idx, TArray<FVector> &Vertices,
Normals.SetNum(0); Normals.SetNum(0);
Uvs.SetNum(0); Uvs.SetNum(0);
Colors.SetNum(0); Colors.SetNum(0);
Colors2.SetNum(0);
Idx++; Idx++;
} }
void USpineSkeletonRendererComponent::UpdateMesh(Skeleton *Skeleton) { void USpineSkeletonRendererComponent::UpdateMesh(USpineSkeletonComponent *component, Skeleton *Skeleton) {
TArray<FVector> vertices; vertices.Empty();
TArray<int32> indices; indices.Empty();
TArray<FVector> normals; normals.Empty();
TArray<FVector2D> uvs; uvs.Empty();
TArray<FColor> colors; colors.Empty();
TArray<FVector> darkColors;
int idx = 0; int idx = 0;
int meshSection = 0; int meshSection = 0;
@ -236,6 +217,7 @@ void USpineSkeletonRendererComponent::UpdateMesh(Skeleton *Skeleton) {
} }
attachmentColor.set(regionAttachment->getColor()); attachmentColor.set(regionAttachment->getColor());
attachmentVertices->setSize(8, 0);
regionAttachment->computeWorldVertices(*slot, *attachmentVertices, 0, 2); regionAttachment->computeWorldVertices(*slot, *attachmentVertices, 0, 2);
attachmentAtlasRegion = (AtlasRegion *) regionAttachment->getRendererObject(); attachmentAtlasRegion = (AtlasRegion *) regionAttachment->getRendererObject();
attachmentIndices = quadIndices; attachmentIndices = quadIndices;
@ -252,6 +234,7 @@ void USpineSkeletonRendererComponent::UpdateMesh(Skeleton *Skeleton) {
} }
attachmentColor.set(mesh->getColor()); attachmentColor.set(mesh->getColor());
attachmentVertices->setSize(mesh->getWorldVerticesLength(), 0);
mesh->computeWorldVertices(*slot, 0, mesh->getWorldVerticesLength(), attachmentVertices->buffer(), 0, 2); mesh->computeWorldVertices(*slot, 0, mesh->getWorldVerticesLength(), attachmentVertices->buffer(), 0, 2);
attachmentAtlasRegion = (AtlasRegion *) mesh->getRendererObject(); attachmentAtlasRegion = (AtlasRegion *) mesh->getRendererObject();
attachmentIndices = mesh->getTriangles().buffer(); attachmentIndices = mesh->getTriangles().buffer();
@ -264,47 +247,6 @@ void USpineSkeletonRendererComponent::UpdateMesh(Skeleton *Skeleton) {
continue; continue;
} }
// if the user switches the atlas data while not having switched
// to the correct skeleton data yet, we won't find any regions.
// ignore regions for which we can't find a material
UMaterialInstanceDynamic *material = nullptr;
switch (slot->getData().getBlendMode()) {
case BlendMode_Normal:
if (!pageToNormalBlendMaterial.Contains(attachmentAtlasRegion->page)) {
clipper.clipEnd(*slot);
continue;
}
material = pageToNormalBlendMaterial[attachmentAtlasRegion->page];
break;
case BlendMode_Additive:
if (!pageToAdditiveBlendMaterial.Contains(attachmentAtlasRegion->page)) {
clipper.clipEnd(*slot);
continue;
}
material = pageToAdditiveBlendMaterial[attachmentAtlasRegion->page];
break;
case BlendMode_Multiply:
if (!pageToMultiplyBlendMaterial.Contains(attachmentAtlasRegion->page)) {
clipper.clipEnd(*slot);
continue;
}
material = pageToMultiplyBlendMaterial[attachmentAtlasRegion->page];
break;
case BlendMode_Screen:
if (!pageToScreenBlendMaterial.Contains(attachmentAtlasRegion->page)) {
clipper.clipEnd(*slot);
continue;
}
material = pageToScreenBlendMaterial[attachmentAtlasRegion->page];
break;
default:
if (!pageToNormalBlendMaterial.Contains(attachmentAtlasRegion->page)) {
clipper.clipEnd(*slot);
continue;
}
material = pageToNormalBlendMaterial[attachmentAtlasRegion->page];
}
if (clipper.isClipping()) { if (clipper.isClipping()) {
clipper.clipTriangles(attachmentVertices->buffer(), attachmentIndices, numIndices, attachmentUvs, 2); clipper.clipTriangles(attachmentVertices->buffer(), attachmentIndices, numIndices, attachmentUvs, 2);
attachmentVertices = &clipper.getClippedVertices(); attachmentVertices = &clipper.getClippedVertices();
@ -318,8 +260,56 @@ void USpineSkeletonRendererComponent::UpdateMesh(Skeleton *Skeleton) {
} }
} }
// if the user switches the atlas data while not having switched
// to the correct skeleton data yet, we won't find any regions.
// ignore regions for which we can't find a material
UMaterialInstanceDynamic* material = nullptr;
int foundPageIndex = -1;
for (int pageIndex = 0; i < component->Atlas->atlasPages.Num(); pageIndex++) {
AtlasPage* page = component->Atlas->GetAtlas()->getPages()[pageIndex];
if (attachmentAtlasRegion->page == page) {
foundPageIndex = pageIndex;
break;
}
}
if (foundPageIndex == -1) {
clipper.clipEnd(*slot);
continue;
}
switch (slot->getData().getBlendMode()) {
case BlendMode_Additive:
if (i >= atlasAdditiveBlendMaterials.Num()) {
clipper.clipEnd(*slot);
continue;
}
material = atlasAdditiveBlendMaterials[i];
break;
case BlendMode_Multiply:
if (i >= atlasMultiplyBlendMaterials.Num()) {
clipper.clipEnd(*slot);
continue;
}
material = atlasMultiplyBlendMaterials[i];
break;
case BlendMode_Screen:
if (i >= atlasScreenBlendMaterials.Num()) {
clipper.clipEnd(*slot);
continue;
}
material = atlasScreenBlendMaterials[i];
break;
case BlendMode_Normal:
default:
if (i >= atlasNormalBlendMaterials.Num()) {
clipper.clipEnd(*slot);
continue;
}
material = atlasNormalBlendMaterials[i];
break;
}
if (lastMaterial != material) { if (lastMaterial != material) {
Flush(meshSection, vertices, indices, normals, uvs, colors, darkColors, lastMaterial); Flush(meshSection, vertices, indices, normals, uvs, colors, lastMaterial);
lastMaterial = material; lastMaterial = material;
idx = 0; idx = 0;
} }
@ -331,56 +321,32 @@ void USpineSkeletonRendererComponent::UpdateMesh(Skeleton *Skeleton) {
uint8 b = static_cast<uint8>(Skeleton->getColor().b * slot->getColor().b * attachmentColor.b * 255); uint8 b = static_cast<uint8>(Skeleton->getColor().b * slot->getColor().b * attachmentColor.b * 255);
uint8 a = static_cast<uint8>(Skeleton->getColor().a * slot->getColor().a * attachmentColor.a * 255); uint8 a = static_cast<uint8>(Skeleton->getColor().a * slot->getColor().a * attachmentColor.a * 255);
float dr = slot->hasDarkColor() ? slot->getDarkColor().r : 0.0f; float* verticesPtr = attachmentVertices->buffer();
float dg = slot->hasDarkColor() ? slot->getDarkColor().g : 0.0f;
float db = slot->hasDarkColor() ? slot->getDarkColor().b : 0.0f;
float *verticesPtr = attachmentVertices->buffer();
for (int j = 0; j < numVertices << 1; j += 2) { for (int j = 0; j < numVertices << 1; j += 2) {
colors.Add(FColor(r, g, b, a)); colors.Add(FColor(r, g, b, a));
darkColors.Add(FVector(dr, dg, db));
vertices.Add(FVector(verticesPtr[j], depthOffset, verticesPtr[j + 1])); vertices.Add(FVector(verticesPtr[j], depthOffset, verticesPtr[j + 1]));
uvs.Add(FVector2D(attachmentUvs[j], attachmentUvs[j + 1])); uvs.Add(FVector2D(attachmentUvs[j], attachmentUvs[j + 1]));
} }
int firstIndex = indices.Num();
for (int j = 0; j < numIndices; j++) { for (int j = 0; j < numIndices; j++) {
indices.Add(idx + attachmentIndices[j]); indices.Add(idx + attachmentIndices[j]);
} }
int numTriangles = indices.Num() / 3;
//Calculate total triangle to add on this loof. for (int j = 0; j < numTriangles; j++) {
const int triangleIndex = j * 3;
int TriangleInitialCount = firstIndex / 3;
int TriangleToAddNum = indices.Num() / 3 - TriangleInitialCount;
int FirstVertexIndex = vertices.Num() - numVertices;
//loof through all the triangles and resolve to be reversed if the triangle has winding order as CCW.
for (int j = 0; j < TriangleToAddNum; j++) {
const int TargetTringleIndex = firstIndex + j * 3;
if (FVector::CrossProduct( if (FVector::CrossProduct(
vertices[indices[TargetTringleIndex + 2]] - vertices[indices[TargetTringleIndex]], vertices[indices[triangleIndex + 2]] - vertices[indices[triangleIndex]],
vertices[indices[TargetTringleIndex + 1]] - vertices[indices[TargetTringleIndex]]) vertices[indices[triangleIndex + 1]] - vertices[indices[triangleIndex]])
.Y < 0.f) { .Y < 0.f) {
const int32 targetVertex = indices[triangleIndex];
const int32 targetVertex = indices[TargetTringleIndex]; indices[triangleIndex] = indices[triangleIndex + 2];
indices[TargetTringleIndex] = indices[TargetTringleIndex + 2]; indices[triangleIndex + 2] = targetVertex;
indices[TargetTringleIndex + 2] = targetVertex;
} }
} }
FVector normal = FVector(0, 1, 0); FVector normal = FVector(0, 1, 0);
//Add normals for vertices.
for (int j = 0; j < numVertices; j++) { for (int j = 0; j < numVertices; j++) {
normals.Add(normal); normals.Add(normal);
} }
@ -390,7 +356,7 @@ void USpineSkeletonRendererComponent::UpdateMesh(Skeleton *Skeleton) {
clipper.clipEnd(*slot); clipper.clipEnd(*slot);
} }
Flush(meshSection, vertices, indices, normals, uvs, colors, darkColors, lastMaterial); Flush(meshSection, vertices, indices, normals, uvs, colors, lastMaterial);
clipper.clipEnd(); clipper.clipEnd();
} }

View File

@ -65,19 +65,15 @@ public:
// Need to hold on to the dynamic instances, or the GC will kill us while updating them // Need to hold on to the dynamic instances, or the GC will kill us while updating them
UPROPERTY(Category = Spine, EditAnywhere, BlueprintReadWrite) UPROPERTY(Category = Spine, EditAnywhere, BlueprintReadWrite)
TArray<UMaterialInstanceDynamic *> atlasNormalBlendMaterials; TArray<UMaterialInstanceDynamic *> atlasNormalBlendMaterials;
TMap<spine::AtlasPage *, UMaterialInstanceDynamic *> pageToNormalBlendMaterial;
UPROPERTY(Category = Spine, EditAnywhere, BlueprintReadWrite) UPROPERTY(Category = Spine, EditAnywhere, BlueprintReadWrite)
TArray<UMaterialInstanceDynamic *> atlasAdditiveBlendMaterials; TArray<UMaterialInstanceDynamic *> atlasAdditiveBlendMaterials;
TMap<spine::AtlasPage *, UMaterialInstanceDynamic *> pageToAdditiveBlendMaterial;
UPROPERTY(Category = Spine, EditAnywhere, BlueprintReadWrite) UPROPERTY(Category = Spine, EditAnywhere, BlueprintReadWrite)
TArray<UMaterialInstanceDynamic *> atlasMultiplyBlendMaterials; TArray<UMaterialInstanceDynamic *> atlasMultiplyBlendMaterials;
TMap<spine::AtlasPage *, UMaterialInstanceDynamic *> pageToMultiplyBlendMaterial;
UPROPERTY(Category = Spine, EditAnywhere, BlueprintReadWrite) UPROPERTY(Category = Spine, EditAnywhere, BlueprintReadWrite)
TArray<UMaterialInstanceDynamic *> atlasScreenBlendMaterials; TArray<UMaterialInstanceDynamic *> atlasScreenBlendMaterials;
TMap<spine::AtlasPage *, UMaterialInstanceDynamic *> pageToScreenBlendMaterial;
UPROPERTY(Category = Spine, EditAnywhere, BlueprintReadWrite) UPROPERTY(Category = Spine, EditAnywhere, BlueprintReadWrite)
float DepthOffset = 0.1f; float DepthOffset = 0.1f;
@ -95,14 +91,23 @@ public:
virtual void FinishDestroy() override; virtual void FinishDestroy() override;
protected: protected:
void UpdateRendererMaterial(spine::AtlasPage *CurrentPage, UTexture2D *Texture, void UpdateMaterial(UTexture2D *Texture, UMaterialInstanceDynamic *&CurrentInstance, UMaterialInterface *ParentMaterial);
UMaterialInstanceDynamic *&CurrentInstance, UMaterialInterface *ParentMaterial,
TMap<spine::AtlasPage *, UMaterialInstanceDynamic *> &PageToBlendMaterial);
void UpdateMesh(spine::Skeleton *Skeleton); void UpdateMesh(USpineSkeletonComponent *component, spine::Skeleton *Skeleton);
void Flush(int &Idx, TArray<FVector> &Vertices, TArray<int32> &Indices, TArray<FVector> &Normals, TArray<FVector2D> &Uvs, TArray<FColor> &Colors, TArray<FVector> &Colors2, UMaterialInstanceDynamic *Material); void Flush(int &Idx, TArray<FVector> &Vertices, TArray<int32> &Indices, TArray<FVector> &Normals, TArray<FVector2D> &Uvs, TArray<FColor> &Colors, UMaterialInstanceDynamic *Material);
spine::Vector<float> worldVertices; spine::Vector<float> worldVertices;
spine::SkeletonClipping clipper; spine::SkeletonClipping clipper;
UPROPERTY();
TArray<FVector> vertices;
UPROPERTY();
TArray<int32> indices;
UPROPERTY();
TArray<FVector> normals;
UPROPERTY();
TArray<FVector2D> uvs;
UPROPERTY();
TArray<FColor> colors;
}; };