Spine深入学习———— 渲染

数据有了之后,就开始渲染

渲染相关

绘制顺序

骨架的绘制顺序就是一个插槽列表,在插槽列表中上方的附件在下方之上绘制,绘制顺序可以在层级树中的骨架下查看。

在这里插入图片描述

基础流程

在这里插入图片描述

渲染实现

以下按照cocos2dx的实现来 (cocos2dx 3.7 spine3.6)

SkeletonRenderer

本身继承于Node,所以渲染部分看draw函数。

void SkeletonRenderer::draw (Renderer* renderer, const Mat4& transform, uint32_t transformFlags) {
	SkeletonBatch* batch = SkeletonBatch::getInstance();
	SkeletonTwoColorBatch* twoColorBatch = SkeletonTwoColorBatch::getInstance();
	bool isTwoColorTint = this->isTwoColorTint();
	
	if (_effect) _effect->begin(_effect, _skeleton);

	Color4F nodeColor;
	nodeColor.r = getDisplayedColor().r / (float)255;
	nodeColor.g = getDisplayedColor().g / (float)255;
	nodeColor.b = getDisplayedColor().b / (float)255;
	nodeColor.a = getDisplayedOpacity() / (float)255;
	
    Color4F color;
	Color4F darkColor;
	AttachmentVertices* attachmentVertices = nullptr;
	TwoColorTrianglesCommand* lastTwoColorTrianglesCommand = nullptr;
	for (int i = 0, n = _skeleton->slotsCount; i < n; ++i) {
		spSlot* slot = _skeleton->drawOrder[i];
		if (!slot->attachment) {
			spSkeletonClipping_clipEnd(_clipper, slot);
			continue;
		}
		
		cocos2d::TrianglesCommand::Triangles triangles;
		TwoColorTriangles trianglesTwoColor;
		
		switch (slot->attachment->type) {
		case SP_ATTACHMENT_REGION: {
			spRegionAttachment* attachment = (spRegionAttachment*)slot->attachment;
			attachmentVertices = getAttachmentVertices(attachment);
			
			if (!isTwoColorTint) {
				triangles.indices = attachmentVertices->_triangles->indices;
				triangles.indexCount = attachmentVertices->_triangles->indexCount;
				triangles.verts = batch->allocateVertices(attachmentVertices->_triangles->vertCount);
				triangles.vertCount = attachmentVertices->_triangles->vertCount;
				memcpy(triangles.verts, attachmentVertices->_triangles->verts, sizeof(cocos2d::V3F_C4B_T2F) * attachmentVertices->_triangles->vertCount);
				spRegionAttachment_computeWorldVertices(attachment, slot->bone, (float*)triangles.verts, 0, 6);
			} else {
				trianglesTwoColor.indices = attachmentVertices->_triangles->indices;
				trianglesTwoColor.indexCount = attachmentVertices->_triangles->indexCount;
				trianglesTwoColor.verts = twoColorBatch->allocateVertices(attachmentVertices->_triangles->vertCount);
				trianglesTwoColor.vertCount = attachmentVertices->_triangles->vertCount;
				for (int ii = 0; ii < trianglesTwoColor.vertCount; ii++) {
					trianglesTwoColor.verts[ii].texCoords = attachmentVertices->_triangles->verts[ii].texCoords;
				}
				spRegionAttachment_computeWorldVertices(attachment, slot->bone, (float*)trianglesTwoColor.verts, 0, 7);
			}
			
            color.r = attachment->color.r;
			color.g = attachment->color.g;
			color.b = attachment->color.b;
			color.a = attachment->color.a;
			
			break;
		}
		case SP_ATTACHMENT_MESH: {
			spMeshAttachment* attachment = (spMeshAttachment*)slot->attachment;
			attachmentVertices = getAttachmentVertices(attachment);
			
			if (!isTwoColorTint) {
				triangles.indices = attachmentVertices->_triangles->indices;
				triangles.indexCount = attachmentVertices->_triangles->indexCount;
				triangles.verts = batch->allocateVertices(attachmentVertices->_triangles->vertCount);
				triangles.vertCount = attachmentVertices->_triangles->vertCount;
				memcpy(triangles.verts, attachmentVertices->_triangles->verts, sizeof(cocos2d::V3F_C4B_T2F) * attachmentVertices->_triangles->vertCount);
				spVertexAttachment_computeWorldVertices(SUPER(attachment), slot, 0, triangles.vertCount * sizeof(cocos2d::V3F_C4B_T2F) / 4, (float*)triangles.verts, 0, 6);
			} else {
				trianglesTwoColor.indices = attachmentVertices->_triangles->indices;
				trianglesTwoColor.indexCount = attachmentVertices->_triangles->indexCount;
				trianglesTwoColor.verts = twoColorBatch->allocateVertices(attachmentVertices->_triangles->vertCount);
				trianglesTwoColor.vertCount = attachmentVertices->_triangles->vertCount;
				for (int ii = 0; ii < trianglesTwoColor.vertCount; ii++) {
					trianglesTwoColor.verts[ii].texCoords = attachmentVertices->_triangles->verts[ii].texCoords;
				}
				spVertexAttachment_computeWorldVertices(SUPER(attachment), slot, 0, trianglesTwoColor.vertCount * sizeof(V3F_C4B_C4B_T2F) / 4, (float*)trianglesTwoColor.verts, 0, 7);
			}
			
			color.r = attachment->color.r;
			color.g = attachment->color.g;
			color.b = attachment->color.b;
			color.a = attachment->color.a;
			
			break;
		}
		case SP_ATTACHMENT_CLIPPING: {
			spClippingAttachment* clip = (spClippingAttachment*)slot->attachment;
			spSkeletonClipping_clipStart(_clipper, slot, clip);
		}
		default:
			spSkeletonClipping_clipEnd(_clipper, slot);
			continue;
		}
		
		if (slot->darkColor) {
			darkColor.r = slot->darkColor->r * 255;
			darkColor.g = slot->darkColor->g * 255;
			darkColor.b = slot->darkColor->b * 255;
		} else {
			darkColor.r = 0;
			darkColor.g = 0;
			darkColor.b = 0;
		}
		
		color.a *= nodeColor.a * _skeleton->color.a * slot->color.a * 255;
		// skip rendering if the color of this attachment is 0
		if (color.a == 0){
			spSkeletonClipping_clipEnd(_clipper, slot);
			continue;
		}
		float multiplier = _premultipliedAlpha ? color.a : 255;
		color.r *= nodeColor.r * _skeleton->color.r * slot->color.r * multiplier;
		color.g *= nodeColor.g * _skeleton->color.g * slot->color.g * multiplier;
		color.b *= nodeColor.b * _skeleton->color.b * slot->color.b * multiplier;
		
		BlendFunc blendFunc;
		switch (slot->data->blendMode) {
			case SP_BLEND_MODE_ADDITIVE:
				blendFunc.src = _premultipliedAlpha ? GL_ONE : GL_SRC_ALPHA;
				blendFunc.dst = GL_ONE;
				break;
			case SP_BLEND_MODE_MULTIPLY:
				blendFunc.src = GL_DST_COLOR;
				blendFunc.dst = GL_ONE_MINUS_SRC_ALPHA;
				break;
			case SP_BLEND_MODE_SCREEN:
				blendFunc.src = GL_ONE;
				blendFunc.dst = GL_ONE_MINUS_SRC_COLOR;
				break;
			default:
				blendFunc.src = _premultipliedAlpha ? GL_ONE : GL_SRC_ALPHA;
				blendFunc.dst = GL_ONE_MINUS_SRC_ALPHA;
		}
		
		if (!isTwoColorTint) {
			if (spSkeletonClipping_isClipping(_clipper)) {
				spSkeletonClipping_clipTriangles(_clipper, (float*)&triangles.verts[0].vertices, triangles.vertCount * sizeof(cocos2d::V3F_C4B_T2F) / 4, triangles.indices, triangles.indexCount, (float*)&triangles.verts[0].texCoords, 6);
				batch->deallocateVertices(triangles.vertCount);
				
				if (_clipper->clippedTriangles->size == 0){
					spSkeletonClipping_clipEnd(_clipper, slot);
					continue;
				}
				
				triangles.vertCount = _clipper->clippedVertices->size >> 1;
				triangles.verts = batch->allocateVertices(triangles.vertCount);
				triangles.indexCount = _clipper->clippedTriangles->size;
				triangles.indices = batch->allocateIndices(triangles.indexCount);
				memcpy(triangles.indices, _clipper->clippedTriangles->items, sizeof(unsigned short) * _clipper->clippedTriangles->size);
				
				cocos2d::TrianglesCommand* batchedTriangles = batch->addCommand(renderer, _globalZOrder, attachmentVertices->_texture, _glProgramState, blendFunc, triangles, transform, transformFlags);
				
				float* verts = _clipper->clippedVertices->items;
				float* uvs = _clipper->clippedUVs->items;
				if (_effect) {
					spColor light;
					spColor dark;
					light.r = color.r / 255.0f;
					light.g = color.g / 255.0f;
					light.b = color.b / 255.0f;
					light.a = color.a / 255.0f;
					dark.r = dark.g = dark.b = dark.a = 0;
					for (int v = 0, vn = batchedTriangles->getTriangles().vertCount, vv = 0; v < vn; ++v, vv+=2) {
						V3F_C4B_T2F* vertex = batchedTriangles->getTriangles().verts + v;
						spColor lightCopy = light;
						spColor darkCopy = dark;
						vertex->vertices.x = verts[vv];
						vertex->vertices.y = verts[vv + 1];
						vertex->texCoords.u = uvs[vv];
						vertex->texCoords.v = uvs[vv + 1];
						_effect->transform(_effect, &vertex->vertices.x, &vertex->vertices.y, &vertex->texCoords.u, &vertex->texCoords.v, &lightCopy, &darkCopy);
						vertex->colors.r = (GLubyte)(lightCopy.r * 255);
						vertex->colors.g = (GLubyte)(lightCopy.g * 255);
						vertex->colors.b = (GLubyte)(lightCopy.b * 255);
						vertex->colors.a = (GLubyte)(lightCopy.a * 255);
					}
				} else {
					for (int v = 0, vn = batchedTriangles->getTriangles().vertCount, vv = 0; v < vn; ++v, vv+=2) {
						V3F_C4B_T2F* vertex = batchedTriangles->getTriangles().verts + v;
						vertex->vertices.x = verts[vv];
						vertex->vertices.y = verts[vv + 1];
						vertex->texCoords.u = uvs[vv];
						vertex->texCoords.v = uvs[vv + 1];
						vertex->colors.r = (GLubyte)color.r;
						vertex->colors.g = (GLubyte)color.g;
						vertex->colors.b = (GLubyte)color.b;
						vertex->colors.a = (GLubyte)color.a;
					}
				}
			} else {
				cocos2d::TrianglesCommand* batchedTriangles = batch->addCommand(renderer, _globalZOrder, attachmentVertices->_texture, _glProgramState, blendFunc, triangles, transform, transformFlags);
				
				if (_effect) {
					spColor light;
					spColor dark;
					light.r = color.r / 255.0f;
					light.g = color.g / 255.0f;
					light.b = color.b / 255.0f;
					light.a = color.a / 255.0f;
					dark.r = dark.g = dark.b = dark.a = 0;
					for (int v = 0, vn = batchedTriangles->getTriangles().vertCount; v < vn; ++v) {
						V3F_C4B_T2F* vertex = batchedTriangles->getTriangles().verts + v;
						spColor lightCopy = light;
						spColor darkCopy = dark;
						_effect->transform(_effect, &vertex->vertices.x, &vertex->vertices.y, &vertex->texCoords.u, &vertex->texCoords.v, &lightCopy, &darkCopy);
						vertex->colors.r = (GLubyte)(lightCopy.r * 255);
						vertex->colors.g = (GLubyte)(lightCopy.g * 255);
						vertex->colors.b = (GLubyte)(lightCopy.b * 255);
						vertex->colors.a = (GLubyte)(lightCopy.a * 255);
					}
				} else {
					for (int v = 0, vn = batchedTriangles->getTriangles().vertCount; v < vn; ++v) {
						V3F_C4B_T2F* vertex = batchedTriangles->getTriangles().verts + v;
						vertex->colors.r = (GLubyte)color.r;
						vertex->colors.g = (GLubyte)color.g;
						vertex->colors.b = (GLubyte)color.b;
						vertex->colors.a = (GLubyte)color.a;
					}
				}
			}
		} else {
			if (spSkeletonClipping_isClipping(_clipper)) {
				spSkeletonClipping_clipTriangles(_clipper, (float*)&trianglesTwoColor.verts[0].position, trianglesTwoColor.vertCount * sizeof(V3F_C4B_C4B_T2F) / 4, trianglesTwoColor.indices, trianglesTwoColor.indexCount, (float*)&trianglesTwoColor.verts[0].texCoords, 7);
				twoColorBatch->deallocateVertices(trianglesTwoColor.vertCount);
				
				if (_clipper->clippedTriangles->size == 0){
					spSkeletonClipping_clipEnd(_clipper, slot);
					continue;
				}
				
				trianglesTwoColor.vertCount = _clipper->clippedVertices->size >> 1;
				trianglesTwoColor.verts = twoColorBatch->allocateVertices(trianglesTwoColor.vertCount);
				trianglesTwoColor.indexCount = _clipper->clippedTriangles->size;
				trianglesTwoColor.indices = twoColorBatch->allocateIndices(trianglesTwoColor.indexCount);
				memcpy(trianglesTwoColor.indices, _clipper->clippedTriangles->items, sizeof(unsigned short) * _clipper->clippedTriangles->size);
				
				TwoColorTrianglesCommand* batchedTriangles = lastTwoColorTrianglesCommand = twoColorBatch->addCommand(renderer, _globalZOrder, attachmentVertices->_texture->getName(), _glProgramState, blendFunc, trianglesTwoColor, transform, transformFlags);
				
				float* verts = _clipper->clippedVertices->items;
				float* uvs = _clipper->clippedUVs->items;
				
				if (_effect) {
					spColor light;
					spColor dark;
					light.r = color.r / 255.0f;
					light.g = color.g / 255.0f;
					light.b = color.b / 255.0f;
					light.a = color.a / 255.0f;
					dark.r = darkColor.r / 255.0f;
					dark.g = darkColor.g / 255.0f;
					dark.b = darkColor.b / 255.0f;
					dark.a = darkColor.a / 255.0f;
					for (int v = 0, vn = batchedTriangles->getTriangles().vertCount, vv = 0; v < vn; ++v, vv += 2) {
						V3F_C4B_C4B_T2F* vertex = batchedTriangles->getTriangles().verts + v;
						spColor lightCopy = light;
						spColor darkCopy = dark;
						vertex->position.x = verts[vv];
						vertex->position.y = verts[vv + 1];
						vertex->texCoords.u = uvs[vv];
						vertex->texCoords.v = uvs[vv + 1];
						_effect->transform(_effect, &vertex->position.x, &vertex->position.y, &vertex->texCoords.u, &vertex->texCoords.v, &lightCopy, &darkCopy);
						vertex->color.r = (GLubyte)(lightCopy.r * 255);
						vertex->color.g = (GLubyte)(lightCopy.g * 255);
						vertex->color.b = (GLubyte)(lightCopy.b * 255);
						vertex->color.a = (GLubyte)(lightCopy.a * 255);
						vertex->color2.r = (GLubyte)(darkCopy.r * 255);
						vertex->color2.g = (GLubyte)(darkCopy.g * 255);
						vertex->color2.b = (GLubyte)(darkCopy.b * 255);
						vertex->color2.a = 1;
					}
				} else {
					for (int v = 0, vn = batchedTriangles->getTriangles().vertCount, vv = 0; v < vn; ++v, vv += 2) {
						V3F_C4B_C4B_T2F* vertex = batchedTriangles->getTriangles().verts + v;
						vertex->position.x = verts[vv];
						vertex->position.y = verts[vv + 1];
						vertex->texCoords.u = uvs[vv];
						vertex->texCoords.v = uvs[vv + 1];
						vertex->color.r = (GLubyte)color.r;
						vertex->color.g = (GLubyte)color.g;
						vertex->color.b = (GLubyte)color.b;
						vertex->color.a = (GLubyte)color.a;
						vertex->color2.r = (GLubyte)darkColor.r;
						vertex->color2.g = (GLubyte)darkColor.g;
						vertex->color2.b = (GLubyte)darkColor.b;
						vertex->color2.a = 1;
					}
				}
			} else {
				TwoColorTrianglesCommand* batchedTriangles = lastTwoColorTrianglesCommand = twoColorBatch->addCommand(renderer, _globalZOrder, attachmentVertices->_texture->getName(), _glProgramState, blendFunc, trianglesTwoColor, transform, transformFlags);
				
				if (_effect) {
					spColor light;
					spColor dark;
					light.r = color.r / 255.0f;
					light.g = color.g / 255.0f;
					light.b = color.b / 255.0f;
					light.a = color.a / 255.0f;
					dark.r = darkColor.r / 255.0f;
					dark.g = darkColor.g / 255.0f;
					dark.b = darkColor.b / 255.0f;
					dark.a = darkColor.a / 255.0f;
					
					for (int v = 0, vn = batchedTriangles->getTriangles().vertCount; v < vn; ++v) {
						V3F_C4B_C4B_T2F* vertex = batchedTriangles->getTriangles().verts + v;
						spColor lightCopy = light;
						spColor darkCopy = dark;
						_effect->transform(_effect, &vertex->position.x, &vertex->position.y, &vertex->texCoords.u, &vertex->texCoords.v, &lightCopy, &darkCopy);
						vertex->color.r = (GLubyte)(lightCopy.r * 255);
						vertex->color.g = (GLubyte)(lightCopy.g * 255);
						vertex->color.b = (GLubyte)(lightCopy.b * 255);
						vertex->color.a = (GLubyte)(lightCopy.a * 255);
						vertex->color2.r = (GLubyte)(darkCopy.r * 255);
						vertex->color2.g = (GLubyte)(darkCopy.g * 255);
						vertex->color2.b = (GLubyte)(darkCopy.b * 255);
						vertex->color2.a = 1;
					}
				} else {
					for (int v = 0, vn = batchedTriangles->getTriangles().vertCount; v < vn; ++v) {
						V3F_C4B_C4B_T2F* vertex = batchedTriangles->getTriangles().verts + v;
						vertex->color.r = (GLubyte)color.r;
						vertex->color.g = (GLubyte)color.g;
						vertex->color.b = (GLubyte)color.b;
						vertex->color.a = (GLubyte)color.a;
						vertex->color2.r = (GLubyte)darkColor.r;
						vertex->color2.g = (GLubyte)darkColor.g;
						vertex->color2.b = (GLubyte)darkColor.b;
						vertex->color2.a = 1;
					}
				}
			}
		}
		spSkeletonClipping_clipEnd(_clipper, slot);
	}
	spSkeletonClipping_clipEnd2(_clipper);
	
	if (lastTwoColorTrianglesCommand) {
		Node* parent = this->getParent();
		
		// We need to decide if we can postpone flushing the current
		// batch. We can postpone if the next sibling node is a
		// two color tinted skeleton with the same global-z.
		// The parent->getChildrenCount() > 100 check is a hack
		// as checking for a sibling is an O(n) operation, and if
		// all children of this nodes parent are skeletons, we
		// are in O(n2) territory.
		if (!parent || parent->getChildrenCount() > 100 || getChildrenCount() != 0) {
			lastTwoColorTrianglesCommand->setForceFlush(true);
		} else {
			Vector<Node*>& children = parent->getChildren();
			Node* sibling = nullptr;
			for (ssize_t i = 0; i < children.size(); i++) {
				if (children.at(i) == this) {
					if (i < children.size() - 1) {
						sibling = children.at(i+1);
						break;
					}
				}
			}
			if (!sibling) {
				lastTwoColorTrianglesCommand->setForceFlush(true);
			} else {
				SkeletonRenderer* siblingSkeleton = dynamic_cast<SkeletonRenderer*>(sibling);
				if (!siblingSkeleton || // flush is next sibling isn't a SkeletonRenderer
					!siblingSkeleton->isTwoColorTint() || // flush if next sibling isn't two color tinted
					!siblingSkeleton->isVisible() || // flush if next sibling is two color tinted but not visible
					(siblingSkeleton->getGlobalZOrder() != this->getGlobalZOrder())) { // flush if next sibling is two color tinted but z-order differs
					lastTwoColorTrianglesCommand->setForceFlush(true);
				}
			}
		}
	}
	
	if (_effect) _effect->end(_effect);

	if (_debugSlots || _debugBones || _debugMeshes) {
        drawDebug(renderer, transform, transformFlags);
	}
}

