Estella 0.113.0
A fast 2D and 3D game engine powered by WebAssembly and ECS
Loading...
Searching...
No Matches
ShapePlugin.cpp
Go to the documentation of this file.
1// SPDX-License-Identifier: Apache-2.0
2// SPDX-FileCopyrightText: Copyright (c) 2024-present ESEngine Team
3#include "ShapePlugin.hpp"
8#include "../rhi/Shader.hpp"
13
14#include <cmath>
15
16namespace esengine {
17
19 // One path for both backends: the WGSL twin rides shape.esshader, so the
20 // stages assemble for the preferred target. Attribute name bindings are a
21 // GL link-time concept; the WGSL twin fixes its locations in-source.
22 // A device rebuild runs init() again; releasing the previous handle is what
23 // keeps the pool from filling with shaders of dead contexts.
24 if (shape_shader_handle_.isValid()) ctx.resources.releaseShader(shape_shader_handle_);
25 const auto target = ctx.resources.preferredShaderTarget();
27 shape_shader_handle_ = ctx.resources.createShaderWithBindings(
30 {{0, "a_position"}, {1, "a_texCoord"}, {2, "a_color"}, {3, "a_shapeInfo"}},
31 ctx.resources.preferredShaderLanguage());
32
33 Shader* shader = ctx.resources.getShader(shape_shader_handle_);
34 shape_shader_id_ = shader ? shader->getProgramId() : 0;
35}
36
39
41 auto& registry = collect_ctx.registry;
42 auto& frustum = collect_ctx.frustum;
43 auto& clips = collect_ctx.clip_state;
44 auto& buffers = collect_ctx.buffer_pool;
45 auto& draw_list = collect_ctx.draw_list;
46 auto& ctx = collect_ctx.frame_context;
47 auto shapeView = registry.activeView<ecs::Transform, ecs::ShapeRenderer>();
48
49 const ecs::GroupOpacityLookup groupOpacity(registry);
50
51 for (auto entity : shapeView) {
52 const auto& shape = shapeView.get<ecs::ShapeRenderer>(entity);
53 if (!shape.enabled) continue;
54 const f32 groupAlpha = groupOpacity.of(entity);
55 if (groupAlpha <= 0.0f) continue;
56
57 auto& transform = shapeView.get<ecs::Transform>(entity);
58 glm::vec3 position = parallaxedWorldPosition(transform, shape.parallax, collect_ctx.camera);
59 const auto& rotation = transform.worldRotation;
60 const auto& scale = transform.worldScale;
61
62 // Turned about `position`, so only the extents move (see flatHalfExtents).
63 glm::vec3 halfExtents = flatHalfExtents(
64 flatTurnZ(rotation),
65 glm::vec3(shape.size.x * scale.x, shape.size.y * scale.y, 0.0f) * 0.5f);
66 if (!frustum.intersectsAABB(position, halfExtents)) {
67 ++collect_ctx.culled;
68 continue;
69 }
70
71 f32 angle = 2.0f * std::atan2(rotation.z, rotation.w);
72 f32 cosA = std::cos(angle);
73 f32 sinA = std::sin(angle);
74
75 glm::vec2 scale2 = glm::vec2(scale);
76 glm::vec2 halfSize = shape.size * scale2 * 0.5f;
77 glm::vec2 pos(position);
78 // The corner radius must scale with the box, or a scaled rounded-rect keeps
79 // its absolute radius and the rounding looks proportionally tighter. Average
80 // the two axes for a uniform-scale approximation (exact when scale is uniform).
81 f32 cornerScale = 0.5f * (std::abs(scale2.x) + std::abs(scale2.y));
82
83 glm::vec2 localCorners[4] = {
84 {-halfSize.x, -halfSize.y},
85 { halfSize.x, -halfSize.y},
86 { halfSize.x, halfSize.y},
87 {-halfSize.x, halfSize.y},
88 };
89
90 glm::vec2 uvCorners[4] = {
91 {-1.0f, -1.0f},
92 { 1.0f, -1.0f},
93 { 1.0f, 1.0f},
94 {-1.0f, 1.0f},
95 };
96
97 ShapeVertex verts[4];
98 for (u32 v = 0; v < 4; ++v) {
99 f32 rx = localCorners[v].x * cosA - localCorners[v].y * sinA;
100 f32 ry = localCorners[v].x * sinA + localCorners[v].y * cosA;
101
102 verts[v].px = pos.x + rx;
103 verts[v].py = pos.y + ry;
104 verts[v].ux = uvCorners[v].x;
105 verts[v].uy = uvCorners[v].y;
106 verts[v].cr = shape.color.r;
107 verts[v].cg = shape.color.g;
108 verts[v].cb = shape.color.b;
109 verts[v].ca = shape.color.a * groupAlpha;
110 verts[v].shapeType = static_cast<f32>(shape.shapeType);
111 verts[v].halfW = halfSize.x;
112 verts[v].halfH = halfSize.y;
113 verts[v].cornerRadius = shape.cornerRadius * cornerScale;
114 }
115
116 BatchDrawKey key{
117 .stage = ctx.current_stage,
118 .layer = shape.layer,
119 .shaderId = shape_shader_id_,
120 .blend = BlendMode::Normal,
121 .depth = collect_ctx.camera.viewDepth(position),
122 .y = position.y,
123 .entity = entity,
124 .type = RenderType::Shape,
125 .layoutId = LayoutId::Shape,
126 };
127 appendIndexedDraw(buffers, draw_list, clips, verts, 4, BATCH_QUAD_INDICES, 6, key);
128 }
129}
130
131} // namespace esengine
The single submission face: the ONLY place a DrawCommand is assembled.
Rendering context containing all renderer state.
Frame resources and the shared scene rendering contract.
Parser for unified .esshader file format.
GPU shader program abstraction.
Unified Transform component with local and world-space data.
GPU shader program for rendering.
Definition Shader.hpp:86
u32 getProgramId() const
Gets the program handle's raw value, for command payloads and sort keys.
Definition Shader.hpp:257
void init(RenderFrameContext &ctx) override
void collect(RenderCollectContext &ctx) override
void shutdown() override
The opacity a draw inherits from the GroupOpacity above it.
static ParsedShader parse(const std::string &source)
Parses shader source into structured format (no include support).
static std::string assembleStage(const ParsedShader &parsed, ShaderStage stage, const std::string &platform="", const std::vector< std::string > &features={}, ShaderTargetLanguage target=ShaderTargetLanguage::GLSL_ES300)
Assembles final GLSL source for a specific stage.
void appendIndexedDraw(TransientBufferPool &pool, DrawList &drawList, const ClipState &clips, const void *verts, u32 vertexCount, const u32 *localIndices, u32 indexCount, const BatchDrawKey &key)
Copies raw vertices into the key's stream, then pushes one DrawCommand spanning them.
glm::vec3 flatHalfExtents(const glm::vec2 &turn, const glm::vec3 &half)
World AABB half-extents of a flat box turned by turn (from flatTurnZ). |R| * half,...
glm::vec3 parallaxedWorldPosition(ecs::Transform &transform, const glm::vec2 &parallax, const CameraView &camera)
std::uint32_t u32
Unsigned 32-bit integer.
Definition Types.hpp:45
glm::vec2 flatTurnZ(const glm::quat &rotation)
The cos/sin of the turn a plane renderable draws with: its entity's rotation about Z,...
static EstellaContext & ctx()
constexpr u32 BATCH_QUAD_INDICES[6]
Canonical two-triangle quad winding shared by every batch path.
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.
f32 viewDepth(const glm::vec3 &worldPos) const
How near worldPos is to the viewer, larger = nearer — the order the sort key wants,...
u32 culled
How many renderables this collect's frustum rejected.
Unified transform component.
Definition Transform.hpp:45