7#include <assimp/mesh.h>
8#include <assimp/vector3.h>
29aiVector3D unitOrUp(
float x,
float y,
float z) {
32 return aiVector3D(0.f, 0.f, 1.f);
36bool ensureNormals(aiMesh *
mesh) {
37 if (!
mesh ||
mesh->mNumVertices == 0 || !
mesh->mVertices)
41 mesh->mNormals =
new aiVector3D[
mesh->mNumVertices];
42 for (
unsigned i = 0; i <
mesh->mNumVertices; ++i)
43 mesh->mNormals[i] = aiVector3D(0.f, 0.f, 1.f);
47bool meshAabbCenter(
const aiMesh *
mesh, aiVector3D *
out) {
48 if (!
mesh ||
mesh->mNumVertices == 0 || !
mesh->mVertices)
50 aiVector3D lo =
mesh->mVertices[0];
51 aiVector3D hi =
mesh->mVertices[0];
52 for (
unsigned i = 1; i <
mesh->mNumVertices; ++i) {
53 const aiVector3D &
p =
mesh->mVertices[i];
54 lo.x = std::min(lo.x,
p.x);
55 lo.y = std::min(lo.y,
p.y);
56 lo.z = std::min(lo.z,
p.z);
57 hi.x = std::max(hi.x,
p.x);
58 hi.y = std::max(hi.y,
p.y);
59 hi.z = std::max(hi.z,
p.z);
61 *
out = aiVector3D((lo.x + hi.x) * 0.5f, (lo.y + hi.y) * 0.5f, (lo.z + hi.z) * 0.5f);
65void applyRadial(aiMesh *
mesh,
const aiVector3D &
origin) {
66 for (
unsigned i = 0; i <
mesh->mNumVertices; ++i) {
67 const aiVector3D &
p =
mesh->mVertices[i];
83Vec3 add(
const Vec3 &
a,
const Vec3 &
b) {
return {
a.
x +
b.x,
a.y +
b.y,
a.z +
b.z}; }
84Vec3 sub(
const Vec3 &
a,
const Vec3 &
b) {
return {
a.
x -
b.x,
a.y -
b.y,
a.z -
b.z}; }
86float dot3(
const Vec3 &
a,
const Vec3 &
b) {
return a.x *
b.x +
a.y *
b.y +
a.z *
b.z; }
88 return {
a.y *
b.z -
a.z *
b.y,
a.z *
b.x -
a.x *
b.z,
a.x *
b.y -
a.y *
b.x};
90Vec3 normalize3(
const Vec3 &
a) {
91 const aiVector3D
n = unitOrUp(
a.x,
a.y,
a.z);
92 return {
n.x,
n.y,
n.z};
95bool barycentric2(
const Vec2 &
p,
const Vec2 &
a,
const Vec2 &
b,
const Vec2 &
c,
float *wa,
float *wb,
97 const float den = (
b.y -
c.y) * (
a.x -
c.x) + (
c.x -
b.x) * (
a.y -
c.y);
98 if (!std::isfinite(den) || std::fabs(den) <= 1e-12f)
100 *wa = ((
b.y -
c.y) * (
p.x -
c.x) + (
c.x -
b.x) * (
p.y -
c.y)) / den;
101 *wb = ((
c.y -
a.y) * (
p.x -
c.x) + (
a.x -
c.x) * (
p.y -
c.y)) / den;
102 *wc = 1.f - *wa - *wb;
108 c.r = std::clamp(
n.x * 0.5f + 0.5f, 0.f, 1.f);
109 c.g = std::clamp(
n.y * 0.5f + 0.5f, 0.f, 1.f);
110 c.b = std::clamp(
n.z * 0.5f + 0.5f, 0.f, 1.f);
115Vec3 toTangent(
const Vec3 &objectNormal,
const Vec3 &pos0,
const Vec3 &pos1,
const Vec3 &pos2,
116 const Vec2 &uv0,
const Vec2 &uv1,
const Vec2 &uv2) {
117 const Vec3 e1 =
sub(pos1, pos0);
118 const Vec3 e2 =
sub(pos2, pos0);
119 const float du1 = uv1.x - uv0.x;
120 const float dv1 = uv1.y - uv0.y;
121 const float du2 = uv2.x - uv0.x;
122 const float dv2 = uv2.y - uv0.y;
123 const float det = du1 * dv2 - du2 * dv1;
125 if (dot3(geometric, objectNormal) < 0.f)
126 geometric =
scale(geometric, -1.f);
129 if (!std::isfinite(det) || std::fabs(det) <= 1e-12f) {
130 tangent = {1.f, 0.f, 0.f};
131 if (std::fabs(dot3(tangent, geometric)) > 0.9f)
132 tangent = {0.f, 1.f, 0.f};
133 tangent = normalize3(
sub(tangent,
scale(geometric, dot3(tangent, geometric))));
134 bitangent = normalize3(
cross3(geometric, tangent));
136 const float inv = 1.f / det;
137 tangent = normalize3(
add(
scale(e1, dv2 * inv),
scale(e2, -dv1 * inv)));
138 bitangent = normalize3(
add(
scale(e1, -du2 * inv),
scale(e2, du1 * inv)));
139 tangent = normalize3(
sub(tangent,
scale(geometric, dot3(tangent, geometric))));
140 const Vec3 reconstructed =
cross3(geometric, tangent);
141 if (dot3(reconstructed, bitangent) < 0.f)
142 bitangent =
scale(reconstructed, -1.f);
144 bitangent = reconstructed;
146 return {dot3(objectNormal, tangent), dot3(objectNormal, bitangent),
147 dot3(objectNormal, geometric)};
154 aiMesh *
mesh = meshAtMutable(meshIndex);
155 if (!
mesh || vertexIndex < 0 ||
static_cast<unsigned>(vertexIndex) >=
mesh->mNumVertices)
157 invalidArg(
"invalid mesh or vertex index",
"model3d.setVertexNormal"));
158 if (!ensureNormals(
mesh))
160 invalidArg(
"mesh has no vertices",
"model3d.setVertexNormal"));
161 mesh->mNormals[vertexIndex] = unitOrUp(
x,
y,
z);
166 aiMesh *
mesh = meshAtMutable(meshIndex);
169 invalidArg(
"invalid mesh index",
"model3d.applyVertexNormals"));
173 invalidArg(
"mesh has no vertices",
"model3d.applyVertexNormals"));
178 float originX,
float originY,
float originZ) {
179 aiMesh *
mesh = meshAtMutable(meshIndex);
182 invalidArg(
"invalid mesh index",
"model3d.applyVertexNormalsFrom"));
183 if (
kind !=
"radial")
185 unsupported(
"unknown vertex-normal kind",
"model3d.applyVertexNormalsFrom.kind"));
186 if (!ensureNormals(
mesh))
188 invalidArg(
"mesh has no vertices",
"model3d.applyVertexNormalsFrom"));
189 applyRadial(
mesh, aiVector3D(originX, originY, originZ));
194 int meshIndex,
int width,
int height,
int uvChannel, std::string_view space)
const {
195 using ImagePtr = std::unique_ptr<image::ImageData>;
196 const aiMesh *
mesh = meshAt(meshIndex);
199 invalidArg(
"invalid mesh index",
"model3d.bakeNormalMap"));
202 invalidArg(
"normal map size must be positive",
"model3d.bakeNormalMap.size"));
203 if (space !=
"tangent" && space !=
"object")
205 unsupported(
"space must be \"tangent\" or \"object\"",
"model3d.bakeNormalMap.space"));
206 if (!
mesh->HasNormals())
208 "mesh has no vertex normals; apply or set them before baking",
209 "model3d.bakeNormalMap.normals"));
210 if (uvChannel < 0 || uvChannel >= AI_MAX_NUMBER_OF_TEXTURECOORDS ||
211 !
mesh->HasTextureCoords(
static_cast<unsigned>(uvChannel)))
214 "model3d.bakeNormalMap.channel"));
216 for (
unsigned f = 0;
f <
mesh->mNumFaces; ++
f) {
217 if (
mesh->mFaces[
f].mNumIndices != 3)
219 "normal-map baking requires triangulated faces",
"model3d.bakeNormalMap.triangle"));
232 const float widthF =
static_cast<float>(
width);
233 const float heightF =
static_cast<float>(
height);
234 const bool tangentSpace = space ==
"tangent";
236 for (
unsigned f = 0;
f <
mesh->mNumFaces; ++
f) {
237 const aiFace &face =
mesh->mFaces[
f];
238 const unsigned i0 = face.mIndices[0];
239 const unsigned i1 = face.mIndices[1];
240 const unsigned i2 = face.mIndices[2];
247 const aiVector3D &t0 =
mesh->mTextureCoords[uvChannel][
i0];
248 const aiVector3D &t1 =
mesh->mTextureCoords[uvChannel][
i1];
249 const aiVector3D &t2 =
mesh->mTextureCoords[uvChannel][
i2];
250 const Vec2 uv0{t0.x, t0.y};
251 const Vec2 uv1{t1.x, t1.y};
252 const Vec2 uv2{t2.x, t2.y};
253 const Vec2 pix0{uv0.x * widthF, (1.f - uv0.y) * heightF};
254 const Vec2 pix1{uv1.x * widthF, (1.f - uv1.y) * heightF};
255 const Vec2 pix2{uv2.x * widthF, (1.f - uv2.y) * heightF};
257 const int minX = std::max(0,
static_cast<int>(std::floor(std::min({pix0.x, pix1.x, pix2.x}))));
259 std::min(
width - 1,
static_cast<int>(std::ceil(std::max({pix0.x, pix1.x, pix2.x}))));
260 const int minY = std::max(0,
static_cast<int>(std::floor(std::min({pix0.y, pix1.y, pix2.y}))));
262 std::min(
height - 1,
static_cast<int>(std::ceil(std::max({pix0.y, pix1.y, pix2.y}))));
264 for (
int y = minY;
y <= maxY; ++
y) {
265 for (
int x = minX;
x <= maxX; ++
x) {
266 const Vec2 sample{
static_cast<float>(
x) + 0.5f,
static_cast<float>(
y) + 0.5f};
267 float wa = 0.f, wb = 0.f, wc = 0.f;
268 if (!barycentric2(sample, pix0, pix1, pix2, &wa, &wb, &wc))
270 constexpr float kEps = -1e-4f;
271 if (wa < kEps || wb < kEps || wc < kEps)
273 Vec3
object = normalize3(add(add(
scale(n0, wa),
scale(n1, wb)),
scale(n2, wc)));
275 tangentSpace ? toTangent(
object, p0, p1, p2, uv0, uv1, uv2) :
object;
276 image->setPixel(
x,
y, encodeNormal(encoded));
Stable, structured diagnostics shared by engine modules.
std::array< float, 3 > scale
Move-only, checked operation results for the common layer.
A structured explanation of a failed, degraded, or noteworthy result.
static Diagnostic error(DiagnosticCode code, std::string message, std::string path={})
Construct an error diagnostic with the standard error severity.
Move-only operation result carrying either a value or Status.
static Result success(T value)
Construct a successful result owning value.
static Result failure(Status status)
Construct a failed result from a structured status.
static Status success(StatusCode code=StatusCode::Ok)
Construct a successful status with an explicit non-error outcome.
medialoader::Colorf Colorf
eve::Result< void > applyVertexNormalsFrom(int meshIndex, std::string_view kind, float originX, float originY, float originZ)
Like applyVertexNormals, but "radial" uses an explicit origin.
eve::Result< void > applyVertexNormals(int meshIndex, std::string_view kind)
Replace every vertex normal on a mesh from a named procedure.
eve::Result< void > setVertexNormal(int meshIndex, int vertexIndex, float x, float y, float z)
Write one unit (or zero) object-space vertex normal. Allocates the normal stream when the mesh has po...
eve::Result< std::unique_ptr< image::ImageData > > bakeNormalMap(int meshIndex, int width, int height, int uvChannel=0, std::string_view space="tangent") const
Rasterize current object-space vertex normals into an RGBA8 normal map.
void cross3(float ax, float ay, float az, float bx, float by, float bz, float &x, float &y, float &z)
ForceVector add(ForceVector a, ForceVector b)
Build metadata (engine git commit, build time, third-party version).