Repository Public License
1// ORIX Convert — Packages/EngineModel3D/Sources/CAssimpShim/include/orix_assimp_shim.h2// Description: Small C layer over Assimp's C API: bounded import with validation, flat accessors for the structs3// that are awkward to read from Swift, and construction of an aiScene from flat arrays for export.4// Author: Simon-Pierre Boucher — contact@spboucher.ai5#ifndef ORIX_ASSIMP_SHIM_H6#define ORIX_ASSIMP_SHIM_H78#include <stddef.h>9#include <stdint.h>10#include <assimp/scene.h>11#include <assimp/material.h>1213#ifdef __cplusplus14extern "C" {15#endif1617typedef struct {18 uint64_t faces; // faces before triangulation (all kinds)19 uint64_t points; // 1-index faces20 uint64_t lines; // 2-index faces21 uint64_t triangles; // 3-index faces22 uint64_t polygons; // faces with more than 3 indices23 uint64_t vertices;24 uint32_t bones;25 uint32_t morphTargets;26 uint32_t maxUVChannels;27 uint32_t maxColorChannels;28} orix_prestats;2930/// Imports `path` with structure validation, measures the face kinds, enforces the limits, then triangulates and31/// drops nothing (points and lines stay as separate meshes). Returns NULL and fills `err` on failure.32/// `limit_hit` is set when the failure is a limit and not a malformed file.33/// Files other than `path` (material libraries, buffers) are only opened when they really lie inside `root`34/// (symbolic links resolved); `blocked` counts the opens that were refused.35const struct aiScene *orix_import(const char *path, const char *root, uint64_t max_vertices, uint64_t max_faces, int *blocked,36 orix_prestats *stats, int *limit_hit, char *err, size_t errlen);37void orix_release(const struct aiScene *scene);3839const char *orix_str(const struct aiString *s);40/// Copies the triangles of a mesh (faces with exactly 3 in-range indices). Returns the number written.41uint32_t orix_mesh_triangles(const struct aiMesh *mesh, uint32_t *out);42/// Number of faces of a mesh that are not triangles (points, lines) or carry out-of-range indices.43uint32_t orix_mesh_nontriangles(const struct aiMesh *mesh);44void orix_copy_vec3(const struct aiVector3D *src, uint32_t n, float *dst_stride16);45void orix_copy_uv(const struct aiVector3D *src, uint32_t n, float *dst_stride8);46const struct aiVector3D *orix_mesh_uv(const struct aiMesh *mesh, uint32_t channel);47const struct aiColor4D *orix_mesh_color(const struct aiMesh *mesh, uint32_t channel);48/// Tangents with handedness in w (glTF convention). Returns 0 when the mesh has no tangents/bitangents/normals.49int orix_copy_tangents(const struct aiMesh *mesh, float *dst_stride16);5051int orix_meta_number(const struct aiMetadata *meta, const char *key, double *out);52int orix_meta_string(const struct aiMetadata *meta, const char *key, char *out, size_t outlen);5354typedef struct {55 char path[1024];56 int present;57 int uvIndex;58} orix_texslot;5960enum { ORIX_EXT_CLEARCOAT = 1, ORIX_EXT_SHEEN = 2, ORIX_EXT_TRANSMISSION = 4, ORIX_EXT_VOLUME = 8,61 ORIX_EXT_SPECGLOSS = 16, ORIX_EXT_ANISOTROPY = 32, ORIX_EXT_EMISSIVE_STRENGTH = 64, ORIX_EXT_SPECULAR = 128,62 ORIX_EXT_IOR = 256 };6364typedef struct {65 char name[256];66 float base[4];67 int hasBase; // 1 = PBR base colour, 2 = Phong diffuse, 0 = none68 float metallic; // < 0 when absent69 float roughness; // < 0 when absent70 float emissive[3];71 float opacity; // < 0 when absent72 float shininess; // < 0 when absent73 float specular[3];74 int hasSpecular;75 int twoSided;76 char alphaMode[16];77 float alphaCutoff;78 unsigned extensions;79 unsigned otherTextures;80 orix_texslot texBaseColor, texNormal, texEmissive, texMetallicRoughness, texOcclusion;81} orix_matinfo;8283void orix_material_info(const struct aiMaterial *mat, orix_matinfo *out);8485/// Embedded texture `index`. `compressed` = 1 when the data is an encoded image file (PNG, JPEG…) of `len` bytes;86/// otherwise the data is `width * height` BGRA texels.87int orix_texture_info(const struct aiScene *scene, uint32_t index, const void **data, size_t *len,88 char *hint, size_t hintlen, char *name, size_t namelen, int *compressed, uint32_t *width, uint32_t *height);8990// ---- Export ----9192typedef struct {93 const char *name;94 uint32_t vertexCount;95 const float *positions; // 3 floats per vertex, packed96 const float *normals; // 3 per vertex or NULL97 const float *uvs; // 2 per vertex or NULL98 const float *colors; // 4 per vertex or NULL99 uint32_t indexCount;100 const uint32_t *indices;101 uint32_t material;102} orix_xmesh;103104typedef struct {105 const char *name;106 float transform[16]; // row-major107 int32_t parent; // -1 for the root; parents come before children108 uint32_t meshCount;109 const uint32_t *meshes;110} orix_xnode;111112typedef struct {113 const char *name;114 float base[4];115 float metallic, roughness;116 float emissive[3];117 int twoSided;118 const char *baseColorTexture; // relative path or NULL119 const char *normalTexture;120 const char *emissiveTexture;121} orix_xmaterial;122123/// Builds an aiScene from the arrays (node 0 is the root) and exports it with Assimp's exporter `format_id`124/// ("collada", "fbx", "obj", "stl", "ply", "glb2", "gltf2", "3mf"…). Returns 0 on success.125int orix_export(const orix_xnode *nodes, uint32_t nodeCount, const orix_xmesh *meshes, uint32_t meshCount,126 const orix_xmaterial *materials, uint32_t materialCount,127 const char *format_id, const char *path, char *err, size_t errlen);128129/// strtod in the C locale, whatever the process locale is.130double orix_strtod(const char *text, char **end);131132/// 1 when an importer for the extension (".fbx") is compiled in.133int orix_can_import(const char *extension);134int orix_can_export(const char *format_id);135const char *orix_assimp_version(void);136137#ifdef __cplusplus138}139#endif140#endif141