I have managed to read/write materials and textures. I would like to extend the functionality further. I have never understood correctly, the concept of mapping channel. Please do post an example to iterate through the render materials including the mapping channels.
Also, an example v6 file with materials with mapped channels. Are there many users using this functionality?
A texture can reference a single mapping channel. Objects have any number of mapping channels, each of which defines a way of calculating texture coordinates from either the object itself (it’s paramter space), a primitive mapping or another object.
Hi @andy,
I am trying to implement what I have understood… I am able to iterate through the materials, but unable to reference Texture mapping info …But I think I am not casting the ON_TextureMapping correctly? Please find the code below
ONx_Model model= ..
ONX_ModelComponentIterator it(model, ON_ModelComponent::Type::RenderMaterial);
const ON_ModelComponent* model_component = nullptr;
for (model_component = it.FirstComponent(); nullptr != model_component; model_component = it.NextComponent())
{
const ON_Material* material = ON_Material::Cast(model_component);
if (nullptr != material)
{
//color, transparency, shininess, etc.. working ok
//trying to get texture mappings
const int tex_count = material->m_textures.Count();
for (int i = 0; i < tex_count; i++)
{
const ON_Texture tex = material->m_textures[i];
const ON_ModelComponentReference texmap_ref = model.ComponentFromId(ON_ModelComponent::Type::TextureMapping, tex.m_texture_id);
const ON_TextureMapping* texmap = ON_TextureMapping::Cast(texmap_ref.ModelComponent());
//texmap always returns nullptr
if (nullptr != texmap)
{
switch (tex.m_type)
{
default:
break;
case ON_Texture::TYPE::bitmap_texture:
{
switch (texmap->m_type)
case ON_TextureMapping::TYPE::srfp_mapping: todo
..
..
}
..
..
}
}
}
}
Yup - you can’t do it like that. Texture mapping is on an object, so you can’t get directly from the material to the texture mapping. You first have to pick an object, and then find the material that’s on it…and the texture mappings that are on it…and match them up.
Hi @andy,
I ran into problems trying to read the attached v5 file. Upon closer inspection, I realised that the mapping_channel_id was returning a value of 0.
Read the documentation… found that
// A value of zero means no mapping is supplied
// and the texture coordinates on the mesh are
// used.
Used the condition ( ON_Texture::IsBuiltinMappingChannel(texture.m_mapping_channel_id) ) to directly map the texture to the surface UV… Results are consistent with the original mapping in the v5 file…
Is it the correct way?