Site Tools


Differences

This shows you the differences between two versions of the page.

Link to this comparison view

developer:sdksamples:textureblend [2015/09/14] (current)
Line 1: Line 1:
 +====== Modifying Texture Intensity ======
 +> **Developer:​** //​[[developer:​dotnetplugins|.NET]]//​
 +> **Summary:​** //Discusses how to modify the intensity of a material'​s texture.//
 +> **WARNING:​** //This example is out-of-date and needs to be replaced with RhinoCommon.//​
  
 +=====Question=====
 +I would like to change the intensity, or blend, percentage of a basic material'​s texture. How can I do this using the .NET SDK?
 +
 +{{:​legacy:​en:​SdkTextureBlend.png}}
 +
 +=====Answer=====
 +See the following example
 +
 +===== C# =====
 +<code c#>
 +
 +public override IRhinoCommand.result RunCommand(IRhinoCommandContext context)
 +{
 +  // Pick a renderable object
 +  MRhinoGetObject go = new MRhinoGetObject();​
 +  go.SetCommandPrompt("​Select surface, polysurface,​ or mesh"​);​
 +  go.SetGeometryFilter(IRhinoGetObject.GEOMETRY_TYPE_FILTER.surface_object | 
 +                       ​IRhinoGetObject.GEOMETRY_TYPE_FILTER.polysrf_object | 
 +                       ​IRhinoGetObject.GEOMETRY_TYPE_FILTER.mesh_object);​
 +  go.GetObjects(1,​ 1);
 +  if (go.CommandResult() != IRhinoCommand.result.success)
 +    return go.CommandResult();​
 +
 +  // Validate the object
 +  IRhinoObject obj = go.Object(0).Object();​
 +  if (obj == null)
 +    return IRhinoCommand.result.failure;​
 +
 +  // Get the object'​s material index
 +  int mat_index = obj.Attributes().m_material_index;​
 +  if (mat_index >= 0)
 +  {
 +    // Get the object'​s material
 +    IRhinoMaterial mat = context.m_doc.m_material_table[mat_index];​
 +    if (mat != null)
 +    {
 +      // See if the material has a texture. In this example, we will look
 +      // for a bitmap texture
 +      int tex_index = mat.FindTexture(null,​ IOnTexture.TYPE.bitmap_texture);​
 +      if (tex_index >= 0)
 +      {
 +        // Make a copy of the object'​s material
 +        OnMaterial new_mat = new OnMaterial(mat);​
 +
 +        // Get the bitmap texture
 +        OnTexture tex = new_mat.m_textures[tex_index];​
 +
 +        // The blend constant ranges from 0.0 to 1.0 (inclusive).
 +        double blend_constant = tex.m_blend_constant_A;​
 +
 +        // Convert to a percentage (can probably skip this step)
 +        int blend_percent = OnUtil.ON_Round(blend_constant * 100.0);
 +
 +        // Modify the percentage (1/2 will do for this example)
 +        int new_blend_percent = OnUtil.ON_Round(blend_percent / 2);
 +
 +        // Convert back to a value between 0.0 to 1.0
 +        double new_blend_constant = (double)(new_blend_percent / 100.0);
 +
 +        // Update the texture
 +        tex.m_blend_constant_A = new_blend_constant;​
 +
 +        // Modify the mode if necessary
 +        if (tex.m_blend_constant_A < 1.0)
 +          tex.m_mode = IOnTexture.MODE.blend_texture;​
 +        else
 +          tex.m_mode = IOnTexture.MODE.decal_texture;​
 +
 +        // Modify the object'​s material
 +        if( context.m_doc.m_material_table.ModifyMaterial(new_mat,​ mat_index, true) )
 +          context.m_doc.Regen();​
 +      }
 +    }
 +  }
 +
 +  return IRhinoCommand.result.success;​
 +}
 +</​code>​
 +
 +
 +{{tag>​Developer dotnet}}
developer/sdksamples/textureblend.txt ยท Last modified: 2015/09/14 (external edit)