27 auto& registry = collect_ctx.
registry;
37 for (
auto entity : spriteView) {
38 if (!collect_ctx.
accepts(entity))
continue;
39 const auto& sprite = spriteView.get<
ecs::Sprite>(entity);
40 if (!sprite.enabled)
continue;
45 const auto& rotation = transform.worldRotation;
46 const auto& scale = transform.worldScale;
48 const bool parented = registry.has<
ecs::Parent>(entity);
50 || sprite.size.x == 0.0f || sprite.size.y == 0.0f)
continue;
52 glm::vec2 finalSize = sprite.size * glm::vec2(scale);
56 const glm::vec2 turn =
flatTurnZ(rotation);
57 const glm::vec3 axisX = parented ? glm::vec3(transform.cachedMatrix_[0])
58 : glm::vec3(turn.x, turn.y, 0.0f) * scale.x;
59 const glm::vec3 axisY = parented ? glm::vec3(transform.cachedMatrix_[1])
60 : glm::vec3(-turn.y, turn.x, 0.0f) * scale.y;
61 const glm::vec2 pivotOffset = (glm::vec2(.5f) - sprite.pivot) * sprite.size;
62 const glm::vec3 aabbCenter = position + axisX * pivotOffset.x + axisY * pivotOffset.y;
63 const glm::vec3 halfExtents = glm::abs(axisX) * std::abs(sprite.size.x) * .5f
64 + glm::abs(axisY) * std::abs(sprite.size.y) * .5f;
68 if (!collect_ctx.
visible(aabbCenter, halfExtents,
false)) {
73 f32 angle = 2.0f * std::atan2(rotation.z, rotation.w);
75 u32 textureId =
ctx.white_texture_id;
76 glm::vec2 texSize{0.0f};
77 bool useNineSlice =
false;
80 if (sprite.texture.isValid()) {
81 Texture* tex =
ctx.resources.getTexture(sprite.texture);
83 textureId = tex->
getId();
88 const auto* metadata =
ctx.resources.getTextureMetadata(sprite.texture);
89 if (metadata && metadata->sliceBorder.hasSlicing()) {
91 sliceBorder = metadata->sliceBorder;
96 glm::vec2 uvOff = sprite.uvOffset;
97 glm::vec2 uvSc = sprite.uvScale;
109 sprite.tileSize.x > 0.0f && sprite.tileSize.y > 0.0f,
111 const bool hasTiling = draw.
tiled;
117 const bool mapBasis = parented || hasTiling;
118 if (mapBasis) angle = 0.0f;
122 .stage =
ctx.current_stage,
123 .layer = collect_ctx.
treeLayer(entity, sprite.layer),
124 .order = sprite.order,
127 .textureId = textureId,
128 .depth = collect_ctx.
sortDepth(position),
137 if (sprite.material != 0) {
140 key.
blend = m->blend;
144 key.
cull =
static_cast<u8>(m->cull);
147 }
else if (sprite.lit &&
ctx.frame) {
150 if (litProgram != 0) key.
shaderId = litProgram;
158 mask && mask->
enabled && mask->alphaCutoff > 0.0f) {
163 glm::vec4 color = sprite.color;
164 color.a *= groupOpacity.
of(entity);
165 if (color.a <= 0.0f)
continue;
168 emitTiledQuads(buffers, draw_list, clips,
169 position, finalSize, sprite.pivot,
170 angle, color, uvOff, uvSc,
171 sprite.tileSize * glm::abs(glm::vec2(scale)),
172 sprite.tileSpacing * glm::abs(glm::vec2(scale)),
174 }
else if (useNineSlice) {
176 position, finalSize, sprite.pivot,
179 uvOff, uvSc, color, key);
182 position, finalSize, sprite.pivot,
183 angle, uvOff, uvSc, color, key);
186 const glm::vec2 divisor = hasTiling ? glm::abs(finalSize) / sprite.size
188 const glm::vec3 basisX = axisX / divisor.x;
189 const glm::vec3 basisY = axisY / divisor.y;
194 const glm::vec3 delta = vertices[i].position - position;
195 vertices[i].position = position + basisX * delta.x + basisY * delta.y;
200 if (sprite.lit || sprite.material != 0) {
208 const u32 lightCount = lights.
count();
211 const u32 step = (end - first) % 4 == 0 ? 4 : std::max(end - first, 1u);
212 for (
u32 q = first; q < end; q += step) {
213 glm::vec2 lo{vertices[q].position};
215 for (
u32 i = q + 1; i < q + step; ++i) {
216 lo = glm::min(lo, glm::vec2{vertices[i].position});
217 hi = glm::max(hi, glm::vec2{vertices[i].position});
221 -
static_cast<u32>(std::popcount(reach));
223 for (
u32 i = q; i < q + step; ++i) vertices[i].sdfBias = lane;
229void SpritePlugin::emitTiledQuads(
231 const glm::vec3& position,
const glm::vec2& size,
const glm::vec2& pivot,
232 f32 angle,
const glm::vec4& color,
233 const glm::vec2& uvOffset,
const glm::vec2& uvScale,
234 const glm::vec2& tileSize,
const glm::vec2& tileSpacing,
237 glm::vec2 step = tileSize + tileSpacing;
238 if (step.x <= 0.0f || step.y <= 0.0f
239 || !std::isfinite(step.x) || !std::isfinite(step.y)
240 || !std::isfinite(tileSize.x) || !std::isfinite(tileSize.y))
return;
242 glm::vec2 absSize = glm::vec2(std::abs(size.x), std::abs(size.y));
243 if (!std::isfinite(absSize.x) || !std::isfinite(absSize.y))
return;
245 f32 baseX = position.x - absSize.x * pivot.x;
246 f32 baseY = position.y - absSize.y * pivot.y;
248 bool hasRotation = std::abs(angle) > 0.001f;
249 f32 cosA = 1.0f, sinA = 0.0f;
251 cosA = std::cos(angle);
252 sinA = std::sin(angle);
257 const f64 countY = std::ceil(
static_cast<f64>(absSize.y) / step.y);
258 const f64 countX = std::ceil(
static_cast<f64>(absSize.x) / step.x);
262 constexpr f64 kMaxTiledQuads = 16384;
263 if (countX > kMaxTiledQuads || countY > kMaxTiledQuads
264 || countX * countY > kMaxTiledQuads) {
265 BatchVertex verts[4];
267 verts[0] = {
rotatePoint(position, baseX, baseY, cosA, sinA), pc, { uvOffset.x, uvOffset.y } };
268 verts[1] = {
rotatePoint(position, baseX + absSize.x, baseY, cosA, sinA), pc, { uvOffset.x + uvScale.x, uvOffset.y } };
269 verts[2] = {
rotatePoint(position, baseX + absSize.x, baseY + absSize.y, cosA, sinA), pc, { uvOffset.x + uvScale.x, uvOffset.y + uvScale.y } };
270 verts[3] = {
rotatePoint(position, baseX, baseY + absSize.y, cosA, sinA), pc, { uvOffset.x, uvOffset.y + uvScale.y } };
272 verts[0] = { { baseX, baseY, position.z }, pc, { uvOffset.x, uvOffset.y } };
273 verts[1] = { { baseX + absSize.x, baseY, position.z }, pc, { uvOffset.x + uvScale.x, uvOffset.y } };
274 verts[2] = { { baseX + absSize.x, baseY + absSize.y, position.z }, pc, { uvOffset.x + uvScale.x, uvOffset.y + uvScale.y } };
275 verts[3] = { { baseX, baseY + absSize.y, position.z }, pc, { uvOffset.x, uvOffset.y + uvScale.y } };
277 appendQuad(buffers, draw_list, clips, verts, key);
281 const i32 tilesX =
static_cast<i32>(countX);
282 const i32 tilesY =
static_cast<i32>(countY);
284 tile_scratch_.clear();
285 for (
i32 iy = 0; iy < tilesY; ++iy) {
287 f32 th = glm::min(tileSize.y, absSize.y - ty);
288 if (th <= 0.0f)
break;
289 f32 vFrac = th / tileSize.y;
291 for (
i32 ix = 0; ix < tilesX; ++ix) {
293 f32 tw = glm::min(tileSize.x, absSize.x - tx);
294 if (tw <= 0.0f)
break;
295 f32 uFrac = tw / tileSize.x;
302 glm::vec2 tileUvScale = { uvScale.x * uFrac, uvScale.y * vFrac };
305 tile_scratch_.push_back({
rotatePoint(position, x0, y0, cosA, sinA), pc, { uvOffset.x, uvOffset.y } });
306 tile_scratch_.push_back({
rotatePoint(position, x1, y0, cosA, sinA), pc, { uvOffset.x + tileUvScale.x, uvOffset.y } });
307 tile_scratch_.push_back({
rotatePoint(position, x1, y1, cosA, sinA), pc, { uvOffset.x + tileUvScale.x, uvOffset.y + tileUvScale.y } });
308 tile_scratch_.push_back({
rotatePoint(position, x0, y1, cosA, sinA), pc, { uvOffset.x, uvOffset.y + tileUvScale.y } });
310 tile_scratch_.push_back({ { x0, y0, position.z }, pc, { uvOffset.x, uvOffset.y } });
311 tile_scratch_.push_back({ { x1, y0, position.z }, pc, { uvOffset.x + tileUvScale.x, uvOffset.y } });
312 tile_scratch_.push_back({ { x1, y1, position.z }, pc, { uvOffset.x + tileUvScale.x, uvOffset.y + tileUvScale.y } });
313 tile_scratch_.push_back({ { x0, y1, position.z }, pc, { uvOffset.x, uvOffset.y + tileUvScale.y } });
317 appendQuads(buffers, draw_list, clips, tile_scratch_.data(),
static_cast<u32>(tile_scratch_.size() / 4), key);
Hierarchy components for parent-child relationships.
Per-frame 2D lighting constants — the third tier of the engine's constant-buffer layering (after per-...
Engine-side 2D light registry — the per-frame LightConstants UBO and its CPU mirror.
Engine-side material registry — the resolved "how to draw" for a material handle.
Rendering context containing all renderer state.
Frame resources and the shared scene rendering contract.
Which geometry a Sprite draws — the rule, apart from the frame.
Turns this entity's Sprite into a stencil the sprites drawn after it can be cut by.
2D sprite rendering component
GPU texture abstraction for 2D images.
void emitQuad(TransientBufferPool &buffers, DrawList &draw_list, const ClipState &clips, const glm::vec3 &position, const glm::vec2 &size, const glm::vec2 &pivotNorm, f32 angle, const glm::vec2 &uvOffset, const glm::vec2 &uvScale, const glm::vec4 &color, const BatchDrawKey &key)
Emits a single rotated/unrotated textured quad.
void emitNineSlice(TransientBufferPool &buffers, DrawList &draw_list, const ClipState &clips, const glm::vec3 &position, const glm::vec2 &size, const glm::vec2 &pivotNorm, f32 angle, const glm::vec2 &texSize, const glm::vec4 &border, const glm::vec2 &uvOffset, const glm::vec2 &uvScale, const glm::vec4 &color, const BatchDrawKey &key)
Emits a 3x3 nine-slice grid of quads sharing one BatchDrawKey.
Holds the per-frame 2D lights and their GPU UBO. Mirrors MaterialStore's UBO lifecycle (lazy create,...
const LightConstants & data() const
void collect(RenderCollectContext &ctx) override
2D texture for GPU rendering
u32 getWidth() const
Gets the texture width in pixels.
u32 getHeight() const
Gets the texture height in pixels.
u32 getId() const
Gets the texture handle's raw value, for command payloads and the WASM boundary.
The opacity a draw inherits from the GroupOpacity above it.
f32 of(Entity entity) const
1 when no enabled group encloses entity (itself included).
bool hasFlatGeometryArea(Transform &transform, bool parented)
void appendQuad(TransientBufferPool &pool, DrawList &drawList, const ClipState &clips, const BatchVertex quad[4], const BatchDrawKey &key)
void appendQuads(TransientBufferPool &pool, DrawList &drawList, const ClipState &clips, const BatchVertex *quads, u32 quadCount, const BatchDrawKey &key)
constexpr u32 SPRITE_LIGHT_REACH_BITS
std::int32_t i32
Signed 32-bit integer.
constexpr SpriteDrawChoice resolveSpriteDraw(ecs::SpriteDrawMode mode, bool hasTileSize, bool hasSliceBorder)
Resolve the geometry from the author's mode and what the data offers.
glm::vec3 parallaxedWorldPosition(ecs::Transform &transform, const glm::vec2 ¶llax, const CameraView &camera)
u32 packColor(f32 r, f32 g, f32 b, f32 a)
std::uint32_t u32
Unsigned 32-bit integer.
std::uint8_t u8
Unsigned 8-bit integer.
u32 lightReachMask2D(const GpuLight *lights, u32 count, const glm::vec2 &lo, const glm::vec2 &hi)
Bit i set when loop light i can put any light on the box lo .. hi (the plane's XY,...
double f64
64-bit floating point
glm::vec2 flatTurnZ(const glm::quat &rotation)
The cos/sin of the turn a plane renderable draws with: its entity's rotation about Z,...
glm::vec3 rotatePoint(const glm::vec3 ¢er, f32 px, f32 py, f32 cosA, f32 sinA)
Rotates a point around center in the XY plane, carrying the center's z through untouched — the rotati...
f32 packSpriteLightLane(i32 layer, u32 reachMask)
The lane a lit quad carries: its layer and the lights that reach it (lightReachMask2D).
static EstellaContext & ctx()
float f32
32-bit floating point
Non-geometry attributes of a batch draw — everything needed to build the sort key + DrawCommand.
i16 depthBias
Depth-buffer units toward the eye.
u8 cull
CullMode: 0 = none, 1 = back, 2 = front.
One vertex of the shared batch stream.
One 2D light, std140-packed (six vec4s, 96 bytes, 16-aligned).
GpuLight lights[MAX_LIGHTS]
A material's resolved render state (P0) plus its packed std140 constants (P1).
const ClipState & clip_state
u32 culled
How many renderables this collect's frustum rejected.
f32 sortDepth(const glm::vec3 &worldPos) const
The sort depth of a draw anchored at worldPos.
RenderFrameContext & frame_context
bool accepts(Entity entity) const
Whether entity belongs to THIS collect.
i32 treeLayer(Entity entity, i32 authored) const
The layer entity draws at: its place in a Canvas's tree, unless its author named one.
bool visible(const glm::vec3 ¢re, const glm::vec3 &halfExtents, bool depthTested)
Whether a renderable bounded by centre / halfExtents is drawn.
TransientBufferPool & buffer_pool
u32 lightsSpared
Light slots the lit quads of this collect were spared: per quad, the loop lights that cannot reach it...
Parent reference component.
A stencil cut from this entity's own Sprite.
bool enabled
Off is not a mask at all: the sprite goes back to being drawn normally, which is what makes "is this ...
UINode — the CSS box-model layout input (the CSS/Flex primary layout model).
Nine-slice border definition for UI sprites.
f32 left
Left border width in pixels.
f32 top
Top border height in pixels.
f32 bottom
Bottom border height in pixels.
f32 right
Right border width in pixels.