Estella 0.113.0
A fast 2D and 3D game engine powered by WebAssembly and ECS
Loading...
Searching...
No Matches
DragonBonesAdapter.cpp
Go to the documentation of this file.
1// SPDX-License-Identifier: Apache-2.0
2// SPDX-FileCopyrightText: Copyright (c) 2024-present ESEngine Team
8
9#include <cmath>
10#include <cstdlib>
11
13
14namespace {
15
17inline void normalize(float& u, float& v, const TextureAtlasData* page) {
18 const float w = page && page->width > 0 ? static_cast<float>(page->width) : 1.0f;
19 const float h = page && page->height > 0 ? static_cast<float>(page->height) : 1.0f;
20 u /= w;
21 v /= h;
22}
23
24} // namespace
25
27 positions.clear();
28 uvs.clear();
29 indices.clear();
30 textureId = 0;
31
32 const auto* vertices =
33 (_deformVertices != nullptr && _display == _meshDisplay) ? _deformVertices->verticesData : nullptr;
34 auto* texture = static_cast<EsTextureData*>(_textureData);
35 if (_displayIndex < 0 || _display == nullptr || texture == nullptr || texture->textureId == 0) return;
36
37 textureId = texture->textureId;
38 const auto& region = texture->region;
39 const auto* page = texture->parent;
40
41 if (vertices != nullptr) {
42 const auto* data = vertices->data;
43 const auto* intArray = data->intArray;
44 const auto* floatArray = data->floatArray;
45 const unsigned vertexCount = intArray[vertices->offset + static_cast<unsigned>(BinaryOffset::MeshVertexCount)];
46 const unsigned triangleCount =
47 intArray[vertices->offset + static_cast<unsigned>(BinaryOffset::MeshTriangleCount)];
48 int vertexOffset = intArray[vertices->offset + static_cast<unsigned>(BinaryOffset::MeshFloatOffset)];
49 // The parser packs this offset as a signed 16-bit value; a mesh far enough
50 // into the float array wraps negative and has to be brought back.
51 if (vertexOffset < 0) vertexOffset += 65536;
52 const unsigned uvOffset = static_cast<unsigned>(vertexOffset) + vertexCount * 2;
53
54 positions.resize(vertexCount * 2);
55 uvs.resize(vertexCount * 2);
56 for (unsigned i = 0; i < vertexCount * 2; i += 2) {
57 positions[i] = floatArray[vertexOffset + i];
58 positions[i + 1] = floatArray[vertexOffset + i + 1];
59
60 const float u = floatArray[uvOffset + i];
61 const float v = floatArray[uvOffset + i + 1];
62 // A rotated atlas entry is stored turned a quarter clockwise, so the
63 // mesh's u walks the region's height and its v walks the width.
64 float su = texture->rotated ? region.x + (1.0f - v) * region.width : region.x + u * region.width;
65 float sv = texture->rotated ? region.y + u * region.height : region.y + v * region.height;
66 normalize(su, sv, page);
67 uvs[i] = su;
68 uvs[i + 1] = sv;
69 }
70
71 indices.resize(triangleCount * 3);
72 for (unsigned i = 0; i < triangleCount * 3; ++i) {
73 indices[i] = static_cast<std::uint16_t>(
74 intArray[vertices->offset + static_cast<unsigned>(BinaryOffset::MeshVertexIndices) + i]);
75 }
76
77 // A weighted mesh is posed in ARMATURE space by _updateMesh, so its
78 // vertices must not be run through the slot transform as well.
79 if (vertices->weight != nullptr) _identityTransform();
80 return;
81 }
82
83 // A plain image: one quad, sized in the same scaled units the pivot is in
84 // (Slot::_updateDisplayData multiplies the pivot by width * scale).
85 const float scale = texture->parent->scale * _armature->_armatureData->scale;
86 const float w = std::abs(region.width) * scale;
87 const float h = std::abs(region.height) * scale;
88
89 positions = {0.0f, 0.0f, 0.0f, h, w, h, w, 0.0f};
90 float u0 = region.x, v0 = region.y;
91 float u1 = region.x + region.width, v1 = region.y + region.height;
92 normalize(u0, v0, page);
93 normalize(u1, v1, page);
94 uvs = {u0, v0, u0, v1, u1, v1, u1, v0};
95 indices = {0, 1, 2, 0, 2, 3};
96}
97
99 if (_deformVertices == nullptr) return;
100 const auto* vertices = _deformVertices->verticesData;
101 if (vertices == nullptr) return;
102
103 const float scale = _armature->_armatureData->scale;
104 const auto& deform = _deformVertices->vertices;
105 const auto& bones = _deformVertices->bones;
106 const auto* weight = vertices->weight;
107 const bool hasDeform = !deform.empty();
108
109 const auto* data = vertices->data;
110 const auto* intArray = data->intArray;
111 const auto* floatArray = data->floatArray;
112 const auto vertexCount =
113 static_cast<std::size_t>(intArray[vertices->offset + static_cast<unsigned>(BinaryOffset::MeshVertexCount)]);
114 if (positions.size() < vertexCount * 2) positions.resize(vertexCount * 2);
115
116 if (weight != nullptr) {
117 int weightFloatOffset = intArray[weight->offset + static_cast<unsigned>(BinaryOffset::WeigthFloatOffset)];
118 if (weightFloatOffset < 0) weightFloatOffset += 65536;
119
120 // Each vertex names its own bone count, then that many (boneIndex, weight)
121 // pairs — so the three cursors advance together and none can be derived.
122 std::size_t iB = weight->offset + static_cast<unsigned>(BinaryOffset::WeigthBoneIndices) + bones.size();
123 std::size_t iV = static_cast<std::size_t>(weightFloatOffset);
124 std::size_t iF = 0;
125
126 for (std::size_t i = 0; i < vertexCount; ++i) {
127 const auto boneCount = static_cast<std::size_t>(intArray[iB++]);
128 float xG = 0.0f, yG = 0.0f;
129 for (std::size_t j = 0; j < boneCount; ++j) {
130 const auto boneIndex = static_cast<unsigned>(intArray[iB++]);
131 const auto* bone = bones[boneIndex];
132 if (bone == nullptr) continue;
133
134 const auto& matrix = bone->globalTransformMatrix;
135 const float w = floatArray[iV++];
136 float xL = floatArray[iV++] * scale;
137 float yL = floatArray[iV++] * scale;
138 if (hasDeform) {
139 xL += deform[iF++];
140 yL += deform[iF++];
141 }
142 xG += (matrix.a * xL + matrix.c * yL + matrix.tx) * w;
143 yG += (matrix.b * xL + matrix.d * yL + matrix.ty) * w;
144 }
145 positions[i * 2] = xG;
146 positions[i * 2 + 1] = yG;
147 }
148 return;
149 }
150
151 if (!hasDeform) return;
152
153 int vertexOffset = intArray[vertices->offset + static_cast<unsigned>(BinaryOffset::MeshFloatOffset)];
154 if (vertexOffset < 0) vertexOffset += 65536;
155 for (std::size_t i = 0; i < vertexCount; ++i) {
156 positions[i * 2] = floatArray[vertexOffset + i * 2] * scale + deform[i * 2];
157 positions[i * 2 + 1] = floatArray[vertexOffset + i * 2 + 1] * scale + deform[i * 2 + 1];
158 }
159}
160
161void EsSlot::emit(es::skeletal::TriangleSink& sink, const float tint[4]) const {
162 if (indices.empty() || textureId == 0 || !_visible) return;
163
164 const std::size_t vertexCount = positions.size() / 2;
165 std::vector<float> world(positions.size());
166
167 if (armatureSpace) {
168 world = positions;
169 } else {
170 const auto& m = globalTransformMatrix;
171 for (std::size_t i = 0; i < vertexCount; ++i) {
172 // The pivot moves the display's origin, and it lives in the same scaled
173 // units as the vertices — so it is subtracted before the matrix, not after.
174 const float x = positions[i * 2] - _pivotX;
175 const float y = positions[i * 2 + 1] - _pivotY;
176 world[i * 2] = m.a * x + m.c * y + m.tx;
177 world[i * 2 + 1] = m.b * x + m.d * y + m.ty;
178 }
179 }
180
181 // DragonBones poses in a y-DOWN space (its authoring tool inherits Flash's
182 // screen axes); the engine's world is y-up. Flipping here, at the one place
183 // geometry leaves the runtime, is what keeps every consumer consistent —
184 // db_getBounds reads these same vertices, so the box follows the character
185 // instead of describing a mirror of it. Winding is left alone: the 2D
186 // pipeline does not cull, and reversing it would only hide the flip if it
187 // ever started to.
188 for (std::size_t i = 0; i < vertexCount; ++i) {
189 world[i * 2 + 1] = -world[i * 2 + 1];
190 }
191
192 // The slot's authored colour and the entity's tint multiply. Replacing rather
193 // than multiplying would throw away what the artist set in DragonBones Pro the
194 // moment a game tinted anything.
195 const float rgba[4] = {
196 _colorTransform.redMultiplier * tint[0],
197 _colorTransform.greenMultiplier * tint[1],
198 _colorTransform.blueMultiplier * tint[2],
199 _colorTransform.alphaMultiplier * tint[3],
200 };
201 sink.emit(world.data(), uvs.data(), static_cast<int>(vertexCount), indices.data(),
202 static_cast<int>(indices.size()), textureId, static_cast<int>(_blendMode), rgba);
203}
204
206 if (_armature != nullptr) {
207 _armature->dispose();
208 _armature = nullptr;
209 }
210}
211
212// — Factory ------------------------------------------------------------------
213
224 _dragonBones = new DragonBones(&_eventDispatcher);
225}
226
228 delete _dragonBones;
229 _dragonBones = nullptr;
230}
231
232void EsFactory::advanceTime(float passedTime) {
233 if (_dragonBones != nullptr) _dragonBones->advanceTime(passedTime);
234}
235
237 static EsFactory factory;
238 return factory;
239}
240
241TextureAtlasData* EsFactory::_buildTextureAtlasData(TextureAtlasData* textureAtlasData, void* textureAtlas) const {
242 if (textureAtlasData != nullptr) {
243 static_cast<EsTextureAtlasData*>(textureAtlasData)
244 ->setTexture(static_cast<std::uint32_t>(reinterpret_cast<std::uintptr_t>(textureAtlas)));
245 return textureAtlasData;
246 }
247 return BaseObject::borrowObject<EsTextureAtlasData>();
248}
249
250Armature* EsFactory::_buildArmature(const BuildArmaturePackage& dataPackage) const {
251 auto* armature = BaseObject::borrowObject<Armature>();
252 // The proxy is an interface, not a pooled BaseObject; the armature owns it for
253 // as long as it lives and drops it in dispose().
254 auto* proxy = new EsArmatureProxy();
255 armature->init(dataPackage.armature, proxy, proxy, _dragonBones);
256 return armature;
257}
258
259Slot* EsFactory::_buildSlot(const BuildArmaturePackage&, const SlotData* slotData, Armature* armature) const {
260 auto* slot = BaseObject::borrowObject<EsSlot>();
261 // The armature is not optional: the slot reads its scale and its bones while
262 // posing, and a slot built without one silently produces nothing.
263 slot->init(slotData, armature, &slot->rawDisplay, &slot->meshDisplay);
264 return slot;
265}
266
267Armature* EsFactory::buildArmature(const std::string& armatureName, const std::string& dragonBonesName) {
268 return BaseFactory::buildArmature(armatureName, dragonBonesName, "", "");
269}
270
271DRAGONBONES_NAMESPACE_END
The host types DragonBones requires, implemented for a module that renders nothing itself and only ha...
The armature's "display": nothing, because there is nothing to display into.
void dispose(bool disposeProxy) override
Slot * _buildSlot(const BuildArmaturePackage &dataPackage, const SlotData *slotData, Armature *armature) const override
~EsFactory() override
static EsFactory & instance()
EsEventDispatcher _eventDispatcher
Armature * buildArmature(const std::string &armatureName, const std::string &dragonBonesName)
Build an armature by name; null when this data holds no such armature.
void advanceTime(float passedTime)
TextureAtlasData * _buildTextureAtlasData(TextureAtlasData *textureAtlasData, void *textureAtlas) const override
Armature * _buildArmature(const BuildArmaturePackage &dataPackage) const override
void _identityTransform() override
std::uint32_t textureId
void _updateFrame() override
bool armatureSpace
positions already have the bones applied
std::vector< float > uvs
u,v per vertex
std::vector< float > positions
x,y per vertex
void emit(es::skeletal::TriangleSink &sink, const float tint[4]) const
std::vector< std::uint16_t > indices
void _updateMesh() override
One atlas page: the texture the host bound to it, and its pixel size.
void normalize(float &u, float &v, const TextureAtlasData *page)
Atlas pixels → 0..1, which is what the renderer samples with.
Where a runtime pours the triangles it posed.