[ue4] Modifying parent materials (of the four blend modes) updates material instances again. Closes #1791.

This commit is contained in:
Harald Csaszar 2021-01-12 20:18:23 +01:00
parent 2039efc5b7
commit a088270186
2 changed files with 49 additions and 59 deletions

View File

@ -36,7 +36,7 @@
using namespace spine; using namespace spine;
USpineSkeletonRendererComponent::USpineSkeletonRendererComponent (const FObjectInitializer& ObjectInitializer) USpineSkeletonRendererComponent::USpineSkeletonRendererComponent (const FObjectInitializer& ObjectInitializer)
: UProceduralMeshComponent(ObjectInitializer) { : UProceduralMeshComponent(ObjectInitializer) {
PrimaryComponentTick.bCanEverTick = true; PrimaryComponentTick.bCanEverTick = true;
bTickInEditor = true; bTickInEditor = true;
@ -44,16 +44,16 @@ USpineSkeletonRendererComponent::USpineSkeletonRendererComponent (const FObjectI
static ConstructorHelpers::FObjectFinder<UMaterialInterface> NormalMaterialRef(TEXT("/SpinePlugin/SpineUnlitNormalMaterial")); static ConstructorHelpers::FObjectFinder<UMaterialInterface> NormalMaterialRef(TEXT("/SpinePlugin/SpineUnlitNormalMaterial"));
NormalBlendMaterial = NormalMaterialRef.Object; NormalBlendMaterial = NormalMaterialRef.Object;
static ConstructorHelpers::FObjectFinder<UMaterialInterface> AdditiveMaterialRef(TEXT("/SpinePlugin/SpineUnlitAdditiveMaterial")); static ConstructorHelpers::FObjectFinder<UMaterialInterface> AdditiveMaterialRef(TEXT("/SpinePlugin/SpineUnlitAdditiveMaterial"));
AdditiveBlendMaterial = AdditiveMaterialRef.Object; AdditiveBlendMaterial = AdditiveMaterialRef.Object;
static ConstructorHelpers::FObjectFinder<UMaterialInterface> MultiplyMaterialRef(TEXT("/SpinePlugin/SpineUnlitMultiplyMaterial")); static ConstructorHelpers::FObjectFinder<UMaterialInterface> MultiplyMaterialRef(TEXT("/SpinePlugin/SpineUnlitMultiplyMaterial"));
MultiplyBlendMaterial = MultiplyMaterialRef.Object; MultiplyBlendMaterial = MultiplyMaterialRef.Object;
static ConstructorHelpers::FObjectFinder<UMaterialInterface> ScreenMaterialRef(TEXT("/SpinePlugin/SpineUnlitScreenMaterial")); static ConstructorHelpers::FObjectFinder<UMaterialInterface> ScreenMaterialRef(TEXT("/SpinePlugin/SpineUnlitScreenMaterial"));
ScreenBlendMaterial = ScreenMaterialRef.Object; ScreenBlendMaterial = ScreenMaterialRef.Object;
TextureParameterName = FName(TEXT("SpriteTexture")); TextureParameterName = FName(TEXT("SpriteTexture"));
worldVertices.ensureCapacity(1024 * 2); worldVertices.ensureCapacity(1024 * 2);
@ -69,12 +69,12 @@ void USpineSkeletonRendererComponent::BeginPlay () {
void USpineSkeletonRendererComponent::TickComponent (float DeltaTime, ELevelTick TickType, FActorComponentTickFunction* ThisTickFunction) { void USpineSkeletonRendererComponent::TickComponent (float DeltaTime, ELevelTick TickType, FActorComponentTickFunction* ThisTickFunction) {
Super::TickComponent(DeltaTime, TickType, ThisTickFunction); Super::TickComponent(DeltaTime, TickType, ThisTickFunction);
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* skeleton = Cast<USpineSkeletonComponent>(owner->GetComponentByClass(skeletonClass));
UpdateRenderer(skeleton); UpdateRenderer(skeleton);
} }
} }
@ -126,41 +126,12 @@ void USpineSkeletonRendererComponent::UpdateRenderer(USpineSkeletonComponent* sk
for (int i = 0; i < skeleton->Atlas->atlasPages.Num(); i++) { for (int i = 0; i < skeleton->Atlas->atlasPages.Num(); i++) {
AtlasPage* currPage = skeleton->Atlas->GetAtlas()->getPages()[i]; AtlasPage* currPage = skeleton->Atlas->GetAtlas()->getPages()[i];
UTexture2D* texture = skeleton->Atlas->atlasPages[i]; UTexture2D* texture = skeleton->Atlas->atlasPages[i];
UTexture* oldTexture = nullptr;
UMaterialInstanceDynamic* current = atlasNormalBlendMaterials[i]; UpdateRendererMaterial(currPage, texture, atlasNormalBlendMaterials[i], NormalBlendMaterial, pageToNormalBlendMaterial);
if (!current || !current->GetTextureParameterValue(TextureParameterName, oldTexture) || oldTexture != texture) { UpdateRendererMaterial(currPage, texture, atlasAdditiveBlendMaterials[i], AdditiveBlendMaterial, pageToAdditiveBlendMaterial);
UMaterialInstanceDynamic* material = UMaterialInstanceDynamic::Create(NormalBlendMaterial, this); UpdateRendererMaterial(currPage, texture, atlasMultiplyBlendMaterials[i], MultiplyBlendMaterial, pageToMultiplyBlendMaterial);
material->SetTextureParameterValue(TextureParameterName, texture); UpdateRendererMaterial(currPage, texture, atlasScreenBlendMaterials[i], ScreenBlendMaterial, pageToScreenBlendMaterial);
atlasNormalBlendMaterials[i] = material;
}
pageToNormalBlendMaterial.Add(currPage, atlasNormalBlendMaterials[i]);
current = atlasAdditiveBlendMaterials[i];
if (!current || !current->GetTextureParameterValue(TextureParameterName, oldTexture) || oldTexture != texture) {
UMaterialInstanceDynamic* material = UMaterialInstanceDynamic::Create(AdditiveBlendMaterial, this);
material->SetTextureParameterValue(TextureParameterName, texture);
atlasAdditiveBlendMaterials[i] = material;
}
pageToAdditiveBlendMaterial.Add(currPage, atlasAdditiveBlendMaterials[i]);
current = atlasMultiplyBlendMaterials[i];
if (!current || !current->GetTextureParameterValue(TextureParameterName, oldTexture) || oldTexture != texture) {
UMaterialInstanceDynamic* material = UMaterialInstanceDynamic::Create(MultiplyBlendMaterial, this);
material->SetTextureParameterValue(TextureParameterName, texture);
atlasMultiplyBlendMaterials[i] = material;
}
pageToMultiplyBlendMaterial.Add(currPage, atlasMultiplyBlendMaterials[i]);
current = atlasScreenBlendMaterials[i];
if (!current || !current->GetTextureParameterValue(TextureParameterName, oldTexture) || oldTexture != texture) {
UMaterialInstanceDynamic* material = UMaterialInstanceDynamic::Create(ScreenBlendMaterial, this);
material->SetTextureParameterValue(TextureParameterName, texture);
atlasScreenBlendMaterials[i] = material;
}
pageToScreenBlendMaterial.Add(currPage, atlasScreenBlendMaterials[i]);
} }
} }
UpdateMesh(skeleton->GetSkeleton()); UpdateMesh(skeleton->GetSkeleton());
@ -170,6 +141,21 @@ void USpineSkeletonRendererComponent::UpdateRenderer(USpineSkeletonComponent* sk
} }
} }
void USpineSkeletonRendererComponent::UpdateRendererMaterial (spine::AtlasPage *CurrentPage, UTexture2D *Texture,
UMaterialInstanceDynamic *&CurrentInstance, UMaterialInterface *ParentMaterial,
TMap<spine::AtlasPage *, UMaterialInstanceDynamic *> &PageToBlendMaterial) {
UTexture* oldTexture = nullptr;
if (!CurrentInstance || !CurrentInstance->GetTextureParameterValue(TextureParameterName, oldTexture) ||
oldTexture != Texture || CurrentInstance->Parent != ParentMaterial) {
UMaterialInstanceDynamic* material = UMaterialInstanceDynamic::Create(ParentMaterial, this);
material->SetTextureParameterValue(TextureParameterName, Texture);
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, TArray<FVector>& Colors2, UMaterialInstanceDynamic* Material) {
if (Vertices.Num() == 0) return; if (Vertices.Num() == 0) return;
SetMaterial(Idx, Material); SetMaterial(Idx, Material);
@ -192,7 +178,7 @@ void USpineSkeletonRendererComponent::UpdateMesh(Skeleton* Skeleton) {
TArray<FVector2D> uvs; TArray<FVector2D> uvs;
TArray<FColor> colors; TArray<FColor> colors;
TArray<FVector> darkColors; TArray<FVector> darkColors;
int idx = 0; int idx = 0;
int meshSection = 0; int meshSection = 0;
UMaterialInstanceDynamic* lastMaterial = nullptr; UMaterialInstanceDynamic* lastMaterial = nullptr;
@ -231,7 +217,7 @@ void USpineSkeletonRendererComponent::UpdateMesh(Skeleton* Skeleton) {
clipper.clipEnd(*slot); clipper.clipEnd(*slot);
continue; continue;
} }
if (attachment->getRTTI().isExactly(RegionAttachment::rtti)) { if (attachment->getRTTI().isExactly(RegionAttachment::rtti)) {
RegionAttachment* regionAttachment = (RegionAttachment*)attachment; RegionAttachment* regionAttachment = (RegionAttachment*)attachment;
@ -258,7 +244,7 @@ void USpineSkeletonRendererComponent::UpdateMesh(Skeleton* Skeleton) {
} }
attachmentColor.set(mesh->getColor()); attachmentColor.set(mesh->getColor());
attachmentAtlasRegion = (AtlasRegion*)mesh->getRendererObject(); attachmentAtlasRegion = (AtlasRegion*)mesh->getRendererObject();
mesh->computeWorldVertices(*slot, 0, mesh->getWorldVerticesLength(), *attachmentVertices, 0, 2); mesh->computeWorldVertices(*slot, 0, mesh->getWorldVerticesLength(), *attachmentVertices, 0, 2);
attachmentIndices = mesh->getTriangles().buffer(); attachmentIndices = mesh->getTriangles().buffer();
attachmentUvs = mesh->getUVs().buffer(); attachmentUvs = mesh->getUVs().buffer();
@ -339,7 +325,7 @@ void USpineSkeletonRendererComponent::UpdateMesh(Skeleton* Skeleton) {
float dr = slot->hasDarkColor() ? slot->getDarkColor().r : 0.0f; float dr = slot->hasDarkColor() ? slot->getDarkColor().r : 0.0f;
float dg = slot->hasDarkColor() ? slot->getDarkColor().g : 0.0f; float dg = slot->hasDarkColor() ? slot->getDarkColor().g : 0.0f;
float db = slot->hasDarkColor() ? slot->getDarkColor().b : 0.0f; float db = slot->hasDarkColor() ? slot->getDarkColor().b : 0.0f;
float* verticesPtr = attachmentVertices->buffer(); float* verticesPtr = attachmentVertices->buffer();
for (int j = 0; j < numVertices << 1; j += 2) { for (int j = 0; j < numVertices << 1; j += 2) {
@ -357,9 +343,9 @@ void USpineSkeletonRendererComponent::UpdateMesh(Skeleton* Skeleton) {
idx += numVertices; idx += numVertices;
depthOffset += this->DepthOffset; depthOffset += this->DepthOffset;
clipper.clipEnd(*slot); clipper.clipEnd(*slot);
} }
Flush(meshSection, vertices, indices, normals, uvs, colors, darkColors, lastMaterial); Flush(meshSection, vertices, indices, normals, uvs, colors, darkColors, lastMaterial);
clipper.clipEnd(); clipper.clipEnd();
} }

View File

@ -39,11 +39,11 @@ UCLASS(ClassGroup=(Spine), meta=(BlueprintSpawnableComponent))
class SPINEPLUGIN_API USpineSkeletonRendererComponent: public UProceduralMeshComponent { class SPINEPLUGIN_API USpineSkeletonRendererComponent: public UProceduralMeshComponent {
GENERATED_BODY() GENERATED_BODY()
public: public:
USpineSkeletonRendererComponent (const FObjectInitializer& ObjectInitializer); USpineSkeletonRendererComponent (const FObjectInitializer& ObjectInitializer);
virtual void BeginPlay () override; virtual void BeginPlay () override;
virtual void TickComponent (float DeltaTime, ELevelTick TickType, FActorComponentTickFunction* ThisTickFunction) override; virtual void TickComponent (float DeltaTime, ELevelTick TickType, FActorComponentTickFunction* ThisTickFunction) override;
/* Updates this skeleton renderer using the provided skeleton animation component. */ /* Updates this skeleton renderer using the provided skeleton animation component. */
@ -52,13 +52,13 @@ public:
// Material Instance parents // Material Instance parents
UPROPERTY(Category = Spine, EditAnywhere, BlueprintReadWrite) UPROPERTY(Category = Spine, EditAnywhere, BlueprintReadWrite)
UMaterialInterface* NormalBlendMaterial; UMaterialInterface* NormalBlendMaterial;
UPROPERTY(Category = Spine, EditAnywhere, BlueprintReadWrite) UPROPERTY(Category = Spine, EditAnywhere, BlueprintReadWrite)
UMaterialInterface* AdditiveBlendMaterial; UMaterialInterface* AdditiveBlendMaterial;
UPROPERTY(Category = Spine, EditAnywhere, BlueprintReadWrite) UPROPERTY(Category = Spine, EditAnywhere, BlueprintReadWrite)
UMaterialInterface* MultiplyBlendMaterial; UMaterialInterface* MultiplyBlendMaterial;
UPROPERTY(Category = Spine, EditAnywhere, BlueprintReadWrite) UPROPERTY(Category = Spine, EditAnywhere, BlueprintReadWrite)
UMaterialInterface* ScreenBlendMaterial; UMaterialInterface* ScreenBlendMaterial;
@ -66,22 +66,22 @@ public:
UPROPERTY(Category = Spine, EditAnywhere, BlueprintReadWrite) UPROPERTY(Category = Spine, EditAnywhere, BlueprintReadWrite)
TArray<UMaterialInstanceDynamic*> atlasNormalBlendMaterials; TArray<UMaterialInstanceDynamic*> atlasNormalBlendMaterials;
TMap<spine::AtlasPage*, UMaterialInstanceDynamic*> pageToNormalBlendMaterial; 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; 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; 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; TMap<spine::AtlasPage*, UMaterialInstanceDynamic*> pageToScreenBlendMaterial;
UPROPERTY(Category = Spine, EditAnywhere, BlueprintReadWrite) UPROPERTY(Category = Spine, EditAnywhere, BlueprintReadWrite)
float DepthOffset = 0.1f; float DepthOffset = 0.1f;
UPROPERTY(Category = Spine, EditAnywhere, BlueprintReadWrite) UPROPERTY(Category = Spine, EditAnywhere, BlueprintReadWrite)
FName TextureParameterName; FName TextureParameterName;
@ -93,12 +93,16 @@ public:
bool bCreateCollision; bool bCreateCollision;
virtual void FinishDestroy() override; virtual void FinishDestroy() override;
protected: protected:
void UpdateRendererMaterial (spine::AtlasPage *CurrentPage, UTexture2D *Texture,
UMaterialInstanceDynamic *&CurrentInstance, UMaterialInterface *ParentMaterial,
TMap<spine::AtlasPage *, UMaterialInstanceDynamic *> &PageToBlendMaterial);
void UpdateMesh (spine::Skeleton* Skeleton); void UpdateMesh (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, TArray<FVector> &Colors2, UMaterialInstanceDynamic* Material);
spine::Vector<float> worldVertices; spine::Vector<float> worldVertices;
spine::SkeletonClipping clipper; spine::SkeletonClipping clipper;
}; };