Skip to content

A fast 2D and 3D game engine — TypeScript SDK, C++/WebAssembly core, visual editor. Ship one project to web, desktop, WeChat MiniGames, playable ads, and native Android / iOS.

fix: resolve spine sequence flickering from stale texture ID

esenginedbf4a6958287
diff --git a/src/esengine/bindings/SpineModuleEntry.cpp b/src/esengine/bindings/SpineModuleEntry.cpp
index 1f7b38dcb..2b7b221e5 100644
--- a/src/esengine/bindings/SpineModuleEntry.cpp
+++ b/src/esengine/bindings/SpineModuleEntry.cpp
@@ -263,7 +263,7 @@ int spine_createInstance(int skeletonHandle) {
inst.skeleton = spSkeleton_create(it->second.skeletonData);
inst.state = spAnimationState_create(it->second.stateData);
spSkeleton_setToSetupPose(inst.skeleton);
-#ifdef ES_SPINE_38
+#if defined(ES_SPINE_38) || defined(ES_SPINE_41)
spSkeleton_updateWorldTransform(inst.skeleton);
#else
spSkeleton_updateWorldTransform(inst.skeleton, SP_PHYSICS_UPDATE);
@@ -324,7 +324,7 @@ void spine_update(int instanceId, float dt) {
spAnimationState_update(it->second.state, dt);
spAnimationState_apply(it->second.state, it->second.skeleton);
-#ifdef ES_SPINE_38
+#if defined(ES_SPINE_38) || defined(ES_SPINE_41)
spSkeleton_updateWorldTransform(it->second.skeleton);
#else
spSkeleton_update(it->second.skeleton, dt);
@@ -491,7 +491,7 @@ static void extractMeshBatches(int instanceId) {
spAttachment* attachment = slot->attachment;
if (!attachment) continue;
-#ifndef ES_SPINE_38
+#if !defined(ES_SPINE_38) && !defined(ES_SPINE_41)
if (!slot->data->visible) continue;
#endif
@@ -512,25 +512,41 @@ static void extractMeshBatches(int instanceId) {
if (attachment->type == SP_ATTACHMENT_REGION) {
auto* region = reinterpret_cast<spRegionAttachment*>(attachment);
+ g_worldVertices.resize(8);
+#ifdef ES_SPINE_38
+ spRegionAttachment_computeWorldVertices(region, slot->bone, g_worldVertices.data(), 0, 2);
+#else
+ spRegionAttachment_computeWorldVertices(region, slot, g_worldVertices.data(), 0, 2);
+#endif
+
uint32_t texId = getRegionTextureId(region);
if (!texId) continue;
- if (!currentBatch || texId != currentTexture || blendMode != currentBlend) {
+ int effectiveBlend = blendMode;
+#ifndef ES_SPINE_38
+ if (region->region) {
+ auto* atlasReg = reinterpret_cast<spAtlasRegion*>(region->region);
+ if (atlasReg->page && atlasReg->page->pma) {
+ if (effectiveBlend == 0) effectiveBlend = 4;
+ else if (effectiveBlend == 1) effectiveBlend = 5;
+ }
+ }
+#endif
+
+ bool needNewBatch = !currentBatch || texId != currentTexture || effectiveBlend != currentBlend;
+ if (!needNewBatch && currentBatch->vertices.size() / 8 + 4 > 65535) {
+ needNewBatch = true;
+ }
+
+ if (needNewBatch) {
g_meshBatches.emplace_back();
currentBatch = &g_meshBatches.back();
currentBatch->textureId = texId;
- currentBatch->blendMode = blendMode;
+ currentBatch->blendMode = effectiveBlend;
currentTexture = texId;
- currentBlend = blendMode;
+ currentBlend = effectiveBlend;
}
- g_worldVertices.resize(8);
-#ifdef ES_SPINE_38
- spRegionAttachment_computeWorldVertices(region, slot->bone, g_worldVertices.data(), 0, 2);
-#else
- spRegionAttachment_computeWorldVertices(region, slot, g_worldVertices.data(), 0, 2);
-#endif
-
float* uvs = region->uvs;
spColor& attachColor = region->color;
@@ -563,24 +579,41 @@ static void extractMeshBatches(int instanceId) {
} else if (attachment->type == SP_ATTACHMENT_MESH) {
auto* mesh = reinterpret_cast<spMeshAttachment*>(attachment);
+ int worldVerticesLength = SUPER(mesh)->worldVerticesLength;
+ int vertexCount = worldVerticesLength / 2;
+
+ g_worldVertices.resize(worldVerticesLength);
+ spVertexAttachment_computeWorldVertices(SUPER(mesh), slot, 0,
+ worldVerticesLength, g_worldVertices.data(), 0, 2);
+
uint32_t texId = getMeshTextureId(mesh);
if (!texId) continue;
- if (!currentBatch || texId != currentTexture || blendMode != currentBlend) {
+ int effectiveBlend = blendMode;
+#ifndef ES_SPINE_38
+ if (mesh->region) {
+ auto* atlasReg = reinterpret_cast<spAtlasRegion*>(mesh->region);
+ if (atlasReg->page && atlasReg->page->pma) {
+ if (effectiveBlend == 0) effectiveBlend = 4;
+ else if (effectiveBlend == 1) effectiveBlend = 5;
+ }
+ }
+#endif
+
+ bool needNewBatch = !currentBatch || texId != currentTexture || effectiveBlend != currentBlend;
+ if (!needNewBatch && currentBatch->vertices.size() / 8 + vertexCount > 65535) {
+ needNewBatch = true;
+ }
+
+ if (needNewBatch) {
g_meshBatches.emplace_back();
currentBatch = &g_meshBatches.back();
currentBatch->textureId = texId;
- currentBatch->blendMode = blendMode;
+ currentBatch->blendMode = effectiveBlend;
currentTexture = texId;
- currentBlend = blendMode;
+ currentBlend = effectiveBlend;
}
- int worldVerticesLength = SUPER(mesh)->worldVerticesLength;
- int vertexCount = worldVerticesLength / 2;
- g_worldVertices.resize(worldVerticesLength);
- spVertexAttachment_computeWorldVertices(SUPER(mesh), slot, 0,
- worldVerticesLength, g_worldVertices.data(), 0, 2);
-
float* uvs = mesh->uvs;
spColor& attachColor = mesh->color;
diff --git a/src/esengine/renderer/RenderFrame.cpp b/src/esengine/renderer/RenderFrame.cpp
index 263289aaf..b87d2d224 100644
--- a/src/esengine/renderer/RenderFrame.cpp
+++ b/src/esengine/renderer/RenderFrame.cpp
@@ -12,6 +12,7 @@
#include <spine/RegionAttachment.h>
#include <spine/MeshAttachment.h>
#include <spine/ClippingAttachment.h>
+#include <spine/Atlas.h>
#endif
#ifdef ES_PLATFORM_WEB
@@ -180,6 +181,7 @@ void RenderFrame::begin(const glm::mat4& view_projection, RenderTargetManager::H
items_.clear();
ext_vertex_storage_.clear();
ext_index_storage_.clear();
+ ext_submit_order_ = 0;
stats_ = Stats{};
bool usePostProcess = post_process_ && post_process_->isInitialized() &&
@@ -395,7 +397,9 @@ void RenderFrame::submitExternalTriangles(
RenderItem item;
item.type = RenderType::ExternalMesh;
item.stage = current_stage_;
- item.texture_id = textureId;
+ item.ext_bind_texture = textureId;
+ item.texture_id = 0;
+ item.depth = 1.0f - static_cast<f32>(ext_submit_order_++) * 0.0001f;
item.blend_mode = static_cast<BlendMode>(blendMode);
item.ext_vertices = ext_vertex_storage_.back().data();
item.ext_vertex_count = vertexCount;
@@ -589,6 +593,10 @@ void RenderFrame::renderSpine(u32 begin, u32 end) {
if (attachment->getRTTI().isExactly(::spine::RegionAttachment::rtti)) {
auto* region = static_cast<::spine::RegionAttachment*>(attachment);
+
+ spine_world_vertices_.resize(8);
+ region->computeWorldVertices(*slot, spine_world_vertices_.data(), 0, 2);
+
auto* regionData = region->getRegion();
if (!regionData) continue;
@@ -598,16 +606,24 @@ void RenderFrame::renderSpine(u32 begin, u32 end) {
if (tex) textureId = tex->getId();
}
- if (textureId != spine_current_texture_ || blendMode != spine_current_blend_) {
+ auto* atlasRegion = static_cast<::spine::AtlasRegion*>(regionData);
+ if (atlasRegion->page && atlasRegion->page->pma) {
+ if (blendMode == BlendMode::Normal) blendMode = BlendMode::PremultipliedAlpha;
+ else if (blendMode == BlendMode::Additive) blendMode = BlendMode::PmaAdditive;
+ }
+
+ bool needFlush = textureId != spine_current_texture_ || blendMode != spine_current_blend_;
+ if (!needFlush && spine_vertices_.size() + 4 > 65535) {
+ needFlush = true;
+ }
+
+ if (needFlush) {
flushSpineBatch();
spine_current_texture_ = textureId;
spine_current_blend_ = blendMode;
RenderCommand::setBlendMode(blendMode);
}
- spine_world_vertices_.resize(8);
- region->computeWorldVertices(*slot, spine_world_vertices_.data(), 0, 2);
-
auto& uvs = region->getUVs();
auto& attachColor = region->getColor();
@@ -638,6 +654,12 @@ void RenderFrame::renderSpine(u32 begin, u32 end) {
} else if (attachment->getRTTI().isExactly(::spine::MeshAttachment::rtti)) {
auto* mesh = static_cast<::spine::MeshAttachment*>(attachment);
+
+ size_t vertexCount = mesh->getWorldVerticesLength() / 2;
+ spine_world_vertices_.resize(mesh->getWorldVerticesLength());
+ mesh->computeWorldVertices(*slot, 0, mesh->getWorldVerticesLength(),
+ spine_world_vertices_.data(), 0, 2);
+
auto* regionData = mesh->getRegion();
if (!regionData) continue;
@@ -647,18 +669,24 @@ void RenderFrame::renderSpine(u32 begin, u32 end) {
if (tex) textureId = tex->getId();
}
- if (textureId != spine_current_texture_ || blendMode != spine_current_blend_) {
+ auto* atlasRegion = static_cast<::spine::AtlasRegion*>(regionData);
+ if (atlasRegion->page && atlasRegion->page->pma) {
+ if (blendMode == BlendMode::Normal) blendMode = BlendMode::PremultipliedAlpha;
+ else if (blendMode == BlendMode::Additive) blendMode = BlendMode::PmaAdditive;
+ }
+
+ bool needFlush = textureId != spine_current_texture_ || blendMode != spine_current_blend_;
+ if (!needFlush && spine_vertices_.size() + vertexCount > 65535) {
+ needFlush = true;
+ }
+
+ if (needFlush) {
flushSpineBatch();
spine_current_texture_ = textureId;
spine_current_blend_ = blendMode;
RenderCommand::setBlendMode(blendMode);
}
- size_t vertexCount = mesh->getWorldVerticesLength() / 2;
- spine_world_vertices_.resize(mesh->getWorldVerticesLength());
- mesh->computeWorldVertices(*slot, 0, mesh->getWorldVerticesLength(),
- spine_world_vertices_.data(), 0, 2);
-
auto& uvs = mesh->getUVs();
auto& triangles = mesh->getTriangles();
auto& attachColor = mesh->getColor();
@@ -746,7 +774,8 @@ void RenderFrame::flushSpineBatch() {
#endif
void RenderFrame::renderExternalMeshes(u32 begin, u32 end) {
- auto* shader = context_.getTextureShader();
+ auto* shader = context_.getExtMeshShader();
+ if (!shader) shader = context_.getTextureShader();
if (!shader) return;
for (u32 idx = begin; idx < end; ++idx) {
@@ -761,10 +790,9 @@ void RenderFrame::renderExternalMeshes(u32 begin, u32 end) {
shader->bind();
shader->setUniform("u_projection", view_projection_);
shader->setUniform("u_model", item->transform);
- shader->setUniform("u_color", glm::vec4(1.0f));
glActiveTexture(GL_TEXTURE0);
- glBindTexture(GL_TEXTURE_2D, item->texture_id);
+ glBindTexture(GL_TEXTURE_2D, item->ext_bind_texture);
shader->setUniform("u_texture", 0);
glBindVertexArray(ext_mesh_vao_);