实现有点长,一点一点分析。


SkeletonBatch* batch = SkeletonBatch::getInstance();
	SkeletonTwoColorBatch* twoColorBatch = SkeletonTwoColorBatch::getInstance();

SkeletonBatch:骨骼合批,将所有骨骼放入合批指令中,本质上是TrianglesCommand指令。

cocos2d::TrianglesCommand* SkeletonBatch::addCommand(cocos2d::Renderer* renderer, float globalOrder, cocos2d::Texture2D* texture, cocos2d::GLProgramState* glProgramState, cocos2d::BlendFunc blendType, const cocos2d::TrianglesCommand::Triangles& triangles, const cocos2d::Mat4& mv, uint32_t flags) {
	TrianglesCommand* command = nextFreeCommand();
	command->init(globalOrder, texture, glProgramState, blendType, triangles, mv, flags);
	renderer->addCommand(command);
	return command;
}

SkeletonTwoColorBatch:双色着色合批。是TwoColorTrianglesCommand指令。

双色着色:TwoColorTint


    //是否双色着色
	bool isTwoColorTint = this->isTwoColorTint();
	//顶点动画
	if (_effect) _effect->begin(_effect, _skeleton);
    
    //计算附加着色颜色
    //用skeleton乘上(multiplying)槽位颜色来计算附件的着色颜色, 每通道的RGBA范围均为[0-1]
	Color4F nodeColor;
	nodeColor.r = getDisplayedColor().r / (float)255;
	nodeColor.g = getDisplayedColor().g / (float)255;
	nodeColor.b = getDisplayedColor().b / (float)255;
	nodeColor.a = getDisplayedOpacity() / (float)255;
	
	//双色着色使用
    Color4F color;
	Color4F darkColor;
	
	//附件顶点
	AttachmentVertices* attachmentVertices = nullptr;
	TwoColorTrianglesCommand* lastTwoColorTrianglesCommand = nullptr;

