BSMaterials.cs 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  1. /*
  2. * Copyright (c) Contributors, http://opensimulator.org/
  3. * See CONTRIBUTORS.TXT for a full list of copyright holders.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions are met:
  7. * * Redistributions of source code must retain the above copyright
  8. * notice, this list of conditions and the following disclaimer.
  9. * * Redistributions in binary form must reproduce the above copyrightD
  10. * notice, this list of conditions and the following disclaimer in the
  11. * documentation and/or other materials provided with the distribution.
  12. * * Neither the name of the OpenSimulator Project nor the
  13. * names of its contributors may be used to endorse or promote products
  14. * derived from this software without specific prior written permission.
  15. *
  16. * THIS SOFTWARE IS PROVIDED BY THE DEVELOPERS ``AS IS'' AND ANY
  17. * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  18. * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  19. * DISCLAIMED. IN NO EVENT SHALL THE CONTRIBUTORS BE LIABLE FOR ANY
  20. * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  21. * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  22. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  23. * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  24. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  25. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  26. */
  27. using System;
  28. using System.Collections.Generic;
  29. using System.Text;
  30. using System.Reflection;
  31. using Nini.Config;
  32. namespace OpenSim.Region.PhysicsModule.BulletS
  33. {
  34. public struct MaterialAttributes
  35. {
  36. // Material type values that correspond with definitions for LSL
  37. public enum Material : int
  38. {
  39. Stone = 0,
  40. Metal,
  41. Glass,
  42. Wood,
  43. Flesh,
  44. Plastic,
  45. Rubber,
  46. Light,
  47. // Hereafter are BulletSim additions
  48. Avatar,
  49. NumberOfTypes // the count of types in the enum.
  50. }
  51. // Names must be in the order of the above enum.
  52. // These names must coorespond to the lower case field names in the MaterialAttributes
  53. // structure as reflection is used to select the field to put the value in.
  54. public static readonly string[] MaterialAttribs = { "Density", "Friction", "Restitution"};
  55. public MaterialAttributes(string t, float d, float f, float r)
  56. {
  57. type = t;
  58. density = d;
  59. friction = f;
  60. restitution = r;
  61. }
  62. public string type;
  63. public float density;
  64. public float friction;
  65. public float restitution;
  66. }
  67. public static class BSMaterials
  68. {
  69. // Attributes for each material type
  70. private static readonly MaterialAttributes[] Attributes;
  71. // Map of material name to material type code
  72. public static readonly Dictionary<string, MaterialAttributes.Material> MaterialMap;
  73. static BSMaterials()
  74. {
  75. // Attribute sets for both the non-physical and physical instances of materials.
  76. Attributes = new MaterialAttributes[(int)MaterialAttributes.Material.NumberOfTypes * 2];
  77. // Map of name to type code.
  78. MaterialMap = new Dictionary<string, MaterialAttributes.Material>();
  79. MaterialMap.Add("Stone", MaterialAttributes.Material.Stone);
  80. MaterialMap.Add("Metal", MaterialAttributes.Material.Metal);
  81. MaterialMap.Add("Glass", MaterialAttributes.Material.Glass);
  82. MaterialMap.Add("Wood", MaterialAttributes.Material.Wood);
  83. MaterialMap.Add("Flesh", MaterialAttributes.Material.Flesh);
  84. MaterialMap.Add("Plastic", MaterialAttributes.Material.Plastic);
  85. MaterialMap.Add("Rubber", MaterialAttributes.Material.Rubber);
  86. MaterialMap.Add("Light", MaterialAttributes.Material.Light);
  87. MaterialMap.Add("Avatar", MaterialAttributes.Material.Avatar);
  88. }
  89. // This is where all the default material attributes are defined.
  90. public static void InitializeFromDefaults(ConfigurationParameters parms)
  91. {
  92. // Values from http://wiki.secondlife.com/wiki/PRIM_MATERIAL
  93. float dDensity = parms.defaultDensity;
  94. float dFriction = parms.defaultFriction;
  95. float dRestitution = parms.defaultRestitution;
  96. Attributes[(int)MaterialAttributes.Material.Stone] =
  97. new MaterialAttributes("stone",dDensity, 0.8f, 0.4f);
  98. Attributes[(int)MaterialAttributes.Material.Metal] =
  99. new MaterialAttributes("metal",dDensity, 0.3f, 0.4f);
  100. Attributes[(int)MaterialAttributes.Material.Glass] =
  101. new MaterialAttributes("glass",dDensity, 0.2f, 0.7f);
  102. Attributes[(int)MaterialAttributes.Material.Wood] =
  103. new MaterialAttributes("wood",dDensity, 0.6f, 0.5f);
  104. Attributes[(int)MaterialAttributes.Material.Flesh] =
  105. new MaterialAttributes("flesh",dDensity, 0.9f, 0.3f);
  106. Attributes[(int)MaterialAttributes.Material.Plastic] =
  107. new MaterialAttributes("plastic",dDensity, 0.4f, 0.7f);
  108. Attributes[(int)MaterialAttributes.Material.Rubber] =
  109. new MaterialAttributes("rubber",dDensity, 0.9f, 0.9f);
  110. Attributes[(int)MaterialAttributes.Material.Light] =
  111. new MaterialAttributes("light",dDensity, dFriction, dRestitution);
  112. Attributes[(int)MaterialAttributes.Material.Avatar] =
  113. new MaterialAttributes("avatar",3.5f, 0.2f, 0f);
  114. Attributes[(int)MaterialAttributes.Material.Stone + (int)MaterialAttributes.Material.NumberOfTypes] =
  115. new MaterialAttributes("stonePhysical",dDensity, 0.8f, 0.4f);
  116. Attributes[(int)MaterialAttributes.Material.Metal + (int)MaterialAttributes.Material.NumberOfTypes] =
  117. new MaterialAttributes("metalPhysical",dDensity, 0.3f, 0.4f);
  118. Attributes[(int)MaterialAttributes.Material.Glass + (int)MaterialAttributes.Material.NumberOfTypes] =
  119. new MaterialAttributes("glassPhysical",dDensity, 0.2f, 0.7f);
  120. Attributes[(int)MaterialAttributes.Material.Wood + (int)MaterialAttributes.Material.NumberOfTypes] =
  121. new MaterialAttributes("woodPhysical",dDensity, 0.6f, 0.5f);
  122. Attributes[(int)MaterialAttributes.Material.Flesh + (int)MaterialAttributes.Material.NumberOfTypes] =
  123. new MaterialAttributes("fleshPhysical",dDensity, 0.9f, 0.3f);
  124. Attributes[(int)MaterialAttributes.Material.Plastic + (int)MaterialAttributes.Material.NumberOfTypes] =
  125. new MaterialAttributes("plasticPhysical",dDensity, 0.4f, 0.7f);
  126. Attributes[(int)MaterialAttributes.Material.Rubber + (int)MaterialAttributes.Material.NumberOfTypes] =
  127. new MaterialAttributes("rubberPhysical",dDensity, 0.9f, 0.9f);
  128. Attributes[(int)MaterialAttributes.Material.Light + (int)MaterialAttributes.Material.NumberOfTypes] =
  129. new MaterialAttributes("lightPhysical",dDensity, dFriction, dRestitution);
  130. Attributes[(int)MaterialAttributes.Material.Avatar + (int)MaterialAttributes.Material.NumberOfTypes] =
  131. new MaterialAttributes("avatarPhysical",3.5f, 0.2f, 0f);
  132. }
  133. // Under the [BulletSim] section, one can change the individual material
  134. // attribute values. The format of the configuration parameter is:
  135. // <materialName><Attribute>["Physical"] = floatValue
  136. // For instance:
  137. // [BulletSim]
  138. // StoneFriction = 0.2
  139. // FleshRestitutionPhysical = 0.8
  140. // Materials can have different parameters for their static and
  141. // physical instantiations. When setting the non-physical value,
  142. // both values are changed. Setting the physical value only changes
  143. // the physical value.
  144. public static void InitializefromParameters(IConfig pConfig)
  145. {
  146. foreach (KeyValuePair<string, MaterialAttributes.Material> kvp in MaterialMap)
  147. {
  148. string matName = kvp.Key;
  149. foreach (string attribName in MaterialAttributes.MaterialAttribs)
  150. {
  151. string paramName = matName + attribName;
  152. if (pConfig.Contains(paramName))
  153. {
  154. float paramValue = pConfig.GetFloat(paramName);
  155. SetAttributeValue((int)kvp.Value, attribName, paramValue);
  156. // set the physical value also
  157. SetAttributeValue((int)kvp.Value + (int)MaterialAttributes.Material.NumberOfTypes, attribName, paramValue);
  158. }
  159. paramName += "Physical";
  160. if (pConfig.Contains(paramName))
  161. {
  162. float paramValue = pConfig.GetFloat(paramName);
  163. SetAttributeValue((int)kvp.Value + (int)MaterialAttributes.Material.NumberOfTypes, attribName, paramValue);
  164. }
  165. }
  166. }
  167. }
  168. // Use reflection to set the value in the attribute structure.
  169. private static void SetAttributeValue(int matType, string attribName, float val)
  170. {
  171. // Get the current attribute values for this material
  172. MaterialAttributes thisAttrib = Attributes[matType];
  173. // Find the field for the passed attribute name (eg, find field named 'friction')
  174. FieldInfo fieldInfo = thisAttrib.GetType().GetField(attribName.ToLower());
  175. if (fieldInfo != null)
  176. {
  177. fieldInfo.SetValue(thisAttrib, val);
  178. // Copy new attributes back to array -- since MaterialAttributes is 'struct', passed by value, not reference.
  179. Attributes[matType] = thisAttrib;
  180. }
  181. }
  182. // Given a material type, return a structure of attributes.
  183. public static MaterialAttributes GetAttributes(MaterialAttributes.Material type, bool isPhysical)
  184. {
  185. int ind = (int)type;
  186. if (isPhysical) ind += (int)MaterialAttributes.Material.NumberOfTypes;
  187. return Attributes[ind];
  188. }
  189. }
  190. }