This example
// Create a material with a texture bitmap
ON_Texture tex;
tex.m_filename = L"full path to your texture.jpg/bmp/...";
tex.m_bOn = true;
tex.m_type = ON_Texture::bitmap_texture;
tex.m_mode = ON_Texture::modulate_texture;
ON_Material mat;
mat.m_diffuse.SetRGB(150,0,0);
mat.m_specular.SetRGB(200,200,200);
mat.m_shine = 0.5*ON_Material::GetMaxShine()
mat.AddTexture(tex);
int mat_index = context.m_doc.m_material_table.AddMaterial(mat);
if ( mat_index < 0 )
return CRhinoCommand::failure;
// Select a mesh to modify
CRhinoGetObject go;
go.SetGeometryFilter(ON::mesh_object);
go.SetCommandPrompt(L"Select a mesh");
go.GetObjects(1,1);
if ( CRhinoCommand::success != go.CommandResult() )
return go.CommandResult();
const CRhinoMeshObject* mesh0_object =
CRhinoMeshObject::Cast(go.Object(0).Object());
if ( 0 == mesh0_object )
return CRhinoCommand::failure;
const ON_Mesh* mesh0 = mesh0_object->Mesh();
if ( 0 == mesh0 )
return CRhinoCommand::failure;
// Copy the mesh and set its texture coordinates
ON_Mesh* mesh1 = new OnMesh(mesh0);
ON_BoundingBox bbox = mesh1->BoundingBox();
ON_Interval x_extents(bbox.m_min.x,bbox.m_max.x);
ON_Interval y_extents(bbox.m_min.y,bbox.m_max.y);
const int vertex_count = mesh1->m_V.Count();
mesh1->m_T.Reserve(vertex_count);
mesh1->m_T.SetCount(0);
for ( int vi = 0; vi < vertex_count; vi++ )
{
const ON_3dPoint& V = mesh->m_V[vi];
ON_2fPoint& tc = mesh1->m_T.AppendNew();
tc.x = (float)x_extents.NormalizedParameterAt(V.x);
tc.y = (float)y_extents.NormalizedParameterAt(V.y);
}
// Update the mesh
CRhinoMeshObject* mesh1_object = new CRhinoMeshObject();
mesh1_object.SetMesh(mesh1);
context.m_doc.ReplaceObject(CRhinoObjRef(mesh0_object),mesh1_object);
ON_3dmObjectAttributes att = mesh1_object->Attributes();
att.m_material_index;
att.SetMaterialSource( ON::material_from_object );
context.m_doc.Redraw();
VB.NET (Rhino 4)
Public Overrides Function RunCommand(ByVal context As IRhinoCommandContext) _
As IRhinoCommand.result
' Create a material with a texture bitmap
Dim tex As New OnTexture()
tex.m_filename = "full path to your texture.jpg/bmp/..."
tex.m_bOn = True
tex.m_type = IOnTexture.TYPE.bitmap_texture
tex.m_mode = IOnTexture.MODE.modulate_texture
Dim mat As New OnMaterial()
mat.m_diffuse.SetRGB(150, 0, 0)
mat.m_specular.SetRGB(200, 200, 200)
mat.m_shine = 0.5 * OnMaterial.MaxShine()
mat.AddTexture(tex)
Dim mat_index As Integer = context.m_doc.m_material_table.AddMaterial(mat)
If (mat_index < 0) Then Return IRhinoCommand.result.failure
' Select a mesh to modify
Dim go As New MRhinoGetObject()
go.SetGeometryFilter(IRhinoGetObject.GEOMETRY_TYPE_FILTER.mesh_object)
go.SetCommandPrompt("Select a mesh")
go.GetObjects(1, 1)
If (go.CommandResult() <> IRhinoCommand.result.success) Then
Return go.CommandResult()
End If
Dim mesh0_object As IRhinoMeshObject = _
MRhinoMeshObject.ConstCast(go.Object(0).Object())
If (mesh0_object Is Nothing) Then Return IRhinoCommand.result.failure
Dim mesh0 As IOnMesh = mesh0_object.Mesh()
If (mesh0 Is Nothing) Then Return IRhinoCommand.result.failure
' Copy the mesh and set its texture coordinates
Dim mesh1 As New OnMesh(mesh0)
Dim bbox As OnBoundingBox = mesh1.BoundingBox()
Dim x_extents As New OnInterval(bbox.m_min.x, bbox.m_max.x)
Dim y_extents As New OnInterval(bbox.m_min.y, bbox.m_max.y)
Dim vertex_count As Integer = mesh1.m_V.Count()
mesh1.m_T.Reserve(vertex_count)
mesh1.m_T.SetCount(0)
For vi As Integer = 0 To vertex_count - 1
Dim V As IOn3fPoint = mesh1.m_V(vi)
Dim tc As New On2fPoint()
tc.x = CType(x_extents.NormalizedParameterAt(V.x), Single)
tc.y = CType(y_extents.NormalizedParameterAt(V.y), Single)
mesh1.m_T.Append(tc)
Next
' Update the mesh
Dim mesh1_object As New MRhinoMeshObject()
mesh1_object.SetMesh(mesh1)
context.m_doc.ReplaceObject(New MRhinoObjRef(mesh0_object), mesh1_object)
Dim att As New On3dmObjectAttributes(mesh1_object.Attributes())
att.m_material_index = mat_index
att.SetMaterialSource(IOn.object_material_source.material_from_object)
mesh1_object.ModifyAttributes(att)
context.m_doc.Redraw()
Return IRhinoCommand.result.success
End Function
C# (Rhino 4)
public override IRhinoCommand.result RunCommand(IRhinoCommandContext context)
{
// Create a material with a texture bitmap
OnTexture tex = new OnTexture();
tex.m_filename = "full path to your texture.jpg/bmp/...";
tex.m_bOn = true;
tex.m_type = IOnTexture.TYPE.bitmap_texture;
tex.m_mode = IOnTexture.MODE.modulate_texture;
OnMaterial mat = new OnMaterial();
mat.m_diffuse.SetRGB(150,0,0);
mat.m_specular.SetRGB(200,200,200);
mat.m_shine = 0.5*OnMaterial.MaxShine();
mat.AddTexture(tex);
int mat_index = context.m_doc.m_material_table.AddMaterial(mat);
if ( mat_index < 0 )
return IRhinoCommand.result.failure;
// Select a mesh to modify
MRhinoGetObject go = new MRhinoGetObject();
go.SetGeometryFilter( IRhinoGetObject.GEOMETRY_TYPE_FILTER.mesh_object );
go.SetCommandPrompt("Select a mesh");
go.GetObjects(1,1);
if( go.CommandResult() != IRhinoCommand.result.success )
return go.CommandResult();
IRhinoMeshObject mesh0_object = MRhinoMeshObject.ConstCast( go.Object(0).Object() );
if( mesh0_object==null )
return IRhinoCommand.result.failure;
IOnMesh mesh0 = mesh0_object.Mesh();
if ( mesh0==null )
return IRhinoCommand.result.failure;
// Copy the mesh and set its texture coordinates
OnMesh mesh1 = new OnMesh(mesh0);
OnBoundingBox bbox = mesh1.BoundingBox();
OnInterval x_extents = new OnInterval(bbox.m_min.x,bbox.m_max.x);
OnInterval y_extents = new OnInterval(bbox.m_min.y,bbox.m_max.y);
int vertex_count = mesh1.m_V.Count();
mesh1.m_T.Reserve(vertex_count);
mesh1.m_T.SetCount(0);
for( int vi=0; vi<vertex_count; vi++ )
{
IOn3fPoint V = mesh1.m_V[vi];
On2fPoint tc = new On2fPoint();
tc.x = (float)x_extents.NormalizedParameterAt(V.x);
tc.y = (float)y_extents.NormalizedParameterAt(V.y);
mesh1.m_T.Append(tc);
}
// Update the mesh
MRhinoMeshObject mesh1_object = new MRhinoMeshObject();
mesh1_object.SetMesh(mesh1);
context.m_doc.ReplaceObject(new MRhinoObjRef(mesh0_object),mesh1_object);
On3dmObjectAttributes att = new On3dmObjectAttributes(mesh1_object.Attributes());
att.m_material_index = mat_index;
att.SetMaterialSource(IOn.object_material_source.material_from_object);
mesh1_object.ModifyAttributes(att);
context.m_doc.Redraw();
return IRhinoCommand.result.success;
}