Rhino 6 Grasshopper render preview materials

unhandled

#1

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.


(Laurent Delrieu) #2

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



(David Rutten) #4

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.


#5

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?


(David Rutten) #6

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)


#7

Very pretty thank you:)


#8

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)
{
}


(David Rutten) #9

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))


#10

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)


(David Rutten) #11

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


(David Rutten) #12

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);
  }

(David Rutten) #13

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.


#14

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.