Hi,
I am trying to create look out of my component similar to the one from the Custom Preview Component. As you can see from my images below, my geometry is transparent is not casting shadows. Any ideas on how I should be doing it?
public override void DrawViewportMeshes(IGH_PreviewArgs args)
{
if (BuildingGeometries == null) { return; }
Material material = new Material();
material.ReflectionColor = Color.LightGray;
Rhino.Display.DisplayMaterial renderMaterial = new Rhino.Display.DisplayMaterial(material);
foreach (Brep brep in BuildingGeometries)
{
Mesh[] meshes = Mesh.CreateFromBrep(brep, MeshingParameters.Default);
foreach (Mesh mesh in meshes)
{
args.Display.DrawMeshShaded(mesh, renderMaterial);
}
}
}
If that is the case, I have been trying to extract the RenderPrimitiveList object from a RhinoObject but I am getting null values. Here is a simple snippet of me trying to get the RenderPrimitiveList. When I run this command I do have an active view with Render view on.
Nope - you just need to create a class derived from CustomMeshProvider, give it a public constructor and you should find that your BuildCustomMeshes function is called automatically.
In the case of Grasshopper meshes, you should only supply meshes when RenderPrimitiveList.RhinoObject is null.
Hello @andy, could you please post a code example of how to do that in a compiled component? I’m trying to do what you wrote here but when I create the derived class I am asked to implement the abstract methods WillBuildCustomMeshes, BuildCustomMeshes, and the Name field, and I do not know what I am supposed to write in the methods bodies (other than returning a boolean). I looked also into Miguel’s link but it’s for Rhino plug-ins. Even a basic example of a simple component that creates a sphere internally and pushes it to the render pipeline correctly would be of great help. Let’s say that I have this code:
@andy, I’m using your suggestion and it works great – with one exception. Whenever my component shares the canvas with a CustomPreview component (with Render enabled), my render-pipeline meshes disappear. They reappear when the CustomPreview Render option is turned off.
Is there something I should be doing in my class (derived from CustomMeshProvider2) or elsewhere to avoid this fate? It would be great if the components played nice together.
Hello @sarg, I tried to implement @andy 's suggestion too, but when I create the derived class from CustomMeshProvider2 I am asked to implement the abstract methods WillBuildCustomMeshes, BuildCustomMeshes, and the Name field - and I do not know what I am supposed to write in the methods bodies (other than returning a boolean).
May I ask you how did you implement the derived class? Did you have to instantiate it and call the instantiation from somewhere (SolevInstance, BeforeSolveInstance, DrawViewportMeshes, elsewhere)?
As @DavidRutten mentions here, the class derived from CustomMeshProvider2 has to be registered to a Rhino plug-in, not a GH add-on. I happen to have both, so I’m registering to the RHP during OnLoad(). Once referenced, the mesh provider is automatically called by Rhino, per @andy’s note.
As for the overrides, WillBuildCustomMeshes() determines which objects your provider is responsible for (return obj == null if you don’t want to interfere with Rhino doc objects). BuildCustomMeshes() determines the meshes and materials provided to the render pipeline (using objMeshes.Add()). Since the call isn’t made by a GH_Component instance, you need to find another way to communicate what should be rendered – e.g. by maintaining a separate singleton class with up-to-date collections of preview geom / visibility states.
This approach “works” on its own, but – at least in my implementation – appears to be in conflict with the CustomPreview component, so I can’t suggest it as a fully-fledged solution quite yet.
Thank you very much, that makes everything a lot clearer to me. Now I’m not sure if in my case it’s worth all the effort to make a Rhino plug-in just for that…