Last active
June 19, 2018 10:50
-
-
Save jesterKing/ea19aabaa62c00b4ceca7050c09d56df to your computer and use it in GitHub Desktop.
Bitmap texture evaluator, Rhino 5, C#
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections.Generic; | |
using Rhino; | |
using Rhino.Commands; | |
using Rhino.Geometry; | |
using Rhino.Input; | |
using Rhino.Input.Custom; | |
namespace MaterialTextureEvaluator | |
{ | |
[System.Runtime.InteropServices.Guid("af615033-e864-4a1e-afc8-463429426672")] | |
public class MaterialTextureEvaluatorCommand : Command | |
{ | |
public MaterialTextureEvaluatorCommand() | |
{ | |
// Rhino only creates one instance of each command class defined in a | |
// plug-in, so it is safe to store a refence in a static property. | |
Instance = this; | |
} | |
///<summary>The only instance of this command.</summary> | |
public static MaterialTextureEvaluatorCommand Instance | |
{ | |
get; private set; | |
} | |
///<returns>The command name as it appears on the Rhino command line.</returns> | |
public override string EnglishName | |
{ | |
get { return "MaterialTextureEvaluatorCommand"; } | |
} | |
protected override Result RunCommand(RhinoDoc doc, RunMode mode) | |
{ | |
int index = doc.Materials.Add(); | |
Rhino.DocObjects.Material mat = doc.Materials[index]; | |
mat.SetBitmapTexture("C:\\colgrid.png"); | |
mat.Name = "Testing"; | |
mat.DiffuseColor = System.Drawing.Color.Chocolate; | |
mat.SpecularColor = System.Drawing.Color.CadetBlue; | |
mat.CommitChanges(); | |
var rm = Rhino.Render.RenderMaterial.CreateBasicMaterial(mat); | |
if(rm == null) | |
{ | |
RhinoApp.WriteLine("no rendermaterial"); | |
return Result.Failure; | |
} | |
var diffslot = rm.TextureChildSlotName(Rhino.Render.RenderMaterial.StandardChildSlots.Diffuse); | |
var rt = rm.FindChild(diffslot) as Rhino.Render.RenderTexture; | |
using (var eval = rt.CreateEvaluator()) | |
{ | |
var zp = new Vector3d(0.0, 0.0, 0.0); | |
for (int y = 0; y < 15; y++) | |
{ | |
for (int x = 0; x < 15; x++) | |
{ | |
var p = new Point3d(x / 15.0, y / 15.0, 0.0); // create a UV coordinate | |
var c = eval.GetColor(p, zp, zp); | |
if (y < 10 && x < 10) RhinoApp.WriteLine($"{p.X},{p.Y},{p.Z} - {c.R} {c.G} {c.B}"); | |
} | |
} | |
} | |
return Result.Success; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment