More UMG code, doesn't render.

This commit is contained in:
badlogic 2018-06-13 18:04:44 +02:00
parent 79e607a5f1
commit 4aceccc1ca
4 changed files with 396 additions and 11 deletions

View File

@ -41,12 +41,16 @@
#include "Slate/SlateVectorArtInstanceData.h"
#include "Slate/SMeshWidget.h"
#include "SlateMaterialBrush.h"
#include <spine/spine.h>
#include "SpineWidget.h"
using namespace spine;
void SSpineWidget::Construct(const FArguments& args) {
}
void SSpineWidget::SetBrush(FSlateBrush* Brush) {
brush = Brush;
void SSpineWidget::SetData(USpineWidget* Widget) {
this->widget = Widget;
}
static void setVertex(FSlateVertex* vertex, float x, float y, float u, float v, const FColor& color, const FVector2D& offset) {
@ -65,7 +69,94 @@ static void setVertex(FSlateVertex* vertex, float x, float y, float u, float v,
int32 SSpineWidget::OnPaint(const FPaintArgs& Args, const FGeometry& AllottedGeometry, const FSlateRect& MyClippingRect, FSlateWindowElementList& OutDrawElements,
int32 LayerId, const FWidgetStyle& InWidgetStyle, bool bParentEnabled) const {
SSpineWidget* self = (SSpineWidget*)this;
if (widget && widget->skeleton && widget->Atlas) {
widget->skeleton->getColor().set(widget->Color.R, widget->Color.G, widget->Color.B, widget->Color.A);
if (widget->atlasNormalBlendMaterials.Num() != widget->Atlas->atlasPages.Num()) {
widget->atlasNormalBlendMaterials.SetNum(0);
widget->pageToNormalBlendMaterial.Empty();
widget->atlasAdditiveBlendMaterials.SetNum(0);
widget->pageToAdditiveBlendMaterial.Empty();
widget->atlasMultiplyBlendMaterials.SetNum(0);
widget->pageToMultiplyBlendMaterial.Empty();
widget->atlasScreenBlendMaterials.SetNum(0);
widget->pageToScreenBlendMaterial.Empty();
for (int i = 0; i < widget->Atlas->atlasPages.Num(); i++) {
AtlasPage* currPage = widget->Atlas->GetAtlas(false)->getPages()[i];
UMaterialInstanceDynamic* material = UMaterialInstanceDynamic::Create(widget->NormalBlendMaterial, widget);
material->SetTextureParameterValue(widget->TextureParameterName, widget->Atlas->atlasPages[i]);
widget->atlasNormalBlendMaterials.Add(material);
widget->pageToNormalBlendMaterial.Add(currPage, material);
material = UMaterialInstanceDynamic::Create(widget->AdditiveBlendMaterial, widget);
material->SetTextureParameterValue(widget->TextureParameterName, widget->Atlas->atlasPages[i]);
widget->atlasAdditiveBlendMaterials.Add(material);
widget->pageToAdditiveBlendMaterial.Add(currPage, material);
material = UMaterialInstanceDynamic::Create(widget->MultiplyBlendMaterial, widget);
material->SetTextureParameterValue(widget->TextureParameterName, widget->Atlas->atlasPages[i]);
widget->atlasMultiplyBlendMaterials.Add(material);
widget->pageToMultiplyBlendMaterial.Add(currPage, material);
material = UMaterialInstanceDynamic::Create(widget->ScreenBlendMaterial, widget);
material->SetTextureParameterValue(widget->TextureParameterName, widget->Atlas->atlasPages[i]);
widget->atlasScreenBlendMaterials.Add(material);
widget->pageToScreenBlendMaterial.Add(currPage, material);
}
} else {
widget->pageToNormalBlendMaterial.Empty();
widget->pageToAdditiveBlendMaterial.Empty();
widget->pageToMultiplyBlendMaterial.Empty();
widget->pageToScreenBlendMaterial.Empty();
for (int i = 0; i < widget->Atlas->atlasPages.Num(); i++) {
AtlasPage* currPage = widget->Atlas->GetAtlas(false)->getPages()[i];
UTexture2D* texture = widget->Atlas->atlasPages[i];
UTexture* oldTexture = nullptr;
UMaterialInstanceDynamic* current = widget->atlasNormalBlendMaterials[i];
if (!current || !current->GetTextureParameterValue(widget->TextureParameterName, oldTexture) || oldTexture != texture) {
UMaterialInstanceDynamic* material = UMaterialInstanceDynamic::Create(widget->NormalBlendMaterial, widget);
material->SetTextureParameterValue(widget->TextureParameterName, texture);
widget->atlasNormalBlendMaterials[i] = material;
}
widget->pageToNormalBlendMaterial.Add(currPage, widget->atlasNormalBlendMaterials[i]);
current = widget->atlasAdditiveBlendMaterials[i];
if (!current || !current->GetTextureParameterValue(widget->TextureParameterName, oldTexture) || oldTexture != texture) {
UMaterialInstanceDynamic* material = UMaterialInstanceDynamic::Create(widget->AdditiveBlendMaterial, widget);
material->SetTextureParameterValue(widget->TextureParameterName, texture);
widget->atlasAdditiveBlendMaterials[i] = material;
}
widget->pageToAdditiveBlendMaterial.Add(currPage, widget->atlasAdditiveBlendMaterials[i]);
current = widget->atlasMultiplyBlendMaterials[i];
if (!current || !current->GetTextureParameterValue(widget->TextureParameterName, oldTexture) || oldTexture != texture) {
UMaterialInstanceDynamic* material = UMaterialInstanceDynamic::Create(widget->MultiplyBlendMaterial, widget);
material->SetTextureParameterValue(widget->TextureParameterName, texture);
widget->atlasMultiplyBlendMaterials[i] = material;
}
widget->pageToMultiplyBlendMaterial.Add(currPage, widget->atlasMultiplyBlendMaterials[i]);
current = widget->atlasScreenBlendMaterials[i];
if (!current || !current->GetTextureParameterValue(widget->TextureParameterName, oldTexture) || oldTexture != texture) {
UMaterialInstanceDynamic* material = UMaterialInstanceDynamic::Create(widget->ScreenBlendMaterial, widget);
material->SetTextureParameterValue(widget->TextureParameterName, texture);
widget->atlasScreenBlendMaterials[i] = material;
}
widget->pageToScreenBlendMaterial.Add(currPage, widget->atlasScreenBlendMaterials[i]);
}
}
// self->UpdateMesh(LayerId, OutDrawElements, AllottedGeometry, widget->skeleton);
}
//return LayerId;
self->renderData.IndexData.SetNumUninitialized(6);
uint32* indexData = (uint32*)renderData.IndexData.GetData();
indexData[0] = 0;
@ -79,14 +170,18 @@ int32 SSpineWidget::OnPaint(const FPaintArgs& Args, const FGeometry& AllottedGeo
FSlateVertex* vertexData = (FSlateVertex*)renderData.VertexData.GetData();
FVector2D offset = AllottedGeometry.AbsolutePosition;
FColor white = FColor(0xffffffff);
float width = AllottedGeometry.GetAbsoluteSize().X;
float height = AllottedGeometry.GetAbsoluteSize().Y;
setVertex(&vertexData[0], 0, 0, 0, 0, white, offset);
setVertex(&vertexData[1], 200, 0, 1, 0, white, offset);
setVertex(&vertexData[2], 200, 200, 1, 1, white, offset);
setVertex(&vertexData[3], 0, 200, 0, 1, white, offset);
setVertex(&vertexData[1], width, 0, 1, 0, white, offset);
setVertex(&vertexData[2], width, height, 1, 1, white, offset);
setVertex(&vertexData[3], 0, height, 0, 1, white, offset);
if (brush && renderData.VertexData.Num() > 0 && renderData.IndexData.Num() > 0) {
FSlateShaderResourceProxy* shaderResource = FSlateDataPayload::ResourceManager->GetShaderResource(*brush);
FSlateResourceHandle resourceHandle = FSlateApplication::Get().GetRenderer()->GetResourceHandle(*brush);
FSlateShaderResourceProxy* shaderResource = FSlateDataPayload::ResourceManager->GetShaderResource(widget->Brush);
FSlateResourceHandle resourceHandle = FSlateApplication::Get().GetRenderer()->GetResourceHandle(widget->Brush);
if (shaderResource)
FSlateDrawElement::MakeCustomVerts(OutDrawElements, LayerId, resourceHandle, renderData.VertexData,
renderData.IndexData, nullptr, 0, 0);
@ -94,3 +189,171 @@ int32 SSpineWidget::OnPaint(const FPaintArgs& Args, const FGeometry& AllottedGeo
return LayerId;
}
void SSpineWidget::Flush(int32 LayerId, FSlateWindowElementList& OutDrawElements, const FGeometry& AllottedGeometry, int &Idx, TArray<FVector> &Vertices, TArray<int32> &Indices, TArray<FVector2D> &Uvs, TArray<FColor> &Colors, TArray<FVector>& Colors2, UMaterialInstanceDynamic* Material) {
if (Vertices.Num() == 0) return;
SSpineWidget* self = (SSpineWidget*)this;
self->renderData.IndexData.SetNumUninitialized(Indices.Num());
uint32* indexData = (uint32*)renderData.IndexData.GetData();
memcpy(indexData, Indices.GetData(), sizeof(uint32) * Indices.Num());
self->renderData.VertexData.SetNumUninitialized(Vertices.Num());
FSlateVertex* vertexData = (FSlateVertex*)renderData.VertexData.GetData();
FVector2D offset = AllottedGeometry.AbsolutePosition;
FColor white = FColor(0xffffffff);
float width = AllottedGeometry.GetAbsoluteSize().X;
float height = AllottedGeometry.GetAbsoluteSize().Y;
for (size_t i = 0; i < Vertices.Num(); i++) {
setVertex(&vertexData[i], Vertices[i].X, Vertices[i].Y, Uvs[i].X, Uvs[i].Y, Colors[i], offset);
}
FSlateBrush brush;
brush.SetResourceObject(Material);
brush = widget->Brush;
FSlateShaderResourceProxy* shaderResource = FSlateDataPayload::ResourceManager->GetShaderResource(brush);
if (shaderResource) {
FSlateResourceHandle resourceHandle = FSlateApplication::Get().GetRenderer()->GetResourceHandle(brush);
FSlateDrawElement::MakeCustomVerts(OutDrawElements, LayerId, resourceHandle, renderData.VertexData, renderData.IndexData, nullptr, 0, 0);
}
Vertices.SetNum(0);
Indices.SetNum(0);
Uvs.SetNum(0);
Colors.SetNum(0);
Colors2.SetNum(0);
Idx++;
}
void SSpineWidget::UpdateMesh(int32 LayerId, FSlateWindowElementList& OutDrawElements, const FGeometry& AllottedGeometry, Skeleton* Skeleton) {
TArray<FVector> vertices;
TArray<int32> indices;
TArray<FVector2D> uvs;
TArray<FColor> colors;
TArray<FVector> darkColors;
int idx = 0;
int meshSection = 0;
UMaterialInstanceDynamic* lastMaterial = nullptr;
SkeletonClipping &clipper = widget->clipper;
Vector<float> &worldVertices = widget->worldVertices;
float depthOffset = 0;
unsigned short quadIndices[] = { 0, 1, 2, 0, 2, 3 };
for (int i = 0; i < Skeleton->getSlots().size(); ++i) {
Vector<float> &attachmentVertices = worldVertices;
unsigned short* attachmentIndices = nullptr;
int numVertices;
int numIndices;
AtlasRegion* attachmentAtlasRegion = nullptr;
Color attachmentColor;
attachmentColor.set(1, 1, 1, 1);
float* attachmentUvs = nullptr;
Slot* slot = Skeleton->getDrawOrder()[i];
Attachment* attachment = slot->getAttachment();
if (!attachment) continue;
if (!attachment->getRTTI().isExactly(RegionAttachment::rtti) && !attachment->getRTTI().isExactly(MeshAttachment::rtti) && !attachment->getRTTI().isExactly(ClippingAttachment::rtti)) continue;
if (attachment->getRTTI().isExactly(RegionAttachment::rtti)) {
RegionAttachment* regionAttachment = (RegionAttachment*)attachment;
attachmentColor.set(regionAttachment->getColor());
attachmentAtlasRegion = (AtlasRegion*)regionAttachment->getRendererObject();
regionAttachment->computeWorldVertices(slot->getBone(), attachmentVertices, 0, 2);
attachmentIndices = quadIndices;
attachmentUvs = regionAttachment->getUVs().buffer();
numVertices = 4;
numIndices = 6;
}
else if (attachment->getRTTI().isExactly(MeshAttachment::rtti)) {
MeshAttachment* mesh = (MeshAttachment*)attachment;
attachmentColor.set(mesh->getColor());
attachmentAtlasRegion = (AtlasRegion*)mesh->getRendererObject();
mesh->computeWorldVertices(*slot, 0, mesh->getWorldVerticesLength(), attachmentVertices, 0, 2);
attachmentIndices = mesh->getTriangles().buffer();
attachmentUvs = mesh->getUVs().buffer();
numVertices = mesh->getWorldVerticesLength() >> 1;
numIndices = mesh->getTriangles().size();
}
else /* clipping */ {
ClippingAttachment* clip = (ClippingAttachment*)attachment;
clipper.clipStart(*slot, clip);
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 (!widget->pageToNormalBlendMaterial.Contains(attachmentAtlasRegion->page)) continue;
material = widget->pageToNormalBlendMaterial[attachmentAtlasRegion->page];
break;
case BlendMode_Additive:
if (!widget->pageToAdditiveBlendMaterial.Contains(attachmentAtlasRegion->page)) continue;
material = widget->pageToAdditiveBlendMaterial[attachmentAtlasRegion->page];
break;
case BlendMode_Multiply:
if (!widget->pageToMultiplyBlendMaterial.Contains(attachmentAtlasRegion->page)) continue;
material = widget->pageToMultiplyBlendMaterial[attachmentAtlasRegion->page];
break;
case BlendMode_Screen:
if (!widget->pageToScreenBlendMaterial.Contains(attachmentAtlasRegion->page)) continue;
material = widget->pageToScreenBlendMaterial[attachmentAtlasRegion->page];
break;
default:
if (!widget->pageToNormalBlendMaterial.Contains(attachmentAtlasRegion->page)) continue;
material = widget->pageToNormalBlendMaterial[attachmentAtlasRegion->page];
}
if (clipper.isClipping()) {
clipper.clipTriangles(attachmentVertices.buffer(), attachmentIndices, numIndices, attachmentUvs, 2);
attachmentVertices = clipper.getClippedVertices();
numVertices = clipper.getClippedVertices().size() >> 1;
attachmentIndices = clipper.getClippedTriangles().buffer();
numIndices = clipper.getClippedTriangles().size();
attachmentUvs = clipper.getClippedUVs().buffer();
if (clipper.getClippedTriangles().size() == 0) continue;
}
if (lastMaterial != material) {
Flush(LayerId, OutDrawElements, AllottedGeometry, meshSection, vertices, indices, uvs, colors, darkColors, lastMaterial);
lastMaterial = material;
idx = 0;
}
uint8 r = static_cast<uint8>(Skeleton->getColor().r * slot->getColor().r * attachmentColor.r * 255);
uint8 g = static_cast<uint8>(Skeleton->getColor().g * slot->getColor().g * attachmentColor.g * 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);
float dr = slot->hasDarkColor() ? slot->getDarkColor().r : 0.0f;
float dg = slot->hasDarkColor() ? slot->getDarkColor().g : 0.0f;
float db = slot->hasDarkColor() ? slot->getDarkColor().b : 0.0f;
for (int j = 0; j < numVertices << 1; j += 2) {
colors.Add(FColor(r, g, b, a));
darkColors.Add(FVector(dr, dg, db));
vertices.Add(FVector(attachmentVertices[j], depthOffset, attachmentVertices[j + 1]));
uvs.Add(FVector2D(attachmentUvs[j], attachmentUvs[j + 1]));
}
for (int j = 0; j < numIndices; j++) {
indices.Add(idx + attachmentIndices[j]);
}
idx += numVertices;
depthOffset += widget->DepthOffset;
clipper.clipEnd(*slot);
}
Flush(LayerId, OutDrawElements, AllottedGeometry, meshSection, vertices, indices, uvs, colors, darkColors, lastMaterial);
clipper.clipEnd();
}

View File

@ -31,17 +31,39 @@
#include "SpinePluginPrivatePCH.h"
#include "SpineWidget.h"
#include "SSpineWidget.h"
#include "Engine.h"
#define LOCTEXT_NAMESPACE "Spine"
USpineWidget::USpineWidget(const FObjectInitializer& ObjectInitializer): Super(ObjectInitializer) {
static ConstructorHelpers::FObjectFinder<UMaterialInterface> NormalMaterialRef(TEXT("/SpinePlugin/SpineUnlitNormalMaterial"));
NormalBlendMaterial = NormalMaterialRef.Object;
static ConstructorHelpers::FObjectFinder<UMaterialInterface> AdditiveMaterialRef(TEXT("/SpinePlugin/SpineUnlitAdditiveMaterial"));
AdditiveBlendMaterial = AdditiveMaterialRef.Object;
static ConstructorHelpers::FObjectFinder<UMaterialInterface> MultiplyMaterialRef(TEXT("/SpinePlugin/SpineUnlitMultiplyMaterial"));
MultiplyBlendMaterial = MultiplyMaterialRef.Object;
static ConstructorHelpers::FObjectFinder<UMaterialInterface> ScreenMaterialRef(TEXT("/SpinePlugin/SpineUnlitScreenMaterial"));
ScreenBlendMaterial = ScreenMaterialRef.Object;
TextureParameterName = FName(TEXT("SpriteTexture"));
worldVertices.ensureCapacity(1024 * 2);
}
void USpineWidget::SynchronizeProperties() {
Super::SynchronizeProperties();
if (slateWidget.IsValid()) {
slateWidget->SetBrush(&Brush);
CheckState();
if (skeleton) {
InternalTick(0);
slateWidget->SetData(this);
} else {
slateWidget->SetData(nullptr);
}
}
}
@ -60,3 +82,37 @@ const FText USpineWidget::GetPaletteCategory() {
return LOCTEXT("Spine", "Spine");
}
#endif
void USpineWidget::InternalTick(float DeltaTime) {
CheckState();
if (skeleton) {
skeleton->updateWorldTransform();
}
}
void USpineWidget::CheckState() {
if (lastAtlas != Atlas || lastData != SkeletonData) {
DisposeState();
if (Atlas && SkeletonData) {
spine::SkeletonData* data = SkeletonData->GetSkeletonData(Atlas->GetAtlas(false), false);
skeleton = new (__FILE__, __LINE__) spine::Skeleton(data);
}
lastAtlas = Atlas;
lastData = SkeletonData;
}
}
void USpineWidget::DisposeState() {
if (skeleton) {
delete skeleton;
skeleton = nullptr;
}
}
void USpineWidget::FinishDestroy() {
DisposeState();
Super::FinishDestroy();
}

View File

@ -32,6 +32,10 @@
#include "SlateCore.h"
#include "Slate/SMeshWidget.h"
#include <spine/spine.h>
#include "SpineAtlasAsset.h"
class USpineWidget;
class SSpineWidget: public SMeshWidget {
@ -42,11 +46,16 @@ public:
void Construct(const FArguments& Args);
void SetBrush(FSlateBrush* Brush);
void SetData(USpineWidget* Widget);
protected:
virtual int32 OnPaint(const FPaintArgs& Args, const FGeometry& AllottedGeometry, const FSlateRect& MyCullingRect, FSlateWindowElementList& OutDrawElements, int32 LayerId, const FWidgetStyle& InWidgetStyle, bool bParentEnabled) const override;
FSlateBrush* brush = nullptr;
void UpdateMesh(int32 LayerId, FSlateWindowElementList& OutDrawElements, const FGeometry& AllottedGeometry, spine::Skeleton* Skeleton);
void Flush(int32 LayerId, FSlateWindowElementList& OutDrawElements, const FGeometry& AllottedGeometry, int &Idx, TArray<FVector> &Vertices, TArray<int32> &Indices, TArray<FVector2D> &Uvs, TArray<FColor> &Colors, TArray<FVector> &Colors2, UMaterialInstanceDynamic* Material);
USpineWidget* widget;
FSlateBrush* brush;
FRenderData renderData;
};

View File

@ -50,11 +50,68 @@ public:
virtual const FText GetPaletteCategory() override;
#endif
UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = Spine)
USpineAtlasAsset* Atlas;
UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = Spine)
USpineSkeletonDataAsset* SkeletonData;
UPROPERTY(Category = Spine, EditAnywhere, BlueprintReadOnly)
UMaterialInterface* NormalBlendMaterial;
UPROPERTY(Category = Spine, EditAnywhere, BlueprintReadOnly)
UMaterialInterface* AdditiveBlendMaterial;
UPROPERTY(Category = Spine, EditAnywhere, BlueprintReadOnly)
UMaterialInterface* MultiplyBlendMaterial;
UPROPERTY(Category = Spine, EditAnywhere, BlueprintReadOnly)
UMaterialInterface* ScreenBlendMaterial;
UPROPERTY(Category = Spine, EditAnywhere, BlueprintReadWrite)
FName TextureParameterName;
UPROPERTY(Category = Spine, EditAnywhere, BlueprintReadWrite)
float DepthOffset = 0.1f;
UPROPERTY(Category = Spine, EditAnywhere, BlueprintReadWrite)
FLinearColor Color = FLinearColor(1, 1, 1, 1);
UPROPERTY(Category = Spine, EditAnywhere, BlueprintReadOnly)
FSlateBrush Brush;
virtual void FinishDestroy() override;
protected:
friend class SSpineWidget;
virtual TSharedRef<SWidget> RebuildWidget() override;
virtual void CheckState();
virtual void InternalTick(float DeltaTime);
virtual void DisposeState();
TSharedPtr<SSpineWidget> slateWidget;
spine::Skeleton* skeleton;
USpineAtlasAsset* lastAtlas = nullptr;
USpineSkeletonDataAsset* lastData = nullptr;
// Need to hold on to the dynamic instances, or the GC will kill us while updating them
UPROPERTY()
TArray<UMaterialInstanceDynamic*> atlasNormalBlendMaterials;
TMap<spine::AtlasPage*, UMaterialInstanceDynamic*> pageToNormalBlendMaterial;
UPROPERTY()
TArray<UMaterialInstanceDynamic*> atlasAdditiveBlendMaterials;
TMap<spine::AtlasPage*, UMaterialInstanceDynamic*> pageToAdditiveBlendMaterial;
UPROPERTY()
TArray<UMaterialInstanceDynamic*> atlasMultiplyBlendMaterials;
TMap<spine::AtlasPage*, UMaterialInstanceDynamic*> pageToMultiplyBlendMaterial;
UPROPERTY()
TArray<UMaterialInstanceDynamic*> atlasScreenBlendMaterials;
TMap<spine::AtlasPage*, UMaterialInstanceDynamic*> pageToScreenBlendMaterial;
spine::Vector<float> worldVertices;
spine::SkeletonClipping clipper;
};