接下来遍历所有骨骼。

for (int i = 0, n = _skeleton->slotsCount; i < n; ++i)

取的时候是根据drawOrder去取的,简单说就是渲染顺序(插槽列表)。

spSlot* slot = _skeleton->drawOrder[i];
if (!slot->attachment) {
	spSkeletonClipping_clipEnd(_clipper, slot);
	continue;
}

如果插槽没有附件,那么就选择不渲染。

接下来,根据插槽附加的类型进行分别处理


SP_ATTACHMENT_REGION : 一个textured矩形

//强制转换为spRegionAttachment结构体
spRegionAttachment* attachment = (spRegionAttachment*)slot->attachment;
//从附件的渲染对象中获取atlas页texture(纹理坐标)
attachmentVertices = getAttachmentVertices(attachment);

//如果不是双色着色
if (!isTwoColorTint) {
  //创建一个TrianglesCommand指令
	triangles.indices = attachmentVertices->_triangles->indices; //data索引 index指针
	triangles.indexCount = attachmentVertices->_triangles->indexCount; //索引index的数量
	triangles.verts = batch->allocateVertices(attachmentVertices->_triangles->vertCount); //初始化顶点数据
	triangles.vertCount = attachmentVertices->_triangles->vertCount; //顶点个数
	//填充顶点数据
	memcpy(triangles.verts, attachmentVertices->_triangles->verts, sizeof(cocos2d::V3F_C4B_T2F) * attachmentVertices->_triangles->vertCount);
	//调用RegionAttachment::computeWorldVertices计算其世界顶点
	spRegionAttachment_computeWorldVertices(attachment, slot->bone, (float*)triangles.verts, 0, 6);
} else {
//如果是双色着色,那么用TwoColorTriangles去构建顶点信息
	trianglesTwoColor.indices = attachmentVertices->_triangles->indices;
	trianglesTwoColor.indexCount = attachmentVertices->_triangles->indexCount;
	trianglesTwoColor.verts = twoColorBatch->allocateVertices(attachmentVertices->_triangles->vertCount);
	trianglesTwoColor.vertCount = attachmentVertices->_triangles->vertCount;
	for (int ii = 0; ii < trianglesTwoColor.vertCount; ii++) {
		trianglesTwoColor.verts[ii].texCoords = attachmentVertices->_triangles->verts[ii].texCoords;
	}
	spRegionAttachment_computeWorldVertices(attachment, slot->bone, (float*)trianglesTwoColor.verts, 0, 7);
}

