Rhino 6 Grasshopper render preview materials

Hi,

I have simple question regarding materials in render preview via grasshopper (custom preview component).
How can I display breps or meshes with a reflective or shiny material?

If I just create custom material in grasshopper, it only changes color.

1 Like

I asked it for months with no luck. The best at the moment is to use a Material Name from Rhino

You can only create very basic materials through the components, back when they were written things like reflection weren’t supported in the display at all so there was no point to them.

If you want the advanced stuff, right now you have to make a material in Rhino and reference it by name.

1 Like

Just updated to current candidate release and now I see custom created materials by typing name. Yipiii!!!

Any time soon we could also see curves in render preview?

Yes, probably the next release candidate. Previews are back to the way they used to be, except for Custom Preview components. They now either draw previews in non-render viewports, or they add meshes+materials (no curves or points) to render viewports.

ps. Here’s a C# script which allows you to create metal like materials: metil_or_normil.gh (8.0 KB)

9 Likes

Very pretty thank you:)

Is it also possible to display these materials in render preview through:

//Return a BoundingBox that contains all the geometry you are about to draw?

public override BoundingBox ClippingBox
{
get
{
return BoundingBox.Empty;
}
}

//Draw all meshes in this method.
public override void DrawViewportMeshes(IGH_PreviewArgs args)
{
}

//Draw all wires and points in this method.
public override void DrawViewportWires(IGH_PreviewArgs args)
{
}

It must be, because that’s how Custom Preview works.

DisplayPipeline.DrawMeshShaded() takes a Rhino.Display.DisplayMaterial as an argument.
If you have a RenderMaterial instead of a DisplayMaterial, then you can generate the latter from the former by using the SimulateMaterial() method:

var dmat = new DisplayMaterial(RenderMaterial.SimulateMaterial(true))

It does not show up in render preview.
What I did wrong between these lines:

     private void RunScript(Mesh x, ref object A)
      {
    bbox.Union(x.GetBoundingBox(false));

    Material material = new Material();
    material.ReflectionColor = Color.Gray;
    material.DiffuseColor = Color.Gray;
    material.AmbientColor = Color.Black;
    material.EmissionColor = Color.Black;
    material.Reflectivity = 0;
    material.ReflectionGlossiness = 1;
    Rhino.Render.RenderMaterial renderMaterial = Rhino.Render.RenderMaterial.CreateBasicMaterial(material);

    Rhino.Display.DisplayMaterial m_ = new Rhino.Display.DisplayMaterial(renderMaterial.SimulateMaterial(true));
    this.m = m_;
  }

  // <Custom additional code> 
  //PreviewStuff
  Mesh mesh = new Mesh();
  BoundingBox bbox = new BoundingBox();
  Rhino.Display.DisplayMaterial m;

  //Return a BoundingBox that contains all the geometry you are about to draw.
  public override BoundingBox ClippingBox
  {
    get
    {
      return bbox;
    }
  }

  //Draw all meshes in this method.
  public override void DrawViewportMeshes(IGH_PreviewArgs args)
  {
    args.Display.DrawMeshShaded(mesh, m);
  }

Render.gh (3.0 KB)

Creating a basic material and then simulating it isn’t going to add any functionality.

Also note you never assign the mesh variable.

  private void RunScript(Mesh x, ref object A)
  {
    if (x == null) return;

    _mesh = x;
    _box = x.GetBoundingBox(false);


    Material material = new Material();
    material.ReflectionColor = Color.Gray;
    material.DiffuseColor = Color.Gray;
    material.AmbientColor = Color.Black;
    material.EmissionColor = Color.Black;
    material.Reflectivity = 1;
    material.ReflectionGlossiness = 0.5;

    // Rhino.Render.RenderMaterial renderMaterial = Rhino.Render.RenderMaterial.CreateBasicMaterial(material);

    _mat = new Rhino.Display.DisplayMaterial(material);
  }

  // <Custom additional code> 
  /// <summary>
  /// This method will be called once every solution, before any calls to RunScript.
  /// </summary>
  public override void BeforeRunScript()
  {
    _mesh = null;
    _mat = null;
    _box = BoundingBox.Empty;
  }

  Mesh _mesh;
  BoundingBox _box;
  Rhino.Display.DisplayMaterial _mat;

  public override BoundingBox ClippingBox
  {
    get { return _box; }
  }
  public override void DrawViewportMeshes(IGH_PreviewArgs args)
  {
    if (_mesh != null && _mat != null)
      args.Display.DrawMeshShaded(_mesh, _mat);
  }

Ah, it seems there’s a problem in Rendered if the 3dm file is empty. The method is called, but Rhino is not showing the box if there’s nothing else to show.

I copied and pasted the code. No errors in C# and I tried both
a) have some object in Rhino file
b) and no objects.

But in Rendered view cube is not shown (in shaded shows as usual)

I also downloaded latest candidate service release.

Hi! I am trying to make grasshopper to custom preview my geometries, but it is slow. So I wonder is it possible such that the custom preview only shows the edges of the objects? If so, any clues on how this could be achieved? Thanks!

Wow, David, thank you so much! This means that I can create a Grasshopper animation consistent with Rhino Rendered view!

Have a great day,
Jonas

Hi Jacqueline, in my experience you can significantly speed it up by converting your geometry to mesh and joining it all up into one (or few) disjoint meshes.

This is an old question but maybe someone sees this.
How can one modify this C# script in such a way that the reflection color comes from a texture color map instead of being the same everywhere? I.e. the material should have not the same reflection color everywhere, but set the reflection color according to the u-v coordinate. All other material properties (diffuse color, ambient color, emission color, reflectivity, glossiness) should stay as in the example.
I tried to call the SetTexture method on the material but it seems it will overwrite all material properties; there’s no way to set only the “reflection texture”.
Thanks.