Estella 0.113.0
A fast 2D and 3D game engine powered by WebAssembly and ECS
Loading...
Searching...
No Matches
LodSelection.hpp
Go to the documentation of this file.
1// SPDX-License-Identifier: Apache-2.0
2// SPDX-FileCopyrightText: Copyright (c) 2024-present ESEngine Team
11#pragma once
12
13#include "../../core/Types.hpp"
14#include "../../math/Math.hpp"
15
16#include <glm/gtc/quaternion.hpp>
17
18#include <cmath>
19
20namespace esengine::lod {
21
23inline constexpr u8 kMaxStandIns = 3;
24
26inline constexpr u8 kCulled = 0xFFu;
27
34inline constexpr f32 kSizeAtEye = 1e30f;
35
43struct LevelSet {
45 f32 cull = 0.0f;
47 u8 count = 0;
48};
49
57inline void boundingSphere(const glm::vec3& position, const glm::quat& rotation,
58 const glm::vec3& scale, const glm::vec3& localMin,
59 const glm::vec3& localMax, glm::vec3& outCentre, f32& outRadius) {
60 const glm::vec3 scaledHalf = glm::abs((localMax - localMin) * 0.5f * scale);
61 outCentre = position + glm::mat3_cast(rotation) * ((localMin + localMax) * 0.5f * scale);
62 outRadius = glm::length(scaledHalf);
63}
64
73inline f32 screenRelativeSize(const glm::mat4& viewProjection, const glm::vec3& centre,
74 f32 radius) {
75 const glm::vec4 clip = viewProjection * glm::vec4(centre, 1.0f);
76 if (clip.w <= 1e-6f) return kSizeAtEye;
77 const glm::vec3 row1(viewProjection[0][1], viewProjection[1][1], viewProjection[2][1]);
78 const glm::vec3 row3(viewProjection[0][3], viewProjection[1][3], viewProjection[2][3]);
79 // The term in ndcY is the stretch toward the edges of a perspective frame; it
80 // falls out on its own under a projection that has none.
81 const glm::vec3 gradient = (row1 - (clip.y / clip.w) * row3) / clip.w;
82 return radius * glm::length(gradient);
83}
84
93inline u8 selectLevel(const LevelSet& set, f32 size, u8 current) {
94 u8 level = 0;
95 for (u8 i = 0; i < set.count && i < kMaxStandIns; ++i) {
96 const u8 candidate = static_cast<u8>(i + 1);
97 const bool coarseSide = current >= candidate;
98 if (size >= set.takeOver[i] * (coarseSide ? 1.0f + set.hysteresis : 1.0f)) break;
99 level = candidate;
100 }
101 if (set.cull > 0.0f) {
102 const bool wasCulled = current == kCulled;
103 if (size < set.cull * (wasCulled ? 1.0f + set.hysteresis : 1.0f)) return kCulled;
104 }
105 return level;
106}
107
119
120 void record(u8 level_) {
121 ++selections;
122 if (level_ == kCulled) ++culled;
123 else if (level_ <= kMaxStandIns) ++level[level_];
124 }
125};
126
127} // namespace esengine::lod
Math utilities and GLM configuration for ESEngine.
Core type definitions and utilities for ESEngine.
constexpr f32 kSizeAtEye
What a point at or behind the eye reports.
u8 selectLevel(const LevelSet &set, f32 size, u8 current)
The level size asks for, given the one shown last.
f32 screenRelativeSize(const glm::mat4 &viewProjection, const glm::vec3 &centre, f32 radius)
How much of the viewport's HEIGHT a sphere covers: 1 = one screen high.
constexpr u8 kCulled
The level of an object small enough that nothing is drawn for it.
void boundingSphere(const glm::vec3 &position, const glm::quat &rotation, const glm::vec3 &scale, const glm::vec3 &localMin, const glm::vec3 &localMax, glm::vec3 &outCentre, f32 &outRadius)
The sphere a group is measured by: its centre in the world and its radius.
constexpr u8 kMaxStandIns
How many stand-ins a group may declare beyond the renderer's own mesh.
std::uint32_t u32
Unsigned 32-bit integer.
Definition Types.hpp:45
std::uint8_t u8
Unsigned 8-bit integer.
Definition Types.hpp:41
float f32
32-bit floating point
Definition Types.hpp:59
The screen sizes a group hands over at, in the order it hands them over.
f32 takeOver[kMaxStandIns]
What a frame's selections came to, counted where each one is made.