Estella 0.113.0
A fast 2D and 3D game engine powered by WebAssembly and ECS
Loading...
Searching...
No Matches
MeshUvDensity.hpp
Go to the documentation of this file.
1// SPDX-License-Identifier: Apache-2.0
2// SPDX-FileCopyrightText: Copyright (c) 2024-present ESEngine Team
15#pragma once
16
17#include "../core/Types.hpp"
19
20#include <cmath>
21#include <cstring>
22
23namespace esengine::resource {
24
25inline f32 meshUvDensity(const u8* vertexBytes, usize vertexByteCount, u32 vertexStride,
26 u32 positionOffset, u32 uvOffset, const u32* indices, usize indexCount) {
27 if (!vertexBytes || vertexStride == 0 || indexCount < 3) return 0.0f;
28 const usize vertexCount = vertexByteCount / vertexStride;
29 const auto read = [&](u32 vertex, u32 offset, u32 k) {
30 f32 value = 0.0f;
31 std::memcpy(&value, vertexBytes + static_cast<usize>(vertex) * vertexStride + offset + k * 4u, sizeof(f32));
32 return value;
33 };
34 f64 world = 0.0, tex = 0.0;
35 for (usize i = 0; i + 2 < indexCount; i += 3) {
36 const u32 a = indices[i], b = indices[i + 1], c = indices[i + 2];
37 if (a >= vertexCount || b >= vertexCount || c >= vertexCount) continue;
38 const f32 ux = read(b, positionOffset, 0) - read(a, positionOffset, 0);
39 const f32 uy = read(b, positionOffset, 1) - read(a, positionOffset, 1);
40 const f32 uz = read(b, positionOffset, 2) - read(a, positionOffset, 2);
41 const f32 vx = read(c, positionOffset, 0) - read(a, positionOffset, 0);
42 const f32 vy = read(c, positionOffset, 1) - read(a, positionOffset, 1);
43 const f32 vz = read(c, positionOffset, 2) - read(a, positionOffset, 2);
44 const f64 cx = uy * vz - uz * vy, cy = uz * vx - ux * vz, cz = ux * vy - uy * vx;
45 world += std::sqrt(cx * cx + cy * cy + cz * cz) * 0.5;
46 const f32 su = read(b, uvOffset, 0) - read(a, uvOffset, 0), sv = read(b, uvOffset, 1) - read(a, uvOffset, 1);
47 const f32 tu = read(c, uvOffset, 0) - read(a, uvOffset, 0), tv = read(c, uvOffset, 1) - read(a, uvOffset, 1);
48 tex += std::abs(static_cast<f64>(su) * tv - static_cast<f64>(sv) * tu) * 0.5;
49 }
50 return world > 0.0 ? static_cast<f32>(std::sqrt(tex / world)) : 0.0f;
51}
52
53} // namespace esengine::resource
Backend-agnostic graphics enumerations.
Core type definitions and utilities for ESEngine.
f32 meshUvDensity(const u8 *vertexBytes, usize vertexByteCount, u32 vertexStride, u32 positionOffset, u32 uvOffset, const u32 *indices, usize indexCount)
std::size_t usize
Size type for indexing and sizes.
Definition Types.hpp:64
std::uint32_t u32
Unsigned 32-bit integer.
Definition Types.hpp:45
std::uint8_t u8
Unsigned 8-bit integer.
Definition Types.hpp:41
double f64
64-bit floating point
Definition Types.hpp:61
float f32
32-bit floating point
Definition Types.hpp:59