Possible define an image and assign to a material?

You can create an in-memory bitmap. Here is an old sample I created:

The code for lazy users (who don’t want to click through):

using System;
using System.Runtime.InteropServices;
using Rhino.Render;
using Rhino.Geometry;
using Rhino.UI;
using Rhino.DocObjects;
using Rhino.Commands;
using Rhino;
using System.Drawing;
using Rhino.Display;

namespace Commands.Commands.Developer
{

	public static class NathanData
	{
		public static Bitmap CreateMemoryBitmap()
		{
			var width = 10;
			var height = width;
			var rnd = new Random();
			Bitmap m_testBmp = new Bitmap(width, height);
			for(var x = 0; x< width; x++)
				for(var y = 0; y< height; y++)
					m_testBmp.SetPixel(x, y, Color.FromArgb(rnd.Next(255), rnd.Next(255), rnd.Next(255)));
			return m_testBmp;
		}

		public static double Fraction(this double nr)
		{
			if(nr >= 0.0) return nr - Math.Floor(nr);
			return nr - Math.Ceiling(nr);
			
		}
		public static double Floor(this double nr)
		{
			return Math.Floor(nr);
		}

	}
	[CommandStyle(Style.Hidden)]
	public class TestNathanMemoryTexture : Command
	{
		public override string EnglishName { get { return "TestNathanMemoryTexture"; } }

		protected override Result RunCommand(RhinoDoc doc, RunMode mode)
		{
			if (Result.Success == Rhino.Input.RhinoGet.GetOneObject("Select object", false, ObjectType.Brep, out ObjRef obj))
			{
				var rtex = new MemoryBitmapTexture() { BitmapTexture = NathanData.CreateMemoryBitmap() };
				var mat = new Material();
				mat.Name = "Testing MemoryBitmapTexture";
				var rm = RenderMaterial.CreateBasicMaterial(mat);
				rm.SetChild(rtex, "bitmap-texture");
				rm.SetChildSlotOn("bitmap-texture", true, RenderContent.ChangeContexts.Ignore);
				rm.SetChildSlotAmount("bitmap-texture", 100.0, RenderContent.ChangeContexts.Ignore);
				var t = rm.GetTextureFromUsage(RenderMaterial.StandardChildSlots.Diffuse);

				rm.DocumentAssoc = doc;

				var hash1 = rm.RenderHash;

				doc.RenderMaterials.Add(rm);
				rtex.BeginChange(RenderContent.ChangeContexts.Ignore);
				rtex.SetProjectionMode(TextureProjectionMode.WcsBox, RenderContent.ChangeContexts.Ignore);
				rtex.SetRepeat(new Vector3d(1.0, 1.0, 0.0), RenderContent.ChangeContexts.Ignore);
				rtex.EndChange();

				var hash2 = rm.RenderHash;

				var o = obj.Object();

				o.RenderMaterial = rm;
				o.CommitChanges();

				return Result.Success;
			}

			return Result.Failure;
		}
	}

	/// <summary>
	/// Evaluator used by MemoryBitmapEvaluator.
	/// </summary>
	internal class MemoryBitmapEvaluator : TextureEvaluator
	{
		private static readonly object locker = new object();
		public Bitmap BitmapTexture { get; set; }
		public MemoryBitmapEvaluator(RenderTexture.TextureEvaluatorFlags evaluatorFlags) : base(evaluatorFlags)
		{
		}

		/// <summary>
		/// Get the color specified at uvw. Currently duvwdx and duvwdy aren't used (but probably should).
		/// </summary>
		/// <param name="uvw"></param>
		/// <param name="duvwdx"></param>
		/// <param name="duvwdy"></param>
		/// <returns>The color for the given uvw coordinate.</returns>
		public override Color4f GetColor(Point3d uvw, Vector3d duvwdx, Vector3d duvwdy)
		{
			Color c = Color.AliceBlue;
			lock (locker)
			{
				var x = Math.Max(0, Math.Min((int)(uvw.X.Fraction() * BitmapTexture.Width), BitmapTexture.Width));
				var y = Math.Max(0, Math.Min((int)(uvw.Y.Fraction() * BitmapTexture.Height), BitmapTexture.Height));
				c = BitmapTexture.GetPixel(x, y);
			}
			var cl = new Color4f(c);

			return cl;
		}
	}


	/// <summary>
	/// A texture that provides a way to use an in-memory Bitmap as data.
	/// </summary>
	[Guid("34D9AC50-B958-476B-82D4-50F4DA1A5511")]
	[CustomRenderContent(Category = "image-based", IsElevated = false, IsBuiltIn = true, ImageBased = true, IsPrivate = true)]
	public class MemoryBitmapTexture : RenderTexture
	{
		/// <summary>
		/// Set or get the Bitmap for this texture.
		/// </summary>
		public Bitmap BitmapTexture { get; set; }

		/// <summary>
		/// Create a new MemoryBitmapTexture instance.
		/// </summary>
		public MemoryBitmapTexture()
		{
			ModifyRenderContentStyles(RenderContentStyles.LocalTextureMapping, RenderContentStyles.TextureSummary | RenderContentStyles.ModalEditing | RenderContentStyles.GraphDisplay);

			SetProjectionMode(TextureProjectionMode.WcsBox, ChangeContexts.Ignore);

			SetRepeat(new Vector3d(1.0, 1.0, 1.0), ChangeContexts.Ignore);
			SetRotation(new Vector3d(0.0, 0.0, 0.0), ChangeContexts.Ignore);
		}

		/// <summary>
		/// Get the TextureEvaluator for this texture.
		/// </summary>
		/// <param name="evaluatorFlags">currently not used</param>
		/// <returns>A TextureEvaluator, or null if no Bitmap was set to BitmapTexture.</returns>
		public override TextureEvaluator CreateEvaluator(TextureEvaluatorFlags evaluatorFlags)
		{
			var bm = BitmapTexture;
			if (bm == null) return null;
			var eval = new MemoryBitmapEvaluator(evaluatorFlags) { BitmapTexture = bm };
			return eval;
		}

		public override string TypeName
		{
			get { return Localization.LocalizeString("MemoryBitmap texture", 1402); }
		}

		public override string TypeDescription
		{
			get { return Localization.LocalizeString("MemoryBitmap texture that is able to read from in-memory Bitmap objects", 1403); }
		}
	}

}
5 Likes