//计算的颜色就是附件的颜色	
color.r = attachment->color.r;
color.g = attachment->color.g;
color.b = attachment->color.b;
color.a = attachment->color.a;

上面的流程就是

在这里插入图片描述


SP_ATTACHMENT_MESH :一个textured网格,其顶点受到多个有权重骨詻的影响

spMeshAttachment* attachment = (spMeshAttachment*)slot->attachment;
attachmentVertices = getAttachmentVertices(attachment);
	
if (!isTwoColorTint) {
	triangles.indices = attachmentVertices->_triangles->indices;
	triangles.indexCount = attachmentVertices->_triangles->indexCount;
	triangles.verts = batch->allocateVertices(attachmentVertices->_triangles->vertCount);
	triangles.vertCount = attachmentVertices->_triangles->vertCount;
	memcpy(triangles.verts, attachmentVertices->_triangles->verts, sizeof(cocos2d::V3F_C4B_T2F) * attachmentVertices->_triangles->vertCount);
	//不同的是这里 计算世界顶点的方式
	spVertexAttachment_computeWorldVertices(SUPER(attachment), slot, 0, triangles.vertCount * sizeof(cocos2d::V3F_C4B_T2F) / 4, (float*)triangles.verts, 0, 6);
} else {
	trianglesTwoColor.indices = attachmentVertices->_triangles->indices;
	trianglesTwoColor.indexCount = attachmentVertices->_triangles->indexCount;
	trianglesTwoColor.verts = twoColorBatch->allocateVertices(attachmentVertices->_triangles->vertCount);
	trianglesTwoColor.vertCount = attachmentVertices->_triangles->vertCount;
	for (int ii = 0; ii < trianglesTwoColor.vertCount; ii++) {
		trianglesTwoColor.verts[ii].texCoords = attachmentVertices->_triangles->verts[ii].texCoords;
	}
	spVertexAttachment_computeWorldVertices(SUPER(attachment), slot, 0, trianglesTwoColor.vertCount * sizeof(V3F_C4B_C4B_T2F) / 4, (float*)trianglesTwoColor.verts, 0, 7);
}
	
