more work on assets/packer

This commit is contained in:
Matthew 2025-11-09 17:41:26 +11:00
parent 1501b5bc07
commit 50b9ce9ee8
2 changed files with 80 additions and 12 deletions

View File

@ -88,12 +88,19 @@ struct AssetInfo
AssetType type; AssetType type;
} }
struct MeshPart
{
u64 start;
u64 length;
}
struct ModelData struct ModelData
{ {
string name; string name;
Vertex[] vtx; Vertex[] vtx;
u32[] idx; u32[] idx;
MaterialData[] mats; MaterialData[] mats;
MeshPart[] parts;
} }
struct MaterialData struct MaterialData

View File

@ -75,17 +75,11 @@ struct Texture
u32 ch; u32 ch;
} }
struct MeshGroup
{
u32 start;
u32 length;
}
union MeshIdx union MeshIdx
{ {
struct struct
{ {
u32 v, n, uv; u32 v, uv, n;
}; };
u32[3] arr; u32[3] arr;
} }
@ -95,7 +89,6 @@ struct Model
Vertex[] v; Vertex[] v;
u32[] idx; u32[] idx;
MaterialData[] mats; MaterialData[] mats;
MeshGroup[] meshes;
} }
u64 g_asset_count = 0; u64 g_asset_count = 0;
@ -469,7 +462,10 @@ OpenFile(string file_name)
Model Model
ConvertObj(string file_name) ConvertObj(string file_name)
{ {
import std.array; /*
TODO:
- Deduplicate vertices
*/
u8[] data = OpenFile(file_name); u8[] data = OpenFile(file_name);
@ -498,8 +494,6 @@ ConvertObj(string file_name)
Vec3[] positions = new Vec3[vcount]; Vec3[] positions = new Vec3[vcount];
Vec3[] normals = new Vec3[ncount]; Vec3[] normals = new Vec3[ncount];
Vec2[] uvs = new Vec2[uvcount]; Vec2[] uvs = new Vec2[uvcount];
MeshGroup[] groups = new MeshGroup[gcount];
Vertex[] vtx = new Vertex[fcount];
MeshIdx[][] idx = []; MeshIdx[][] idx = [];
MaterialData[] mtls = []; MaterialData[] mtls = [];
@ -575,6 +569,7 @@ ConvertObj(string file_name)
if(tokens[i][0] == "g" && part_idx.length > 0) if(tokens[i][0] == "g" && part_idx.length > 0)
{ {
idx ~= part_idx; idx ~= part_idx;
Logf("%s %s", idx.length, part_idx.length);
part_idx = []; part_idx = [];
continue; continue;
} }
@ -644,11 +639,77 @@ ConvertObj(string file_name)
} }
} }
assert(mtls[mtls.length-1].name == "sp_zid_vani"); u64 face_count;
foreach(part; idx)
{
face_count += part.length;
}
positions = Deduplicate(positions, idx, 0);
uvs = Deduplicate(uvs, idx, 1);
normals = Deduplicate(normals, idx, 2);
ModelData md = {
name: baseName(file_name, ".obj"),
vtx: new Vertex[face_count*3],
};
u64 vtx_count = 0;
foreach(part; idx)
{
for(u64 i = 0; i < part.length; i += 1)
{
MeshIdx* mi = part.ptr + i;
if(mi.v ) md.vtx[vtx_count].pos = positions[mi.v-1];
if(mi.n ) md.vtx[vtx_count].normal = normals[mi.n-1];
if(mi.uv) md.vtx[vtx_count].uv = uvs[mi.uv-1];
vtx_count += 1;
}
}
return model; return model;
} }
T[]
Deduplicate(T)(T[] values, MeshIdx[][] indices, u32 arr_idx)
{
u64 dup_count;
for(u64 i = 0; i < values.length; i += 1)
{
for(u64 j = i+1; j < values.length; j += 1)
{
if(values[i] == values[j])
{
dup_count += 1;
Logf("before");
values[j .. $] = values[j+1 .. values.length-1];
Logf("after");
foreach(ref part; indices)
{
for(u64 k = 0; k < part.length; k += 1)
{
if(part[k].arr[arr_idx] > j+1)
{
part[k].arr[arr_idx] -= 1;
}
if(part[k].arr[arr_idx] == j+1)
{
part[k].arr[arr_idx] = cast(u32)(i+1);
}
}
}
}
}
}
return values[0 .. values.length-dup_count];
}
string string
GetFilePath(string file_name) GetFilePath(string file_name)
{ {