Duplicated preview on custom gh component

Hi!
I want to give custom color to a mesh or left the default color when no color param are available, but it seems the preview runs twice and I do not know why.

Check out this picture. The color inside is the default color preview of gh (green when selected), and if the user gives no color the result is the default color of GH but with less transparency (as if doubled).

I guess it is doubling the preview, but not sure. Can anyone tell me what am I doing wrong?
Thanks in advance.

This is the code involved, the parameters _col and _mesh are filled in the solve instance.

Private _mesh As Mesh = Nothing
Private _col As Drawing.Color = Nothing

Public Overrides Sub DrawViewPortMeshes(args As IGH_PreviewArgs)
  MyBase.DrawViewportMeshes(args)
  If (Hidden) Then Return
  If (Locked) Then Return
  Dim mat As New Rhino.Display.DisplayMaterial()
  If (_col = Nothing) Or (_col = Drawing.Color.Empty) Then
    If (Attributes.Selected) Then
      mat = args.ShadeMaterial_Selected
    Else
      mat = args.ShadeMaterial
    End If
  Else
    Dim t As Double = (Convert.ToDouble(_col.A) / 255) * (-1) + 1
    mat = New Rhino.Display.DisplayMaterial(_col, t)
    mat.Shine = 0.3
    mat.Emission = Drawing.Color.Black
    mat.Specular = Drawing.Color.White
  End If
  args.Display.DrawMeshShaded(_mesh, mat)
End Sub

Public Overrides Sub DrawViewportWires(args As IGH_PreviewArgs)
  MyBase.DrawViewportWires(args)
  If (Hidden) Then Return
  If (Locked) Then Return
  Dim color As New System.Drawing.Color()
  If (_col = Nothing) Or (_col = Drawing.Color.Empty) Then
    If (Grasshopper.CentralSettings.PreviewMeshEdges) Then
      If (Attributes.Selected) Then
        color = args.WireColour_Selected
      Else
        color = args.WireColour
      End If
      args.Display.DrawMeshWires(_mesh, color)
    End If
  Else
    If (Grasshopper.CentralSettings.PreviewMeshEdges) Then
      If (MyBase.Attributes.Selected) Then
        color = Drawing.Color.FromArgb(255, 255, 0)
      Else
        color = _col
      End If
      args.Display.DrawMeshWires(_mesh, color)
    End If
  End If
End Sub

Public Overrides ReadOnly Property ClippingBox As BoundingBox
  Get
    Dim box As BoundingBox = MyBase.ClippingBox
    If (_mesh IsNot Nothing) AndAlso (_mesh.IsValid) Then
      box.Union(_mesh.GetBoundingBox(True))
    End If
  Return box
  End Get
End Property

Protected Overrides Sub BeforeSolveInstance()
  MyBase.ClearData()
  _mesh = Nothing
  _col = Nothing
End Sub
1 Like

Resolved.
MyBase.DrawViewportMeshes(args) was calling the preview with the defaults values.
Removing this line the problem is corrected.
It seems that args.Display.DrawMeshShaded(_mesh, mat) also calls the preview.