Estella 0.113.0
A fast 2D and 3D game engine powered by WebAssembly and ECS
Loading...
Searching...
No Matches
MeshGeometry.hpp
Go to the documentation of this file.
1// SPDX-License-Identifier: Apache-2.0
2// SPDX-FileCopyrightText: Copyright (c) 2024-present ESEngine Team
7#pragma once
8
14#include <algorithm>
15
16namespace esengine {
17
19struct LodLevels {
22 glm::vec3 localMin{0.0f};
23 glm::vec3 localMax{0.0f};
24 bool gap = false;
25 bool disordered = false;
26 bool skinMismatch = false;
27};
28
30inline bool gatherLodLevels(resource::ResourceManager& resources, const ecs::LODGroup& group,
31 const Mesh& base, LodLevels& out) {
32 out = LodLevels{};
33 out.mesh[0] = &base;
34 out.localMin = base.localMin;
35 out.localMax = base.localMax;
36 out.set.cull = group.cullSize;
37 out.set.hysteresis = group.hysteresis;
38
39 const resource::MeshHandle handles[lod::kMaxStandIns] = {group.lod1, group.lod2, group.lod3};
40 const f32 sizes[lod::kMaxStandIns] = {group.lod1Size, group.lod2Size, group.lod3Size};
41 for (u8 i = 0; i < lod::kMaxStandIns; ++i) {
42 const Mesh* standIn = handles[i].isValid() ? resources.getMesh(handles[i]) : nullptr;
43 if (!standIn || !standIn->isDrawable()) {
44 // A missing level ends the reachable prefix, even if later slots are populated.
45 for (u8 j = static_cast<u8>(i + 1); j < lod::kMaxStandIns; ++j) {
46 if (handles[j].isValid()) out.gap = true;
47 }
48 break;
49 }
50 if (base.isSkinned() && standIn->inverseBind.size() != base.inverseBind.size()) {
51 out.skinMismatch = true;
52 break;
53 }
54 if (i > 0 && sizes[i] >= sizes[i - 1]) out.disordered = true;
55 out.mesh[i + 1] = standIn;
56 out.set.takeOver[i] = sizes[i];
57 out.set.count = static_cast<u8>(i + 1);
58 out.localMin = glm::min(out.localMin, standIn->localMin);
59 out.localMax = glm::max(out.localMax, standIn->localMax);
60 }
61 // A cull size alone cannot validate a group whose first stand-in has an incompatible skeleton.
62 return out.set.count > 0 || (out.set.cull > 0.0f && !out.skinMismatch);
63}
64
66inline const Mesh* placementCoarseMesh(resource::ResourceManager& resources, const Mesh& base,
67 const ecs::MeshInstances& placements) {
68 const Mesh* coarse = placements.lod1Mesh.isValid() ? resources.getMesh(placements.lod1Mesh) : nullptr;
69 return coarse && coarse->isDrawable() && coarse->hasNormals == base.hasNormals
70 && coarse->hasTangents == base.hasTangents && coarse->hasLightmapUV == base.hasLightmapUV ? coarse : nullptr;
71}
72
74inline void meshLocalBounds(const Mesh& base, const Mesh* coarse, f32 displacement,
75 glm::vec3& lo, glm::vec3& hi) {
76 const glm::vec3 padding(std::max(displacement, 0.0f));
77 lo = (coarse ? glm::min(base.localMin, coarse->localMin) : base.localMin) - padding;
78 hi = (coarse ? glm::max(base.localMax, coarse->localMax) : base.localMax) + padding;
79}
80
81} // namespace esengine
The cheaper geometry that may stand in for a MeshRenderer once it is small.
Which representation an object's size on screen asks for.
Arbitrary-geometry 2D mesh renderable (deformed sprites, polygons, water surfaces,...
Geometry that lives on the GPU rather than in a frame.
Central resource management system.
Buffers, the layout that describes them, and the bounds culling reads.
Definition Mesh.hpp:85
glm::vec3 localMin
Definition Mesh.hpp:143
bool isSkinned() const
Definition Mesh.hpp:150
std::vector< glm::mat4 > inverseBind
Definition Mesh.hpp:148
bool hasTangents
Definition Mesh.hpp:136
bool hasLightmapUV
Definition Mesh.hpp:132
glm::vec3 localMax
Definition Mesh.hpp:144
bool isDrawable() const
Definition Mesh.hpp:199
bool hasNormals
Definition Mesh.hpp:127
bool isValid() const
Checks if the handle references a valid resource.
Definition Handle.hpp:82
Central manager for GPU resources.
Mesh * getMesh(MeshHandle handle)
The mesh a handle names, or null.
constexpr u8 kMaxStandIns
How many stand-ins a group may declare beyond the renderer's own mesh.
Handle< esengine::Mesh > MeshHandle
Handle to GPU-resident geometry.
Definition Handle.hpp:226
void meshLocalBounds(const Mesh &base, const Mesh *coarse, f32 displacement, glm::vec3 &lo, glm::vec3 &hi)
std::uint8_t u8
Unsigned 8-bit integer.
Definition Types.hpp:41
bool gatherLodLevels(resource::ResourceManager &resources, const ecs::LODGroup &group, const Mesh &base, LodLevels &out)
const Mesh * placementCoarseMesh(resource::ResourceManager &resources, const Mesh &base, const ecs::MeshInstances &placements)
float f32
32-bit floating point
Definition Types.hpp:59
const Mesh * mesh[lod::kMaxStandIns+1]
Stand-in geometry chosen by how much of the screen the object covers.
Definition LODGroup.hpp:30
f32 lod1Size
Fraction of the viewport height below which lod1 replaces the MeshRenderer's own mesh....
Definition LODGroup.hpp:46
resource::MeshHandle lod1
Geometry drawn once the object is smaller than lod1Size.
Definition LODGroup.hpp:33
resource::MeshHandle lod3
Geometry drawn once the object is smaller than lod3Size.
Definition LODGroup.hpp:41
f32 cullSize
Fraction of the viewport height below which nothing is drawn at all. 0 disables it,...
Definition LODGroup.hpp:57
resource::MeshHandle lod2
Geometry drawn once the object is smaller than lod2Size.
Definition LODGroup.hpp:37
f32 hysteresis
How much further than a threshold the object must grow before the finer level comes back — 0....
Definition LODGroup.hpp:63
Many placements of the one mesh a MeshRenderer draws, as ONE renderable.
resource::MeshHandle lod1Mesh
The cheaper mesh that stands in past lodDistance; invalid = the mesh itself at every distance....
The screen sizes a group hands over at, in the order it hands them over.
f32 takeOver[kMaxStandIns]