S&box Wiki

Revision Difference

material_attributes#551619

<cat>Material.Intro</cat> <title>Material Attributes</title> # What are they?⤶ Material attributes just are a basic way to put custom data into a material, they consist of a name and any value you want really. # How to define a Material Attribute ⤶ So to define a Material Attribute you can do it as so. First open your material, and locate the Attributes tab.⤶ # What Are Material Attributes⤶ Material attributes are an easy way to put custom data into a material, they consist of a name and a value. # How To Define a Material Attribute ⤶ 1) First, locate the Attributes tab. ![Attributes Tab](https://i.imgur.com/SKGpItp.png) ⤶ Inside this tab you will see this big region, this is where all your material attributes are contained, and where you add or remove them! ⤶ 2) The section highlighted below is where you can add, edit, and remove Attributes. ![User Material Attributes](https://i.imgur.com/cdCspU5.png) ⤶ So now you can add an attribute like so, and here is how you can name it and define the value! (note any value can be put here really!) ⤶ 3) To add a new value, click the plus button at the bottom. Double click on the name or value fields to edit them. To remove a value, click the X button. ![Adding Attributes](https://i.imgur.com/kRPZzg2.gif) ⤶ and here is an example of multiple, as long as the name contains no spaces you can put what you want!⤶ ⤶ ![Multiple Attributes](https://i.imgur.com/ASPsTlL.png) ⤶ # Using Attributes to Override specific materials in code!⤶ ⤶ Currently the only main thing you can do with these attributes are to make it so only the material with the attribute set can be overridden in game.⤶ ⤶ What this is useful for is, if your model has multiple materials, but you only want to replace 1 specific material at runtime (without material groups), you can use attributes to specify to only override this specific material!⤶ ⤶ What I mean by this is, for example, the citizen skin!⤶ ⤶ <warning> For this to work in code, the value must be set to 1 </warning>⤶ ⤶ Like so.⤶ ⤶ # Using Attributes⤶ ⤶ After giving your material an attribute, it can be referenced through code. The Example below shows how to change the material of the citizen model's skin in real time. ⤶ The skin material for the citizen has a material attribute named **Skin** with the value set to 1⤶ ![Skin Attribute](https://i.imgur.com/FGmwhr3.png) ⤶ Now what they do with this is they do something along the lines of this⤶ ``` public class Example : AnimatedEntity { public override void Spawn() { base.Spawn(); SetModel( "models/citizen/citizen.vmdl" ); Material darkerSkin = Material.Load("models/citizen/skin/citizen_skin03.vmat_c"); // here we just grab any materialthis.SetMaterialOverride(darkerSkin, "skin" ); // this method is used to override materials in the model⤶ // however the 2nd value given the method, will check all the models materials ⤶ // and only override the materials that have a "skin" attribute with the value set to 1⤶ Material darkerSkin = Material.Load("models/citizen/skin/citizen_skin03.vmat_c") // This overrides all materials on the citizen model with the material attribute "skin"⤶ this.SetMaterialOverride(darkerSkin, "skin" ); } } ```