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.

refactor: migrate Spine rendering to C++ renderer plugin and remove ExternalMeshPlugin

esengine359e19673688
diff --git a/CMakeLists.txt b/CMakeLists.txt
index d978a274f..f5c88d516 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -54,6 +54,7 @@ set(ESENGINE_SOURCES
src/esengine/renderer/plugins/UIElementPlugin.cpp
src/esengine/renderer/plugins/TextPlugin.cpp
src/esengine/renderer/plugins/ShapePlugin.cpp
+ src/esengine/renderer/plugins/TilemapRenderPlugin.cpp
src/esengine/renderer/plugins/ParticlePlugin.cpp
src/esengine/text/BitmapFont.cpp
src/esengine/platform/input/Input.cpp
@@ -71,7 +72,6 @@ if(ES_ENABLE_SPINE)
src/esengine/spine/SpineExtension.cpp
src/esengine/spine/SpineResourceManager.cpp
src/esengine/spine/SpineSystem.cpp
- src/esengine/spine/SpineRenderer.cpp
src/esengine/renderer/plugins/SpinePlugin.cpp
)
endif()
@@ -129,6 +129,7 @@ set(ESENGINE_HEADERS
src/esengine/renderer/plugins/UIElementPlugin.hpp
src/esengine/renderer/plugins/TextPlugin.hpp
src/esengine/renderer/plugins/ShapePlugin.hpp
+ src/esengine/renderer/plugins/TilemapRenderPlugin.hpp
src/esengine/renderer/plugins/SpinePlugin.hpp
src/esengine/renderer/plugins/ParticlePlugin.hpp
src/esengine/platform/Platform.hpp
@@ -153,7 +154,6 @@ if(ES_ENABLE_SPINE)
src/esengine/spine/SpineExtension.hpp
src/esengine/spine/SpineResourceManager.hpp
src/esengine/spine/SpineSystem.hpp
- src/esengine/spine/SpineRenderer.hpp
)
endif()
diff --git a/src/esengine/bindings/RendererBindings.cpp b/src/esengine/bindings/RendererBindings.cpp
index 61c7e5e42..c0710f65d 100644
--- a/src/esengine/bindings/RendererBindings.cpp
+++ b/src/esengine/bindings/RendererBindings.cpp
@@ -73,6 +73,90 @@ SpineBounds getSpineBounds(ecs::Registry& registry, Entity entity) {
}
return bounds;
}
+
+void spine_update(ecs::Registry& registry, f32 dt) {
+ if (!g_spineSystem) return;
+ g_spineSystem->update(registry, dt);
+}
+
+bool spine_play(Entity entity, const std::string& animation, bool loop, i32 track) {
+ if (!g_spineSystem) return false;
+ return g_spineSystem->playAnimation(entity, animation, loop, track);
+}
+
+bool spine_addAnimation(Entity entity, const std::string& animation, bool loop, f32 delay, i32 track) {
+ if (!g_spineSystem) return false;
+ return g_spineSystem->addAnimation(entity, animation, loop, delay, track);
+}
+
+bool spine_setSkin(Entity entity, const std::string& skinName) {
+ if (!g_spineSystem) return false;
+ return g_spineSystem->setSkin(entity, skinName);
+}
+
+emscripten::val spine_getBonePosition(Entity entity, const std::string& boneName) {
+ if (!g_spineSystem) return emscripten::val::null();
+ f32 x = 0, y = 0;
+ if (!g_spineSystem->getBonePosition(entity, boneName, x, y)) {
+ return emscripten::val::null();
+ }
+ auto result = emscripten::val::object();
+ result.set("x", x);
+ result.set("y", y);
+ return result;
+}
+
+bool spine_hasInstance(Entity entity) {
+ if (!g_spineSystem) return false;
+ return g_spineSystem->getInstance(entity) != nullptr;
+}
+
+void spine_reloadAssets(ecs::Registry& registry) {
+ if (!g_spineSystem) return;
+ g_spineSystem->reloadAssets(registry);
+}
+
+emscripten::val spine_getAnimations(Entity entity) {
+ auto result = emscripten::val::array();
+ if (!g_spineSystem) return result;
+ auto names = g_spineSystem->getAnimationNames(entity);
+ for (size_t i = 0; i < names.size(); ++i) {
+ result.call<void>("push", names[i]);
+ }
+ return result;
+}
+
+emscripten::val spine_getSkins(Entity entity) {
+ auto result = emscripten::val::array();
+ if (!g_spineSystem) return result;
+ auto names = g_spineSystem->getSkinNames(entity);
+ for (size_t i = 0; i < names.size(); ++i) {
+ result.call<void>("push", names[i]);
+ }
+ return result;
+}
+
+void renderer_submitSpineBatch(
+ uintptr_t verticesPtr, i32 vertexCount,
+ uintptr_t indicesPtr, i32 indexCount,
+ u32 textureId, i32 blendMode,
+ uintptr_t transformPtr,
+ Entity entity, i32 layer, f32 depth
+) {
+ if (!g_initialized || !g_renderFrame) return;
+ auto* vertices = reinterpret_cast<const f32*>(verticesPtr);
+ auto* indices = reinterpret_cast<const u16*>(indicesPtr);
+ auto* transform = reinterpret_cast<const f32*>(transformPtr);
+ g_renderFrame->submitSpineBatch(
+ vertices, vertexCount, indices, indexCount,
+ textureId, blendMode, transform, entity, layer, depth);
+}
+
+void spine_setNeedsReload(ecs::Registry& registry, Entity entity, bool value) {
+ if (!registry.has<ecs::SpineAnimation>(entity)) return;
+ auto& comp = registry.get<ecs::SpineAnimation>(entity);
+ comp.needsReload = value;
+}
#endif
void renderFrame(ecs::Registry& registry, i32 viewportWidth, i32 viewportHeight) {
@@ -182,11 +266,13 @@ void renderer_resize(u32 width, u32 height) {
g_renderFrame->resize(width, height);
}
+void renderer_beginFrame() {
+ ctx().setTransformsUpdated(false);
+}
+
void renderer_begin(uintptr_t matrixPtr, u32 targetHandle) {
if (!g_renderFrame) return;
- ctx().setTransformsUpdated(false);
-
const f32* matrixData = reinterpret_cast<const f32*>(matrixPtr);
glm::mat4 viewProjection = glm::make_mat4(matrixData);
@@ -238,12 +324,16 @@ void renderer_submitParticles(ecs::Registry& registry) {
(void)registry;
}
+void renderer_updateTransforms(ecs::Registry& registry) {
+ ensureTransformsUpdated(registry);
+}
+
void renderer_submitAll(ecs::Registry& registry, u32 skipFlags, i32 vpX, i32 vpY, i32 vpW, i32 vpH) {
if (!g_renderFrame) return;
ensureTransformsUpdated(registry);
g_renderFrame->processMasks(registry, vpX, vpY, vpW, vpH);
- g_renderFrame->collectAll(registry);
+ g_renderFrame->collectAll(registry, skipFlags);
}
void particle_update(ecs::Registry& registry, f32 dt) {
@@ -274,24 +364,6 @@ u32 particle_getAliveCount(Entity entity) {
return g_particleSystem->aliveCount(entity);
}
-void renderer_submitTriangles(
- uintptr_t verticesPtr, i32 vertexCount,
- uintptr_t indicesPtr, i32 indexCount,
- u32 textureId, i32 blendMode,
- uintptr_t transformPtr) {
- if (!g_renderFrame) return;
-
- const f32* vertices = reinterpret_cast<const f32*>(verticesPtr);
- const u16* indices = reinterpret_cast<const u16*>(indicesPtr);
- const f32* transform = transformPtr ? reinterpret_cast<const f32*>(transformPtr) : nullptr;
-
- g_renderFrame->submitExternalTriangles(
- vertices, vertexCount,
- indices, indexCount,
- textureId, blendMode,
- transform);
-}
-
void renderer_setStage(i32 stage) {
if (!g_renderFrame) return;
g_renderFrame->setStage(static_cast<RenderStage>(stage));
diff --git a/src/esengine/bindings/RendererBindings.hpp b/src/esengine/bindings/RendererBindings.hpp
index d1d6b0c1c..f20ab45c5 100644
--- a/src/esengine/bindings/RendererBindings.hpp
+++ b/src/esengine/bindings/RendererBindings.hpp
@@ -25,6 +25,16 @@ struct SpineBounds {
};
SpineBounds getSpineBounds(ecs::Registry& registry, Entity entity);
+
+void spine_update(ecs::Registry& registry, f32 dt);
+bool spine_play(Entity entity, const std::string& animation, bool loop, i32 track);
+bool spine_addAnimation(Entity entity, const std::string& animation, bool loop, f32 delay, i32 track);
+bool spine_setSkin(Entity entity, const std::string& skinName);
+emscripten::val spine_getBonePosition(Entity entity, const std::string& boneName);
+bool spine_hasInstance(Entity entity);
+void spine_reloadAssets(ecs::Registry& registry);
+emscripten::val spine_getAnimations(Entity entity);
+emscripten::val spine_getSkins(Entity entity);
#endif
void renderFrame(ecs::Registry& registry, i32 viewportWidth, i32 viewportHeight);
@@ -33,6 +43,7 @@ void renderFrameWithMatrix(ecs::Registry& registry, i32 viewportWidth, i32 viewp
void renderer_init(u32 width, u32 height);
void renderer_resize(u32 width, u32 height);
+void renderer_beginFrame();
void renderer_begin(uintptr_t matrixPtr, u32 targetHandle);
void renderer_flush();
void renderer_end();
@@ -42,13 +53,17 @@ void renderer_submitBitmapText(ecs::Registry& registry);
void renderer_submitShapes(ecs::Registry& registry);
#ifdef ES_ENABLE_SPINE
void renderer_submitSpine(ecs::Registry& registry);
-#endif
-void renderer_submitTriangles(
+void renderer_submitSpineBatch(
uintptr_t verticesPtr, i32 vertexCount,
uintptr_t indicesPtr, i32 indexCount,
u32 textureId, i32 blendMode,
- uintptr_t transformPtr);
+ uintptr_t transformPtr,
+ Entity entity, i32 layer, f32 depth
+);
+void spine_setNeedsReload(ecs::Registry& registry, Entity entity, bool value);
+#endif
void renderer_submitParticles(ecs::Registry& registry);
+void renderer_updateTransforms(ecs::Registry& registry);
void renderer_submitAll(ecs::Registry& registry, u32 skipFlags, i32 vpX, i32 vpY, i32 vpW, i32 vpH);
void particle_update(ecs::Registry& registry, f32 dt);
void particle_play(ecs::Registry& registry, Entity entity);
diff --git a/src/esengine/bindings/WebSDKEntry.cpp b/src/esengine/bindings/WebSDKEntry.cpp
index 7f9d2f10d..7d0104a02 100644
--- a/src/esengine/bindings/WebSDKEntry.cpp
+++ b/src/esengine/bindings/WebSDKEntry.cpp
@@ -37,6 +37,8 @@
#include "../renderer/plugins/TextPlugin.hpp"
#include "../renderer/plugins/ShapePlugin.hpp"
#include "../renderer/plugins/ParticlePlugin.hpp"
+#include "../renderer/plugins/TilemapRenderPlugin.hpp"
+#include "../tilemap/TilemapSystem.hpp"
#ifdef ES_ENABLE_SPINE
#include "../renderer/plugins/SpinePlugin.hpp"
#endif
@@ -63,6 +65,8 @@ static_assert(sizeof(void*) == 4, "EM_JS pointer passing assumes wasm32 (4-byte
namespace esengine {
+tilemap::TilemapSystem& getTilemapSystem();
+
static EngineContext& ctx() { return EngineContext::instance(); }
#define g_initialized (ctx().isInitialized())
@@ -224,6 +228,11 @@ static void initSubsystems() {
renderFrame->addPlugin(std::make_unique<UIElementPlugin>());
renderFrame->addPlugin(std::make_unique<TextPlugin>());
renderFrame->addPlugin(std::make_unique<ShapePlugin>());
+ {
+ auto tilemapPlugin = std::make_unique<TilemapRenderPlugin>();
+ tilemapPlugin->setTilemapSystem(&getTilemapSystem());
+ renderFrame->addPlugin(std::move(tilemapPlugin));
+ }
#ifdef ES_ENABLE_SPINE
{
auto spinePlugin = std::make_unique<SpinePlugin>();
@@ -497,6 +506,18 @@ EMSCRIPTEN_BINDINGS(esengine_renderer) {
.field("valid", &esengine::SpineBounds::valid);
emscripten::function("getSpineBounds", &esengine::getSpineBounds);
+
+ emscripten::function("spine_update", &esengine::spine_update);
+ emscripten::function("spine_play", &esengine::spine_play);
+ emscripten::function("spine_addAnimation", &esengine::spine_addAnimation);
+ emscripten::function("spine_setSkin", &esengine::spine_setSkin);
+ emscripten::function("spine_getBonePosition", &esengine::spine_getBonePosition);
+ emscripten::function("spine_hasInstance", &esengine::spine_hasInstance);
+ emscripten::function("spine_reloadAssets", &esengine::spine_reloadAssets);
+ emscripten::function("spine_getAnimations", &esengine::spine_getAnimations);
+ emscripten::function("spine_getSkins", &esengine::spine_getSkins);
+ emscripten::function("renderer_submitSpineBatch", &esengine::renderer_submitSpineBatch);
+ emscripten::function("spine_setNeedsReload", &esengine::spine_setNeedsReload);
#endif
emscripten::function("invalidateMaterialCache", &esengine::invalidateMaterialCache);
@@ -556,6 +577,7 @@ EMSCRIPTEN_BINDINGS(esengine_renderer) {
emscripten::function("renderer_init", &esengine::renderer_init);
emscripten::function("renderer_resize", &esengine::renderer_resize);
+ emscripten::function("renderer_beginFrame", &esengine::renderer_beginFrame);
emscripten::function("renderer_begin", &esengine::renderer_begin);
emscripten::function("renderer_flush", &esengine::renderer_flush);
emscripten::function("renderer_end", &esengine::renderer_end);
@@ -566,8 +588,8 @@ EMSCRIPTEN_BINDINGS(esengine_renderer) {
#ifdef ES_ENABLE_SPINE
emscripten::function("renderer_submitSpine", &esengine::renderer_submitSpine);
#endif
- emscripten::function("renderer_submitTriangles", &esengine::renderer_submitTriangles);
emscripten::function("renderer_submitParticles", &esengine::renderer_submitParticles);
+ emscripten::function("renderer_updateTransforms", &esengine::renderer_updateTransforms);
emscripten::function("renderer_submitAll", &esengine::renderer_submitAll);
emscripten::function("particle_update", &esengine::particle_update);
emscripten::function("particle_play", &esengine::particle_play);
diff --git a/src/esengine/renderer/RenderFrame.cpp b/src/esengine/renderer/RenderFrame.cpp
index d19ac362b..5a243d845 100644
--- a/src/esengine/renderer/RenderFrame.cpp
+++ b/src/esengine/renderer/RenderFrame.cpp
@@ -17,9 +17,10 @@
#include <glad/glad.h>
#endif
+#include <glm/gtc/type_ptr.hpp>
+
#include <algorithm>
#include <cmath>
-#include <glm/gtc/type_ptr.hpp>
#include <unordered_set>
namespace esengine {
@@ -627,55 +628,53 @@ void RenderFrame::submitTileQuad(
draw_list_.push(cmd);
}
-void RenderFrame::submitExternalTriangles(
+void RenderFrame::submitSpineBatch(
const f32* vertices, i32 vertexCount,
const u16* indices, i32 indexCount,
u32 textureId, i32 blendMode,
- const f32* transform16
+ const f32* transform16,
+ Entity entity, i32 layer, f32 depth
) {
if (vertexCount <= 0 || indexCount <= 0) return;
- glm::mat4 model(1.0f);
- if (transform16) {
- model = glm::make_mat4(transform16);
- }
+ constexpr i32 FLOATS_PER_VERTEX = 8;
+ glm::mat4 model = glm::make_mat4(transform16);
+ BlendMode blend = static_cast<BlendMode>(std::clamp(blendMode, 0, 5));
+
+ u32 vBytes = static_cast<u32>(vertexCount) * sizeof(TileVertex);
+ u32 vOff = pool_.allocVertices(vBytes);
+ auto* dst = reinterpret_cast<TileVertex*>(pool_.vertexData() + vOff);
- constexpr i32 FLOATS_PER_VERT = 8;
- std::vector<TileVertex> batchVerts(vertexCount);
for (i32 i = 0; i < vertexCount; ++i) {
- const f32* v = vertices + i * FLOATS_PER_VERT;
- glm::vec4 pos = model * glm::vec4(v[0], v[1], 0.0f, 1.0f);
- batchVerts[i].position = glm::vec2(pos.x, pos.y);
- batchVerts[i].color = packColor(glm::vec4(v[2], v[3], v[4], v[5]));
- batchVerts[i].texCoord = glm::vec2(v[6], v[7]);
+ const f32* v = vertices + i * FLOATS_PER_VERTEX;
+ glm::vec4 worldPos = model * glm::vec4(v[0], v[1], 0.0f, 1.0f);
+ u32 pc = packColor(glm::vec4(v[4], v[5], v[6], v[7]));
+ dst[i] = { {worldPos.x, worldPos.y}, pc, {v[2], v[3]} };
}
- u32 vBytes = static_cast<u32>(vertexCount) * sizeof(TileVertex);
- u32 vOff = pool_.appendVertices(batchVerts.data(), vBytes);
u32 baseVertex = vOff / sizeof(TileVertex);
-
- std::vector<u16> remapped(indexCount);
+ std::vector<u16> offsetIndices(indexCount);
for (i32 i = 0; i < indexCount; ++i) {
- remapped[i] = static_cast<u16>(baseVertex + indices[i]);
+ offsetIndices[i] = static_cast<u16>(baseVertex + indices[i]);
}
- u32 iOff = pool_.appendIndices(remapped.data(), static_cast<u32>(indexCount));
+ u32 iOff = pool_.appendIndices(offsetIndices.data(), static_cast<u32>(indexCount));
DrawCommand cmd{};
cmd.sort_key = DrawCommand::buildSortKey(
- current_stage_, 0, batch_shader_id_,
- static_cast<BlendMode>(blendMode), 0, textureId, 0.0f);
+ current_stage_, layer, batch_shader_id_, blend, 0, textureId, depth);
cmd.index_offset = iOff;
cmd.index_count = static_cast<u32>(indexCount);
cmd.vertex_byte_offset = vOff;
cmd.shader_id = batch_shader_id_;
- cmd.blend_mode = static_cast<BlendMode>(blendMode);
+ cmd.blend_mode = blend;
cmd.layout_id = LayoutId::Batch;
cmd.texture_count = 1;
cmd.texture_ids[0] = textureId;
- cmd.entity = INVALID_ENTITY;
- cmd.type = RenderType::ExternalMesh;
- cmd.layer = 0;
+ cmd.entity = entity;
+ cmd.type = RenderType::Spine;
+ cmd.layer = layer;
+ clip_state_.applyTo(entity, cmd);
draw_list_.push(cmd);
}
@@ -703,7 +702,7 @@ void RenderFrame::buildClipState() {
}
}
-void RenderFrame::collectAll(ecs::Registry& registry) {
+void RenderFrame::collectAll(ecs::Registry& registry, u32 skipFlags) {
buildClipState();
RenderFrameContext ctx{
@@ -716,6 +715,7 @@ void RenderFrame::collectAll(ecs::Registry& registry) {
};
for (auto& plugin : plugins_) {
+ if (skipFlags != 0 && (skipFlags & plugin->skipFlag()) != 0) continue;
plugin->collect(registry, frustum_, clip_state_, pool_, draw_list_, ctx);
}
}
diff --git a/src/esengine/renderer/RenderFrame.hpp b/src/esengine/renderer/RenderFrame.hpp
index 299a17aef..640f4e102 100644
--- a/src/esengine/renderer/RenderFrame.hpp
+++ b/src/esengine/renderer/RenderFrame.hpp
@@ -85,11 +85,12 @@ public:
Entity entity, i32 layer, f32 depth
);
- void submitExternalTriangles(
+ void submitSpineBatch(
const f32* vertices, i32 vertexCount,
const u16* indices, i32 indexCount,
u32 textureId, i32 blendMode,
- const f32* transform16
+ const f32* transform16,
+ Entity entity, i32 layer, f32 depth
);
void setStage(RenderStage stage) { current_stage_ = stage; }
@@ -108,7 +109,7 @@ public:
u32 getSnapshotHeight() const { return height_; }
void addPlugin(std::unique_ptr<RenderTypePlugin> plugin);
- void collectAll(ecs::Registry& registry);
+ void collectAll(ecs::Registry& registry, u32 skipFlags = 0);
static constexpr u32 STAGE_COUNT = 4;
diff --git a/src/esengine/renderer/RenderTypePlugin.hpp b/src/esengine/renderer/RenderTypePlugin.hpp
index 63a320bb0..bc106afc6 100644
--- a/src/esengine/renderer/RenderTypePlugin.hpp
+++ b/src/esengine/renderer/RenderTypePlugin.hpp
@@ -34,6 +34,8 @@ public:
virtual void init(RenderFrameContext& ctx) { (void)ctx; }
virtual void shutdown() {}
+ virtual u32 skipFlag() const { return 0; }
+
virtual void collect(
ecs::Registry& registry,
const Frustum& frustum,
diff --git a/src/esengine/renderer/TransientBufferPool.cpp b/src/esengine/renderer/TransientBufferPool.cpp
index c8c7e1ef5..de733709a 100644
--- a/src/esengine/renderer/TransientBufferPool.cpp
+++ b/src/esengine/renderer/TransientBufferPool.cpp
@@ -113,7 +113,6 @@ void TransientBufferPool::upload() {
vbo_capacity_ = vertex_write_pos_;
glBufferData(GL_ARRAY_BUFFER, vbo_capacity_, vertex_staging_.data(), GL_DYNAMIC_DRAW);
} else {
- glBufferData(GL_ARRAY_BUFFER, vbo_capacity_, nullptr, GL_DYNAMIC_DRAW);
glBufferSubData(GL_ARRAY_BUFFER, 0, vertex_write_pos_, vertex_staging_.data());
}
@@ -125,7 +124,6 @@ void TransientBufferPool::upload() {
glBufferData(GL_ELEMENT_ARRAY_BUFFER, ebo_capacity_ * sizeof(u16),
index_staging_.data(), GL_DYNAMIC_DRAW);
} else {
- glBufferData(GL_ELEMENT_ARRAY_BUFFER, eboCapBytes, nullptr, GL_DYNAMIC_DRAW);
glBufferSubData(GL_ELEMENT_ARRAY_BUFFER, 0, eboBytes, index_staging_.data());
}
}
diff --git a/src/esengine/renderer/TransientBufferPool.hpp b/src/esengine/renderer/TransientBufferPool.hpp
index 0fa500e8d..0f5939cbb 100644
--- a/src/esengine/renderer/TransientBufferPool.hpp
+++ b/src/esengine/renderer/TransientBufferPool.hpp
@@ -36,6 +36,7 @@ public:
void bindLayout(LayoutId layout);
+ u8* vertexData() { return vertex_staging_.data(); }
u32 vertexBytesUsed() const { return vertex_write_pos_; }
u32 indicesUsed() const { return index_write_pos_; }
u32 vboId() const { return vbo_; }
diff --git a/src/esengine/renderer/plugins/ExternalMeshPlugin.cpp b/src/esengine/renderer/plugins/ExternalMeshPlugin.cpp
deleted file mode 100644
index 6ce095da3..000000000
--- a/src/esengine/renderer/plugins/ExternalMeshPlugin.cpp
+++ /dev/null
@@ -1,79 +0,0 @@
-#include "ExternalMeshPlugin.hpp"
-#include "../RenderContext.hpp"
-
-#include <glm/gtc/type_ptr.hpp>
-
-namespace esengine {
-
-void ExternalMeshPlugin::submitExternalTriangles(
- const f32* vertices, i32 vertexCount,
- const u16* indices, i32 indexCount,
- u32 textureId, i32 blendMode,
- const f32* transform16,
- RenderStage stage
-) {
- ExternalMeshData mesh;
- i32 floatCount = vertexCount * 8;
- mesh.vertices.assign(vertices, vertices + floatCount);
- mesh.indices.assign(indices, indices + indexCount);
- mesh.texture_id = textureId;
- mesh.blend_mode = static_cast<BlendMode>(blendMode);
- mesh.stage = stage;
- mesh.depth = 1.0f - static_cast<f32>(submit_order_++) * 0.0001f;
-
- if (transform16) {
- mesh.transform = glm::make_mat4(transform16);
- }
-
- meshes_.push_back(std::move(mesh));
-}
-
-void ExternalMeshPlugin::collect(
- ecs::Registry& registry,
- const Frustum& frustum,
- const ClipState& clips,
- TransientBufferPool& buffers,
- DrawList& draw_list,
- RenderFrameContext& ctx
-) {
- (void)registry;
- (void)frustum;
- (void)clips;
- (void)buffers;
-
- for (u32 i = 0; i < meshes_.size(); ++i) {
- const auto& mesh = meshes_[i];
-
- DrawCommand cmd{};
- cmd.sort_key = DrawCommand::buildSortKey(mesh.stage, 0, 0, mesh.blend_mode, 0, mesh.texture_id, mesh.depth);
- cmd.index_offset = i;
- cmd.index_count = static_cast<u32>(mesh.indices.size());
- cmd.vertex_byte_offset = 0;
- cmd.shader_id = 0;
- cmd.blend_mode = mesh.blend_mode;
- cmd.layout_id = LayoutId::ExtMesh;
- cmd.texture_count = 1;
- cmd.texture_ids[0] = mesh.texture_id;
- cmd.entity = static_cast<Entity>(0);
- cmd.type = RenderType::ExternalMesh;
- cmd.layer = 0;
-
- draw_list.push(cmd);
- }
-}
-
-void ExternalMeshPlugin::customDraw(
- const DrawCommand& cmd,
- StateTracker& state,
- TransientBufferPool& buffers,
- RenderFrameContext& ctx
-) {
- u32 meshIndex = cmd.index_offset;
- if (meshIndex >= meshes_.size()) return;
-
- (void)state;
- (void)buffers;
- (void)ctx;
-}
-
-} // namespace esengine
diff --git a/src/esengine/renderer/plugins/ExternalMeshPlugin.hpp b/src/esengine/renderer/plugins/ExternalMeshPlugin.hpp
deleted file mode 100644
index 4234b4442..000000000
--- a/src/esengine/renderer/plugins/ExternalMeshPlugin.hpp
+++ /dev/null
@@ -1,52 +0,0 @@
-#pragma once
-
-#include "../RenderTypePlugin.hpp"
-
-namespace esengine {
-
-class ExternalMeshPlugin : public RenderTypePlugin {
-public:
- void init(RenderFrameContext& ctx) override {}
- void shutdown() override {}
-
- void submitExternalTriangles(
- const f32* vertices, i32 vertexCount,
- const u16* indices, i32 indexCount,
- u32 textureId, i32 blendMode,
- const f32* transform16,
- RenderStage stage
- );
-
- void collect(
- ecs::Registry& registry,
- const Frustum& frustum,
- const ClipState& clips,
- TransientBufferPool& buffers,
- DrawList& draw_list,
- RenderFrameContext& ctx
- ) override;
-
- bool needsCustomDraw() const override { return true; }
- void customDraw(const DrawCommand& cmd,
- StateTracker& state,
- TransientBufferPool& buffers,
- RenderFrameContext& ctx) override;
-
- void clearFrameData() { meshes_.clear(); submit_order_ = 0; }
-
-private:
- struct ExternalMeshData {
- std::vector<f32> vertices;
- std::vector<u16> indices;
- u32 texture_id = 0;
- BlendMode blend_mode = BlendMode::Normal;
- glm::mat4 transform{1.0f};
- RenderStage stage = RenderStage::Transparent;
- f32 depth = 0.0f;
- };
-
- std::vector<ExternalMeshData> meshes_;
- u32 submit_order_ = 0;
-};
-
-} // namespace esengine
diff --git a/src/esengine/renderer/plugins/ParticlePlugin.hpp b/src/esengine/renderer/plugins/ParticlePlugin.hpp
index fd18333db..d0f979749 100644
--- a/src/esengine/renderer/plugins/ParticlePlugin.hpp
+++ b/src/esengine/renderer/plugins/ParticlePlugin.hpp
@@ -11,6 +11,8 @@ public:
void init(RenderFrameContext& ctx) override;
void shutdown() override {}
+ u32 skipFlag() const override { return 2; }
+
void setParticleSystem(particle::ParticleSystem* system) { particle_system_ = system; }
void collect(
diff --git a/src/esengine/renderer/plugins/SpinePlugin.cpp b/src/esengine/renderer/plugins/SpinePlugin.cpp
index c45f0ed8c..1791fb600 100644
--- a/src/esengine/renderer/plugins/SpinePlugin.cpp
+++ b/src/esengine/renderer/plugins/SpinePlugin.cpp
@@ -8,6 +8,10 @@
#include "../../ecs/components/SpineAnimation.hpp"
#include "../../spine/SpineSystem.hpp"
+#include <spine/RegionAttachment.h>
+#include <spine/MeshAttachment.h>
+#include <spine/ClippingAttachment.h>
+
#include <glm/gtc/matrix_transform.hpp>
#include <glm/gtc/quaternion.hpp>
@@ -15,11 +19,217 @@ namespace esengine {
void SpinePlugin::init(RenderFrameContext& ctx) {
batch_shader_id_ = ctx.batch_shader_id;
+ vertices_.reserve(512);
+ indices_.reserve(1024);
+ world_vertices_.reserve(512);
+}
+
+u32 SpinePlugin::packColor(f32 r, f32 g, f32 b, f32 a) {
+ auto clamp = [](f32 v) -> u8 { return static_cast<u8>(std::min(std::max(v, 0.0f), 1.0f) * 255.0f + 0.5f); };
+ return static_cast<u32>(clamp(r))
+ | (static_cast<u32>(clamp(g)) << 8)
+ | (static_cast<u32>(clamp(b)) << 16)
+ | (static_cast<u32>(clamp(a)) << 24);
+}
+
+u32 SpinePlugin::getTextureId(void* spineTexture, RenderFrameContext& ctx) {
+ if (!spineTexture) {
+ return ctx.white_texture_id;
+ }
+ u32 handleId = static_cast<u32>(reinterpret_cast<uintptr_t>(spineTexture)) - 1;
+ resource::TextureHandle handle(handleId);
+ auto* texture = ctx.resources.getTexture(handle);
+ if (texture) {
+ return texture->getId();
+ }
+ return ctx.white_texture_id;
+}
+
+BlendMode SpinePlugin::mapBlendMode(::spine::BlendMode mode) {
+ switch (mode) {
+ case ::spine::BlendMode_Additive: return BlendMode::Additive;
+ case ::spine::BlendMode_Multiply: return BlendMode::Multiply;
+ case ::spine::BlendMode_Screen: return BlendMode::Screen;
+ default: return BlendMode::Normal;
+ }
+}
+
+void SpinePlugin::emitBatch(
+ u32 textureId, BlendMode blend,
+ TransientBufferPool& buffers,
+ DrawList& draw_list,
+ const ClipState& clips,
+ Entity entity, i32 layer, f32 depth,
+ RenderFrameContext& ctx
+) {
+ if (vertices_.empty() || indices_.empty()) return;
+
+ u32 vBytes = static_cast<u32>(vertices_.size()) * sizeof(BatchVertex);
+ u32 vOff = buffers.appendVertices(vertices_.data(), vBytes);
+ u32 baseVertex = vOff / sizeof(BatchVertex);
+
+ for (auto& idx : indices_) {
+ idx = static_cast<u16>(idx + baseVertex);
+ }
+ u32 iOff = buffers.appendIndices(indices_.data(), static_cast<u32>(indices_.size()));
+
+ DrawCommand cmd{};
+ cmd.sort_key = DrawCommand::buildSortKey(
+ ctx.current_stage, layer, batch_shader_id_, blend, 0, textureId, depth);
+ cmd.index_offset = iOff;
+ cmd.index_count = static_cast<u32>(indices_.size());
+ cmd.vertex_byte_offset = vOff;
+ cmd.shader_id = batch_shader_id_;
+ cmd.blend_mode = blend;
+ cmd.layout_id = LayoutId::Batch;
+ cmd.texture_count = 1;
+ cmd.texture_ids[0] = textureId;
+ cmd.entity = entity;
+ cmd.type = RenderType::Spine;
+ cmd.layer = layer;
+
+ clips.applyTo(entity, cmd);
+ draw_list.push(cmd);
+
+ vertices_.clear();
+ indices_.clear();
+}
+
+void SpinePlugin::emitRegionAttachment(
+ ::spine::RegionAttachment* attachment,
+ ::spine::Slot& slot,
+ const glm::mat4& transform,
+ const glm::vec4& tintColor,
+ TransientBufferPool& buffers,
+ DrawList& draw_list,
+ const ClipState& clips,
+ Entity entity, i32 layer, f32 depth,
+ RenderFrameContext& ctx
+) {
+ auto* region = attachment->getRegion();
+ if (!region) return;
+
+ u32 textureId = getTextureId(region->rendererObject, ctx);
+ BlendMode blend = mapBlendMode(slot.getData().getBlendMode());
+
+ world_vertices_.resize(8);
+ attachment->computeWorldVertices(slot, world_vertices_.data(), 0, 2);
+
+ auto& uvs = attachment->getUVs();
+ auto& attachColor = attachment->getColor();
+ auto& slotColor = slot.getColor();
+ auto& skelColor = slot.getSkeleton().getColor();
+
+ f32 r = skelColor.r * slotColor.r * attachColor.r * tintColor.r;
+ f32 g = skelColor.g * slotColor.g * attachColor.g * tintColor.g;
+ f32 b = skelColor.b * slotColor.b * attachColor.b * tintColor.b;
+ f32 a = skelColor.a * slotColor.a * attachColor.a * tintColor.a;
+ u32 pc = packColor(r, g, b, a);
+
+ u16 base = static_cast<u16>(vertices_.size());
+
+ for (int j = 0; j < 4; ++j) {
+ glm::vec4 pos(world_vertices_[j * 2], world_vertices_[j * 2 + 1], 0.0f, 1.0f);
+ pos = transform * pos;
+ vertices_.push_back({
+ glm::vec2(pos.x, pos.y),
+ pc,
+ glm::vec2(uvs[j * 2], uvs[j * 2 + 1])
+ });
+ }
+
+ indices_.push_back(base);
+ indices_.push_back(base + 1);
+ indices_.push_back(base + 2);
+ indices_.push_back(base + 2);
+ indices_.push_back(base + 3);
+ indices_.push_back(base);
+
+ emitBatch(textureId, blend, buffers, draw_list, clips, entity, layer, depth, ctx);
+}
+
+void SpinePlugin::emitMeshAttachment(
+ ::spine::MeshAttachment* attachment,
+ ::spine::Slot& slot,
+ const glm::mat4& transform,
+ const glm::vec4& tintColor,
+ TransientBufferPool& buffers,
+ DrawList& draw_list,
+ const ClipState& clips,
+ Entity entity, i32 layer, f32 depth,
+ RenderFrameContext& ctx
+) {
+ auto* region = attachment->getRegion();
+ if (!region) return;
+
+ u32 textureId = getTextureId(region->rendererObject, ctx);
+ BlendMode blend = mapBlendMode(slot.getData().getBlendMode());
+
+ size_t vertexCount = attachment->getWorldVerticesLength() / 2;
+ world_vertices_.resize(attachment->getWorldVerticesLength());
+ attachment->computeWorldVertices(slot, 0, attachment->getWorldVerticesLength(),
+ world_vertices_.data(), 0, 2);
+
+ auto& uvs = attachment->getUVs();
+ auto& triangles = attachment->getTriangles();
+ auto& attachColor = attachment->getColor();
+ auto& slotColor = slot.getColor();
+ auto& skelColor = slot.getSkeleton().getColor();
+
+ f32 r = skelColor.r * slotColor.r * attachColor.r * tintColor.r;
+ f32 g = skelColor.g * slotColor.g * attachColor.g * tintColor.g;
+ f32 b = skelColor.b * slotColor.b * attachColor.b * tintColor.b;
+ f32 a = skelColor.a * slotColor.a * attachColor.a * tintColor.a;
+ u32 pc = packColor(r, g, b, a);
+
+ u16 base = static_cast<u16>(vertices_.size());
+
+ if (clipper_.isClipping()) {
+ clipper_.clipTriangles(world_vertices_.data(),
+ const_cast<unsigned short*>(triangles.buffer()),
+ triangles.size(),
+ uvs.buffer(), 2);
+
+ auto& clippedVerts = clipper_.getClippedVertices();
+ auto& clippedUVs = clipper_.getClippedUVs();
+ auto& clippedTris = clipper_.getClippedTriangles();
+
+ size_t clippedVertCount = clippedVerts.size() / 2;
+ for (size_t j = 0; j < clippedVertCount; ++j) {
+ glm::vec4 pos(clippedVerts[j * 2], clippedVerts[j * 2 + 1], 0.0f, 1.0f);
+ pos = transform * pos;
+ vertices_.push_back({
+ glm::vec2(pos.x, pos.y),
+ pc,
+ glm::vec2(clippedUVs[j * 2], clippedUVs[j * 2 + 1])
+ });
+ }
+
+ for (size_t j = 0; j < clippedTris.size(); ++j) {
+ indices_.push_back(static_cast<u16>(base + clippedTris[j]));
+ }
+ } else {
+ for (size_t j = 0; j < vertexCount; ++j) {
+ glm::vec4 pos(world_vertices_[j * 2], world_vertices_[j * 2 + 1], 0.0f, 1.0f);
+ pos = transform * pos;
+ vertices_.push_back({
+ glm::vec2(pos.x, pos.y),
+ pc,
+ glm::vec2(uvs[j * 2], uvs[j * 2 + 1])
+ });
+ }
+
+ for (size_t j = 0; j < triangles.size(); ++j) {
+ indices_.push_back(static_cast<u16>(base + triangles[j]));
+ }
+ }
+
+ emitBatch(textureId, blend, buffers, draw_list, clips, entity, layer, depth, ctx);
}
void SpinePlugin::collect(
ecs::Registry& registry,
- const Frustum& frustum,
+ const Frustum& /* frustum */,
const ClipState& clips,
TransientBufferPool& buffers,
DrawList& draw_list,
@@ -27,7 +237,6 @@ void SpinePlugin::collect(
) {
if (!spine_system_) return;
- spine_data_.clear();
auto view = registry.view<ecs::SpineAnimation>();
for (auto entity : view) {
@@ -48,49 +257,46 @@ void SpinePlugin::collect(
scale = t.worldScale;
}
- SpineRenderData sd;
- sd.transform = glm::mat4(1.0f);
- sd.transform = glm::translate(sd.transform, position);
- sd.transform *= glm::mat4_cast(rotation);
- sd.transform = glm::scale(sd.transform, scale);
- sd.skeleton = instance->skeleton.get();
- sd.tint_color = comp.color;
- sd.material_id = comp.material;
-
- u32 dataIndex = static_cast<u32>(spine_data_.size());
- spine_data_.push_back(sd);
-
- DrawCommand cmd{};
- cmd.sort_key = DrawCommand::buildSortKey(ctx.current_stage, comp.layer, batch_shader_id_, BlendMode::Normal, 0, 0, position.z);
- cmd.index_offset = dataIndex;
- cmd.index_count = 0;
- cmd.vertex_byte_offset = 0;
- cmd.shader_id = batch_shader_id_;
- cmd.blend_mode = BlendMode::Normal;
- cmd.layout_id = LayoutId::Batch;
- cmd.texture_count = 0;
- cmd.entity = entity;
- cmd.type = RenderType::Spine;
- cmd.layer = comp.layer;
-
- clips.applyTo(entity, cmd);
-
- draw_list.push(cmd);
- }
-}
+ glm::mat4 transform = glm::mat4(1.0f);
+ transform = glm::translate(transform, position);
+ transform *= glm::mat4_cast(rotation);
+ transform = glm::scale(transform, scale);
-void SpinePlugin::customDraw(
- const DrawCommand& cmd,
- StateTracker& state,
- TransientBufferPool& buffers,
- RenderFrameContext& ctx
-) {
- u32 dataIndex = cmd.index_offset;
- if (dataIndex >= spine_data_.size()) return;
+ auto* skeleton = instance->skeleton.get();
+ auto& drawOrder = skeleton->getDrawOrder();
+
+ for (size_t i = 0; i < drawOrder.size(); ++i) {
+ ::spine::Slot* slot = drawOrder[i];
+ if (!slot) continue;
+
+ ::spine::Attachment* attachment = slot->getAttachment();
+ if (!attachment) continue;
+
+ if (!slot->getData().isVisible()) continue;
+
+ if (attachment->getRTTI().isExactly(::spine::ClippingAttachment::rtti)) {
+ auto* clip = static_cast<::spine::ClippingAttachment*>(attachment);
+ clipper_.clipStart(*slot, clip);
+ continue;
+ }
- (void)state;
- (void)buffers;
- (void)ctx;
+ if (attachment->getRTTI().isExactly(::spine::RegionAttachment::rtti)) {
+ emitRegionAttachment(
+ static_cast<::spine::RegionAttachment*>(attachment),
+ *slot, transform, comp.color,
+ buffers, draw_list, clips, entity, comp.layer, position.z, ctx);
+ } else if (attachment->getRTTI().isExactly(::spine::MeshAttachment::rtti)) {
+ emitMeshAttachment(
+ static_cast<::spine::MeshAttachment*>(attachment),
+ *slot, transform, comp.color,
+ buffers, draw_list, clips, entity, comp.layer, position.z, ctx);
+ }
+
+ clipper_.clipEnd(*slot);
+ }
+
+ clipper_.clipEnd();
+ }
}
} // namespace esengine
diff --git a/src/esengine/renderer/plugins/SpinePlugin.hpp b/src/esengine/renderer/plugins/SpinePlugin.hpp
index 653c850d7..6e7a990ef 100644
--- a/src/esengine/renderer/plugins/SpinePlugin.hpp
+++ b/src/esengine/renderer/plugins/SpinePlugin.hpp
@@ -4,10 +4,8 @@
#ifdef ES_ENABLE_SPINE
-namespace spine {
-class Skeleton;
-class SkeletonClipping;
-}
+#include <spine/spine.h>
+#include <vector>
namespace esengine {
@@ -18,6 +16,8 @@ public:
void init(RenderFrameContext& ctx) override;
void shutdown() override {}
+ u32 skipFlag() const override { return 1; }
+
void setSpineSystem(spine::SpineSystem* system) { spine_system_ = system; }
void collect(
@@ -29,23 +29,58 @@ public:
RenderFrameContext& ctx
) override;
- bool needsCustomDraw() const override { return true; }
- void customDraw(const DrawCommand& cmd,
- StateTracker& state,
- TransientBufferPool& buffers,
- RenderFrameContext& ctx) override;
-
private:
- struct SpineRenderData {
- ::spine::Skeleton* skeleton = nullptr;
- glm::mat4 transform{1.0f};
- glm::vec4 tint_color{1.0f};
- u32 material_id = 0;
+ struct BatchVertex {
+ glm::vec2 position;
+ u32 color;
+ glm::vec2 texCoord;
};
+ static u32 packColor(f32 r, f32 g, f32 b, f32 a);
+ u32 getTextureId(void* spineTexture, RenderFrameContext& ctx);
+
+ static BlendMode mapBlendMode(::spine::BlendMode mode);
+
+ void emitRegionAttachment(
+ ::spine::RegionAttachment* attachment,
+ ::spine::Slot& slot,
+ const glm::mat4& transform,
+ const glm::vec4& tintColor,
+ TransientBufferPool& buffers,
+ DrawList& draw_list,
+ const ClipState& clips,
+ Entity entity, i32 layer, f32 depth,
+ RenderFrameContext& ctx
+ );
+
+ void emitMeshAttachment(
+ ::spine::MeshAttachment* attachment,
+ ::spine::Slot& slot,
+ const glm::mat4& transform,
+ const glm::vec4& tintColor,
+ TransientBufferPool& buffers,
+ DrawList& draw_list,
+ const ClipState& clips,
+ Entity entity, i32 layer, f32 depth,
+ RenderFrameContext& ctx
+ );
+
+ void emitBatch(
+ u32 textureId, BlendMode blend,
+ TransientBufferPool& buffers,
+ DrawList& draw_list,
+ const ClipState& clips,
+ Entity entity, i32 layer, f32 depth,
+ RenderFrameContext& ctx
+ );
+
spine::SpineSystem* spine_system_ = nullptr;
u32 batch_shader_id_ = 0;
- std::vector<SpineRenderData> spine_data_;
+
+ std::vector<BatchVertex> vertices_;
+ std::vector<u16> indices_;
+ std::vector<f32> world_vertices_;
+ ::spine::SkeletonClipping clipper_;
};
} // namespace esengine
diff --git a/src/esengine/spine/SpineRenderer.cpp b/src/esengine/spine/SpineRenderer.cpp
deleted file mode 100644
index f8d05293f..000000000
--- a/src/esengine/spine/SpineRenderer.cpp
+++ /dev/null
@@ -1,395 +0,0 @@
-/**
- * @file SpineRenderer.cpp
- * @brief Spine skeleton renderer implementation
- */
-
-// =============================================================================
-// Includes
-// =============================================================================
-
-#include "SpineRenderer.hpp"
-#include "../core/Log.hpp"
-#include "../ecs/components/SpineAnimation.hpp"
-#include "../ecs/components/Transform.hpp"
-#include "../renderer/Renderer.hpp"
-#include "../renderer/RenderCommand.hpp"
-
-#include <spine/RegionAttachment.h>
-#include <spine/MeshAttachment.h>
-#include <spine/ClippingAttachment.h>
-
-#ifdef ES_PLATFORM_WEB
- #include <GLES3/gl3.h>
-#else
- #ifdef _WIN32
- #include <windows.h>
- #endif
- #include <glad/glad.h>
-#endif
-
-namespace esengine::spine {
-
-// =============================================================================
-// SpineRenderer Implementation
-// =============================================================================
-
-SpineRenderer::SpineRenderer(RenderContext& context,
- resource::ResourceManager& resourceManager,
- SpineSystem& spineSystem)
- : context_(context)
- , resource_manager_(resourceManager)
- , spine_system_(spineSystem) {
-}
-
-SpineRenderer::~SpineRenderer() {
- if (initialized_) {
- shutdown();
- }
-}
-
-void SpineRenderer::init() {
- if (initialized_) return;
-
- vertices_.reserve(1024);
- indices_.reserve(2048);
- world_vertices_.reserve(1024);
-
- glGenVertexArrays(1, &vao_);
- glGenBuffers(1, &vbo_);
- glGenBuffers(1, &ebo_);
-
- glBindVertexArray(vao_);
- glBindBuffer(GL_ARRAY_BUFFER, vbo_);
- glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, ebo_);
-
- glEnableVertexAttribArray(0);
- glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, sizeof(SpineRenderVertex),
- reinterpret_cast<void*>(offsetof(SpineRenderVertex, position)));
- glEnableVertexAttribArray(1);
- glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, sizeof(SpineRenderVertex),
- reinterpret_cast<void*>(offsetof(SpineRenderVertex, uv)));
- glEnableVertexAttribArray(2);
- glVertexAttribPointer(2, 4, GL_FLOAT, GL_FALSE, sizeof(SpineRenderVertex),
- reinterpret_cast<void*>(offsetof(SpineRenderVertex, color)));
-
- glBindVertexArray(0);
-
- initialized_ = true;
- ES_LOG_INFO("SpineRenderer initialized");
-}
-
-void SpineRenderer::shutdown() {
- if (!initialized_) return;
-
- if (ebo_) { glDeleteBuffers(1, &ebo_); ebo_ = 0; }
- if (vbo_) { glDeleteBuffers(1, &vbo_); vbo_ = 0; }
- if (vao_) { glDeleteVertexArrays(1, &vao_); vao_ = 0; }
- vbo_capacity_ = 0;
- ebo_capacity_ = 0;
-
- vertices_.clear();
- indices_.clear();
- world_vertices_.clear();
-
- initialized_ = false;
- ES_LOG_INFO("SpineRenderer shutdown");
-}
-
-void SpineRenderer::begin(const glm::mat4& viewProjection) {
- view_projection_ = viewProjection;
- triangle_count_ = 0;
- draw_call_count_ = 0;
-
- glEnable(GL_BLEND);
- glBlendFuncSeparate(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA, GL_ONE, GL_ONE_MINUS_SRC_ALPHA);
- glDisable(GL_DEPTH_TEST);
-}
-
-void SpineRenderer::submit(ecs::Registry& registry) {
- auto view = registry.view<ecs::SpineAnimation>();
-
- for (auto entity : view) {
- auto& comp = registry.get<ecs::SpineAnimation>(entity);
- auto* instance = spine_system_.getInstance(entity);
-
- if (!instance || !instance->skeleton) continue;
-
- glm::vec3 position{0.0f};
- glm::quat rotation{1.0f, 0.0f, 0.0f, 0.0f};
- glm::vec3 scale{1.0f};
-
- if (registry.has<ecs::Transform>(entity)) {
- auto& t = registry.get<ecs::Transform>(entity);
- t.ensureDecomposed();
- position = t.worldPosition;
- rotation = t.worldRotation;
- scale = t.worldScale;
- }
-
- renderSkeleton(instance->skeleton.get(), position, rotation, scale,
- comp.color, comp.layer);
- }
-}
-
-void SpineRenderer::renderSkeleton(::spine::Skeleton* skeleton,
- const glm::vec3& position,
- const glm::quat& rotation,
- const glm::vec3& scale,
- const glm::vec4& tintColor,
- i32 layer) {
- if (!skeleton) return;
-
- glm::mat4 transform = glm::mat4(1.0f);
- transform = glm::translate(transform, position);
- transform *= glm::mat4_cast(rotation);
- transform = glm::scale(transform, scale);
-
- auto& drawOrder = skeleton->getDrawOrder();
- for (size_t i = 0; i < drawOrder.size(); ++i) {
- ::spine::Slot* slot = drawOrder[i];
- if (!slot) continue;
-
- ::spine::Attachment* attachment = slot->getAttachment();
- if (!attachment) continue;
-
- if (!slot->getData().isVisible()) continue;
-
- if (attachment->getRTTI().isExactly(::spine::ClippingAttachment::rtti)) {
- auto* clip = static_cast<::spine::ClippingAttachment*>(attachment);
- clipper_.clipStart(*slot, clip);
- continue;
- }
-
- if (attachment->getRTTI().isExactly(::spine::RegionAttachment::rtti)) {
- auto* region = static_cast<::spine::RegionAttachment*>(attachment);
- renderRegionAttachment(region, *slot, transform, tintColor);
- } else if (attachment->getRTTI().isExactly(::spine::MeshAttachment::rtti)) {
- auto* mesh = static_cast<::spine::MeshAttachment*>(attachment);
- renderMeshAttachment(mesh, *slot, transform, tintColor);
- }
-
- clipper_.clipEnd(*slot);
- }
-
- clipper_.clipEnd();
- flushBatch();
-}
-
-void SpineRenderer::renderRegionAttachment(::spine::RegionAttachment* attachment,
- ::spine::Slot& slot,
- const glm::mat4& transform,
- const glm::vec4& tintColor) {
- auto* region = attachment->getRegion();
- if (!region) return;
-
- world_vertices_.resize(8);
- attachment->computeWorldVertices(slot, world_vertices_.data(), 0, 2);
-
- auto& uvs = attachment->getUVs();
- auto& attachColor = attachment->getColor();
- auto& slotColor = slot.getColor();
- auto& skelColor = slot.getSkeleton().getColor();
-
- f32 r = skelColor.r * slotColor.r * attachColor.r * tintColor.r;
- f32 g = skelColor.g * slotColor.g * attachColor.g * tintColor.g;
- f32 b = skelColor.b * slotColor.b * attachColor.b * tintColor.b;
- f32 a = skelColor.a * slotColor.a * attachColor.a * tintColor.a;
-
- u32 textureId = getTextureId(region->rendererObject);
- auto blendMode = slot.getData().getBlendMode();
-
- if (textureId != current_texture_id_ || blendMode != current_blend_mode_) {
- flushBatch();
- current_texture_id_ = textureId;
- current_blend_mode_ = blendMode;
- setBlendMode(blendMode);
- }
-
- u32 baseIndex = static_cast<u32>(vertices_.size());
-
- for (int j = 0; j < 4; ++j) {
- glm::vec4 pos(world_vertices_[j * 2], world_vertices_[j * 2 + 1], 0.0f, 1.0f);
- pos = transform * pos;
-
- SpineRenderVertex vertex;
- vertex.position = glm::vec2(pos.x, pos.y);
- vertex.uv = glm::vec2(uvs[j * 2], uvs[j * 2 + 1]);
- vertex.color = glm::vec4(r, g, b, a);
- vertices_.push_back(vertex);
- }
-
- indices_.push_back(baseIndex);
- indices_.push_back(baseIndex + 1);
- indices_.push_back(baseIndex + 2);
- indices_.push_back(baseIndex + 2);
- indices_.push_back(baseIndex + 3);
- indices_.push_back(baseIndex);
-}
-
-void SpineRenderer::renderMeshAttachment(::spine::MeshAttachment* attachment,
- ::spine::Slot& slot,
- const glm::mat4& transform,
- const glm::vec4& tintColor) {
- auto* region = attachment->getRegion();
- if (!region) return;
-
- size_t vertexCount = attachment->getWorldVerticesLength() / 2;
- world_vertices_.resize(attachment->getWorldVerticesLength());
- attachment->computeWorldVertices(slot, 0, attachment->getWorldVerticesLength(),
- world_vertices_.data(), 0, 2);
-
- auto& uvs = attachment->getUVs();
- auto& triangles = attachment->getTriangles();
- auto& attachColor = attachment->getColor();
- auto& slotColor = slot.getColor();
- auto& skelColor = slot.getSkeleton().getColor();
-
- f32 r = skelColor.r * slotColor.r * attachColor.r * tintColor.r;
- f32 g = skelColor.g * slotColor.g * attachColor.g * tintColor.g;
- f32 b = skelColor.b * slotColor.b * attachColor.b * tintColor.b;
- f32 a = skelColor.a * slotColor.a * attachColor.a * tintColor.a;
-
- u32 textureId = getTextureId(region->rendererObject);
- auto blendMode = slot.getData().getBlendMode();
-
- if (textureId != current_texture_id_ || blendMode != current_blend_mode_) {
- flushBatch();
- current_texture_id_ = textureId;
- current_blend_mode_ = blendMode;
- setBlendMode(blendMode);
- }
-
- u32 baseIndex = static_cast<u32>(vertices_.size());
-
- if (clipper_.isClipping()) {
- clipper_.clipTriangles(world_vertices_.data(),
- const_cast<unsigned short*>(triangles.buffer()),
- triangles.size(),
- uvs.buffer(), 2);
-
- auto& clippedVerts = clipper_.getClippedVertices();
- auto& clippedUVs = clipper_.getClippedUVs();
- auto& clippedTris = clipper_.getClippedTriangles();
-
- size_t clippedVertCount = clippedVerts.size() / 2;
- for (size_t j = 0; j < clippedVertCount; ++j) {
- glm::vec4 pos(clippedVerts[j * 2], clippedVerts[j * 2 + 1], 0.0f, 1.0f);
- pos = transform * pos;
-
- SpineRenderVertex vertex;
- vertex.position = glm::vec2(pos.x, pos.y);
- vertex.uv = glm::vec2(clippedUVs[j * 2], clippedUVs[j * 2 + 1]);
- vertex.color = glm::vec4(r, g, b, a);
- vertices_.push_back(vertex);
- }
-
- for (size_t j = 0; j < clippedTris.size(); ++j) {
- indices_.push_back(baseIndex + clippedTris[j]);
- }
- } else {
- for (size_t j = 0; j < vertexCount; ++j) {
- glm::vec4 pos(world_vertices_[j * 2], world_vertices_[j * 2 + 1], 0.0f, 1.0f);
- pos = transform * pos;
-
- SpineRenderVertex vertex;
- vertex.position = glm::vec2(pos.x, pos.y);
- vertex.uv = glm::vec2(uvs[j * 2], uvs[j * 2 + 1]);
- vertex.color = glm::vec4(r, g, b, a);
- vertices_.push_back(vertex);
- }
-
- for (size_t j = 0; j < triangles.size(); ++j) {
- indices_.push_back(baseIndex + triangles[j]);
- }
- }
-}
-
-u32 SpineRenderer::getTextureId(void* spineTexture) {
- if (!spineTexture) {
- return context_.getWhiteTextureId();
- }
-
- u32 handleId = static_cast<u32>(reinterpret_cast<uintptr_t>(spineTexture)) - 1;
- resource::TextureHandle handle(handleId);
-
- auto* texture = resource_manager_.getTexture(handle);
- if (texture) {
- return texture->getId();
- }
-
- return context_.getWhiteTextureId();
-}
-
-void SpineRenderer::setBlendMode(::spine::BlendMode mode) {
- switch (mode) {
- case ::spine::BlendMode_Normal:
- glBlendFuncSeparate(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA, GL_ONE, GL_ONE_MINUS_SRC_ALPHA);
- break;
- case ::spine::BlendMode_Additive:
- glBlendFuncSeparate(GL_SRC_ALPHA, GL_ONE, GL_ONE, GL_ONE);
- break;
- case ::spine::BlendMode_Multiply:
- glBlendFuncSeparate(GL_DST_COLOR, GL_ONE_MINUS_SRC_ALPHA, GL_ONE, GL_ONE_MINUS_SRC_ALPHA);
- break;
- case ::spine::BlendMode_Screen:
- glBlendFuncSeparate(GL_ONE, GL_ONE_MINUS_SRC_COLOR, GL_ONE, GL_ONE_MINUS_SRC_COLOR);
- break;
- }
-}
-
-void SpineRenderer::flushBatch() {
- if (vertices_.empty() || indices_.empty()) {
- return;
- }
-
- auto* shader = context_.getTextureShader();
- if (!shader) {
- vertices_.clear();
- indices_.clear();
- return;
- }
-
- shader->bind();
- shader->setUniform("u_projection", view_projection_);
- shader->setUniform("u_model", glm::mat4(1.0f));
- shader->setUniform("u_color", glm::vec4(1.0f));
-
- glActiveTexture(GL_TEXTURE0);
- glBindTexture(GL_TEXTURE_2D, current_texture_id_);
- shader->setUniform("u_texture", 0);
-
- glBindVertexArray(vao_);
-
- auto vertexBytes = static_cast<GLsizeiptr>(vertices_.size() * sizeof(SpineRenderVertex));
- glBindBuffer(GL_ARRAY_BUFFER, vbo_);
- if (static_cast<u32>(vertexBytes) > vbo_capacity_) {
- vbo_capacity_ = static_cast<u32>(vertexBytes) * 2;
- glBufferData(GL_ARRAY_BUFFER, static_cast<GLsizeiptr>(vbo_capacity_), nullptr, GL_STREAM_DRAW);
- }
- glBufferSubData(GL_ARRAY_BUFFER, 0, vertexBytes, vertices_.data());
-
- auto indexBytes = static_cast<GLsizeiptr>(indices_.size() * sizeof(u32));
- glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, ebo_);
- if (static_cast<u32>(indexBytes) > ebo_capacity_) {
- ebo_capacity_ = static_cast<u32>(indexBytes) * 2;
- glBufferData(GL_ELEMENT_ARRAY_BUFFER, static_cast<GLsizeiptr>(ebo_capacity_), nullptr, GL_STREAM_DRAW);
- }
- glBufferSubData(GL_ELEMENT_ARRAY_BUFFER, 0, indexBytes, indices_.data());
-
- glDrawElements(GL_TRIANGLES, static_cast<GLsizei>(indices_.size()),
- GL_UNSIGNED_INT, nullptr);
-
- triangle_count_ += static_cast<u32>(indices_.size() / 3);
- draw_call_count_++;
-
- glBindVertexArray(0);
-
- vertices_.clear();
- indices_.clear();
-}
-
-void SpineRenderer::end() {
- flushBatch();
-}
-
-} // namespace esengine::spine
diff --git a/src/esengine/spine/SpineRenderer.hpp b/src/esengine/spine/SpineRenderer.hpp
deleted file mode 100644
index fd6d56d29..000000000
--- a/src/esengine/spine/SpineRenderer.hpp
+++ /dev/null
@@ -1,165 +0,0 @@
-/**
- * @file SpineRenderer.hpp
- * @brief Spine skeleton renderer
- * @details Renders Spine skeletons using the batch renderer.
- *
- * @author ESEngine Team
- * @date 2026
- *
- * @copyright Copyright (c) 2026 ESEngine Team
- * Licensed under the MIT License.
- */
-#pragma once
-
-// =============================================================================
-// Includes
-// =============================================================================
-
-#include "../core/Types.hpp"
-#include "../math/Math.hpp"
-#include "../ecs/Registry.hpp"
-#include "../renderer/RenderContext.hpp"
-#include "../resource/ResourceManager.hpp"
-#include "SpineSystem.hpp"
-
-#include <spine/spine.h>
-
-#include <vector>
-
-namespace esengine::spine {
-
-// =============================================================================
-// SpineRenderVertex
-// =============================================================================
-
-/**
- * @brief Vertex data for Spine rendering
- */
-struct SpineRenderVertex {
- glm::vec2 position;
- glm::vec2 uv;
- glm::vec4 color;
-};
-
-// =============================================================================
-// SpineRenderer
-// =============================================================================
-
-/**
- * @brief Renders Spine skeletons
- *
- * @details Extracts mesh data from Spine skeletons and submits triangles
- * to the BatchRenderer2D for efficient rendering.
- *
- * @code
- * SpineRenderer spineRenderer(context, resourceManager, spineSystem);
- * spineRenderer.init();
- *
- * // In render loop
- * spineRenderer.begin(viewProjection);
- * spineRenderer.submit(registry);
- * spineRenderer.end();
- *
- * spineRenderer.shutdown();
- * @endcode
- */
-class SpineRenderer {
-public:
- SpineRenderer(RenderContext& context,
- resource::ResourceManager& resourceManager,
- SpineSystem& spineSystem);
- ~SpineRenderer();
-
- SpineRenderer(const SpineRenderer&) = delete;
- SpineRenderer& operator=(const SpineRenderer&) = delete;
-
- // =========================================================================
- // Lifecycle
- // =========================================================================
-
- void init();
- void shutdown();
-
- // =========================================================================
- // Rendering
- // =========================================================================
-
- /**
- * @brief Begins a render frame
- * @param viewProjection Combined view-projection matrix
- */
- void begin(const glm::mat4& viewProjection);
-
- /**
- * @brief Submits all Spine entities for rendering
- * @param registry ECS registry containing SpineAnimation components
- */
- void submit(ecs::Registry& registry);
-
- /**
- * @brief Renders a single skeleton at a transform
- * @param skeleton The skeleton to render
- * @param position World position
- * @param rotation World rotation
- * @param scale World scale
- * @param tintColor Color multiplier
- * @param layer Render layer
- */
- void renderSkeleton(::spine::Skeleton* skeleton,
- const glm::vec3& position,
- const glm::quat& rotation,
- const glm::vec3& scale,
- const glm::vec4& tintColor,
- i32 layer);
-
- /**
- * @brief Ends the render frame and flushes batches
- */
- void end();
-
- // =========================================================================
- // Statistics
- // =========================================================================
-
- u32 getTriangleCount() const { return triangle_count_; }
- u32 getDrawCallCount() const { return draw_call_count_; }
-
-private:
- void renderRegionAttachment(::spine::RegionAttachment* attachment,
- ::spine::Slot& slot,
- const glm::mat4& transform,
- const glm::vec4& tintColor);
-
- void renderMeshAttachment(::spine::MeshAttachment* attachment,
- ::spine::Slot& slot,
- const glm::mat4& transform,
- const glm::vec4& tintColor);
-
- u32 getTextureId(void* spineTexture);
- void setBlendMode(::spine::BlendMode mode);
- void flushBatch();
-
- RenderContext& context_;
- resource::ResourceManager& resource_manager_;
- SpineSystem& spine_system_;
-
- glm::mat4 view_projection_;
- std::vector<SpineRenderVertex> vertices_;
- std::vector<u32> indices_;
- ::spine::SkeletonClipping clipper_;
- std::vector<f32> world_vertices_;
-
- u32 current_texture_id_ = 0;
- ::spine::BlendMode current_blend_mode_ = ::spine::BlendMode_Normal;
- u32 triangle_count_ = 0;
- u32 draw_call_count_ = 0;
- bool initialized_ = false;
-
- u32 vao_ = 0;
- u32 vbo_ = 0;
- u32 ebo_ = 0;
- u32 vbo_capacity_ = 0;
- u32 ebo_capacity_ = 0;
-};
-
-} // namespace esengine::spine
diff --git a/src/esengine/spine/SpineSystem.cpp b/src/esengine/spine/SpineSystem.cpp
index 73b881109..1f6d4d508 100644
--- a/src/esengine/spine/SpineSystem.cpp
+++ b/src/esengine/spine/SpineSystem.cpp
@@ -218,4 +218,32 @@ bool SpineSystem::getSkeletonBounds(Entity entity, f32& outX, f32& outY,
return true;
}
+std::vector<std::string> SpineSystem::getAnimationNames(Entity entity) const {
+ std::vector<std::string> result;
+ auto it = instances_.find(entity);
+ if (it == instances_.end() || !it->second.skeleton) return result;
+
+ auto* data = it->second.skeleton->getData();
+ auto& animations = data->getAnimations();
+ result.reserve(animations.size());
+ for (size_t i = 0; i < animations.size(); ++i) {
+ result.emplace_back(animations[i]->getName().buffer());
+ }
+ return result;
+}
+
+std::vector<std::string> SpineSystem::getSkinNames(Entity entity) const {
+ std::vector<std::string> result;
+ auto it = instances_.find(entity);
+ if (it == instances_.end() || !it->second.skeleton) return result;
+
+ auto* data = it->second.skeleton->getData();
+ auto& skins = data->getSkins();
+ result.reserve(skins.size());
+ for (size_t i = 0; i < skins.size(); ++i) {
+ result.emplace_back(skins[i]->getName().buffer());
+ }
+ return result;
+}
+
} // namespace esengine::spine
diff --git a/src/esengine/spine/SpineSystem.hpp b/src/esengine/spine/SpineSystem.hpp
index d74afb2b8..d6e8ce63b 100644
--- a/src/esengine/spine/SpineSystem.hpp
+++ b/src/esengine/spine/SpineSystem.hpp
@@ -166,6 +166,9 @@ public:
bool getSkeletonBounds(Entity entity, f32& outX, f32& outY,
f32& outWidth, f32& outHeight) const;
+ std::vector<std::string> getAnimationNames(Entity entity) const;
+ std::vector<std::string> getSkinNames(Entity entity) const;
+
private:
void loadSkeletonForEntity(Entity entity, ecs::SpineAnimation& comp);
void updateAnimation(Entity entity, ecs::SpineAnimation& comp, f32 deltaTime);