Wish list: Expose CRhinoDib and GetDibAsTextureFileReference in C#


I need to draw textures from memory in a display conduit in Rhinocommon, like in this thread. I’d like to ask whether a particular set of methods from C++ SDK can be exposed in Rhinocommon to make this happen.

Here there’s a simple sample drawing a texture from memory on a sphere inside a conduit:
cmdSampleShadingDibTexture.cpp (4.8 KB)
I would like to have the Rhinocommon equivalent of it.

The following elements are crucial:

  1. The multi-threaded pixel shader that evaluates the texture, which in C++ is carried by CRhinoDib and
 CRhinoDib::ProcessPixels(bool(*f)(Pixel&, void* pvData), void* pvData = nullptr);

It’s important that some data may be passed through void* member.
An example:

const int width = 1000, height = 1000, depth = 32;
CRhinoDib dib(width, height, depth);

auto func = [](CRhinoDib::Pixel& pixel, void*)
	pixel.Set(rand() & 255, rand() & 255, rand() & 255, 255);
	return true;

  1. The method that transfers CRhinoDib into ON_Texture (its field ON_Reference m_image_file_reference) through
ON_FileReference RhinoGetDibAsTextureFileReference(const CRhinoDib& dib, ON__UINT32 crc);
  1. The method to pass ON_Texture into ON_Material
ON_Material::AddTexture(const ON_Texture& tx);

I believe point 3 is already in C# but couldn’t find points 1/2. From what @stevebaer said in the linked thread I know that maybe this is not yet exposed or developed at this level, in which case I add my vote for it to be there at some point.

Question for @nathanletwory: I was close to translate my sample to C# using your TestNathanMemoryTexture, but I was stuck when having to dynamically update the texture in a conduit. It seems like once the texture is created it can’t be further changed unless the Guid is modified, but this is tricky when it needs to be done very often. Maybe there’s a way to fix this and make your code run inside a conduit?
Other than that, it looks very promising, for example I can see that TextureEvaluator.GetColor(Point3d uvw, Vector3d duvwdx, Vector3d duvwdy) is similar to the shader function I show in point 1 and is multi-threaded. Is it possible to pass in some custom user data as well?

Thank you,

Hi @Pablo_Garcia-Amorena,

You should be able to write your own equivalent to CRhinoDib::ProcessPixels by just using Bitmap.LockBits and an parallel for loop, which is all CRhinoDib does.


I’ve added a wish for exposing RhinoGetDibAsTextureFileReference.

– Dale

Thank you @dale , I’ll use Bitmap through the link you sent.

@andy please take a look at exposing RhinoGetDibAsTextureFileReference to C#, maybe it is straightforward? This is such a powerful feature in the C++ SDK and the only element preventing our plug-in from running in Rhino for Mac.