color.r = attachment->color.r;
color.g = attachment->color.g;
color.b = attachment->color.b;
color.a = attachment->color.a;
	
break;

SP_ATTACHMENT_CLIPPING:一个多边形,用于在绘制中裁剪其他附件

case SP_ATTACHMENT_CLIPPING: {
	spClippingAttachment* clip = (spClippingAttachment*)slot->attachment;
	spSkeletonClipping_clipStart(_clipper, slot, clip);
}
default:
	spSkeletonClipping_clipEnd(_clipper, slot);
	continue;
}

计算附件颜色值

用skeleton乘上(multiplying)槽位颜色来计算附件的着色颜色, 每通道的RGBA范围均为[0-1]

color.a *= nodeColor.a * _skeleton->color.a * slot->color.a * 255;
// skip rendering if the color of this attachment is 0
if (color.a == 0){
	spSkeletonClipping_clipEnd(_clipper, slot);
	continue;
}
float multiplier = _premultipliedAlpha ? color.a : 255;
color.r *= nodeColor.r * _skeleton->color.r * slot->color.r * multiplier;
color.g *= nodeColor.g * _skeleton->color.g * slot->color.g * multiplier;
color.b *= nodeColor.b * _skeleton->color.b * slot->color.b * multiplier;

接下来颜色混合

