50 auto& registry = collect_ctx.
registry;
57 for (
auto entity : uiView) {
58 if (!collect_ctx.
accepts(entity))
continue;
63 const auto* node = registry.tryGet<
ecs::UINode>(entity);
64 if (!node || node->hidden_in_tree_)
continue;
65 const auto* mask = registry.tryGet<
ecs::UIMask>(entity);
70 && renderer.color.a * node->alpha_in_tree_ == 0.0f
71 && !clips.isStencilMask(entity))
continue;
72 if (clips.isEmptyScissor(entity)) { ++collect_ctx.
culled;
continue; }
73 f32 w = node->computed_size_.x, h = node->computed_size_.y;
74 f32 pivotX = 0.5f, pivotY = 0.5f;
75 if (w <= 0.0f || h <= 0.0f)
continue;
81 glm::vec3 position = collect_ctx.
isOverlay() ? glm::vec3(transform.worldPosition) :
rebased(transform.worldPosition);
82 const auto& rotation = transform.worldRotation;
83 const auto& scale = transform.worldScale;
84 const bool parented = registry.has<
ecs::Parent>(entity);
86 const glm::vec3 basisOrigin = position;
87 const auto& matrix = transform.cachedMatrix_;
88 const bool mapBasis = parented && (matrix[0].y != 0 || matrix[1].x != 0
89 || matrix[0].z != 0 || matrix[1].z != 0 || matrix[0].x < 0 || matrix[1].y < 0);
92 f32 dx = (0.5f - pivotX) * w * scale.x;
93 f32 dy = (0.5f - pivotY) * h * scale.y;
94 f32 sinHalf = rotation.z;
95 if (sinHalf * sinHalf > 1e-6f) {
96 f32 cosHalf = rotation.w;
97 f32 s = 2.0f * sinHalf * cosHalf;
98 f32 c = cosHalf * cosHalf - sinHalf * sinHalf;
99 f32 rdx = dx * c - dy * s;
100 f32 rdy = dx * s + dy * c;
109 flatTurnZ(rotation), glm::vec3(w * scale.x, h * scale.y, 0.0f) * 0.5f);
111 halfExtents = glm::abs(glm::vec3(transform.cachedMatrix_[0])) * w * .5f
112 + glm::abs(glm::vec3(transform.cachedMatrix_[1])) * h * .5f;
114 if (!collect_ctx.
visible(position, halfExtents,
false)) { ++collect_ctx.
culled;
continue; }
116 f32 angle = 2.0f * std::atan2(rotation.z, rotation.w);
117 if (mapBasis) angle = 0.0f;
120 u32 textureId =
ctx.white_texture_id;
121 glm::vec2 texSize{0.0f};
122 bool useNineSlice =
false;
123 glm::vec4 sliceBorder{0.0f};
125 if (renderer.texture.isValid()) {
126 Texture* tex =
ctx.resources.getTexture(renderer.texture);
128 textureId = tex->
getId();
133 const auto* metadata =
ctx.resources.getTextureMetadata(renderer.texture);
134 if (metadata && metadata->sliceBorder.hasSlicing()) {
136 sliceBorder = glm::vec4(
137 metadata->sliceBorder.left,
138 metadata->sliceBorder.right,
139 metadata->sliceBorder.top,
140 metadata->sliceBorder.bottom
148 if (sliceBorder == glm::vec4(0.0f)) {
149 sliceBorder = renderer.sliceBorder;
156 glm::vec2 uvOffset = renderer.uvOffset;
157 glm::vec2 uvScale = renderer.uvScale;
159 if (renderer.tileSize.x > 0.0f && renderer.tileSize.y > 0.0f) {
160 uvScale.x = renderer.uvScale.x * (w / renderer.tileSize.x);
161 uvScale.y = renderer.uvScale.y * (h / renderer.tileSize.y);
164 f32 amount = std::clamp(renderer.fillAmount, 0.0f, 1.0f);
166 uvScale.x = renderer.uvScale.x * amount;
168 uvOffset.x = renderer.uvOffset.x + renderer.uvScale.x * (1.0f - amount);
171 uvScale.y = renderer.uvScale.y * amount;
173 uvOffset.y = renderer.uvOffset.y + renderer.uvScale.y * (1.0f - amount);
179 glm::vec2 finalSize = glm::vec2(w, h) * glm::vec2(scale);
188 && texSize.x > 0.0f && texSize.y > 0.0f
189 && finalSize.x > 0.0f && finalSize.y > 0.0f) {
190 const f32 boxRatio = finalSize.x / finalSize.y;
191 const f32 texRatio = texSize.x / texSize.y;
193 if (texRatio > boxRatio) finalSize.y = finalSize.x / texRatio;
194 else finalSize.x = finalSize.y * texRatio;
196 if (texRatio > boxRatio) {
197 const f32 keep = boxRatio / texRatio;
198 uvOffset.x += uvScale.x * (1.0f - keep) * 0.5f;
201 const f32 keep = texRatio / boxRatio;
202 uvOffset.y += uvScale.y * (1.0f - keep) * 0.5f;
219 useNineSlice =
false;
221 f32 amount = std::clamp(renderer.fillAmount, 0.0f, 1.0f);
222 f32 offX = 0.0f, offY = 0.0f;
224 f32 removed = finalSize.x * (1.0f - amount);
226 finalSize.x *= amount;
228 f32 removed = finalSize.y * (1.0f - amount);
230 finalSize.y *= amount;
232 if (std::abs(angle) > 0.001f) {
233 f32 cosA = std::cos(angle), sinA = std::sin(angle);
234 f32 rx = offX * cosA - offY * sinA;
235 f32 ry = offX * sinA + offY * cosA;
245 .stage =
ctx.current_stage,
249 .textureId = textureId,
250 .depth = collect_ctx.
sortDepth(position),
251 .cullBit = renderer.uiCullBit,
266 if (renderer.material != 0) {
267 if (
const MaterialRecord* m =
ctx.materials ?
ctx.materials->find(renderer.material) :
nullptr) {
269 key.
blend = m->blend;
273 key.
cull =
static_cast<u8>(m->cull);
278 constexpr glm::vec2 CENTERED_PIVOT{0.5f, 0.5f};
283 glm::vec4 color = renderer.color;
284 color.a *= node->alpha_in_tree_;
288 f32 sweep = std::clamp(renderer.fillAmount, 0.0f, 1.0f)
291 position, finalSize, angle,
293 uvOffset, uvScale, color, key);
294 }
else if (useNineSlice) {
296 position, finalSize, CENTERED_PIVOT,
297 angle, texSize, sliceBorder,
298 uvOffset, uvScale, color, key);
301 position, finalSize, CENTERED_PIVOT,
302 angle, uvOffset, uvScale, color, key);
305 const glm::vec3 basisX = glm::vec3(transform.cachedMatrix_[0]) / scale.x;
306 const glm::vec3 basisY = glm::vec3(transform.cachedMatrix_[1]) / scale.y;
310 const auto delta = vertices[i].position - basisOrigin;
311 vertices[i].position = basisOrigin + basisX * delta.x + basisY * delta.y;