天天看點

基于Spine3.3的換裝(紙娃娃,avatar)功能

void SkeletonAnimation::SetSlotTexture(unsigned index, const std::string& slot_name, const std::string& attach_name, const std::string& texture_path)
{
	if (index >= _custom_pages.size())
	{
		_custom_pages.resize(index + 1, 0);
	}

	spAtlasPage* old_page = _custom_pages[index];
	if (old_page)
	{
		//just return if new page equals old page
		if (strcmp(old_page->name, texture_path.c_str()) == 0)
		{
			return;
		}
		//release old page
		else
		{
			spAtlasPage_dispose(old_page);
		}
	}


	spAtlasPage* new_page = spAtlasPage_create(_atlas, texture_path.c_str());
	new_page->magFilter = new_page->minFilter = SP_ATLAS_LINEAR;
	new_page->uWrap = new_page->vWrap = SP_ATLAS_CLAMPTOEDGE;
	_spAtlasPage_createTexture(new_page, texture_path.c_str());
	
	// cache new_page
	_custom_pages[index] = new_page;

	int tw = new_page->width;
	int th = new_page->height;
/*
	spAtlasRegion* new_reg = spAtlasRegion_create();
	new_reg->page = new_page;*/

	spAttachment* attach_ment = getAttachment(slot_name, attach_name);
	if (attach_ment && attach_ment->type == SP_ATTACHMENT_REGION)
	{
		spRegionAttachment* sp_attach = (spRegionAttachment*)attach_ment;
		spRegionAttachment_setUVs(sp_attach, 0, 0, 1, 1, 0);

		sp_attach->regionOffsetX = 0; // zero if not doing whitespace stripping
		sp_attach->regionOffsetY = 0;
		sp_attach->regionWidth = tw;
		sp_attach->regionHeight = th;
		sp_attach->regionOriginalWidth = sp_attach->regionWidth; // same if not doing whitespace stripping
		sp_attach->regionOriginalHeight = sp_attach->regionHeight;

		sp_attach->x = 0;
		sp_attach->y = 0;
		sp_attach->scaleX = 1;
		sp_attach->scaleY = 1;
		sp_attach->rotation = 0;
		sp_attach->width = tw;
		sp_attach->height = th;
		spRegionAttachment_updateOffset(sp_attach);

		AttachmentVertices* attachmentVertices = (AttachmentVertices*)sp_attach->rendererObject;
		attachmentVertices->_texture = (Texture2D*)new_page->rendererObject;
		V3F_C4B_T2F* vertices = attachmentVertices->_triangles->verts;
		for (int i = 0, ii = 0; i < 4; ++i, ii += 2) {
			vertices[i].texCoords.u = sp_attach->uvs[ii];
			vertices[i].texCoords.v = sp_attach->uvs[ii + 1];
		}
	}
}