- 수정됨
Hello, I'm trying to render some spine animations using the runtime of sfml 3.8. I hope to obtain the bounding box with image attachments and render all attachments correctly. The following is my try:
SkeletonDrawable* drawable = new SkeletonDrawable(skeletonData);
drawable->timeScale = 1.0f;
drawable->setUsePremultipliedAlpha(true);
// 初始化骨骼
Skeleton* skeleton = drawable->skeleton;
int size = 768;
AnimationState_setAnimationByName(drawable->state, 0, "Default", false);
drawable->state->tracks[0]->trackTime = 0;
Skeleton_setToSetupPose(skeleton);
AnimationState_apply(drawable->state, skeleton);
Skeleton_updateWorldTransform(skeleton);
SkeletonBounds* bounds = SkeletonBounds_create();
SkeletonBounds_update(bounds, skeleton, 1);
float minX = std::numeric_limits<float>::max();
float minY = std::numeric_limits<float>::max();
float maxX = std::numeric_limits<float>::lowest();
float maxY = std::numeric_limits<float>::lowest();
bool hasAttachment = false;
for (int i = 0; i < skeleton->slotsCount; ++i) {
spSlot* slot = skeleton->drawOrder[i];
spAttachment* attachment = slot->attachment;
if (!attachment) continue;
if (attachment->type == SP_ATTACHMENT_MESH) {
spMeshAttachment* mesh = (spMeshAttachment*)attachment;
if (!mesh->rendererObject) continue;
hasAttachment = true;
int vertexCount = mesh->super.worldVerticesLength;
std::vector<float> worldVertices(vertexCount);
spVertexAttachment_computeWorldVertices(
(spVertexAttachment*)mesh, slot, 0, vertexCount, worldVertices.data(), 0, 2
);
for (int v = 0; v < vertexCount; v += 2) {
float x = worldVertices[v];
float y = worldVertices[v + 1];
if (x < minX) minX = x;
if (y < minY) minY = y;
if (x > maxX) maxX = x;
if (y > maxY) maxY = y;
}
}
}
if (!hasAttachment) {
printf("No RegionAttachment found!\n");
return 2;
}
// minX = std::max(minX, -512.0f);
// minY = std::max(minY, -512.0f);
// maxX = std::min(maxX, 512.0f);
// maxY = std::min(maxY, 512.0f);
printf("RegionAttachment Bounding Box: Min(%.2f, %.2f), Max(%.2f, %.2f)\n", minX, minY, maxX, maxY);
float bboxWidth = maxX - minX;
float bboxHeight = maxY - minY;
skeleton->x = size / 2.0f - (minX + bboxWidth / 2.0f);
skeleton->y = size / 2.0f - (minY + bboxHeight / 2.0f);
Skeleton_updateWorldTransform(skeleton);
sf::RenderTexture renderTexture;
renderTexture.create(size, size);
renderTexture.clear(sf::Color::Transparent);
renderTexture.draw(*drawable);
renderTexture.display();
if (!renderTexture.getTexture().copyToImage().saveToFile(path)) {
printf("Failed to save image\n");
return 3;
}
else {
printf("Successfully exported to: %s\n", path);
}
It can be seen that I hoped the image area would actually be centered, but some Meshes failed to do so.
It can be seen here that attachments that work properly through the official skeletonViewer do not work properly in my code.
What should I do?