Custom Preview - performance issues

Hi all
I’m developing a plug-in component in Visual Studio to ‘voxelize’ a random shape.
(i) First component generates a dataTree (or list) of points.
(ii) Second component makes ‘Center Boxes’.

I would like to add a custom preview to this second component, which will have different colors in a later stage… But I can’t seem to find the best method for this.

I’m testing the performances of different strategies in C# in GH; but I can’t seem to find methods that are as fast a the existing ‘Custom Preview’ component of GH. :frowning:.

Using the standard display method of GH is very fast. But I don’t like the transparency.
I’ve used ‘Preview Override’ (see code below), but it has a duration of 72 ms compared to 19 ms. How can I speed up this process? Any ideas anyone? I really need it to be as fast as possible. Voxel settings are still very coarse now.

Thanks! -Gieljan

My code

using System.Drawing;

private void RunScript(Box voxels)
    _materials.Add(new Rhino.Display.DisplayMaterial(System.Drawing.Color.Black, System.Drawing.Color.White, System.Drawing.Color.White, System.Drawing.Color.Coral, 0.0, 0.0));

private readonly List<Box> _boxes = new List<Box>();
private readonly List<Rhino.Display.DisplayMaterial> _materials = new List<Rhino.Display.DisplayMaterial>();
private BoundingBox _box;

public override void BeforeRunScript()
    _box = BoundingBox.Empty;

public override BoundingBox ClippingBox
    get { return _box;}

public override void DrawViewportMeshes(IGH_PreviewArgs args)
    if (_boxes.Count == 0)
 for (int i = 0; i < _boxes.Count; i++)
      args.Display.DrawMeshShaded(Mesh.CreateFromBox(_boxes[i], 1, 1, 1), new Rhino.Display.DisplayMaterial(_materials[i]));

public override void DrawViewportWires(IGH_PreviewArgs args)
    if (_boxes.Count == 0)
    for (int i = 0; i < _boxes.Count; i++)
      args.Display.DrawBox(_boxes[i], System.Drawing.Color.Black);

I’ve made a small movie of my problem as well.
This is to illustrate the drop in performances.
The standard custom component of GH runs very smooth, while my own script is running quite slow.
Maybe the speed of the component is not the problem, but the speed of the display override. i’m not sure.
Please help me :slight_smile: (27.7 KB)

You’re recreating a brand new mesh from each box every single frame of draw. Save a list of Meshes instead of a list of boxes and create the meshes in runscript, it should be much faster.

1 Like

I can’t thank you enough! Thanks for sharing the code!
It works and it is quite fast! :slight_smile: