Spine深入学习———— 渲染

news2024/9/28 17:32:08

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

渲染相关

绘制顺序

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

在这里插入图片描述

基础流程

在这里插入图片描述

渲染实现

以下按照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渲染指令

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

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

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

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

leetCode 216.组合总和 III + 回溯算法 + 剪枝 + 图解 + 笔记

找出所有相加之和为 n 的 k 个数的组合&#xff0c;且满足下列条件&#xff1a; 只使用数字1到9每个数字 最多使用一次 返回 所有可能的有效组合的列表 。该列表不能包含相同的组合两次&#xff0c;组合可以以任何顺序返回 示例 1: 输入: k 3, n 7 输出: [[1,2,4]] 解释…

两部手机数据传输后备忘录不见了怎么回事

想必很多人都遇到过&#xff0c;当两部手机进行备忘录数据传输后&#xff0c;突然发现备忘录不见了&#xff0c;这让人不禁着急上火&#xff0c;我也曾经遇到过这种事情导致很多重要的内容都丢失了。 一般出现这种情况可能是因为&#xff0c;两部手机使用的是不同的云服务&…

重生奇迹MU魔法师操作技能

重生奇迹MU魔法师增加伤害加点方式 一、智力敏捷加点&#xff1a;2点智力1点敏捷&#xff0c;这种加点就是智敏结合的加点了&#xff0c;属性是不错的&#xff0c;提升了非常多的属性点&#xff0c;智力是偏重输出的&#xff0c;也是法师最常见的一种加点了&#xff0c;输出伤…

Mac电脑音乐标签管理 Yate 激活最新 for Mac

Yate是一款非常实用的音频编辑和标记软件&#xff0c;它提供了丰富的功能和工具来帮助用户编辑、整理和管理音频文件。无论是在音乐收藏管理、DJ和音乐制作方面&#xff0c;还是在其他需要处理大量音频文件的领域&#xff0c;Yate都是非常值得推荐的工具。 Yate for Mac功能特…

Java线程同步

认识线程同步 解决方案 方法一&#xff1a;同步代码块 package com.itheima.d3;public class ThreadTest {public static void main(String[] args) {Accout acc new Accout("ICBC-110",100000);new DrawThread(acc,"小明").start();//小明new DrawThread…

phpstudy安装redis

Redis 是一个开源的高性能键值存储数据库&#xff0c;广泛用于缓存、消息队列、会话管理和实时数据分析等应用场景。 使用 PHP Redis 扩展&#xff0c;你可以在 PHP 代码中使用一系列的函数来连接到 Redis 服务器&#xff0c;并执行各种操作&#xff0c;如设置和获取键值对、操…

ubuntu配置ssh

本教程中的涉及路径的所有命令都是在root用户下的&#xff0c;读者可将路径中的/root更改为/home/用户名 1、重置密码 新安装的系统需要在服务器控制台点击“重置密码”&#xff0c;为root用户设置一个密码 ————————————————————————————————…

城市安全守护者:分析无人机在交通领域的应用

随着科技的进步&#xff0c;无人机在交通领域的应用不断增加&#xff0c;为智慧交通管理提供了新便利。无人机凭借其灵活性&#xff0c;在违章取证、交通事故侦查、交通疏导等方面展现出巨大的应用潜力。无人机在交通领域的应用有哪些&#xff1f;跟着我们一探究竟。 1、违章取…

同为科技(TOWE)模块化定制化让每条PDU实现专属供电解决方案

作为追求最高功率和空间效率的动态数据中心的理想产品&#xff0c;模块化、定制化PDU是追求最高功率和空间效率的动态数据中心的理想产品。同为科技&#xff08;TOWE&#xff09;是我国PDU行业的开创者和领导者&#xff0c;曾率先于中国电源分配单元http://www.pdu.com.cn网站上…

第13周 预习、实验与作业:Java网络编程

