Skip to content

A fast 2D and 3D game engine — TypeScript SDK, C++/WebAssembly core, visual editor. Ship one project to web, desktop, WeChat MiniGames, playable ads, and native Android / iOS.

feat(fbx): the second UV set survives that front too

esengine18de3acc1239
diff --git a/build-tools/ufbx/ufbx-load.wasm b/build-tools/ufbx/ufbx-load.wasm
index 7202ad9f7..2f843cefc 100755
Binary files a/build-tools/ufbx/ufbx-load.wasm and b/build-tools/ufbx/ufbx-load.wasm differ
diff --git a/desktop b/desktop
index 80b80cacd..ae88aead8 160000
--- a/desktop
+++ b/desktop
@@ -1 +1 @@
-Subproject commit 80b80cacd6fad15ce249c3661104f1ddbfbac1b1
+Subproject commit ae88aead875ddcf3ced22331545afbb1659110e9
diff --git a/pipeline/src/assets/fbxImport.ts b/pipeline/src/assets/fbxImport.ts
index 9638dcaff..05c816851 100644
--- a/pipeline/src/assets/fbxImport.ts
+++ b/pipeline/src/assets/fbxImport.ts
@@ -80,6 +80,8 @@ interface FbxMeshPart {
positions: Slice;
normals: Slice | null;
uvs: Slice | null;
+ /** The second UV set, where the source has one. */
+ uvs1: Slice | null;
colors: Slice | null;
joints: Slice | null;
weights: Slice | null;
@@ -426,6 +428,7 @@ function buildMesh(part: FbxMeshPart, name: string, payload: Uint8Array,
const positions = floats(payload, part.positions);
const normals = part.normals ? floats(payload, part.normals) : null;
const uvs = part.uvs ? floats(payload, part.uvs) : null;
+ const uvs1 = part.uvs1 ? floats(payload, part.uvs1) : null;
const joints = part.joints ? ushorts(payload, part.joints) : null;
const weights = part.weights ? floats(payload, part.weights) : null;
const indices = uints(payload, part.indices);
@@ -451,6 +454,9 @@ function buildMesh(part: FbxMeshPart, name: string, payload: Uint8Array,
{ semantic: MeshChannel.Color, components: 4, type: MeshChannelType.UNorm8 },
...(normals ? [{ semantic: MeshChannel.Normal, components: 3,
type: MeshChannelType.Float32 }] : []),
+ // Ahead of the skinning pair, which the two lines below find by position.
+ ...(uvs1 ? [{ semantic: MeshChannel.TexCoord1, components: 2,
+ type: MeshChannelType.Float32 }] : []),
...(skinned ? [
{ semantic: MeshChannel.Joints, components: 4, type: MeshChannelType.UInt16 },
{ semantic: MeshChannel.Weights, components: 4, type: MeshChannelType.Float32 },
@@ -458,6 +464,7 @@ function buildMesh(part: FbxMeshPart, name: string, payload: Uint8Array,
]);
const jointsChannel = skinned ? channels[channels.length - 2]! : null;
const weightsChannel = skinned ? channels[channels.length - 1]! : null;
+ const uv1Channel = channels.find(c => c.semantic === MeshChannel.TexCoord1) ?? null;
const vertices = new Uint8Array(vertexCount * vertexStride);
const dv = new DataView(vertices.buffer);
@@ -475,6 +482,11 @@ function buildMesh(part: FbxMeshPart, name: string, payload: Uint8Array,
// left, which is where the engine's textures start too.
dv.setFloat32(at + channels[1]!.offset, uvs ? uvs[i * 2] ?? 0 : 0, true);
dv.setFloat32(at + channels[1]!.offset + 4, uvs ? uvs[i * 2 + 1] ?? 0 : 0, true);
+ if (uv1Channel && uvs1) {
+ // No flip, as UV0 above: the two sets share one convention.
+ dv.setFloat32(at + uv1Channel.offset, uvs1[i * 2] ?? 0, true);
+ dv.setFloat32(at + uv1Channel.offset + 4, uvs1[i * 2 + 1] ?? 0, true);
+ }
for (let c = 0; c < 4; c++) {
const v = colors ? colors[i * 4 + c] ?? 1 : 1;
dv.setUint8(at + channels[2]!.offset + c,
diff --git a/tools/ufbx-wasm/bridge.c b/tools/ufbx-wasm/bridge.c
index f5d7b5ef9..f8fa7f8a0 100644
--- a/tools/ufbx-wasm/bridge.c
+++ b/tools/ufbx-wasm/bridge.c
@@ -202,12 +202,15 @@ typedef struct {
es_vec3 *positions;
es_vec3 *normals;
es_vec2 *uvs;
+ /** The second UV set, where the mesh has one — a lightmap or detail unwrap. */
+ es_vec2 *uvs1;
es_vec4 *colors;
es_joints *joints;
es_vec4 *weights;
uint32_t *indices;
int has_normals;
int has_uvs;
+ int has_uvs1;
int has_colors;
int has_skin;
} es_part;
@@ -216,6 +219,7 @@ static void es_part_free(es_part *p) {
free(p->positions);
free(p->normals);
free(p->uvs);
+ free(p->uvs1);
free(p->colors);
free(p->joints);
free(p->weights);
@@ -271,6 +275,8 @@ static int build_part(es_writer *w, const ufbx_mesh *mesh, const ufbx_mesh_part
out->has_normals = mesh->vertex_normal.exists;
out->has_uvs = mesh->vertex_uv.exists;
+ // uv_sets[0] IS vertex_uv; a second set is the one the engine could not carry.
+ out->has_uvs1 = mesh->uv_sets.count > 1 && mesh->uv_sets.data[1].vertex_uv.exists;
out->has_colors = mesh->vertex_color.exists;
out->has_skin = skin != NULL;
@@ -278,6 +284,7 @@ static int build_part(es_writer *w, const ufbx_mesh *mesh, const ufbx_mesh_part
out->indices = (uint32_t *)malloc(corners * sizeof(uint32_t));
if (out->has_normals) out->normals = (es_vec3 *)malloc(corners * sizeof(es_vec3));
if (out->has_uvs) out->uvs = (es_vec2 *)malloc(corners * sizeof(es_vec2));
+ if (out->has_uvs1) out->uvs1 = (es_vec2 *)malloc(corners * sizeof(es_vec2));
if (out->has_colors) out->colors = (es_vec4 *)malloc(corners * sizeof(es_vec4));
if (out->has_skin) {
out->joints = (es_joints *)malloc(corners * sizeof(es_joints));
@@ -287,6 +294,7 @@ static int build_part(es_writer *w, const ufbx_mesh *mesh, const ufbx_mesh_part
uint32_t *tri = (uint32_t *)malloc(tri_cap * sizeof(uint32_t));
if (!out->positions || !out->indices || !tri
|| (out->has_normals && !out->normals) || (out->has_uvs && !out->uvs)
+ || (out->has_uvs1 && !out->uvs1)
|| (out->has_colors && !out->colors)
|| (out->has_skin && (!out->joints || !out->weights))) {
free(tri);
@@ -311,6 +319,11 @@ static int build_part(es_writer *w, const ufbx_mesh *mesh, const ufbx_mesh_part
out->uvs[at].x = (float)uv.x;
out->uvs[at].y = (float)uv.y;
}
+ if (out->has_uvs1) {
+ ufbx_vec2 uv = ufbx_get_vertex_vec2(&mesh->uv_sets.data[1].vertex_uv, ix);
+ out->uvs1[at].x = (float)uv.x;
+ out->uvs1[at].y = (float)uv.y;
+ }
if (out->has_colors) {
ufbx_vec4 c = ufbx_get_vertex_vec4(&mesh->vertex_color, ix);
out->colors[at].x = (float)c.x;
@@ -331,7 +344,7 @@ static int build_part(es_writer *w, const ufbx_mesh *mesh, const ufbx_mesh_part
return 0;
}
- ufbx_vertex_stream streams[6];
+ ufbx_vertex_stream streams[7];
size_t stream_count = 0;
streams[stream_count].data = out->positions;
streams[stream_count].vertex_count = at;
@@ -346,6 +359,11 @@ static int build_part(es_writer *w, const ufbx_mesh *mesh, const ufbx_mesh_part
streams[stream_count].vertex_count = at;
streams[stream_count++].vertex_size = sizeof(es_vec2);
}
+ if (out->has_uvs1) {
+ streams[stream_count].data = out->uvs1;
+ streams[stream_count].vertex_count = at;
+ streams[stream_count++].vertex_size = sizeof(es_vec2);
+ }
if (out->has_colors) {
streams[stream_count].data = out->colors;
streams[stream_count].vertex_count = at;
@@ -562,6 +580,13 @@ static void write_meshes(es_writer *w, const ufbx_scene *scene) {
es_buf_text(&w->json, "null");
}
es_buf_text(&w->json, ",");
+ json_key(&w->json, "uvs1");
+ if (built.has_uvs1) {
+ json_slice(w, built.uvs1, built.vertex_count * sizeof(es_vec2));
+ } else {
+ es_buf_text(&w->json, "null");
+ }
+ es_buf_text(&w->json, ",");
json_key(&w->json, "colors");
if (built.has_colors) {
json_slice(w, built.colors, built.vertex_count * sizeof(es_vec4));