Estella 0.113.0
A fast 2D and 3D game engine powered by WebAssembly and ECS
Loading...
Searching...
No Matches
BatchPlugin.cpp
Go to the documentation of this file.
1// SPDX-License-Identifier: Apache-2.0
2// SPDX-FileCopyrightText: Copyright (c) 2024-present ESEngine Team
13#include "BatchPlugin.hpp"
14
15#include <algorithm>
16#include <cmath>
17
18namespace esengine {
19
21 TransientBufferPool& buffers, DrawList& draw_list, const ClipState& clips,
22 const glm::vec3& position, const glm::vec2& size, const glm::vec2& pivotNorm,
23 f32 angle, const glm::vec2& uvOffset, const glm::vec2& uvScale,
24 const glm::vec4& color, const BatchDrawKey& key
25) {
26 f32 ox = 0.5f - pivotNorm.x;
27 f32 oy = 0.5f - pivotNorm.y;
28 const u32 packedColor = packColor(color); // constant across the 4 verts
29
30 BatchVertex verts[4];
31 if (std::abs(angle) > 0.001f) {
32 f32 cosA = std::cos(angle);
33 f32 sinA = std::sin(angle);
34 for (u32 i = 0; i < 4; ++i) {
35 f32 lx = (BATCH_QUAD_POSITIONS[i].x + ox) * size.x;
36 f32 ly = (BATCH_QUAD_POSITIONS[i].y + oy) * size.y;
37 verts[i].position = glm::vec3(
38 position.x + lx * cosA - ly * sinA,
39 position.y + lx * sinA + ly * cosA,
40 position.z
41 );
42 verts[i].color = packedColor;
43 verts[i].texCoord = BATCH_QUAD_TEX_COORDS[i] * uvScale + uvOffset;
44 }
45 } else {
46 for (u32 i = 0; i < 4; ++i) {
47 verts[i].position = glm::vec3(
48 position.x + (BATCH_QUAD_POSITIONS[i].x + ox) * size.x,
49 position.y + (BATCH_QUAD_POSITIONS[i].y + oy) * size.y,
50 position.z
51 );
52 verts[i].color = packedColor;
53 verts[i].texCoord = BATCH_QUAD_TEX_COORDS[i] * uvScale + uvOffset;
54 }
55 }
56
57 appendQuad(buffers, draw_list, clips, verts, key);
58}
59
61 TransientBufferPool& buffers, DrawList& draw_list, const ClipState& clips,
62 const glm::vec3& position, const glm::vec2& size, const glm::vec2& pivotNorm,
63 f32 angle, const glm::vec2& texSize, const glm::vec4& border,
64 const glm::vec2& uvOffset, const glm::vec2& uvScale,
65 const glm::vec4& color, const BatchDrawKey& key
66) {
67 f32 L = border.x;
68 f32 R = border.y;
69 f32 T = border.z;
70 f32 B = border.w;
71 if (size.x == 0.0f || size.y == 0.0f) return;
72 if (uvScale.x < 0.0f) std::swap(L, R);
73 if (uvScale.y < 0.0f) std::swap(T, B);
74
75 // Fit destination borders when the box is smaller than their sum, by ONE
76 // factor for both axes — the rule CSS applies to border radii. Fitting each
77 // axis on its own squeezes a short panel's corners into ellipses: a 20px
78 // round corner on a 30px-tall box became 20x15. Source UV borders still
79 // sample the original texture region.
80 const f32 directionX = std::copysign(1.0f, size.x);
81 const f32 directionY = std::copysign(1.0f, size.y);
82 const f32 fitX = L + R > 0.0f ? std::abs(size.x) / (L + R) : 1.0f;
83 const f32 fitY = T + B > 0.0f ? std::abs(size.y) / (T + B) : 1.0f;
84 const f32 fit = std::min(1.0f, std::min(fitX, fitY));
85 const f32 sx = directionX * fit;
86 const f32 sy = directionY * fit;
87
88 f32 baseX = position.x - size.x * pivotNorm.x;
89 f32 baseY = position.y - size.y * pivotNorm.y;
90
91 f32 x[4] = { baseX, baseX + L * sx, baseX + size.x - R * sx, baseX + size.x };
92 f32 y[4] = { baseY, baseY + B * sy, baseY + size.y - T * sy, baseY + size.y };
93
94 f32 u[4], v[4];
95 if (texSize.x > 0.0f && texSize.y > 0.0f) {
96 const f32 uvDirectionX = std::copysign(1.0f, uvScale.x);
97 const f32 uvDirectionY = std::copysign(1.0f, uvScale.y);
98 u[0] = uvOffset.x;
99 u[1] = uvOffset.x + uvDirectionX * L / texSize.x;
100 u[2] = uvOffset.x + uvScale.x - uvDirectionX * R / texSize.x;
101 u[3] = uvOffset.x + uvScale.x;
102 v[0] = uvOffset.y;
103 v[1] = uvOffset.y + uvDirectionY * B / texSize.y;
104 v[2] = uvOffset.y + uvScale.y - uvDirectionY * T / texSize.y;
105 v[3] = uvOffset.y + uvScale.y;
106 } else {
107 u[0] = uvOffset.x;
108 u[1] = uvOffset.x;
109 u[2] = uvOffset.x + uvScale.x;
110 u[3] = uvOffset.x + uvScale.x;
111 v[0] = uvOffset.y;
112 v[1] = uvOffset.y;
113 v[2] = uvOffset.y + uvScale.y;
114 v[3] = uvOffset.y + uvScale.y;
115 }
116
117 f32 cosA = std::cos(angle);
118 f32 sinA = std::sin(angle);
119 const u32 pc = packColor(color);
120
121 BatchVertex verts[36];
122 u32 quads = 0;
123 for (i32 row = 0; row < 3; ++row) {
124 for (i32 col = 0; col < 3; ++col) {
125 f32 pw = x[col + 1] - x[col];
126 f32 ph = y[row + 1] - y[row];
127 if (pw * directionX <= 0.0f || ph * directionY <= 0.0f) continue;
128
129 BatchVertex* cell = verts + quads * 4;
130 cell[0] = { rotatePoint(position, x[col], y[row], cosA, sinA), pc, {u[col], v[row]} };
131 cell[1] = { rotatePoint(position, x[col + 1], y[row], cosA, sinA), pc, {u[col + 1], v[row]} };
132 cell[2] = { rotatePoint(position, x[col + 1], y[row + 1], cosA, sinA), pc, {u[col + 1], v[row + 1]} };
133 cell[3] = { rotatePoint(position, x[col], y[row + 1], cosA, sinA), pc, {u[col], v[row + 1]} };
134 ++quads;
135 }
136 }
137 appendQuads(buffers, draw_list, clips, verts, quads, key);
138}
139
141 TransientBufferPool& buffers, DrawList& draw_list, const ClipState& clips,
142 const glm::vec3& center, const glm::vec2& size, f32 angle,
143 f32 startAngle, f32 sweep,
144 const glm::vec2& uvOffset, const glm::vec2& uvScale,
145 const glm::vec4& color, const BatchDrawKey& key
146) {
147 constexpr f32 TWO_PI = 6.2831853071795864f;
148 sweep = std::clamp(sweep, 0.0f, TWO_PI);
149 const glm::vec2 absSize = glm::abs(size);
150 const glm::vec2 direction{size.x < 0 ? -1.0f : 1.0f, size.y < 0 ? -1.0f : 1.0f};
151 f32 hx = absSize.x * 0.5f, hy = absSize.y * 0.5f;
152 if (sweep <= 0.0001f || hx <= 0.0f || hy <= 0.0f) return;
153
154 // Sample angles are the two endpoints plus any box corner the sweep crosses,
155 // measured as a clockwise distance from startAngle. Because the box edge
156 // between two corners is straight, a fan vertex at each corner reproduces the
157 // rectangle boundary exactly — no faceting.
158 f32 deltas[6];
159 u32 n = 0;
160 deltas[n++] = 0.0f;
161 const f32 cornerAngle[4] = {
162 std::atan2(hy, hx), std::atan2(hy, -hx),
163 std::atan2(-hy, -hx), std::atan2(-hy, hx),
164 };
165 for (u32 k = 0; k < 4; ++k) {
166 f32 d = startAngle - cornerAngle[k];
167 d -= TWO_PI * std::floor(d / TWO_PI); // wrap into [0, TWO_PI)
168 if (d > 0.0001f && d < sweep - 0.0001f) deltas[n++] = d;
169 }
170 deltas[n++] = sweep;
171 for (u32 i = 1; i < n; ++i) { // insertion sort (n <= 6)
172 f32 v = deltas[i];
173 i32 j = static_cast<i32>(i) - 1;
174 while (j >= 0 && deltas[j] > v) { deltas[j + 1] = deltas[j]; --j; }
175 deltas[j + 1] = v;
176 }
177
178 const f32 cosR = std::cos(angle), sinR = std::sin(angle);
179 const u32 pc = packColor(color);
180
181 BatchVertex verts[7];
182 verts[0] = { center, pc, uvOffset + glm::vec2(0.5f, 0.5f) * uvScale }; // fan hub = box center
183 for (u32 i = 0; i < n; ++i) {
184 f32 a = startAngle - deltas[i];
185 f32 ca = std::cos(a), sa = std::sin(a);
186 f32 aca = std::abs(ca), asa = std::abs(sa);
187 // Ray from center at angle a exits the centered box at the nearer axis wall.
188 f32 t = std::min(aca > 1e-6f ? hx / aca : 1e30f,
189 asa > 1e-6f ? hy / asa : 1e30f);
190 glm::vec2 p{ t * ca, t * sa }; // box-local, centered
191 verts[i + 1] = {
192 rotatePoint(center, center.x + p.x * direction.x, center.y + p.y * direction.y, cosR, sinR),
193 pc,
194 uvOffset + glm::vec2(p.x / absSize.x + 0.5f, p.y / absSize.y + 0.5f) * uvScale,
195 };
196 }
197
198 u32 indices[15];
199 u32 ic = 0;
200 for (u32 i = 1; i + 1 <= n; ++i) { // fan triangles (hub, i, i+1)
201 indices[ic++] = 0;
202 indices[ic++] = i;
203 indices[ic++] = i + 1;
204 }
205 if (ic == 0) return;
206 appendIndexedBatch(buffers, draw_list, clips, verts, n + 1, indices, ic, key);
207}
208
209} // namespace esengine
Base class for render-type plugins that emit textured quads into the Batch stream.
void emitRadialFill(TransientBufferPool &buffers, DrawList &draw_list, const ClipState &clips, const glm::vec3 &center, const glm::vec2 &size, f32 angle, f32 startAngle, f32 sweep, const glm::vec2 &uvOffset, const glm::vec2 &uvScale, const glm::vec4 &color, const BatchDrawKey &key)
Emits a centered angular wedge (Radial Filled): a triangle fan from the box center out to its perimet...
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.
void appendQuad(TransientBufferPool &pool, DrawList &drawList, const ClipState &clips, const BatchVertex quad[4], const BatchDrawKey &key)
constexpr glm::vec2 BATCH_QUAD_TEX_COORDS[4]
Unit-quad texture coordinates matching BATCH_QUAD_POSITIONS.
void appendQuads(TransientBufferPool &pool, DrawList &drawList, const ClipState &clips, const BatchVertex *quads, u32 quadCount, const BatchDrawKey &key)
std::int32_t i32
Signed 32-bit integer.
Definition Types.hpp:54
constexpr glm::vec4 BATCH_QUAD_POSITIONS[4]
Canonical centered unit-quad corner positions (CCW).
u32 packColor(f32 r, f32 g, f32 b, f32 a)
std::uint32_t u32
Unsigned 32-bit integer.
Definition Types.hpp:45
void appendIndexedBatch(TransientBufferPool &pool, DrawList &drawList, const ClipState &clips, const BatchVertex *verts, u32 vertexCount, const u32 *localIndices, u32 indexCount, const BatchDrawKey &key)
Copies verts into the Batch stream, then pushes one DrawCommand spanning them.
glm::vec3 rotatePoint(const glm::vec3 &center, 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...
float f32
32-bit floating point
Definition Types.hpp:59
Non-geometry attributes of a batch draw — everything needed to build the sort key + DrawCommand.
One vertex of the shared batch stream.