The PhysicallyBasedMaterial class doesn’t have a constructor and if I create a normal Material and call Default() and then ToPhysicallyBased() it also doesn’t seem to do anything:
Remind me in which language you are using rhino3dm?
I recently wrapped toPhysicallyBased() for js and py. I don’t recall when it was added for dotnet. In js you would do something like:
Rhino.FileIO.File3dm file3dm = new Rhino.FileIO.File3dm();
var material = new Rhino.DocObjects.Material();
material.ToPhysicallyBased();
material.PhysicallyBased.BaseColor = new Rhino.Display.Color4f(1, 0, 0, 0);
material.PhysicallyBased.Metallic = 0.7;
material.PhysicallyBased.Roughness = 0.5;
file3dm.AllMaterials.Add(material);
var oa = new Rhino.DocObjects.ObjectAttributes
{
MaterialIndex = 0,
MaterialSource = Rhino.DocObjects.ObjectMaterialSource.MaterialFromObject
};
var sphere = new Rhino.Geometry.Sphere(Rhino.Geometry.Point3d.Origin, 10);
file3dm.Objects.AddSphere(sphere, oa);
file3dm.Write("fileWithMaterial.3dm", null);
Just returning to answer my own question for anyone else wondering after tinkering all day, merging bits of this post with this (some bits of that post seem no longer relevant? or maybe just for python):
using Rhino;
using Rhino.Render;
RhinoDoc doc = RhinoDoc.ActiveDoc;
// Create new Rhino material and convert to PBR
var rMaterial = new Rhino.DocObjects.Material();
rMaterial.ToPhysicallyBased();
// Create simulated texture from image path
SimulatedTexture simtex = new SimulatedTexture(doc);
simtex.Filename = “C:\Users\tsrch\Documents\test.jpg”;
// Assign texture to base color
rMaterial.PhysicallyBased.SetTexture(simtex.Texture(), Rhino.DocObjects.TextureType.PBR_BaseColor);
// Convert to RenderMaterial and add to document, this allows the material to show in the material editor
// without being assigned to any objects
var pbr = RenderMaterial.FromMaterial(rMaterial, doc);
pbr.Name = “testo”;
doc.RenderMaterials.Add(pbr);