SOPMaterial.cs 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342
  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 copyright
  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.Text;
  29. using System.Runtime.InteropServices;
  30. using System.Security.Cryptography; // for computing md5 hash
  31. using OpenSim.Framework;
  32. using OpenMetaverse;
  33. using OpenMetaverse.StructuredData;
  34. namespace OpenSim.Region.Framework.Scenes
  35. {
  36. public static class SOPMaterialData
  37. {
  38. public enum SopMaterial : int // redundante and not in use for now
  39. {
  40. Stone = 0,
  41. Metal = 1,
  42. Glass = 2,
  43. Wood = 3,
  44. Flesh = 4,
  45. Plastic = 5,
  46. Rubber = 6,
  47. light = 7 // compatibility with old viewers
  48. }
  49. private struct MaterialData
  50. {
  51. public float friction;
  52. public float bounce;
  53. public MaterialData(float f, float b)
  54. {
  55. friction = f;
  56. bounce = b;
  57. }
  58. }
  59. private static MaterialData[] m_materialdata = {
  60. new MaterialData(0.8f,0.4f), // Stone
  61. new MaterialData(0.3f,0.4f), // Metal
  62. new MaterialData(0.2f,0.7f), // Glass
  63. new MaterialData(0.6f,0.5f), // Wood
  64. new MaterialData(0.9f,0.3f), // Flesh
  65. new MaterialData(0.4f,0.7f), // Plastic
  66. new MaterialData(0.9f,0.95f), // Rubber
  67. new MaterialData(0.0f,0.0f) // light ??
  68. };
  69. public static Material MaxMaterial
  70. {
  71. get { return (Material)(m_materialdata.Length - 1); }
  72. }
  73. public static float friction(Material material)
  74. {
  75. int indx = (int)material;
  76. if (indx < m_materialdata.Length)
  77. return (m_materialdata[indx].friction);
  78. else
  79. return 0;
  80. }
  81. public static float bounce(Material material)
  82. {
  83. int indx = (int)material;
  84. if (indx < m_materialdata.Length)
  85. return (m_materialdata[indx].bounce);
  86. else
  87. return 0;
  88. }
  89. }
  90. [StructLayout(LayoutKind.Sequential)]
  91. public class FaceMaterial
  92. {
  93. // ll material data
  94. public byte DiffuseAlphaMode = 1;
  95. public byte AlphaMaskCutoff = 0;
  96. public byte SpecularLightExponent = 51;
  97. public byte EnvironmentIntensity = 0;
  98. // need to have 4 bytes here
  99. public float NormalOffsetX = 0.0f;
  100. public float NormalOffsetY = 0.0f;
  101. public float NormalRepeatX = 1.0f;
  102. public float NormalRepeatY = 1.0f;
  103. public float NormalRotation = 0.0f;
  104. public float SpecularOffsetX = 0.0f;
  105. public float SpecularOffsetY = 0.0f;
  106. public float SpecularRepeatX = 1.0f;
  107. public float SpecularRepeatY = 1.0f;
  108. public float SpecularRotation = 0.0f;
  109. public byte SpecularLightColorR = 255;
  110. public byte SpecularLightColorG = 255;
  111. public byte SpecularLightColorB = 255;
  112. public byte SpecularLightColorA = 255;
  113. // data size 12 ints so far
  114. public UUID NormalMapID = UUID.Zero;
  115. public UUID SpecularMapID = UUID.Zero;
  116. // other data
  117. public UUID ID;
  118. private int inthash;
  119. private bool validinthash;
  120. public FaceMaterial()
  121. { }
  122. public FaceMaterial(FaceMaterial other)
  123. {
  124. if (other == null)
  125. return;
  126. DiffuseAlphaMode = other.DiffuseAlphaMode;
  127. AlphaMaskCutoff = other.AlphaMaskCutoff;
  128. SpecularLightExponent = other.SpecularLightExponent;
  129. EnvironmentIntensity = other.EnvironmentIntensity;
  130. NormalOffsetX = other.NormalOffsetX;
  131. NormalOffsetY = other.NormalOffsetY;
  132. NormalRepeatX = other.NormalRepeatX;
  133. NormalRepeatY = other.NormalRepeatY;
  134. NormalRotation = other.NormalRotation;
  135. SpecularOffsetX = other.SpecularOffsetX;
  136. SpecularOffsetY = other.SpecularOffsetY;
  137. SpecularRepeatX = other.SpecularRepeatX;
  138. SpecularRepeatY = other.SpecularRepeatY;
  139. SpecularRotation = other.SpecularRotation;
  140. SpecularLightColorR = other.SpecularLightColorR;
  141. SpecularLightColorG = other.SpecularLightColorG;
  142. SpecularLightColorB = other.SpecularLightColorB;
  143. NormalMapID = other.NormalMapID;
  144. SpecularMapID = other.SpecularMapID;
  145. }
  146. public FaceMaterial(OSDMap mat)
  147. {
  148. if (mat == null)
  149. return;
  150. const float scale = 0.0001f;
  151. NormalMapID = mat["NormMap"].AsUUID();
  152. NormalOffsetX = scale * (float)mat["NormOffsetX"].AsReal();
  153. NormalOffsetY = scale * (float)mat["NormOffsetY"].AsReal();
  154. NormalRepeatX = scale * (float)mat["NormRepeatX"].AsReal();
  155. NormalRepeatY = scale * (float)mat["NormRepeatY"].AsReal();
  156. NormalRotation = scale * (float)mat["NormRotation"].AsReal();
  157. SpecularMapID = mat["SpecMap"].AsUUID();
  158. SpecularOffsetX = scale * (float)mat["SpecOffsetX"].AsReal();
  159. SpecularOffsetY = scale * (float)mat["SpecOffsetY"].AsReal();
  160. SpecularRepeatX = scale * (float)mat["SpecRepeatX"].AsReal();
  161. SpecularRepeatY = scale * (float)mat["SpecRepeatY"].AsReal();
  162. SpecularRotation = scale * (float)mat["SpecRotation"].AsReal();
  163. Color4 SpecularLightColortmp = mat["SpecColor"].AsColor4(); // we can read as color4
  164. SpecularLightColorR = (byte)(SpecularLightColortmp.R);
  165. SpecularLightColorG = (byte)(SpecularLightColortmp.G);
  166. SpecularLightColorB = (byte)(SpecularLightColortmp.B);
  167. SpecularLightExponent = (Byte)mat["SpecExp"].AsUInteger();
  168. EnvironmentIntensity = (Byte)mat["EnvIntensity"].AsUInteger();
  169. DiffuseAlphaMode = (Byte)mat["DiffuseAlphaMode"].AsUInteger();
  170. AlphaMaskCutoff = (Byte)mat["AlphaMaskCutoff"].AsUInteger();
  171. }
  172. public void genID()
  173. {
  174. string lslx = toLLSDxml();
  175. Byte[] data = System.Text.Encoding.ASCII.GetBytes(lslx);
  176. using (var md5 = MD5.Create())
  177. ID = new UUID(md5.ComputeHash(data), 0);
  178. }
  179. public unsafe override int GetHashCode()
  180. {
  181. if (!validinthash)
  182. {
  183. unchecked
  184. {
  185. // if you don't like this, don't read...
  186. int* ptr;
  187. fixed (byte* ptrbase = &DiffuseAlphaMode)
  188. {
  189. ptr = (int*)ptrbase;
  190. inthash = *ptr;
  191. for (int i = 0; i < 11; i++)
  192. inthash ^= *ptr++;
  193. }
  194. fixed (Guid* ptrbase = &NormalMapID.Guid)
  195. {
  196. ptr = (int*)ptrbase;
  197. for (int i = 0; i < 16; i++)
  198. inthash ^= ptr[i];
  199. }
  200. fixed (Guid* ptrbase = &SpecularMapID.Guid)
  201. {
  202. ptr = (int*)ptrbase;
  203. for (int i = 0; i < 16; i++)
  204. inthash ^= ptr[i];
  205. }
  206. }
  207. validinthash = true;
  208. }
  209. return inthash;
  210. }
  211. public override bool Equals(Object o)
  212. {
  213. if (o == null || !(o is FaceMaterial))
  214. return false;
  215. FaceMaterial other = (FaceMaterial)o;
  216. return (
  217. DiffuseAlphaMode == other.DiffuseAlphaMode
  218. && AlphaMaskCutoff == other.AlphaMaskCutoff
  219. && SpecularLightExponent == other.SpecularLightExponent
  220. && EnvironmentIntensity == other.EnvironmentIntensity
  221. && NormalMapID == other.NormalMapID
  222. && NormalOffsetX == other.NormalOffsetX
  223. && NormalOffsetY == other.NormalOffsetY
  224. && NormalRepeatX == other.NormalRepeatX
  225. && NormalRepeatY == other.NormalRepeatY
  226. && NormalRotation == other.NormalRotation
  227. && SpecularMapID == other.SpecularMapID
  228. && SpecularOffsetX == other.SpecularOffsetX
  229. && SpecularOffsetY == other.SpecularOffsetY
  230. && SpecularRepeatX == other.SpecularRepeatX
  231. && SpecularRepeatY == other.SpecularRepeatY
  232. && SpecularRotation == other.SpecularRotation
  233. && SpecularLightColorR == other.SpecularLightColorR
  234. && SpecularLightColorG == other.SpecularLightColorG
  235. && SpecularLightColorB == other.SpecularLightColorB
  236. );
  237. }
  238. public OSDMap toOSD()
  239. {
  240. OSDMap mat = new OSDMap();
  241. float scale = 10000f;
  242. mat["NormMap"] = NormalMapID;
  243. mat["NormOffsetX"] = (int)(scale * NormalOffsetX);
  244. mat["NormOffsetY"] = (int)(scale * NormalOffsetY);
  245. mat["NormRepeatX"] = (int)(scale * NormalRepeatX);
  246. mat["NormRepeatY"] = (int)(scale * NormalRepeatY);
  247. mat["NormRotation"] = (int)(scale * NormalRotation);
  248. mat["SpecMap"] = SpecularMapID;
  249. mat["SpecOffsetX"] = (int)(scale * SpecularOffsetX);
  250. mat["SpecOffsetY"] = (int)(scale * SpecularOffsetY);
  251. mat["SpecRepeatX"] = (int)(scale * SpecularRepeatX);
  252. mat["SpecRepeatY"] = (int)(scale * SpecularRepeatY);
  253. mat["SpecRotation"] = (int)(scale * SpecularRotation);
  254. OSDArray carray = new OSDArray(4);
  255. carray.Add(SpecularLightColorR);
  256. carray.Add(SpecularLightColorG);
  257. carray.Add(SpecularLightColorB);
  258. carray.Add(255); // solid color
  259. mat["SpecColor"] = carray;
  260. mat["SpecExp"] = SpecularLightExponent;
  261. mat["EnvIntensity"] = EnvironmentIntensity;
  262. mat["DiffuseAlphaMode"] = DiffuseAlphaMode;
  263. mat["AlphaMaskCutoff"] = AlphaMaskCutoff;
  264. return mat;
  265. }
  266. public string toLLSDxml(StringBuilder sb = null)
  267. {
  268. const float scale = 10000f;
  269. bool fullLLSD = false;
  270. if (sb == null)
  271. {
  272. sb = LLSDxmlEncode.Start(1024, false);
  273. fullLLSD = true;
  274. }
  275. LLSDxmlEncode.AddMap(sb);
  276. LLSDxmlEncode.AddElem("NormMap", NormalMapID, sb);
  277. LLSDxmlEncode.AddElem("NormOffsetX", (int)(scale * NormalOffsetX + 0.5f), sb);
  278. LLSDxmlEncode.AddElem("NormOffsetY", (int)(scale * NormalOffsetY + 0.5f), sb);
  279. LLSDxmlEncode.AddElem("NormRepeatX", (int)(scale * NormalRepeatX + 0.5f), sb);
  280. LLSDxmlEncode.AddElem("NormRepeatY", (int)(scale * NormalRepeatY + 0.5f), sb);
  281. LLSDxmlEncode.AddElem("NormRotation", (int)(scale * NormalRotation + 0.5f), sb);
  282. LLSDxmlEncode.AddElem("SpecMap", SpecularMapID, sb);
  283. LLSDxmlEncode.AddElem("SpecOffsetX", (int)(scale * SpecularOffsetX + 0.5f), sb);
  284. LLSDxmlEncode.AddElem("SpecOffsetY", (int)(scale * SpecularOffsetY + 0.5f), sb);
  285. LLSDxmlEncode.AddElem("SpecRepeatX", (int)(scale * SpecularRepeatX + 0.5f), sb);
  286. LLSDxmlEncode.AddElem("SpecRepeatY", (int)(scale * SpecularRepeatY + 0.5f), sb);
  287. LLSDxmlEncode.AddElem("SpecRotation", (int)(scale * SpecularRotation + 0.5f), sb);
  288. LLSDxmlEncode.AddArray("SpecColor", sb);
  289. LLSDxmlEncode.AddElem(SpecularLightColorR, sb);
  290. LLSDxmlEncode.AddElem(SpecularLightColorG, sb);
  291. LLSDxmlEncode.AddElem(SpecularLightColorB, sb);
  292. LLSDxmlEncode.AddElem(255, sb);
  293. LLSDxmlEncode.AddEndArray(sb);
  294. LLSDxmlEncode.AddElem("SpecExp", SpecularLightExponent, sb);
  295. LLSDxmlEncode.AddElem("EnvIntensity", EnvironmentIntensity, sb);
  296. LLSDxmlEncode.AddElem("DiffuseAlphaMode", DiffuseAlphaMode, sb);
  297. LLSDxmlEncode.AddElem("AlphaMaskCutoff", AlphaMaskCutoff, sb);
  298. LLSDxmlEncode.AddEndMap(sb);
  299. if (fullLLSD)
  300. {
  301. return LLSDxmlEncode.End(sb);
  302. }
  303. else
  304. return String.Empty; // ignored if appending
  305. }
  306. }
  307. }