How to start and operate the Displacement?

help me please, understand how to manage and create Displacement mesh.
how to implement and manage Resample Texture Settings in Displacement ?
I can’t create a deformed mesh.

using System;
using System.Collections.Generic;
using System.Drawing;
using System.Windows.Forms;
using Grasshopper.Kernel;
using Rhino;
using Rhino.Display;
using Rhino.DocObjects;
using Rhino.Geometry;
using Rhino.Render;

namespace GG_Render_Tools
{
    public class GG_MeshDisplacement : GH_Component
    {
        public GG_MeshDisplacement() : base("MeshDisplacement",
            "MeshDisplacement",
            "Get Render Meshes",
             "GGold", "Render")
        {
        }
        protected override void RegisterInputParams(GH_Component.GH_InputParamManager pManager)
        {
            pManager.AddTextParameter("Path", "P", "Path to Picture", GH_ParamAccess.item);
            pManager.AddGenericParameter("Guid", "G", "The Object Guid", GH_ParamAccess.item);
            pManager.AddBooleanParameter("Get", "G", "Get Render Meshes", GH_ParamAccess.item, false);
            pManager.AddBoxParameter("Box", "Box", "Box", GH_ParamAccess.item);
        }

        protected override void RegisterOutputParams(GH_Component.GH_OutputParamManager pManager)
        {
            pManager.AddMeshParameter("Mesh", "M", "Render Meshes", GH_ParamAccess.item);
            pManager.AddGenericParameter("RenderTexture", "G", "The Object Guid", GH_ParamAccess.item);
            pManager.AddGenericParameter("TextureMapping", "G", "The Object Guid", GH_ParamAccess.item);
        }


