From cf68a29da1395f74ddfc36c819ebddb9065aaa03 Mon Sep 17 00:00:00 2001 From: Harald Csaszar Date: Mon, 13 Feb 2023 11:11:45 +0100 Subject: [PATCH 1/3] [unity] Fixed `Apply Regions as Texture Sprite Slices` failing when filename contains . before extension. Closes #2246. --- .../spine-unity/Editor/Asset Types/SpineAtlasAssetInspector.cs | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/spine-unity/Assets/Spine/Editor/spine-unity/Editor/Asset Types/SpineAtlasAssetInspector.cs b/spine-unity/Assets/Spine/Editor/spine-unity/Editor/Asset Types/SpineAtlasAssetInspector.cs index 22466efde..9c337d51d 100644 --- a/spine-unity/Assets/Spine/Editor/spine-unity/Editor/Asset Types/SpineAtlasAssetInspector.cs +++ b/spine-unity/Assets/Spine/Editor/spine-unity/Editor/Asset Types/SpineAtlasAssetInspector.cs @@ -320,12 +320,11 @@ namespace Spine.Unity.Editor { List sprites = new List(spriteSheet); List regions = SpineAtlasAssetInspector.GetRegions(atlas); - char[] FilenameDelimiter = { '.' }; int updatedCount = 0; int addedCount = 0; foreach (AtlasRegion r in regions) { - string pageName = r.page.name.Split(FilenameDelimiter, StringSplitOptions.RemoveEmptyEntries)[0]; + string pageName = System.IO.Path.GetFileNameWithoutExtension(r.page.name); string textureName = texture.name; bool pageMatch = string.Equals(pageName, textureName, StringComparison.Ordinal); From 2751d7642672cb1059e1001ff00db3f21381e0cf Mon Sep 17 00:00:00 2001 From: Mario Zechner Date: Mon, 13 Feb 2023 14:55:26 +0100 Subject: [PATCH 2/3] [godot] Closes #2245, adds mesh based rendering for Godot 4.0, improving performance 100x fold. --- spine-godot/spine_godot/SpineSprite.cpp | 103 +++++++++++++++++++----- spine-godot/spine_godot/SpineSprite.h | 30 +++++-- 2 files changed, 107 insertions(+), 26 deletions(-) diff --git a/spine-godot/spine_godot/SpineSprite.cpp b/spine-godot/spine_godot/SpineSprite.cpp index a9ed74fef..5723f1fa9 100644 --- a/spine-godot/spine_godot/SpineSprite.cpp +++ b/spine-godot/spine_godot/SpineSprite.cpp @@ -33,6 +33,10 @@ #include "SpineSkeleton.h" #include "SpineRendererObject.h" #include "SpineSlotNode.h" +#include "core/math/transform_2d.h" +#include "core/variant/array.h" +#include "scene/resources/mesh.h" +#include "servers/rendering_server.h" #if VERSION_MAJOR > 3 #include "core/config/engine.h" @@ -54,6 +58,14 @@ static spine::Vector quad_indices; static spine::Vector scratch_vertices; static Vector scratch_points; +static void clear_triangles(SpineMesh2D *mesh_instance) { +#if VERSION_MAJOR > 3 + RenderingServer::get_singleton()->canvas_item_clear(mesh_instance->get_canvas_item()); +#else + VisualServer::get_singleton()->canvas_item_clear(mesh_instance->get_canvas_item()); +#endif +} + static void add_triangles(SpineMesh2D *mesh_instance, const Vector &vertices, const Vector &uvs, @@ -61,15 +73,7 @@ static void add_triangles(SpineMesh2D *mesh_instance, const Vector &indices, SpineRendererObject *renderer_object) { #if VERSION_MAJOR > 3 - RenderingServer::get_singleton()->canvas_item_add_triangle_array(mesh_instance->get_canvas_item(), - indices, - vertices, - colors, - uvs, - Vector(), - Vector(), - renderer_object->canvas_texture.is_valid() ? renderer_object->canvas_texture->get_rid() : RID(), - -1); + mesh_instance->update_mesh(vertices, uvs, colors, indices, renderer_object); #else auto texture = renderer_object->texture; auto normal_map = renderer_object->normal_map; @@ -99,8 +103,8 @@ void SpineMesh2D::_notification(int what) { update(); #endif break; - case NOTIFICATION_DRAW: - //clear_triangles(this); + case NOTIFICATION_DRAW: + clear_triangles(this); if (renderer_object) add_triangles(this, vertices, uvs, colors, indices, renderer_object); break; @@ -112,6 +116,66 @@ void SpineMesh2D::_notification(int what) { void SpineMesh2D::_bind_methods() { } +void SpineMesh2D::update_mesh(const Vector &vertices, + const Vector &uvs, + const Vector &colors, + const Vector &indices, + SpineRendererObject *renderer_object) { +#if VERSION_MAJOR > 3 + if (!mesh.is_valid() || vertices.size() != num_vertices || indices.size() != num_indices || last_indices_id != indices_id) { + if (mesh.is_valid()) { + RS::get_singleton()->free(mesh); + } + mesh = RS::get_singleton()->mesh_create(); + Array arrays; + arrays.resize(Mesh::ARRAY_MAX); + arrays[Mesh::ARRAY_VERTEX] = vertices; + arrays[Mesh::ARRAY_TEX_UV] = uvs; + arrays[Mesh::ARRAY_COLOR] = colors; + arrays[Mesh::ARRAY_INDEX] = indices; + RS::SurfaceData surface; + uint32_t skin_stride; + RS::get_singleton()->mesh_create_surface_data_from_arrays(&surface, (RS::PrimitiveType) Mesh::PRIMITIVE_TRIANGLES, arrays, TypedArray(), Dictionary(), Mesh::ArrayFormat::ARRAY_FLAG_USE_DYNAMIC_UPDATE); + RS::get_singleton()->mesh_add_surface(mesh, surface); + RS::get_singleton()->mesh_surface_make_offsets_from_format(surface.format, surface.vertex_count, surface.index_count, surface_offsets, vertex_stride, attribute_stride, skin_stride); + num_vertices = vertices.size(); + num_indices = indices.size(); + vertex_buffer = surface.vertex_data; + attribute_buffer = surface.attribute_data; + last_indices_id = indices_id; + } else { + AABB aabb_new; + uint8_t *vertex_write_buffer = vertex_buffer.ptrw(); + uint8_t *attribute_write_buffer = attribute_buffer.ptrw(); + uint8_t color[4] = { + uint8_t(CLAMP(colors[0].r * 255.0, 0.0, 255.0)), + uint8_t(CLAMP(colors[0].g * 255.0, 0.0, 255.0)), + uint8_t(CLAMP(colors[0].b * 255.0, 0.0, 255.0)), + uint8_t(CLAMP(colors[0].a * 255.0, 0.0, 255.0))}; + + for (int i = 0; i < vertices.size(); i++) { + Vector2 vertex(vertices[i]); + if (i == 0) { + aabb_new.position = Vector3(vertex.x, vertex.y, 0); + aabb_new.size = Vector3(); + } else { + aabb_new.expand_to(Vector3(vertex.x, vertex.y, 0)); + } + + float uv[2] = {(float) uvs[i].x, (float) uvs[i].y}; + memcpy(&vertex_write_buffer[i * vertex_stride + surface_offsets[RS::ARRAY_VERTEX]], &vertex, sizeof(float) * 2); + memcpy(&attribute_write_buffer[i * attribute_stride + surface_offsets[RS::ARRAY_COLOR]], color, 4); + memcpy(&attribute_write_buffer[i * attribute_stride + surface_offsets[RS::ARRAY_TEX_UV]], uv, 8); + } + RS::get_singleton()->mesh_surface_update_vertex_region(mesh, 0, 0, vertex_buffer); + RS::get_singleton()->mesh_surface_update_attribute_region(mesh, 0, 0, attribute_buffer); + RS::get_singleton()->mesh_set_custom_aabb(mesh, aabb_new); + } + + RenderingServer::get_singleton()->canvas_item_add_mesh(this->get_canvas_item(), mesh, Transform2D(), Color(1, 1, 1, 1), renderer_object->canvas_texture->get_rid()); +#endif +} + void SpineSprite::_bind_methods() { ClassDB::bind_method(D_METHOD("set_skeleton_data_res", "skeleton_data_res"), &SpineSprite::set_skeleton_data_res); ClassDB::bind_method(D_METHOD("get_skeleton_data_res"), &SpineSprite::get_skeleton_data_res); @@ -560,7 +624,7 @@ void SpineSprite::update_meshes(Ref skeleton_ref) { spine::Attachment *attachment = slot->getAttachment(); SpineMesh2D *mesh_instance = mesh_instances[i]; mesh_instance->renderer_object = nullptr; - + if (!attachment) { skeleton_clipper->clipEnd(*slot); continue; @@ -644,6 +708,7 @@ void SpineSprite::update_meshes(Ref skeleton_ref) { } mesh_instance->renderer_object = renderer_object; + mesh_instance->indices_id = (uint64_t)indices; spine::BlendMode blend_mode = slot->getData().getBlendMode(); Ref custom_material; @@ -690,17 +755,18 @@ void SpineSprite::update_meshes(Ref skeleton_ref) { // Set the custom material, or the default material if (custom_material.is_valid()) mesh_instance->set_material(custom_material); else - mesh_instance->set_material(default_materials[slot->getData().getBlendMode()]); - } + mesh_instance->set_material(default_materials[slot->getData().getBlendMode()]); + } skeleton_clipper->clipEnd(*slot); } skeleton_clipper->clipEnd(); } void SpineSprite::draw() { - if (!animation_state.is_valid() && !skeleton.is_valid()) return; - + if (!animation_state.is_valid() && !skeleton.is_valid()) return; if (!Engine::get_singleton()->is_editor_hint() && !get_tree()->is_debugging_collisions_hint()) return; + + RS::get_singleton()->canvas_item_clear(this->get_canvas_item()); auto mouse_position = get_local_mouse_position(); spine::Slot *hovered_slot = nullptr; @@ -827,9 +893,9 @@ void SpineSprite::draw() { auto mouse_local_position = bone_transform.affine_inverse().xform(mouse_position); if (GEOMETRY2D::is_point_in_polygon(mouse_local_position, scratch_points)) { hovered_bone = bone; - } + } } - + if (debug_bones) { auto &bones = skeleton->get_spine_object()->getBones(); for (int i = 0; i < (int) bones.size(); i++) { @@ -907,6 +973,7 @@ void SpineSprite::draw() { #endif } #endif + } void SpineSprite::draw_bone(spine::Bone *bone, const Color &color) { diff --git a/spine-godot/spine_godot/SpineSprite.h b/spine-godot/spine_godot/SpineSprite.h index 842d8c19e..9a8258a16 100644 --- a/spine-godot/spine_godot/SpineSprite.h +++ b/spine-godot/spine_godot/SpineSprite.h @@ -55,16 +55,30 @@ protected: Vector colors; Vector indices; SpineRendererObject *renderer_object; - int slotIndex; - Attachment *attachment; - ArrayMesh *mesh; + uint64_t last_indices_id; + uint64_t indices_id; + RID mesh; + uint32_t surface_offsets[RS::ARRAY_MAX]; + int num_vertices; + int num_indices; + PackedByteArray vertex_buffer; + PackedByteArray attribute_buffer; + uint32_t vertex_stride; + uint32_t attribute_stride; public: - SpineMesh2D() : renderer_object(nullptr), slotIndex(-1), attachment(nullptr), mesh(nullptr) {}; - ~SpineMesh2D(){ - if (mesh) memdelete(mesh); - }; - ArrayMesh *get_mesh() {return mesh;}; + SpineMesh2D() : renderer_object(nullptr), last_indices_id(0), indices_id(0), num_vertices(0), num_indices(0), vertex_stride(0), attribute_stride(0) {}; + ~SpineMesh2D() { + if (mesh.is_valid()) { + RS::get_singleton()->free(mesh); + } + } + + void update_mesh(const Vector &vertices, + const Vector &uvs, + const Vector &colors, + const Vector &indices, + SpineRendererObject *renderer_object); }; class SpineSprite : public Node2D, From b8a2facf93accbf63030e87d135cab506b10cea3 Mon Sep 17 00:00:00 2001 From: badlogic Date: Tue, 14 Feb 2023 13:43:53 +0100 Subject: [PATCH 3/3] [ue4] Reworked renderer component, does not fail GC when single attachment > 1000 vertices. --- .../SpineSkeletonRendererComponent.cpp | 236 ++++++++---------- .../Public/SpineSkeletonRendererComponent.h | 23 +- 2 files changed, 115 insertions(+), 144 deletions(-) diff --git a/spine-ue4/Plugins/SpinePlugin/Source/SpinePlugin/Private/SpineSkeletonRendererComponent.cpp b/spine-ue4/Plugins/SpinePlugin/Source/SpinePlugin/Private/SpineSkeletonRendererComponent.cpp index 022079d14..b3456fb2c 100644 --- a/spine-ue4/Plugins/SpinePlugin/Source/SpinePlugin/Private/SpineSkeletonRendererComponent.cpp +++ b/spine-ue4/Plugins/SpinePlugin/Source/SpinePlugin/Private/SpineSkeletonRendererComponent.cpp @@ -75,74 +75,57 @@ void USpineSkeletonRendererComponent::TickComponent(float DeltaTime, ELevelTick AActor *owner = GetOwner(); if (owner) { UClass *skeletonClass = USpineSkeletonComponent::StaticClass(); - USpineSkeletonComponent *skeleton = Cast(owner->GetComponentByClass(skeletonClass)); + USpineSkeletonComponent *skeletonComponent = Cast(owner->GetComponentByClass(skeletonClass)); - UpdateRenderer(skeleton); + UpdateRenderer(skeletonComponent); } } -void USpineSkeletonRendererComponent::UpdateRenderer(USpineSkeletonComponent *skeleton) { - if (skeleton && !skeleton->IsBeingDestroyed() && skeleton->GetSkeleton() && skeleton->Atlas) { - skeleton->GetSkeleton()->getColor().set(Color.R, Color.G, Color.B, Color.A); +void USpineSkeletonRendererComponent::UpdateRenderer(USpineSkeletonComponent *component) { + if (component && !component->IsBeingDestroyed() && component->GetSkeleton() && component->Atlas) { + 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); - pageToNormalBlendMaterial.Empty(); atlasAdditiveBlendMaterials.SetNum(0); - pageToAdditiveBlendMaterial.Empty(); atlasMultiplyBlendMaterials.SetNum(0); - pageToMultiplyBlendMaterial.Empty(); atlasScreenBlendMaterials.SetNum(0); - pageToScreenBlendMaterial.Empty(); - for (int i = 0; i < skeleton->Atlas->atlasPages.Num(); i++) { - AtlasPage *currPage = skeleton->Atlas->GetAtlas()->getPages()[i]; + for (int i = 0; i < component->Atlas->atlasPages.Num(); i++) { + AtlasPage *currPage = component->Atlas->GetAtlas()->getPages()[i]; UMaterialInstanceDynamic *material = UMaterialInstanceDynamic::Create(NormalBlendMaterial, this); - material->SetTextureParameterValue(TextureParameterName, skeleton->Atlas->atlasPages[i]); + material->SetTextureParameterValue(TextureParameterName, component->Atlas->atlasPages[i]); atlasNormalBlendMaterials.Add(material); - pageToNormalBlendMaterial.Add(currPage, material); material = UMaterialInstanceDynamic::Create(AdditiveBlendMaterial, this); - material->SetTextureParameterValue(TextureParameterName, skeleton->Atlas->atlasPages[i]); + material->SetTextureParameterValue(TextureParameterName, component->Atlas->atlasPages[i]); atlasAdditiveBlendMaterials.Add(material); - pageToAdditiveBlendMaterial.Add(currPage, material); material = UMaterialInstanceDynamic::Create(MultiplyBlendMaterial, this); - material->SetTextureParameterValue(TextureParameterName, skeleton->Atlas->atlasPages[i]); + material->SetTextureParameterValue(TextureParameterName, component->Atlas->atlasPages[i]); atlasMultiplyBlendMaterials.Add(material); - pageToMultiplyBlendMaterial.Add(currPage, material); material = UMaterialInstanceDynamic::Create(ScreenBlendMaterial, this); - material->SetTextureParameterValue(TextureParameterName, skeleton->Atlas->atlasPages[i]); + material->SetTextureParameterValue(TextureParameterName, component->Atlas->atlasPages[i]); atlasScreenBlendMaterials.Add(material); - pageToScreenBlendMaterial.Add(currPage, material); } } else { - pageToNormalBlendMaterial.Empty(); - pageToAdditiveBlendMaterial.Empty(); - pageToMultiplyBlendMaterial.Empty(); - pageToScreenBlendMaterial.Empty(); - - for (int i = 0; i < skeleton->Atlas->atlasPages.Num(); i++) { - 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); + for (int i = 0; i < component->Atlas->atlasPages.Num(); i++) { + UTexture2D *texture = component->Atlas->atlasPages[i]; + UpdateMaterial(texture, atlasNormalBlendMaterials[i], NormalBlendMaterial); + UpdateMaterial(texture, atlasAdditiveBlendMaterials[i], AdditiveBlendMaterial); + UpdateMaterial(texture, atlasMultiplyBlendMaterials[i], MultiplyBlendMaterial); + UpdateMaterial(texture, atlasScreenBlendMaterials[i], ScreenBlendMaterial); } } - UpdateMesh(skeleton->GetSkeleton()); + UpdateMesh(component, component->GetSkeleton()); } else { ClearAllMeshSections(); } } -void USpineSkeletonRendererComponent::UpdateRendererMaterial(spine::AtlasPage *CurrentPage, UTexture2D *Texture, - UMaterialInstanceDynamic *&CurrentInstance, UMaterialInterface *ParentMaterial, - TMap &PageToBlendMaterial) { +void USpineSkeletonRendererComponent::UpdateMaterial(UTexture2D *Texture, UMaterialInstanceDynamic *&CurrentInstance, UMaterialInterface *ParentMaterial) { UTexture *oldTexture = nullptr; if (!CurrentInstance || !CurrentInstance->GetTextureParameterValue(TextureParameterName, oldTexture) || @@ -152,10 +135,10 @@ void USpineSkeletonRendererComponent::UpdateRendererMaterial(spine::AtlasPage *C material->SetTextureParameterValue(TextureParameterName, Texture); CurrentInstance = material; } - PageToBlendMaterial.Add(CurrentPage, CurrentInstance); + } -void USpineSkeletonRendererComponent::Flush(int &Idx, TArray &Vertices, TArray &Indices, TArray &Normals, TArray &Uvs, TArray &Colors, TArray &Colors2, UMaterialInstanceDynamic *Material) { +void USpineSkeletonRendererComponent::Flush(int &Idx, TArray &Vertices, TArray &Indices, TArray &Normals, TArray &Uvs, TArray &Colors, UMaterialInstanceDynamic *Material) { if (Vertices.Num() == 0) return; SetMaterial(Idx, Material); @@ -175,24 +158,22 @@ void USpineSkeletonRendererComponent::Flush(int &Idx, TArray &Vertices, Normals.SetNum(0); Uvs.SetNum(0); Colors.SetNum(0); - Colors2.SetNum(0); Idx++; } -void USpineSkeletonRendererComponent::UpdateMesh(Skeleton *Skeleton) { - TArray vertices; - TArray indices; - TArray normals; - TArray uvs; - TArray colors; - TArray darkColors; - +void USpineSkeletonRendererComponent::UpdateMesh(USpineSkeletonComponent *component, Skeleton *Skeleton) { + vertices.Empty(); + indices.Empty(); + normals.Empty(); + uvs.Empty(); + colors.Empty(); + int idx = 0; int meshSection = 0; UMaterialInstanceDynamic *lastMaterial = nullptr; ClearAllMeshSections(); - + // Early out if skeleton is invisible if (Skeleton->getColor().a == 0) return; @@ -236,6 +217,7 @@ void USpineSkeletonRendererComponent::UpdateMesh(Skeleton *Skeleton) { } attachmentColor.set(regionAttachment->getColor()); + attachmentVertices->setSize(8, 0); regionAttachment->computeWorldVertices(*slot, *attachmentVertices, 0, 2); attachmentAtlasRegion = (AtlasRegion *) regionAttachment->getRendererObject(); attachmentIndices = quadIndices; @@ -244,14 +226,15 @@ void USpineSkeletonRendererComponent::UpdateMesh(Skeleton *Skeleton) { numIndices = 6; } else if (attachment->getRTTI().isExactly(MeshAttachment::rtti)) { MeshAttachment *mesh = (MeshAttachment *) attachment; - + // Early out if region is invisible if (mesh->getColor().a == 0) { clipper.clipEnd(*slot); continue; } - + attachmentColor.set(mesh->getColor()); + attachmentVertices->setSize(mesh->getWorldVerticesLength(), 0); mesh->computeWorldVertices(*slot, 0, mesh->getWorldVerticesLength(), attachmentVertices->buffer(), 0, 2); attachmentAtlasRegion = (AtlasRegion *) mesh->getRendererObject(); attachmentIndices = mesh->getTriangles().buffer(); @@ -264,47 +247,6 @@ void USpineSkeletonRendererComponent::UpdateMesh(Skeleton *Skeleton) { 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()) { clipper.clipTriangles(attachmentVertices->buffer(), attachmentIndices, numIndices, attachmentUvs, 2); attachmentVertices = &clipper.getClippedVertices(); @@ -317,70 +259,94 @@ void USpineSkeletonRendererComponent::UpdateMesh(Skeleton *Skeleton) { 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; + 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) { - Flush(meshSection, vertices, indices, normals, uvs, colors, darkColors, lastMaterial); + Flush(meshSection, vertices, indices, normals, uvs, colors, lastMaterial); lastMaterial = material; idx = 0; } - + SetMaterial(meshSection, material); - + uint8 r = static_cast(Skeleton->getColor().r * slot->getColor().r * attachmentColor.r * 255); uint8 g = static_cast(Skeleton->getColor().g * slot->getColor().g * attachmentColor.g * 255); uint8 b = static_cast(Skeleton->getColor().b * slot->getColor().b * attachmentColor.b * 255); uint8 a = static_cast(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; - - float *verticesPtr = attachmentVertices->buffer(); + + float* verticesPtr = attachmentVertices->buffer(); 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(verticesPtr[j], depthOffset, verticesPtr[j + 1])); uvs.Add(FVector2D(attachmentUvs[j], attachmentUvs[j + 1])); } - - int firstIndex = indices.Num(); + for (int j = 0; j < numIndices; j++) { indices.Add(idx + attachmentIndices[j]); } - - - //Calculate total triangle to add on this loof. - - 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; - + + int numTriangles = indices.Num() / 3; + for (int j = 0; j < numTriangles; j++) { + const int triangleIndex = j * 3; if (FVector::CrossProduct( - vertices[indices[TargetTringleIndex + 2]] - vertices[indices[TargetTringleIndex]], - vertices[indices[TargetTringleIndex + 1]] - vertices[indices[TargetTringleIndex]]) - .Y < 0.f) { - - const int32 targetVertex = indices[TargetTringleIndex]; - indices[TargetTringleIndex] = indices[TargetTringleIndex + 2]; - indices[TargetTringleIndex + 2] = targetVertex; + vertices[indices[triangleIndex + 2]] - vertices[indices[triangleIndex]], + vertices[indices[triangleIndex + 1]] - vertices[indices[triangleIndex]]) + .Y < 0.f) { + const int32 targetVertex = indices[triangleIndex]; + indices[triangleIndex] = indices[triangleIndex + 2]; + indices[triangleIndex + 2] = targetVertex; } } - - + FVector normal = FVector(0, 1, 0); - - //Add normals for vertices. - for (int j = 0; j < numVertices; j++) { - normals.Add(normal); } @@ -390,7 +356,7 @@ void USpineSkeletonRendererComponent::UpdateMesh(Skeleton *Skeleton) { clipper.clipEnd(*slot); } - Flush(meshSection, vertices, indices, normals, uvs, colors, darkColors, lastMaterial); + Flush(meshSection, vertices, indices, normals, uvs, colors, lastMaterial); clipper.clipEnd(); } diff --git a/spine-ue4/Plugins/SpinePlugin/Source/SpinePlugin/Public/SpineSkeletonRendererComponent.h b/spine-ue4/Plugins/SpinePlugin/Source/SpinePlugin/Public/SpineSkeletonRendererComponent.h index c4539a37b..d45ed2e7f 100644 --- a/spine-ue4/Plugins/SpinePlugin/Source/SpinePlugin/Public/SpineSkeletonRendererComponent.h +++ b/spine-ue4/Plugins/SpinePlugin/Source/SpinePlugin/Public/SpineSkeletonRendererComponent.h @@ -65,19 +65,15 @@ public: // Need to hold on to the dynamic instances, or the GC will kill us while updating them UPROPERTY(Category = Spine, EditAnywhere, BlueprintReadWrite) TArray atlasNormalBlendMaterials; - TMap pageToNormalBlendMaterial; UPROPERTY(Category = Spine, EditAnywhere, BlueprintReadWrite) TArray atlasAdditiveBlendMaterials; - TMap pageToAdditiveBlendMaterial; UPROPERTY(Category = Spine, EditAnywhere, BlueprintReadWrite) TArray atlasMultiplyBlendMaterials; - TMap pageToMultiplyBlendMaterial; UPROPERTY(Category = Spine, EditAnywhere, BlueprintReadWrite) TArray atlasScreenBlendMaterials; - TMap pageToScreenBlendMaterial; UPROPERTY(Category = Spine, EditAnywhere, BlueprintReadWrite) float DepthOffset = 0.1f; @@ -95,14 +91,23 @@ public: virtual void FinishDestroy() override; protected: - void UpdateRendererMaterial(spine::AtlasPage *CurrentPage, UTexture2D *Texture, - UMaterialInstanceDynamic *&CurrentInstance, UMaterialInterface *ParentMaterial, - TMap &PageToBlendMaterial); + void UpdateMaterial(UTexture2D *Texture, UMaterialInstanceDynamic *&CurrentInstance, UMaterialInterface *ParentMaterial); - void UpdateMesh(spine::Skeleton *Skeleton); + void UpdateMesh(USpineSkeletonComponent *component, spine::Skeleton *Skeleton); - void Flush(int &Idx, TArray &Vertices, TArray &Indices, TArray &Normals, TArray &Uvs, TArray &Colors, TArray &Colors2, UMaterialInstanceDynamic *Material); + void Flush(int &Idx, TArray &Vertices, TArray &Indices, TArray &Normals, TArray &Uvs, TArray &Colors, UMaterialInstanceDynamic *Material); spine::Vector worldVertices; spine::SkeletonClipping clipper; + + UPROPERTY(); + TArray vertices; + UPROPERTY(); + TArray indices; + UPROPERTY(); + TArray normals; + UPROPERTY(); + TArray uvs; + UPROPERTY(); + TArray colors; };