I would like to develop a plugin using RhinoCommon that can map textures from an image onto a complex mesh. Currently, I am using a grayscale image to create the mesh texture, but I’m unsure of what to do next. Can anyone help me?
protected override Result RunCommand(RhinoDoc doc, RunMode mode)
{
string imagePath = "C:\\Users\\Administrator\\Desktop\\texture.jpg";
Bitmap bitmap = new Bitmap(imagePath);
int width = bitmap.Width;
int height = bitmap.Height;
Mesh mesh = new Mesh();
for (int y = 0; y < height; y++)
{
for (int x = 0; x < width; x++)
{
Color pixelColor = bitmap.GetPixel(x, y);
int grayValue = (int)(0.3 * pixelColor.R + 0.59 * pixelColor.G + 0.11 * pixelColor.B);
Point3d point = new Point3d(x, y, grayValue/30);
mesh.Vertices.Add(point);
}
}
for (int y = 0; y < height - 1; y++)
{
for (int x = 0; x < width - 1; x++)
{
int i0 = y * width + x;
int i1 = y * width + (x + 1);
int i2 = (y + 1) * width + (x + 1);
int i3 = (y + 1) * width + x;
mesh.Faces.AddFace(i0, i1, i2, i3);
}
}
var go = new GetObject();
go.GeometryFilter = Rhino.DocObjects.ObjectType.Mesh;
go.SetCommandPrompt("Select mesh");
var res = go.Get();
if (go.ObjectCount == 0) return Result.Failure;
Mesh targetMesh = go.Object(0).Mesh();
if(targetMesh ==null) return Result.Failure;
doc.Views.Redraw();
return Result.Success;
}
this is the textrue pictrue
this is a conplex mesh geometry
this is what I want
mesh.stl (7.8 MB)