        protected override void SolveInstance(IGH_DataAccess DA)
        {
            try
            {
                Box unset = Box.Unset;
                string text = "";
                Guid guid = new Guid();
                bool add = false;
                List<RhinoObject> list = new List<RhinoObject>();

                if (!DA.GetData(0, ref text)) return;
                if (!DA.GetData(1, ref guid)) return;
                DA.GetData(2, ref add);
                if (!DA.GetData(3, ref unset)) return;

                RhinoObject objj = Rhino.RhinoDoc.ActiveDoc.Objects.FindId(guid);
                if (objj != null)
                    list.Add(objj);

                /*
                if (objj != null)
                {
                    // Get render meshes from the object

                    var renderMeshes = objj.GetMeshes(Rhino.Geometry.MeshType.Render);
                    if (renderMeshes != null && renderMeshes.Length > 0 && renderMeshes[0] != null)
                    {
                        // Create mapping mesh from render meshes

                        Mesh mappingMesh = new Mesh();
                        foreach (var renderMesh in renderMeshes)
                        {
                            if (renderMesh != null)
                            {
                                mappingMesh.Append(renderMesh);
                            }
                        }
                        for (int vi = 0; vi < mappingMesh.Vertices.Count; vi++)
                        {
                           // Point2f tc = new Point2f(mappingMesh.Vertices[vi].X, mappingMesh.Vertices[vi].Y);
                           // mappingMesh.TextureCoordinates.SetTextureCoordinate(vi, tc);
                        }

                        // Create custom mesh texture mapping

                      //  TextureMapping textureMapping = TextureMapping.CreateCustomMeshMapping(mappingMesh);
              
                        // Apply mapping to object

                       // objj.SetTextureMapping(1, textureMapping);
                    }
                }
                */
                Texture texture = new Texture();
                texture.FileName = text;
                texture.TextureType = TextureType.Bitmap;
         
                var doc = RhinoDoc.ActiveDoc;
                int index = doc.Materials.Add();
                Rhino.DocObjects.Material mat = doc.Materials[index];
                mat.SetBitmapTexture(texture);
               // mat.SetBumpTexture(text);
                mat.Name = "Testing";
                mat.CommitChanges();


                objj.CreateMeshes(MeshType.Render, MeshingParameters.QualityRenderMesh, false);
                Rhino.DocObjects.ObjectAttributes attr = objj.Attributes.Duplicate();
                attr.MaterialIndex = index;
                attr.MaterialSource = Rhino.DocObjects.ObjectMaterialSource.MaterialFromObject;
                objj.Attributes = attr;
                objj.CommitChanges();
                doc.Views.Redraw();

                // RenderMaterial rm = Rhino.Render.RenderMaterial.CreateBasicMaterial(mat);
                // var diffslot = rm.TextureChildSlotName(Rhino.Render.RenderMaterial.StandardChildSlots.Diffuse);
                // var s1 = rm.FindChild(diffslot) as Rhino.Render.RenderTexture;
                //  hino.Render.RenderTexture s1 = rm.GetTextureFromUsage(Rhino.Render.RenderMaterial.StandardChildSlots.Diffuse);
                Rhino.DocObjects.Material matt = RhinoDoc.ActiveDoc.Materials[objj.Attributes.MaterialIndex];
             
                Rhino.Render.RenderTexture s1 = matt.RenderMaterial.GetTextureFromUsage(Rhino.Render.RenderMaterial.StandardChildSlots.Diffuse);
                Rhino.Render.TextureMapping s2 = TextureMapping.CreateBoxMapping(unset.Plane, unset.X, unset.Y, unset.Z, true);
                s2.Name = "name";

                if (s1 != null || s2 != null)
                {
                   objj.SetTextureMapping(1, s2);
                   // objj.CommitChanges();

                   // mm.TextureCoordinates.SetTextureCoordinates(s2);
                   // mm.SetTextureCoordinates(s2, tfm, false);
                   // mm.SetCachedTextureCoordinates(s2, ref tfm);
                }

                //  s1 = matt.RenderMaterial.GetTextureFromUsage(Rhino.Render.RenderMaterial.StandardChildSlots.Diffuse);
                //  var rt = Rhino.Render.RenderTexture.NewBitmapTexture(simulated, Rhino.RhinoDoc.ActiveDoc);
                //  var t = s1.SimulatedTexture(RenderTexture.TextureGeneration.Allow).Texture();
     
                Rhino.Geometry.MeshDisplacementInfo k = new Rhino.Geometry.MeshDisplacementInfo(s1, s2);
                  k.ChannelNumber = 1;
              //  k.PostWeldAngle = 40;
              //  k.RefineSensitivity = 0.5;
               // k.SweepPitch = 0.5;
               // k.White = 0.1;
               // k.WhiteMove = 0.1;
              // k.Black = 0.5;
               k.BlackMove = 0.5;
               k.FaceLimit = 1000;

                Mesh Rendermesh = new Mesh();
                Rhino.DocObjects.ObjRef[] meshObjRefs = Rhino.DocObjects.RhinoObject.GetRenderMeshes(list, true, false);
                if (null != meshObjRefs)
                {
                    for (int i = 0; i < meshObjRefs.Length; i++)
                    {
                        Rhino.DocObjects.ObjRef meshObjRef = meshObjRefs[i];
                        if (null != meshObjRef)
                        {
                            Rhino.Geometry.Mesh mesh = meshObjRef.Mesh();
                            Rendermesh.Append(mesh);
                        }
                    }
                }
       
                DA.SetData(0, Rendermesh);
                DA.SetData(1, s1);
                DA.SetData(2, s2);

                if (add)
                {
                    if (null != Rendermesh)
                    {
                        RhinoDoc.ActiveDoc.Objects.AddMesh(Rendermesh);

                        RhinoDoc.ActiveDoc.Views.Redraw();
                    }
                }
               

            }
            catch (Exception ex)
            {
                MessageBox.Show($"An error: {ex.Message}", "Error", MessageBoxButtons.OK);
            }
        }

        public override GH_Exposure Exposure
        {
            get
            {
                return GH_Exposure.primary;
            }
        }
        protected override Bitmap Icon
        {
            get
            {
                return null;
            }
        }

        public override Guid ComponentGuid
        {
            get
            {
                return new Guid("{87867d19-568f-4388-95e2-9f7adf731c94}");
            }
        }
    }
}

You need to use Rhino.Geometry.Mesh.WithDisplacement(MeshDisplacementInfo)

1 Like
 Mesh meshArray = new Mesh();

var renderMeshes = rhinoObject.GetMeshes(MeshType.Render);
            foreach (Mesh mesh in renderMeshes)


            {
                Rhino.Render.TextureMapping map = TextureMapping.CreateBoxMapping(unset.Plane, unset.X, unset.Y, unset.Z, true);

                MeshDisplacementInfo k = new Rhino.Geometry.MeshDisplacementInfo(s1, s2);
                Mesh geometry = mesh.WithDisplacement(k);
                meshArray.Append(geometry);
            }

Thank you.