I am reading a .obj and .mtl file and trying to add materials to a mesh which I import using some C++ code. The .mtl file has entries like:
newmtl material_2
Ka 0.0000 0.0000 0.0000
Kd 1.0000 0.0000 0.0000
Ks 1.0000 1.0000 1.0000
Tf 0.0000 0.0000 0.0000
d 1.0000
Ns 127.5
In my C++ code for the material definition part I tried:
// Create attributes for mesh.
ON_3dmObjectAttributes attribs;
// Assign default values.
pDoc->GetDefaultObjectAttributes(attribs);
// Create new material.
ON_Material mat;
// Converted material_2 string to wchar_t* namew to name material.
mat.SetName(namew);
// Parsed Ka 3 floats, converted to 0-255 range stored in rgb[3] and then set ambient color.
mat.SetAmbient(ON_Color(rgb[0], rgb[1], rgb[2]));
// Parsed Kd 3 floats, converted to 0-255 range stored in rgb[3] list and then set diffuse color.
mat.SetDiffuse(ON_Color(rgb[0], rgb[1], rgb[2]));
// Parsed Ks 3 floats, converted to 0-255 range stored in rgb[3] list and then set specular color.
mat.SetSpecular(ON_Color(rgb[0], rgb[1], rgb[2]));
// Get material index for new material.
const int32_t matIndex = pDoc->m_material_table.AddMaterial(mat);
// Add Material index.
attribs.m_material_index = matIndex;
// Set color source to be from material. THIS GOT IT WORKING.
attribs.SetColorSource(ON::color_from_material);
// Set material source to be from object.
attribs.SetMaterialSource(ON::material_from_object);
// Add mesh with attribs to Document.
CRhinoMeshObject* meshObject = new CRhinoMeshObject(attribs);
pDoc->AddObject(meshObject
I did not know what to do with Ns.
When I look at the mesh I see:
which shows that the name and color showed up fine in material_2 definition.
But when I look at the mesh in Rendered view all I see is black:
instead of red that was in the original mesh which was exported to .obj file:
Here are material_2 details for the original mesh:
It has Gloss set to 50% and Reflection set to 5% which is different but not enough to control whether the diffuse color is shown.
Curiously, for the original mesh, the Textures Bump setting is 100% whereas in my imported version it appears as 30%. How is this even possible?
Obviously something is missing.
I need help with handling Ns from the .mtl file. Will adding this fix the problem?
Here is the .3dm file of the imported mesh:
vertex_colors materials not showing up.3dm (4.1 MB)
Here is the .mtl file (copied flat as .mtl extension not allowed):
# Rhino
newmtl material_2
Ka 0.0000 0.0000 0.0000
Kd 1.0000 0.0000 0.0000
Ks 1.0000 1.0000 1.0000
Tf 0.0000 0.0000 0.0000
d 1.0000
Ns 127.5000
newmtl Default
Ka 0.0000 0.0000 0.0000
Kd 0.9804 0.9804 0.9804
Ks 1.0000 1.0000 1.0000
Tf 0.0000 0.0000 0.0000
d 1.0000
Ns 0.0000
newmtl material_1
Ka 0.0000 0.0000 0.0000
Kd 0.5451 0.3529 0.0000
Ks 1.0000 1.0000 1.0000
Tf 0.0000 0.0000 0.0000
d 1.0000
Ns 38.2500
newmtl material_3
Ka 0.0000 0.0000 0.0000
Kd 1.0000 0.4980 0.0000
Ks 1.0000 1.0000 1.0000
Tf 0.0000 0.0000 0.0000
d 1.0000
Ns 0.0000
Here is the .obj file:
vertex_colors no texture.obj (5.6 MB)
Regards,
Terry.