I am trying to preview textured geometry in Grasshopper, and I am having trouble displaying objects with pixel perfect texture previews for some reason.
I used the custom preview components from several plugins (ShapeDiver and Human), and they both produce the same issue. Below you can see what I mean with the image I am using (top) and the textured result (bottom) after I display it on a planar surface with dimensions matching exactly the image:
I wrote a version of this preview to try and see if I could solve the issue myself. Here is what the C# component looks like:
private void RunScript(string path, Plane plane, ref object A, ref object outBrep)
{
material = new Material();
material.SetBitmapTexture(path);
material.DiffuseColor = Color.White;
System.Drawing.Bitmap img = new Bitmap(path);
brep = Brep.CreateFromSurface(new PlaneSurface(plane, new Interval(0, img.Width), new Interval(0, img.Height)));
tm = Rhino.Render.TextureMapping.CreatePlaneMapping(plane, new Interval(0, img.Width), new Interval(0, img.Height), new Interval(0, 1));
}
// <Custom additional code>
Material material;
Brep brep;
Rhino.Render.TextureMapping tm;
//Draw all meshes in this method.
public override void DrawViewportMeshes(IGH_PreviewArgs args)
{
Mesh[] mesh = Mesh.CreateFromBrep(brep, MeshingParameters.Default);
mesh[0].SetTextureCoordinates(tm, Transform.Identity, true);
args.Display.DrawMeshShaded(mesh[0], new Rhino.Display.DisplayMaterial(material));
}
It seems to work fine, except that it has the exact same discrepancy than the two plugins mentioned above. This led me to two possible guesses:
- Obviously, resizing an image will tamper with the pixels since the resizing algorithm will interpolate the original colors to the new dimensions. I thought that maybe my surfaces were not exactly the right dimensions. However, if I bake the surface from Grasshopper and use the image as a texture directly in Rhino, I get exactly the result I want (see top image above).
- Somehow, the display pipeline in Grasshopper automatically does some sort of resizing and/or compression which I have no control over. In my script above, I tried playing with all the texture parameters without success.
I appreciate any help regarding this issue.