Assign Material via .Net during Runtime

Hy,

I successfully assign a Material to a component during simulation but the different color of the material won’t appear!? If I assign the material afterwards manually everything is fine…

But what should I do that this working just by code!?

I’m using IMaterial, Color, ISimNode.

Thx & Regards
Feature

So you are assigning material to root node of component? If a feature has a material assigned to it already, the component material won’t override it. What you can do is force material inheritance.

Color color = new VisualComponents.Create3D.Color((byte)red, (byte)green, (byte)blue, (byte)alpha); //Create the color
customMaterial.Color = color; //Set the color over RGBA
customMaterial.Ambient = color; //Set the Ambient over RGBA
customMaterial.Specular = color; //Set the Specular over RGBA
customMaterial.Emissive = color; //Set the Emissive over RGBA
customMaterial.Opacity = (byte)alpha; //Set Transparency becauseAlphaValue works not well...
customMaterial.ForceUpdate(); //Update Material

ISimNode sn = (ISimNode)component;
sn.MaterialInheritance = MaterialInheritance.ForceInheritNode;
sn.Material = customMaterial;
sn.Rebuild();

I tried different ways to force material inheritance, but it always shows strange results… :frowning:

What is working is:

ISimNode sn = (ISimNode)component;
sn.HighlightMaterial = customMaterial;
sn.Rebuild();

but AlphaValue is not working at all…

Can you tell me how to Update/Render the whole component correctly that the result is shown immediately?

Regards
Feature

Sounds like you should do render because changing material does require render update. You can try contacting support or others can help. I won’t be able to test until later today.

_RenderService.RequestRender(); does the trick! :slight_smile:

Thx & Regards
Feature

1 Like

Hello, could you share the useful code, I get the same problem too.

You can see that I wrote all you wrote but did not work…

Hy,

try it so:

  • Get a standardmaterial
  • Create your userdefined Material
  • Copy all from the standardmaterial without the color, choose your color as you do!

Then it should work! :slight_smile:

Regards
Feature