public void SetMaterial(int material) { Material = (MaterialAttributes.Material)material; // Setting the material sets the material attributes also. MaterialAttributes matAttrib = BSMaterials.GetAttributes(Material, false); Friction = matAttrib.friction; Restitution = matAttrib.restitution; Density = matAttrib.density / BSParam.DensityScaleFactor; // DetailLog("{0},{1}.SetMaterial,Mat={2},frict={3},rest={4},den={5}", LocalID, TypeName, Material, Friction, Restitution, Density); }
// Use reflection to set the value in the attribute structure. static void SetAttributeValue(int matType, string attribName, float val) { // Get the current attribute values for this material MaterialAttributes thisAttrib = Attributes[matType]; // Find the field for the passed attribute name (eg, find field named 'friction') FieldInfo fieldInfo = thisAttrib.GetType().GetField(attribName.ToLower()); if (fieldInfo != null) { fieldInfo.SetValue(thisAttrib, val); // Copy new attributes back to array -- since MaterialAttributes is 'struct', passed by value, not reference. Attributes[matType] = thisAttrib; } }
// Given a material type, return a structure of attributes. public static MaterialAttributes GetAttributes(MaterialAttributes.Material type, bool isPhysical) { int ind = (int)type; if (isPhysical) ind += (int)MaterialAttributes.Material.NumberOfTypes; return Attributes[ind]; }