Pārlūkot izejas kodu

[c][cpp][sfml][cocos2d-x] Skip rendering of skeleton/slot/attachment early if alpha is 0. See #1145

badlogic 7 gadi atpakaļ
vecāks
revīzija
6e83e6aed1

+ 23 - 0
spine-cocos2dx/src/spine/SkeletonRenderer.cpp

@@ -266,6 +266,11 @@ void SkeletonRenderer::draw (Renderer* renderer, const Mat4& transform, uint32_t
 	SkeletonTwoColorBatch* twoColorBatch = SkeletonTwoColorBatch::getInstance();
 	bool isTwoColorTint = this->isTwoColorTint();
 	
+	// Early exit if the skeleton is invisible
+	if (getDisplayedOpacity() == 0 || _skeleton->color.a == 0){
+		return;
+	}
+	
 	if (_effect) _effect->begin(_effect, _skeleton);
 
 	Color4F nodeColor;
@@ -301,6 +306,12 @@ void SkeletonRenderer::draw (Renderer* renderer, const Mat4& transform, uint32_t
 			continue;
 		}
 		
+		// Early exit if slot is invisible
+		if (slot->color.a == 0) {
+			spSkeletonClipping_clipEnd(_clipper, slot);
+			continue;
+		}
+		
 		cocos2d::TrianglesCommand::Triangles triangles;
 		TwoColorTriangles trianglesTwoColor;
 		
@@ -309,6 +320,12 @@ void SkeletonRenderer::draw (Renderer* renderer, const Mat4& transform, uint32_t
 			spRegionAttachment* attachment = (spRegionAttachment*)slot->attachment;
 			attachmentVertices = getAttachmentVertices(attachment);
 			
+			// Early exit if attachment is invisible
+			if (attachment->color.a == 0) {
+				spSkeletonClipping_clipEnd(_clipper, slot);
+				continue;
+			}
+			
 			if (!isTwoColorTint) {
 				triangles.indices = attachmentVertices->_triangles->indices;
 				triangles.indexCount = attachmentVertices->_triangles->indexCount;
@@ -338,6 +355,12 @@ void SkeletonRenderer::draw (Renderer* renderer, const Mat4& transform, uint32_t
 			spMeshAttachment* attachment = (spMeshAttachment*)slot->attachment;
 			attachmentVertices = getAttachmentVertices(attachment);
 			
+			// Early exit if attachment is invisible
+			if (attachment->color.a == 0) {
+				spSkeletonClipping_clipEnd(_clipper, slot);
+				continue;
+			}
+			
 			if (!isTwoColorTint) {
 				triangles.indices = attachmentVertices->_triangles->indices;
 				triangles.indexCount = attachmentVertices->_triangles->indexCount;

+ 26 - 2
spine-sfml/c/src/spine/spine-sfml.cpp

@@ -116,6 +116,9 @@ void SkeletonDrawable::draw (RenderTarget& target, RenderStates states) const {
 	states.texture = 0;
 	unsigned short quadIndices[6] = { 0, 1, 2, 2, 3, 0 };
 
+	// Early out if skeleton is invisible
+	if (skeleton->color.a == 0) return;
+
 	if (vertexEffect != 0) vertexEffect->begin(vertexEffect, skeleton);
 
 	sf::Vertex vertex;
@@ -125,6 +128,12 @@ void SkeletonDrawable::draw (RenderTarget& target, RenderStates states) const {
 		Attachment* attachment = slot->attachment;
 		if (!attachment) continue;
 
+		// Early out if slot is invisible
+		if (slot->color.a == 0) {
+			spSkeletonClipping_clipEnd(clipper, slot);
+			continue;
+		}
+
 		float* vertices = worldVertices;
 		int verticesCount = 0;
 		float* uvs = 0;
@@ -134,16 +143,31 @@ void SkeletonDrawable::draw (RenderTarget& target, RenderStates states) const {
 
 		if (attachment->type == ATTACHMENT_REGION) {
 			RegionAttachment* regionAttachment = (RegionAttachment*)attachment;
+			attachmentColor = &regionAttachment->color;
+
+			// Early out if slot is invisible
+			if (attachmentColor->a == 0) {
+				spSkeletonClipping_clipEnd(clipper, slot);
+				continue;
+			}
+
 			spRegionAttachment_computeWorldVertices(regionAttachment, slot->bone, vertices, 0, 2);
 			verticesCount = 4;
 			uvs = regionAttachment->uvs;
 			indices = quadIndices;
 			indicesCount = 6;
 			texture = (Texture*)((AtlasRegion*)regionAttachment->rendererObject)->page->rendererObject;
-			attachmentColor = &regionAttachment->color;
 
 		} else if (attachment->type == ATTACHMENT_MESH) {
 			MeshAttachment* mesh = (MeshAttachment*)attachment;
+			attachmentColor = &mesh->color;
+
+			// Early out if slot is invisible
+			if (attachmentColor->a == 0) {
+				spSkeletonClipping_clipEnd(clipper, slot);
+				continue;
+			}
+
 			if (mesh->super.worldVerticesLength > SPINE_MESH_VERTEX_COUNT_MAX) continue;
 			texture = (Texture*)((AtlasRegion*)mesh->rendererObject)->page->rendererObject;
 			spVertexAttachment_computeWorldVertices(SUPER(mesh), slot, 0, mesh->super.worldVerticesLength, worldVertices, 0, 2);
@@ -151,7 +175,7 @@ void SkeletonDrawable::draw (RenderTarget& target, RenderStates states) const {
 			uvs = mesh->uvs;
 			indices = mesh->triangles;
 			indicesCount = mesh->trianglesCount;
-			attachmentColor = &mesh->color;
+
 		} else if (attachment->type == SP_ATTACHMENT_CLIPPING) {
 			spClippingAttachment* clip = (spClippingAttachment*)slot->attachment;
 			spSkeletonClipping_clipStart(clipper, slot, clip);

+ 25 - 2
spine-sfml/cpp/src/spine/spine-sfml.cpp

@@ -91,6 +91,9 @@ void SkeletonDrawable::draw(RenderTarget &target, RenderStates states) const {
 	vertexArray->clear();
 	states.texture = NULL;
 
+	// Early out if the skeleton alpha is 0
+	if (skeleton->getColor().a == 0) return;
+
 	if (vertexEffect != NULL) vertexEffect->begin(*skeleton);
 
 	sf::Vertex vertex;
@@ -100,6 +103,12 @@ void SkeletonDrawable::draw(RenderTarget &target, RenderStates states) const {
 		Attachment *attachment = slot.getAttachment();
 		if (!attachment) continue;
 
+		// Early out if the slot color is 0
+		if (slot.getColor().a == 0) {
+			clipper.clipEnd(slot);
+			continue;
+		}
+
 		Vector<float> *vertices = &worldVertices;
 		int verticesCount = 0;
 		Vector<float> *uvs = NULL;
@@ -109,6 +118,14 @@ void SkeletonDrawable::draw(RenderTarget &target, RenderStates states) const {
 
 		if (attachment->getRTTI().isExactly(RegionAttachment::rtti)) {
 			RegionAttachment *regionAttachment = (RegionAttachment *) attachment;
+			attachmentColor = &regionAttachment->getColor();
+
+			// Early out if the attachment color is 0
+			if (attachmentColor->a == 0) {
+				clipper.clipEnd(slot);
+				continue;
+			}
+
 			worldVertices.setSize(8, 0);
 			regionAttachment->computeWorldVertices(slot.getBone(), worldVertices, 0, 2);
 			verticesCount = 4;
@@ -116,10 +133,17 @@ void SkeletonDrawable::draw(RenderTarget &target, RenderStates states) const {
 			indices = &quadIndices;
 			indicesCount = 6;
 			texture = (Texture *) ((AtlasRegion *) regionAttachment->getRendererObject())->page->rendererObject;
-			attachmentColor = &regionAttachment->getColor();
 
 		} else if (attachment->getRTTI().isExactly(MeshAttachment::rtti)) {
 			MeshAttachment *mesh = (MeshAttachment *) attachment;
+			attachmentColor = &mesh->getColor();
+
+			// Early out if the attachment color is 0
+			if (attachmentColor->a == 0) {
+				clipper.clipEnd(slot);
+				continue;
+			}
+
 			worldVertices.setSize(mesh->getWorldVerticesLength(), 0);
 			texture = (Texture *) ((AtlasRegion *) mesh->getRendererObject())->page->rendererObject;
 			mesh->computeWorldVertices(slot, 0, mesh->getWorldVerticesLength(), worldVertices, 0, 2);
@@ -127,7 +151,6 @@ void SkeletonDrawable::draw(RenderTarget &target, RenderStates states) const {
 			uvs = &mesh->getUVs();
 			indices = &mesh->getTriangles();
 			indicesCount = mesh->getTriangles().size();
-			attachmentColor = &mesh->getColor();
 		} else if (attachment->getRTTI().isExactly(ClippingAttachment::rtti)) {
 			ClippingAttachment *clip = (ClippingAttachment *) slot.getAttachment();
 			clipper.clipStart(slot, clip);