collada/header: reference node transforms by index rather than by type

This commit is contained in:
Zack Buhman 2026-01-26 21:19:53 -06:00
parent f939e70bf9
commit b052718d7e
3 changed files with 27 additions and 20 deletions

View File

@ -549,15 +549,20 @@ target_attributes = {
"A", "ANGLE", "B", "G", "P", "Q", "R", "S", "T", "TIME", "U", "V", "W", "X", "Y", "Z" "A", "ANGLE", "B", "G", "P", "Q", "R", "S", "T", "TIME", "U", "V", "W", "X", "Y", "Z"
} }
def render_transform_type(transformation_element): def find_transform_index_in_node(node, transform):
return { for i, node_transform in enumerate(node.transformation_elements):
types.Lookat: "LOOKAT", if node_transform is transform:
types.Matrix: "MATRIX", return i
types.Rotate: "ROTATE", assert False, (node, transform)
types.Scale: "SCALE",
types.Skew: "SKEW", def transform_sid_lookup(node, sid):
types.Translate: "TRANSLATE", transform_types = {
}[type(transformation_element)] types.Lookat, types.Matrix, types.Rotate,
types.Scale, types.Skew, types.Translate
}
transform = node.sid_lookup[sid]
assert type(transform) in transform_types, transform
return transform
def render_channel(state, collada, channel): def render_channel(state, collada, channel):
sampler = collada.lookup(channel.source, types.Sampler) sampler = collada.lookup(channel.source, types.Sampler)
@ -574,7 +579,9 @@ def render_channel(state, collada, channel):
node = collada.lookup(f"#{node_id}", types.Node) node = collada.lookup(f"#{node_id}", types.Node)
node_name_id = get_node_name_id(node) node_name_id = get_node_name_id(node)
node_name = sanitize_name(state, node_name_id, node) node_name = sanitize_name(state, node_name_id, node)
transformation_element = node.sid_lookup[node_transform_sid]
transform = transform_sid_lookup(node, node_transform_sid)
transform_index = find_transform_index_in_node(node, transform)
target_name = sanitize_name(state, channel.target, channel, allow_slash=True) target_name = sanitize_name(state, channel.target, channel, allow_slash=True)
assert target_name not in state.node_animation_channels[node.id] assert target_name not in state.node_animation_channels[node.id]
@ -582,7 +589,7 @@ def render_channel(state, collada, channel):
yield f"channel const node_channel_{target_name} = {{" yield f"channel const node_channel_{target_name} = {{"
yield f".source_sampler = &sampler_{sampler_name}," yield f".source_sampler = &sampler_{sampler_name},"
yield f".target_transform_type = transform_type::{render_transform_type(transformation_element)}," yield f".target_transform_index = {transform_index},"
yield f".target_attribute = target_attribute::{target_attribute}," yield f".target_attribute = target_attribute::{target_attribute},"
yield "};" yield "};"

View File

@ -257,7 +257,7 @@ namespace collada {
struct channel { struct channel {
sampler const * const source_sampler; sampler const * const source_sampler;
int const target_node_index; // an index into the nodes array int const target_node_index; // an index into the nodes array
transform_type const target_transform_type; int const target_transform_index;
target_attribute const target_attribute; target_attribute const target_attribute;
}; };

View File

@ -132,13 +132,13 @@ sampler const sampler_node_cube_translation_y_sampler = {
channel const node_channel_node_cube_translation_x = { channel const node_channel_node_cube_translation_x = {
.source_sampler = &sampler_node_cube_translation_x_sampler, .source_sampler = &sampler_node_cube_translation_x_sampler,
.target_transform_type = transform_type::TRANSLATE, .target_transform_index = 0,
.target_attribute = target_attribute::X, .target_attribute = target_attribute::X,
}; };
channel const node_channel_node_cube_translation_y = { channel const node_channel_node_cube_translation_y = {
.source_sampler = &sampler_node_cube_translation_y_sampler, .source_sampler = &sampler_node_cube_translation_y_sampler,
.target_transform_type = transform_type::TRANSLATE, .target_transform_index = 0,
.target_attribute = target_attribute::Y, .target_attribute = target_attribute::Y,
}; };
@ -543,7 +543,7 @@ node const * const node_children_node_cube = {
transform const transforms_node_cube[] = { transform const transforms_node_cube[] = {
{ {
.type = transform_type::TRANSLATE, .type = transform_type::TRANSLATE,
.translate = {10.0f, 0.0f, 0.0f}, .translate = {10.0f, -1.14258e-07f, 0.0f},
}, },
}; };
@ -552,6 +552,10 @@ instance_material const instance_materials_node_cube_0[] = {
.element_index = 1, // an index into mesh.triangles .element_index = 1, // an index into mesh.triangles
.material = &material_material__15_material, .material = &material_material__15_material,
}, },
{
.element_index = 0, // an index into mesh.triangles
.material = &material_material__16_material,
},
{ {
.element_index = 5, // an index into mesh.triangles .element_index = 5, // an index into mesh.triangles
.material = &material_material__17_material, .material = &material_material__17_material,
@ -564,10 +568,6 @@ instance_material const instance_materials_node_cube_0[] = {
.element_index = 2, // an index into mesh.triangles .element_index = 2, // an index into mesh.triangles
.material = &material_material__19_material, .material = &material_material__19_material,
}, },
{
.element_index = 0, // an index into mesh.triangles
.material = &material_material__16_material,
},
{ {
.element_index = 4, // an index into mesh.triangles .element_index = 4, // an index into mesh.triangles
.material = &material_material__20_material, .material = &material_material__20_material,
@ -583,8 +583,8 @@ instance_geometry const instance_geometries_node_cube[] = {
}; };
channel const * const node_channels_node_cube[] = { channel const * const node_channels_node_cube[] = {
&node_channel_node_cube_translation_x,
&node_channel_node_cube_translation_y, &node_channel_node_cube_translation_y,
&node_channel_node_cube_translation_x,
}; };
node const node_node_cube = { node const node_node_cube = {