目录 1 课前问题列表 1.编写一个网络程序&#xff0c;为了与其他网络程序通信&#xff0c;至少要知道对方的什么信息&#xff1f; 2.TCP与UDP协议有什么不同的呢&#xff1f;什么时候该选择哪种协议&#xff1f;HTTP使用的是TCP还是UDP&#xff1f;不重要的短信息传送之类的功能…

Docker Compose;docker-compose;docker compose

(一) Docker Compose | 菜鸟教程 --> --> --> -->

借助工具落地提高外包软件项目代码提交规范

随着外包软件项目的不断增加&#xff0c;代码提交规范成为了一个必须解决的问题。由于外包项目的特殊性&#xff0c;很难保证每个开发者都按照统一的规范开发代码。为了解决这个问题&#xff0c;我们可以借助工具来提高代码提交规范。Codigger这个工具来解决外包软件项目中的代…

aikit 2023 3D与机械臂结合!

引言 今天我们主要了解3D摄像头是如何跟机械臂应用相结合的。我们最近准备推出一款新的机械臂套装AI Kit 2023 3D&#xff0c;熟悉我们的老用户应该知道&#xff0c;我们之前的AI Kit 2023套装使用的是2D摄像头。 随着技术进步&#xff0c;市场需求和领域的扩大&#xff0c;2D的…

vue3 + element-plus + ts el-table封装

vue3 element-plus ts el-table封装 博客参考https://blog.csdn.net/weixin_45291937/article/details/125523244 1. 文件位置&#xff08;根据自己的需求&#xff09; 2. 在 custom 文件夹下面 创建 mytable 文件夹 3. 直接上代码 // index.vue<template><div …

centos7搭建 PXE 服务安装 window10/11 系统

最近想搭建之前基于 window server 的 window 批量安装&#xff0c;但想想装 window server 真的太麻烦了&#xff0c;我只是为了 PXE 安装系统而已&#xff0c;这些装一个极度消耗资源的系统真是相当麻烦呀&#xff0c;之前装的 server 不维护的话&#xff0c;不是被挖矿盯上就…

Linux新加磁盘的完整步骤

目录 新加磁盘的完整步骤磁盘分区磁盘文件命名经典分区方案fdiskparted 分区格式化挂载分区 新加磁盘的完整步骤 物理连接 --> 分区 --> 格式化 --> 挂载 --> 更新/etc/fstab文件实现永久挂载 磁盘分区 主分区primary用来安装操作系统、存放数据&#xff0c;可以…

历时三个月,我发布了一款领取外卖红包小程序

近几年&#xff0c;推广外卖红包爆火&#xff0c;各种推广外卖红包的公众号层出不穷。于是&#xff0c;我就在想外卖红包究竟是怎么一回事。就这样&#xff0c;我带着问题开始了关于外卖红包的研究。 在研究的过程中&#xff0c;我开始了解隐藏优惠券、cps等一系列相关的术语。…

UCP通信

一&#xff0c;概括 二 &#xff0c;常用方法 三&#xff0c;实现步骤&#xff08;一发一收&#xff09; 四&#xff0c;案例&#xff08;一发一收&#xff09; &#xff08;1&#xff09;&#xff1a;客户端 &#xff08;2&#xff09;&#xff1a;服务端 &#xff08;3&…

解决:ModuleNotFoundError: No module named ‘PyQt5‘

解决&#xff1a;ModuleNotFoundError: No module named ‘PyQt5’ 文章目录 解决&#xff1a;ModuleNotFoundError: No module named PyQt5背景报错问题报错翻译报错位置代码报错原因解决方法安装PyQt5在PyCharm中配置PyQt5对于新项目对于已有项目 今天的分享就到此结束了 背景…

NX二次开发UF_CURVE_convert_conic_to_gen 函数介绍

文章作者&#xff1a;里海 来源网站&#xff1a;https://blog.csdn.net/WangPaiFeiXingYuan UF_CURVE_convert_conic_to_gen Defined in: uf_curve.h int UF_CURVE_convert_conic_to_gen(UF_CURVE_conic_p_t conic_data, UF_CURVE_genconic_t * gen_conic_data ) overview 概…