Skip to content

Commit

Permalink
Add texture support to models
Browse files Browse the repository at this point in the history
  • Loading branch information
Daft-Freak committed Nov 4, 2023
1 parent 8d73385 commit 15543a7
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 6 deletions.
Binary file modified assets/cube.bin
Binary file not shown.
40 changes: 36 additions & 4 deletions model-conv.py
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,10 @@ def handle_mesh(filename, json_data, json_mesh, matrix):
if 'COLOR_0' in prim_attribs:
colour_accessor = accessors[prim_attribs['COLOR_0']]

tex_coord_accessor = None
if 'TEXCOORD_0' in prim_attribs:
tex_coord_accessor = accessors[prim_attribs['TEXCOORD_0']]

# TODO: may not exist, non-indexed data
index_accessor = accessors[primitive['indices']]

Expand All @@ -103,25 +107,53 @@ def handle_mesh(filename, json_data, json_mesh, matrix):
else:
colour_data = np.full((len(position_data), 4), [1, 1, 1, 1])

# get tex coords or all zeros
if tex_coord_accessor:
tex_coord_data = get_accessor_data(filename, json_data, tex_coord_accessor)
else:
tex_coord_data = np.full((len(position_data), 2), [0, 0])

index_data = get_accessor_data(filename, json_data, index_accessor, False)

# check if material is textures
material_id = primitive['material']
material = json_data['materials'][material_id]

# TODO: other material types?
tex_id = 0 # no tex
if 'pbrMetallicRoughness' in material and 'baseColorTexture' in material['pbrMetallicRoughness']:
tex_id = material['pbrMetallicRoughness']['baseColorTexture']['index'] + 1

# triangles
for i in index_data:
# convert data
# transform pos/normal
pos = matrix @ np.append(position_data[i], 1)
nor = rot_matrix @ normal_data[i]

pos[1] *= -1 # flip y
nor[1] *= -1

tex_coord = tex_coord_data[i]

# make tex coords positive
if tex_coord[0] < 0.0:
tex_coord[0] = 1.0 - (tex_coord[0] % 1)
if tex_coord[1] < 0.0:
tex_coord[1] = 1.0 - (tex_coord[1] % 1)

# TODO: wrap >1?

# convert data
pos = np.floor(pos * (1 << 16)).astype(int)
nor = np.floor(nor * 0x7FFF).astype(int)
col = np.floor(colour_data[i] * 0xFF).astype(int)
tex_coord = np.floor(tex_coord * 0xFFFF).astype(int)

packed_vertex = struct.pack('<3i4B3hxx',
packed_vertex = struct.pack('<3i4B3h2Hxx',
pos[0], pos[1], pos[2],
col[0], col[1], col[2], col[3],
nor[0], nor[1], nor[2])
col[0], col[1], col[2], tex_id,
nor[0], nor[1], nor[2],
tex_coord[0], tex_coord[1])

out_vertices.append(packed_vertex)

Expand Down
9 changes: 8 additions & 1 deletion model.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,14 @@ void model_lit_shader(const uint8_t *in, Render3D::VertexOutData *out, const Ren
out->g = int32_t(dot * vertex->g);
out->b = int32_t(dot * vertex->b);

out->tex_index = 0;
out->tex_index = vertex->tex_index;

if(vertex->tex_index)
{
// close enough (should be / 65535)
out->u = Fixed16<12>(Fixed32<>::from_raw(vertex->u));
out->v = Fixed16<12>(Fixed32<>::from_raw(vertex->v));
}
}

Model::Model(const uint8_t *asset_data)
Expand Down
3 changes: 2 additions & 1 deletion model.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,9 @@ class Model final
struct Vertex
{
uint32_t x, y, z;
uint8_t r, g, b, a;
uint8_t r, g, b, tex_index;
int16_t nx, ny, nz;
uint16_t u, v;
uint16_t pad;
};

Expand Down

0 comments on commit 15543a7

Please sign in to comment.