28#include "common/config.h"
36#include <assimp/matrix3x3.h>
37#include <assimp/matrix4x4.h>
38#include <assimp/mesh.h>
39#include <assimp/vector3.h>
53#include <glm/gtc/constants.hpp>
68 throw Exception(
"newMeshFromArrays: invalid index data");
75 auto&
v =
verts[
static_cast<size_t>(i)];
76 v.pos = {posXYZ[i * 3 + 0], posXYZ[i * 3 + 1], posXYZ[i * 3 + 2]};
78 v.normal = {nrmXYZ[i * 3 + 0], nrmXYZ[i * 3 + 1], nrmXYZ[i * 3 + 2]};
80 v.normal = {0.f, 0.f, 1.f};
83 v.uv = {uvST[i * 2 + 0], uvST[i * 2 + 1]};
87 if (colorRGBA)
v.color = {colorRGBA[i * 4], colorRGBA[i * 4 + 1], colorRGBA[i * 4 + 2], colorRGBA[i * 4 + 3]};
90 auto gpu = std::make_unique<GpuMesh>();
96 WGPUBufferDescriptor vbd{};
97 vbd.label =
sv(
"eve_mesh_vb");
99 vbd.usage = WGPUBufferUsage_CopyDst | WGPUBufferUsage_Vertex | WGPUBufferUsage_Storage;
100 vbd.mappedAtCreation =
false;
101 gpu->vertexBuffer = device.CreateBuffer(
reinterpret_cast<const wgpu::BufferDescriptor*
>(&vbd));
102 queue.WriteBuffer(
gpu->vertexBuffer, 0,
verts.data(), vbd.size);
103 gpu->vertexCapacity = vbd.size;
108 std::vector<uint16_t> idx16;
111 WGPUBufferDescriptor ibd{};
112 ibd.label =
sv(
"eve_mesh_ib");
113 if (idx16.size() % 2 != 0) idx16.push_back(0);
114 ibd.size = uint64_t(idx16.size()) *
sizeof(uint16_t);
115 ibd.usage = WGPUBufferUsage_CopyDst | WGPUBufferUsage_Index | WGPUBufferUsage_Storage;
116 ibd.mappedAtCreation =
false;
117 gpu->indexBuffer = device.CreateBuffer(
reinterpret_cast<const wgpu::BufferDescriptor*
>(&ibd));
118 queue.WriteBuffer(
gpu->indexBuffer, 0, idx16.data(), ibd.size);
119 gpu->indexFormat = wgpu::IndexFormat::Uint16;
120 gpu->indexCapacity = ibd.size;
122 WGPUBufferDescriptor ibd{};
123 ibd.label =
sv(
"eve_mesh_ib");
124 ibd.size = uint64_t(
indexCount) *
sizeof(uint32_t);
125 ibd.usage = WGPUBufferUsage_CopyDst | WGPUBufferUsage_Index | WGPUBufferUsage_Storage;
126 ibd.mappedAtCreation =
false;
127 gpu->indexBuffer = device.CreateBuffer(
reinterpret_cast<const wgpu::BufferDescriptor*
>(&ibd));
128 queue.WriteBuffer(
gpu->indexBuffer, 0,
indices, ibd.size);
129 gpu->indexFormat = wgpu::IndexFormat::Uint32;
130 gpu->indexCapacity = ibd.size;
139 ownedGpuMeshes.push_back(std::move(
gpu));
140 ownedMeshes.push_back(std::unique_ptr<Mesh>(
mesh));
150 std::find_if(ownedGpuMeshes.begin(), ownedGpuMeshes.end(),
151 [
gpu](
const std::unique_ptr<GpuMesh>& candidate) { return candidate.get() == gpu; });
152 if (
owned == ownedGpuMeshes.end())
156 gpu->
vertexCount,
gpu->indexCount,
gpu->vertexStride,
gpu->indexFormat == wgpu::IndexFormat::Uint16 ? 2u : 4u});
163 const aiMatrix4x4 world(worldTransform[0][0], worldTransform[1][0], worldTransform[2][0], worldTransform[3][0],
164 worldTransform[0][1], worldTransform[1][1], worldTransform[2][1], worldTransform[3][1],
165 worldTransform[0][2], worldTransform[1][2], worldTransform[2][2], worldTransform[3][2],
166 worldTransform[0][3], worldTransform[1][3], worldTransform[2][3], worldTransform[3][3]);
168 std::vector<float>
pos, nrm,
uv;
169 std::vector<uint32_t>
idx;
170 pos.reserve(
mesh.mNumVertices * 3);
171 nrm.reserve(
mesh.mNumVertices * 3);
172 uv.reserve(
mesh.mNumVertices * 2);
173 idx.reserve(
mesh.mNumFaces * 3);
175 for (
unsigned i = 0; i <
mesh.mNumVertices; ++i) {
176 aiVector3D
p = world *
mesh.mVertices[i];
181 aiMatrix3x3 rot(world);
182 aiVector3D
n = rot *
mesh.mNormals[i];
192 if (
mesh.mTextureCoords[0]) {
193 uv.push_back(
mesh.mTextureCoords[0][i].x);
194 uv.push_back(
mesh.mTextureCoords[0][i].y);
200 for (
unsigned f = 0;
f <
mesh.mNumFaces; ++
f) {
201 for (
unsigned v = 0;
v <
mesh.mFaces[
f].mNumIndices; ++
v)
idx.push_back(
mesh.mFaces[
f].mIndices[
v]);
204 if (
m)
m->captureImportedAttributes(
mesh);
205 if (
m &&
mesh.mNumAnimMeshes > 0) {
206 m->initMorphBase(
int(
mesh.mNumVertices),
pos.data(), nrm.data(),
uv.data());
207 for (
unsigned a = 0;
a <
mesh.mNumAnimMeshes; ++
a) {
208 const aiAnimMesh* am =
mesh.mAnimMeshes[
a];
209 std::vector<float> absPos(am->mNumVertices * 3);
210 for (
unsigned i = 0; i < am->mNumVertices; ++i) {
211 absPos[i * 3 + 0] = am->mVertices[i].x;
212 absPos[i * 3 + 1] = am->mVertices[i].y;
213 absPos[i * 3 + 2] = am->mVertices[i].z;
215 std::string
name = am->mName.C_Str();
216 if (
name.empty())
name =
"morph" + std::to_string(
a);
217 m->addMorphTargetAbsolute(
name, absPos.data());
219 std::vector<float> deltas(am->mNumVertices * 3u);
220 for (
unsigned i = 0; i < am->mNumVertices; ++i) {
221 deltas[i * 3] = am->mNormals[i].x - nrm[i * 3];
222 deltas[i * 3 + 1] = am->mNormals[i].y - nrm[i * 3 + 1];
223 deltas[i * 3 + 2] = am->mNormals[i].z - nrm[i * 3 + 2];
234 if (!
mesh || !
mesh->hasMorphData() || !
mesh->isMorphDirty())
return false;
236 if (!
gpu || !
gpu->vertexBuffer)
return false;
237 std::vector<float>
pos, nrm;
238 mesh->computeMorphedPositions(
pos, nrm);
239 if (
pos.empty())
return false;
240 mesh->computeBounds(
pos.data(),
mesh->getVertexCount());
242 if (
verts.size() !=
static_cast<size_t>(
mesh->getVertexCount()))
return false;
243 const auto& uvs =
mesh->baseUv();
244 for (
int i = 0; i <
mesh->getVertexCount(); ++i) {
245 auto&
v =
verts[
static_cast<size_t>(i)];
246 v.pos = {
pos[i * 3 + 0],
pos[i * 3 + 1],
pos[i * 3 + 2]};
247 if (nrm.size() >= size_t((i + 1) * 3)) {
248 v.normal = {nrm[i * 3 + 0], nrm[i * 3 + 1], nrm[i * 3 + 2]};
250 v.normal = {0.f, 0.f, 1.f};
252 if (uvs.size() >= size_t((i + 1) * 2)) {
253 v.uv = {uvs[i * 2 + 0], uvs[i * 2 + 1]};
259 mesh->markMorphClean();
265 if (!
mesh || !
mesh->gpuHandle || !joints4 || !weights4)
return false;
267 if (vertexCount <= 0 || gpu->cpuVertices.size() !=
static_cast<size_t>(
vertexCount))
return false;
269 const size_t base =
static_cast<size_t>(i) * 4u;
271 v.joints = glm::u16vec4(joints4[base], joints4[base + 1], joints4[base + 2], joints4[base + 3]);
272 v.weights = glm::vec4(weights4[base], weights4[base + 1], weights4[base + 2], weights4[base + 3]);
274 queue.WriteBuffer(
gpu->vertexBuffer, 0,
gpu->cpuVertices.data(),
gpu->cpuVertices.size() *
sizeof(
MeshVertex));
275 mesh->markGpuSkinned(
true);
280 if (!
mesh || !
mesh->gpuHandle)
return false;
283 auto gpuIt = std::find_if(ownedGpuMeshes.begin(), ownedGpuMeshes.end(),
284 [&](
const std::unique_ptr<GpuMesh>&
g) { return g.get() == gpu; });
285 if (gpuIt == ownedGpuMeshes.end())
return false;
287 auto meshIt = std::find_if(ownedMeshes.begin(), ownedMeshes.end(),
288 [&](
const std::unique_ptr<Mesh>&
m) { return m.get() == mesh; });
289 if (meshIt == ownedMeshes.end())
return false;
291 mesh->gpuHandle =
nullptr;
292 ownedGpuMeshes.erase(gpuIt);
294 (void)meshIt->release();
295 ownedMeshes.erase(meshIt);
300 std::vector<float>
pos, nrm,
uv;
301 std::vector<uint32_t>
idx;
302 for (
int y = 0;
y <= stacks; ++
y) {
303 for (
int x = 0;
x <= slices; ++
x) {
304 float u = float(
x) / float(slices);
305 float v = float(
y) / float(stacks);
306 float theta =
u * 2.f * glm::pi<float>();
307 float phi =
v * glm::pi<float>();
308 float sx = std::sin(
phi) * std::cos(theta);
309 float sy = std::cos(
phi);
310 float sz = std::sin(
phi) * std::sin(theta);
321 for (
int y = 0;
y < stacks; ++
y) {
322 for (
int x = 0;
x < slices; ++
x) {
323 int a =
y * (slices + 1) +
x;
324 int b =
a + slices + 1;
326 idx.push_back(
a + 1);
329 idx.push_back(
a + 1);
330 idx.push_back(
b + 1);
337 std::vector<float>
pos, nrm,
uv;
338 std::vector<uint32_t>
idx;
339 for (
int y = 0;
y <= stacks; ++
y) {
340 for (
int x = 0;
x <= slices; ++
x) {
341 float u = float(
x) / float(slices);
342 float v = float(
y) / float(stacks);
343 float theta =
u * 2.f * glm::pi<float>();
344 pos.push_back(std::cos(theta));
345 pos.push_back(
v * 2.f - 1.f);
346 pos.push_back(std::sin(theta));
347 nrm.push_back(std::cos(theta));
349 nrm.push_back(std::sin(theta));
354 for (
int y = 0;
y < stacks; ++
y) {
355 for (
int x = 0;
x < slices; ++
x) {
356 int a =
y * (slices + 1) +
x;
357 int b =
a + slices + 1;
360 idx.push_back(
a + 1);
362 idx.push_back(
b + 1);
363 idx.push_back(
a + 1);
367 int base = int(
pos.size() / 3);
376 for (
int x = 0;
x < slices; ++
x) {
377 float theta0 = float(
x) / float(slices) * 2.f * glm::pi<float>();
378 float theta1 = float(
x + 1) / float(slices) * 2.f * glm::pi<float>();
379 int i0 = int(
pos.size() / 3);
380 pos.push_back(std::cos(theta0));
382 pos.push_back(std::sin(theta0));
386 uv.push_back(0.5f + 0.5f * std::cos(theta0));
387 uv.push_back(0.5f + 0.5f * std::sin(theta0));
388 int i1 = int(
pos.size() / 3);
389 pos.push_back(std::cos(theta1));
391 pos.push_back(std::sin(theta1));
395 uv.push_back(0.5f + 0.5f * std::cos(theta1));
396 uv.push_back(0.5f + 0.5f * std::sin(theta1));
401 base = int(
pos.size() / 3);
410 for (
int x = 0;
x < slices; ++
x) {
411 float theta0 = float(
x) / float(slices) * 2.f * glm::pi<float>();
412 float theta1 = float(
x + 1) / float(slices) * 2.f * glm::pi<float>();
413 int i0 = int(
pos.size() / 3);
414 pos.push_back(std::cos(theta0));
416 pos.push_back(std::sin(theta0));
420 uv.push_back(0.5f + 0.5f * std::cos(theta0));
421 uv.push_back(0.5f + 0.5f * std::sin(theta0));
422 int i1 = int(
pos.size() / 3);
423 pos.push_back(std::cos(theta1));
425 pos.push_back(std::sin(theta1));
429 uv.push_back(0.5f + 0.5f * std::cos(theta1));
430 uv.push_back(0.5f + 0.5f * std::sin(theta1));
std::vector< std::uint32_t > verts
std::uint32_t vertexCount
std::vector< std::uint32_t > indices
std::vector< float > normals
std::shared_ptr< OceanGpuSpectrum > gpu
Local-probe upload record kept off the high-fanout IGraphics3D header.
std::vector< double > phi
static Diagnostic error(DiagnosticCode code, std::string message, std::string path={})
Construct an error diagnostic with the standard error severity.
EVENGINE_API_FOUNDATION public API.
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.
GPU mesh handle (+ optional CPU morph targets).
Mesh * newMeshSphere(int slices=32, int stacks=16) override
Creates a mesh sphere. @ownership Caller deletes unless documented otherwise.
eve::Result< eve::graphics::MeshBackendDescriptor > describeMesh(Mesh *mesh) const override
Return layout facts from the owned WebGPU mesh upload.
Mesh * newMeshFromArraysColored(const float *posXYZ, const float *nrmXYZ, const float *uvST, const float *colorRGBA, int vertexCount, const uint32_t *indices, int indexCount) override
Create a mesh with a packed linear RGBA vertex-color stream.
Mesh * newMeshFromAssimp(const ::aiMesh &mesh) override
Creates a mesh from assimp. @ownership Caller deletes unless documented otherwise.
bool bakeMeshMorph(Mesh *mesh) override
If mesh morph weights are dirty, bake blended positions and upload to the GPU VBO....
Mesh * newMeshCylinder(int slices=32, int stacks=1, bool caps=true) override
Creates a mesh cylinder. @ownership Caller deletes unless documented otherwise.
Mesh * newMeshFromArrays(const float *posXYZ, const float *nrmXYZ, const float *uvST, int vertexCount, const uint32_t *indices, int indexCount) override
Creates a mesh from arrays. @ownership Caller deletes unless documented otherwise.
bool setMeshSkinningData(Mesh *mesh, const uint16_t *joints4, const float *weights4, int vertexCount) override
Sets the mesh skinning data.
bool releaseMesh(Mesh *mesh) override
Release mesh.
WGPUStringView sv(const char *s)
Build a WGPUStringView from a C string (null-safe, length auto-computed).
Backend-owned layout facts for a mesh uploaded through Graphics.
std::uint32_t vertexCount
Vertex/index buffers for one mesh.
std::vector< MeshVertex > cpuVertices