BlendFunc blendFunc;
switch (slot->data->blendMode) {
	case SP_BLEND_MODE_ADDITIVE:
		blendFunc.src = _premultipliedAlpha ? GL_ONE : GL_SRC_ALPHA;
		blendFunc.dst = GL_ONE;
		break;
	case SP_BLEND_MODE_MULTIPLY:
		blendFunc.src = GL_DST_COLOR;
		blendFunc.dst = GL_ONE_MINUS_SRC_ALPHA;
		break;
	case SP_BLEND_MODE_SCREEN:
		blendFunc.src = GL_ONE;
		blendFunc.dst = GL_ONE_MINUS_SRC_COLOR;
		break;
	default:
		blendFunc.src = _premultipliedAlpha ? GL_ONE : GL_SRC_ALPHA;
		blendFunc.dst = GL_ONE_MINUS_SRC_ALPHA;
}

根据是否有裁剪附件,来添加batch渲染指令

如果有裁剪,优先处理裁剪,剔除掉裁剪区域外的纹理和顶点坐标。

然后在判断是否有顶点动画

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:/a/194569.html

如若内容造成侵权/违法违规/事实不符,请联系我们进行投诉反馈qq邮箱809451989@qq.com,一经查实,立即删除!

相关文章

内部类, Comparable接口, Comparator接口, Cloneable接口 ---java

