Browse Source

Merge branch '4.1' into 4.2-beta

badlogic 2 years ago
parent
commit
fe8f51810a

+ 85 - 18
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<unsigned short> quad_indices;
 static spine::Vector<float> scratch_vertices;
 static Vector<Vector2> 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<Point2> &vertices,
 						  const Vector<Point2> &uvs,
@@ -61,15 +73,7 @@ static void add_triangles(SpineMesh2D *mesh_instance,
 						  const Vector<int> &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<int>(),
-																	 Vector<float>(),
-																	 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<Point2> &vertices,
+							  const Vector<Point2> &uvs,
+							  const Vector<Color> &colors,
+							  const Vector<int> &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<Array>(), 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<SpineSkeleton> 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<SpineSkeleton> skeleton_ref) {
 			}
 
 			mesh_instance->renderer_object = renderer_object;
+			mesh_instance->indices_id = (uint64_t)indices;			
 			spine::BlendMode blend_mode = slot->getData().getBlendMode();
 			Ref<Material> custom_material;
 
@@ -690,17 +755,18 @@ void SpineSprite::update_meshes(Ref<SpineSkeleton> 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) {

+ 22 - 8
spine-godot/spine_godot/SpineSprite.h

@@ -55,16 +55,30 @@ protected:
 	Vector<Color> colors;
 	Vector<int> 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<Point2> &vertices,
+					 const Vector<Point2> &uvs,
+					 const Vector<Color> &colors,
+					 const Vector<int> &indices,
+					 SpineRendererObject *renderer_object);
 };
 
 class SpineSprite : public Node2D,

+ 101 - 135
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<USpineSkeletonComponent>(owner->GetComponentByClass(skeletonClass));
+		USpineSkeletonComponent *skeletonComponent = Cast<USpineSkeletonComponent>(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<spine::AtlasPage *, UMaterialInstanceDynamic *> &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<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;
 	SetMaterial(Idx, Material);
 
@@ -175,24 +158,22 @@ void USpineSkeletonRendererComponent::Flush(int &Idx, TArray<FVector> &Vertices,
 	Normals.SetNum(0);
 	Uvs.SetNum(0);
 	Colors.SetNum(0);
-	Colors2.SetNum(0);
 	Idx++;
 }
 
-void USpineSkeletonRendererComponent::UpdateMesh(Skeleton *Skeleton) {
-	TArray<FVector> vertices;
-	TArray<int32> indices;
-	TArray<FVector> normals;
-	TArray<FVector2D> uvs;
-	TArray<FColor> colors;
-	TArray<FVector> 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->getRegion();
 			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->getRegion();
 			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<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;
-
-		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();
 }
 

+ 14 - 9
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<UMaterialInstanceDynamic *> atlasNormalBlendMaterials;
-	TMap<spine::AtlasPage *, UMaterialInstanceDynamic *> pageToNormalBlendMaterial;
 
 	UPROPERTY(Category = Spine, EditAnywhere, BlueprintReadWrite)
 	TArray<UMaterialInstanceDynamic *> atlasAdditiveBlendMaterials;
-	TMap<spine::AtlasPage *, UMaterialInstanceDynamic *> pageToAdditiveBlendMaterial;
 
 	UPROPERTY(Category = Spine, EditAnywhere, BlueprintReadWrite)
 	TArray<UMaterialInstanceDynamic *> atlasMultiplyBlendMaterials;
-	TMap<spine::AtlasPage *, UMaterialInstanceDynamic *> pageToMultiplyBlendMaterial;
 
 	UPROPERTY(Category = Spine, EditAnywhere, BlueprintReadWrite)
 	TArray<UMaterialInstanceDynamic *> atlasScreenBlendMaterials;
-	TMap<spine::AtlasPage *, UMaterialInstanceDynamic *> 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<spine::AtlasPage *, UMaterialInstanceDynamic *> &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<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::SkeletonClipping clipper;
+	
+	UPROPERTY();
+	TArray<FVector> vertices;
+	UPROPERTY();
+    TArray<int32> indices;
+    UPROPERTY();
+    TArray<FVector> normals;
+    UPROPERTY();
+    TArray<FVector2D> uvs;
+    UPROPERTY();
+    TArray<FColor> colors;
 };

+ 1 - 2
spine-unity/Assets/Spine/Editor/spine-unity/Editor/Asset Types/SpineAtlasAssetInspector.cs

@@ -320,12 +320,11 @@ namespace Spine.Unity.Editor {
 			List<SpriteMetaData> sprites = new List<SpriteMetaData>(spriteSheet);
 
 			List<AtlasRegion> 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);