Color in Custom Preview component

Hi everyone.

i’m writing a component in C# which overwrites DrawViewportMeshes, and i want to replicate the color that is displayed when using the Custom Preview, but i’m not getting it. As it may be seen in this image, the colors don’t match exactly (left is rendered with Custom Preview component, right with my personal code in DrawViewportMeshes), although the same color input is used in both cases.

Here is a very simplified definition in order to show what is happening.

CustomPreviewTest.gh (11.7 KB)

What i suppose is that Custom Preview creates a material with some specific properties (ambient, diffuse, emission, specular, …), and perhaps someone knows what this properties are.

What i’m using is the same color for all of them, as it may be seen in this image and in the c# script component in the model attached.

By the way, I don’t know how the transparency is implemented, because, when playing with the alpha channel in the color swatch component, strange results happen. See video.

Could anyone give me a hint?

Thanks in advance!!!

Priority order.
Try to copy-paste and delete the original of the preview component, or do the same with the c# component.
Which of the 2 is the last make difference.

Thank you for your message.

But, sorry, i have checked what you say, and nothing different happens. Both rectangles do not overlap. So, I don`t know how the priority will make any difference.

Or perhaps I have not understood you correctly. Could you send me a video or any file to verify your solution?

Yeah, sorry, you are right this is a different problem.

Grasshopper Preview component actually need a “Shader” not just a color.
There is a conversion going on.
To better see what is happening, internalize the value into a shader parameter and right click > “Manage shader collection”:

So you can fix you code:

  • alpha and transparency are the opposite, maximum transparency is zero alpha, and vice-versa
  • specular is always white
  • shine is always 0.5
  • emission channels is 76/255 of diffuse color (i don’t know the exact math)
    public override void DrawViewportMeshes(IGH_PreviewArgs args)
    {
        double k = 76.0/255.0;
        DisplayMaterial material = new DisplayMaterial()
        {
            Diffuse = color,
            Emission = Color.FromArgb(255,(int)(k*color.R),(int)(k*color.G),(int)(k*color.B)),            
            Specular = Color.White,
            Transparency = 1 - color.A / 255.0,
            Shine = 0.5,
            IsTwoSided = false
        };
        args.Display.DrawMeshShaded(surfaceMesh, material);   
    }

You don’t have to assign color to mesh vertices color, that is not what happens in normal Preview component…

Thank you very much!

Definitively, I was mistaken with the transparency and alpha channel.

And your are right. With your definition of the shader/material/color I can get the same color, except some transparency is set. If no transparency, the colors match exactly. If any transparency is set, the colors do not match. So, there is still some other parameters used…

In both cases, I have discarded the vertex color assignment. Although in my implementation, I need to set the color when creating the mesh. But that is another problem, and has nothing to do with the transparency issue.

Any ideas?