目录 一. 内部类 1.1 静态内部类 1.2 实例内部类 1.3匿名内部类 二. 接口的使用实例 2.1 Comparable接口 2.2 Comparator接口 ---比较器 2.3 Cloneable接口 深拷贝浅拷贝 一. 内部类 当一个事物的内部&#xff0c;还有一个部分需要一个完整的结构进行描述&#xff0…

机器学习——支持向量机(SVM)

1.线性支持向量机 1.1数学模型 机器学习最终都是求解目标函数的最优问题&#xff1b; 一般都是讲问题转化为最小值来求解。 数学模型获得是一个不等式约束的最小化问题&#xff0c;求解时可通过构建拉格朗日函数求解。 1.2 拉格朗日函数及对偶问题求解 1.3 SMO算法求解 SMO算…

vue中的keep-alive详解与应用场景

​&#x1f308;个人主页&#xff1a;前端青山 &#x1f525;系列专栏&#xff1a;Vue篇 &#x1f516;人终将被年少不可得之物困其一生 依旧青山,本期给大家带来vue篇专栏内容:vue-keep-alive 目录 一、Keep-alive 是什么 二、使用场景 三、原理分析 四、案例实现 activa…

虹科干货 | 适用于基于FPGA的网络设备的IEEE 1588透明时钟架构

导读&#xff1a;在基于FPGA的网络设备中&#xff0c;精确的时间同步至关重要。IEEE 1588标准定义的精确时间协议&#xff08;PTP&#xff09;为网络中的设备提供了纳秒级的时间同步。本文将介绍虹科提供的适用于基于FPGA的网络设备的IEEE 1588透明时钟&#xff08;TC&#xff…

flink源码分析之功能组件(三)-rpc组件

简介 本系列是flink源码分析的第二个系列,上一个《flink源码分析之集群与资源》分析集群与资源,本系列分析功能组件,kubeclient,rpc,心跳,高可用,slotpool,rest,metrics,future。 本文解释rpc组件,rpc组件用于个核心组件,包括作业管理器,资源管理器和任务管理器之…

uni-app 离线打包安卓Apk(小白上手)

场景&#xff1a; 在使用uni-app 开发apk时&#xff0c;使用云打包有次数限制。尤其对于测试阶段是无比难受的&#xff0c;通常是浪费打包次数进行打包或者通过usb 给测试机更新开发环境&#xff0c;但这都是无比漫长的过程 尤其有多个测试机真的是噩梦般的存在 下载离线打包示…

基于mpvue实现的cnode社区demo(附精选源码32套,涵盖商城团购等)

社区类目没有开放给个人开发者&#xff0c;所以没能上线。 预览 项目配置文件&#xff0c;更改appid {"description": "项目配置文件","setting": {"urlCheck": true,"es6": false,"postcss": false,"minif…

扫地机器人市场持续火爆,景联文科技数据采集标注方案助力扫地机器人智能化升级

随着消费者对智能家居和清洁卫生的需求增加&#xff0c;扫地机器人市场规模不断扩大。市场竞争也日益激烈&#xff0c;各品牌都在努力提升产品性能和服务质量&#xff0c;以获取更大的市场份额。 IDC的统计数据显示&#xff0c;今年双十一前两周&#xff08;2023年10月23日至20…

