载入中...
搜索中...
未找到
WorldAnchorProjection.cpp
浏览该文件的文档.
2
3#include <glm/geometric.hpp>
4#include <glm/mat4x4.hpp>
5#include <glm/vec3.hpp>
6#include <glm/vec4.hpp>
7
8#include <algorithm>
9#include <cmath>
10
11namespace eve::ui {
12namespace {
13
14struct AnchorRect {
15 float left;
16 float top;
17 float right;
18 float bottom;
19};
20
21AnchorRect anchorRect(const WorldAnchorLayoutItem &item, float x, float y) {
22 const float padding = std::max(0.f, item.padding) * 0.5f;
23 const float width = std::max(1.f, item.width);
24 const float height = std::max(1.f, item.height);
25 const float left = x - item.pivotX * width;
26 const float top = y - item.pivotY * height;
27 return {left - padding, top - padding, left + width + padding,
28 top + height + padding};
29}
30
31bool intersects(const AnchorRect &a, const AnchorRect &b) {
32 return a.left < b.right && a.right > b.left && a.top < b.bottom &&
33 a.bottom > b.top;
34}
35
36bool insideViewport(const AnchorRect &rect, float margin, float width, float height) {
37 margin = std::max(0.f, margin);
38 return rect.left >= margin && rect.top >= margin && rect.right <= width - margin &&
39 rect.bottom <= height - margin;
40}
41
42} // namespace
43
45 const glm::mat4 &viewProjection, float cameraX,
46 float cameraY, float cameraZ, float viewportWidth,
47 float viewportHeight) {
49 if (!anchor.enabled) return result;
50 if (viewportWidth <= 0.f || viewportHeight <= 0.f) {
52 return result;
53 }
54 const glm::vec4 clip = viewProjection * glm::vec4(anchor.worldX, anchor.worldY,
55 anchor.worldZ, 1.f);
56 if (!std::isfinite(clip.w) || clip.w <= 0.f) {
58 result.render = !anchor.hideBehindCamera;
59 return result;
60 }
61 const glm::vec3 ndc = glm::vec3(clip) / clip.w;
62 result.depth = ndc.z;
63 result.screenX = (ndc.x * 0.5f + 0.5f) * viewportWidth + anchor.offsetX;
64 result.screenY = (ndc.y * 0.5f + 0.5f) * viewportHeight + anchor.offsetY;
65 const bool outside = ndc.x < -1.f || ndc.x > 1.f || ndc.y < -1.f || ndc.y > 1.f ||
66 ndc.z < 0.f || ndc.z > 1.f;
67 if (outside && anchor.edgePolicy == WorldAnchorEdgePolicy::Hide) {
69 return result;
70 }
71 if (outside) {
72 const float margin = std::max(0.f, anchor.safeMargin);
73 result.screenX = std::clamp(result.screenX, margin,
74 std::max(margin, viewportWidth - margin));
75 result.screenY = std::clamp(result.screenY, margin,
76 std::max(margin, viewportHeight - margin));
77 }
79 result.render = true;
80 if (anchor.distanceScale) {
81 const float distance = glm::length(glm::vec3(anchor.worldX - cameraX,
82 anchor.worldY - cameraY,
83 anchor.worldZ - cameraZ));
84 const float reference = std::max(anchor.referenceDistance, 0.001f);
85 result.scale = std::clamp(reference / std::max(distance, 0.001f),
86 std::min(anchor.minScale, anchor.maxScale),
87 std::max(anchor.minScale, anchor.maxScale));
88 }
89 return result;
90}
91
92std::vector<WorldAnchorLayoutResult> resolveWorldAnchorOverlaps(
93 std::vector<WorldAnchorLayoutItem> items, float viewportWidth, float viewportHeight) {
94 std::vector<WorldAnchorLayoutResult> results;
95 results.reserve(items.size());
96 for (const auto &item : items) {
97 results.push_back({item.stableIndex, item.screenX, item.screenY, 0.f, 0.f, false});
98 }
99 std::sort(results.begin(), results.end(), [](const auto &a, const auto &b) {
100 return a.stableIndex < b.stableIndex;
101 });
102 const auto resultFor = [&](std::size_t stableIndex) -> WorldAnchorLayoutResult & {
103 return *std::lower_bound(results.begin(), results.end(), stableIndex,
104 [](const auto &result, std::size_t value) {
105 return result.stableIndex < value;
106 });
107 };
108 if (viewportWidth <= 0.f || viewportHeight <= 0.f) return results;
109
110 std::vector<AnchorRect> occupied;
111 occupied.reserve(items.size());
112 for (const auto &item : items) {
113 if (item.avoidOverlap) continue;
114 occupied.push_back(anchorRect(item, item.screenX, item.screenY));
115 resultFor(item.stableIndex).render = true;
116 }
117
118 std::stable_sort(items.begin(), items.end(), [](const auto &a, const auto &b) {
119 if (a.avoidOverlap != b.avoidOverlap) return a.avoidOverlap > b.avoidOverlap;
120 if (a.priority != b.priority) return a.priority > b.priority;
121 if (a.depth != b.depth) return a.depth < b.depth;
122 return a.stableIndex < b.stableIndex;
123 });
124
125 for (const auto &item : items) {
126 if (!item.avoidOverlap) continue;
127 const float step = std::max(1.f, std::max(1.f, item.height) +
128 std::max(0.f, item.padding));
129 const int attempts = std::max(0, int(std::floor(std::max(0.f, item.maxDisplacement) /
130 step)));
131 bool placed = false;
132 for (int attempt = 0; attempt <= attempts * 2; ++attempt) {
133 const int level = (attempt + 1) / 2;
134 const float direction = attempt == 0 ? 0.f : (attempt % 2 == 1 ? -1.f : 1.f);
135 const float displacementY = direction * float(level) * step;
136 const float y = item.screenY + displacementY;
137 const AnchorRect candidate = anchorRect(item, item.screenX, y);
138 if (!insideViewport(candidate, item.safeMargin, viewportWidth, viewportHeight))
139 continue;
140 if (std::any_of(occupied.begin(), occupied.end(), [&](const AnchorRect &other) {
141 return intersects(candidate, other);
142 }))
143 continue;
144 auto &result = resultFor(item.stableIndex);
145 result.screenY = y;
146 result.displacementY = displacementY;
147 result.render = true;
148 occupied.push_back(candidate);
149 placed = true;
150 break;
151 }
152 if (!placed) resultFor(item.stableIndex).render = false;
153 }
154
155 return results;
156}
157
158} // namespace eve::ui
double y
double x
AuthorityStoreHandleRef reference
Definition Authority.cpp:24
glm::vec4 clip
HexVec3 left
HexVec3 right
std::uint32_t height
std::uint32_t width
Poco::JSON::Object::Ptr result
MeleePoint3 b
Definition MeleeHit.cpp:41
MeleePoint3 a
Definition MeleeHit.cpp:40
float distance
int level
Object::Ptr value
RoadLaneDirection direction
bool occupied
Definition Shadow.cpp:155
bool placed
int margin
float step
Definition TreeMesh.cpp:451
float bottom
SQInteger top
Definition Snapshot.cpp:115
std::vector< WorldAnchorLayoutResult > resolveWorldAnchorOverlaps(std::vector< WorldAnchorLayoutItem > items, float viewportWidth, float viewportHeight)
Resolves projected host overlap with bounded deterministic vertical displacement.
WorldAnchorProjection projectWorldAnchor(const UIHost::WorldAnchor &anchor, const glm::mat4 &viewProjection, float cameraX, float cameraY, float cameraZ, float viewportWidth, float viewportHeight)
Projects one world-space UI anchor using a Vulkan RH-ZO view-projection matrix.
Optional 3D-world projection for this UI host.
Definition UIHost.h:351
WorldAnchorEdgePolicy edgePolicy
Definition UIHost.h:359
Owning placement result corresponding to WorldAnchorLayoutItem::stableIndex.
Immutable result of projecting a UI world anchor into a viewport.