stream流和方法引用

1.Stream流 1.1体验Stream流【理解】 案例需求 按照下面的要求完成集合的创建和遍历 创建一个集合&#xff0c;存储多个字符串元素把集合中所有以"张"开头的元素存储到一个新的集合把"张"开头的集合中的长度为3的元素存储到一个新的集合遍历上一步得到的集…

jsoup登录日志平台后调企业微信机器人自动发送错误日志告警

一、需求&#xff1a;错误日志Top10告警发送 二、需求分解 jsoup实现登录&#xff0c;获取到cookie和token等用户鉴权信息获取接口相应的key值调用日志平台错误日志Top榜接口&#xff0c;查询到结果集调用企业微信机器人发送消息接口加上定时任务&#xff0c;可以实现定时发送…

顺子日期(14)

顺着日期 public class Main {public static void main(String[] args) {int res 0;//2022年int[] days new int[] {31,28,31,30,31,30,31,31,30,31,30,31};//31,28,31,30,31,30,31,31,30,31,30,31//一三五七八十腊//构造2022年每一天的日期yyyymmddStringBuffer date new…

用python实现kindle文件转换pdf

上一篇文章讲了下用工具转换相关的格式&#xff1a;https://blog.csdn.net/weixin_42019349/article/details/134654695 今天来分享一个python库实现上述功能&#xff0c;实现文件转换自由 ^_^ 主角就是pypandoc库 # 安装方式 pip install pypandoc# pypandoc主要有三个函数…

ModuleNotFoundError: No module named ‘mdtex2html‘ module已经安装还是报错,怎么办?

用streamlit运行ChatGLM/basic_model/web_demo.py的时候&#xff0c;出现了module not found&#xff1a; ModuleNotFoundError: No module named mdtex2html Traceback: File "/home/haiyue/.local/lib/python3.10/site-packages/streamlit/runtime/scriptrunner/script…

机器学习的复习笔记3-回归的细谈

一、回归的细分 机器学习中的回归问题是一种用于预测连续型输出变量的任务。回归问题的类型和特点如下&#xff1a; 线性回归&#xff08;Linear Regression&#xff09;&#xff1a;线性回归是回归问题中最简单的一种方法。它假设自变量与因变量之间存在线性关系&#xff0c…

普通表计读数开发思路

一、普通表计类型介绍&#x1f349; 常见的普通表计有SF6&#xff0c;压力表&#xff0c;油位表&#xff08;指针类&#xff09;等。 图1&#xff1a;( 压力表) 图2&#xff1a;&#xff08;油位表-指针类&#xff09; 图3&#xff1a;&#xff08;SF6表&#xff09; 图4:&a…

巧妙之中见真章:深入解析常用的创建型设计模式

设计模式之创建型设计模式详解 一、设计模式是什么&#xff1f;二、模板方法2.1、代码结构2.2、符合的设计原则2.3、如何扩展代码2.4、小结 三、观察者模式3.1、代码结构3.2、符合的设计原则3.3、如何扩展代码3.4、小结 四、策略模式4.1、代码结构4.2、符合的设计原则4.3、如何…

K 最近邻算法

K 最近邻算法 简单 KNN海伦约会手写数字识别KNN 算法的优缺点 K 最近邻&#xff08;K-NearestNeighbor&#xff0c;KNN&#xff09;算法&#xff0c;是 1967 年由 Cover T 和 Hart P 提出的一种用于分类与回归的方法。 基本原理&#xff1a;存在一个带标签的数据集&#xff08;…

C语言第三十五弹---打印九九乘法表

C语言打印九九乘法表 思路&#xff1a;观察每一行可以看出乘号右边的一行值都是相同的&#xff0c;而乘号左边不断变化&#xff0c;所以使用嵌套循环&#xff0c;控制好 乘号左右值变化的条件即可。 #include <stdio.h>int main() {for (int i 1; i < 9; i){for (in…

【微服务】java 规则引擎使用详解

目录 一、什么是规则引擎 1.1 规则引擎概述 1.2 规则引擎执行过程 二、为什么要使用规则引擎 2.1 使用规则引擎的好处 2.1.1 易于维护和更新 2.1.2 增强应用程序的准确性和效率 2.1.3 加快应用程序的开发和部署 2.1.4 支持可视化和可管理性 2.2 规则引擎使用场景 三、…

开源四轴协作机械臂ultraArm激光雕刻技术案例!

注意安全事项 开始之前&#xff0c;请确保您已采取适当的安全措施&#xff0c;例如用于激光操作的防护眼镜、灭火器和通风良好的区域。 引言 随着科技的不断进步&#xff0c;激光雕刻技术已经成为当今制造行业中不可或缺的一部分。它以其高精度、高效率和广泛的材料适应性&…