1
0

SOPMaterial.cs 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341
  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. Byte[] data = toLLSDxml();
  175. using (var md5 = MD5.Create())
  176. ID = new UUID(md5.ComputeHash(data), 0);
  177. }
  178. public unsafe override int GetHashCode()
  179. {
  180. if (!validinthash)
  181. {
  182. unchecked
  183. {
  184. // if you don't like this, don't read...
  185. int* ptr;
  186. fixed (byte* ptrbase = &DiffuseAlphaMode)
  187. {
  188. ptr = (int*)ptrbase;
  189. inthash = *ptr;
  190. for (int i = 0; i < 11; i++)
  191. inthash ^= *ptr++;
  192. }
  193. fixed (Guid* ptrbase = &NormalMapID.Guid)
  194. {
  195. ptr = (int*)ptrbase;
  196. for (int i = 0; i < 16; i++)
  197. inthash ^= ptr[i];
  198. }
  199. fixed (Guid* ptrbase = &SpecularMapID.Guid)
  200. {
  201. ptr = (int*)ptrbase;
  202. for (int i = 0; i < 16; i++)
  203. inthash ^= ptr[i];
  204. }
  205. }
  206. validinthash = true;
  207. }
  208. return inthash;
  209. }
  210. public override bool Equals(Object o)
  211. {
  212. if (o == null || !(o is FaceMaterial))
  213. return false;
  214. FaceMaterial other = (FaceMaterial)o;
  215. return (
  216. DiffuseAlphaMode == other.DiffuseAlphaMode
  217. && AlphaMaskCutoff == other.AlphaMaskCutoff
  218. && SpecularLightExponent == other.SpecularLightExponent
  219. && EnvironmentIntensity == other.EnvironmentIntensity
  220. && NormalMapID == other.NormalMapID
  221. && NormalOffsetX == other.NormalOffsetX
  222. && NormalOffsetY == other.NormalOffsetY
  223. && NormalRepeatX == other.NormalRepeatX
  224. && NormalRepeatY == other.NormalRepeatY
  225. && NormalRotation == other.NormalRotation
  226. && SpecularMapID == other.SpecularMapID
  227. && SpecularOffsetX == other.SpecularOffsetX
  228. && SpecularOffsetY == other.SpecularOffsetY
  229. && SpecularRepeatX == other.SpecularRepeatX
  230. && SpecularRepeatY == other.SpecularRepeatY
  231. && SpecularRotation == other.SpecularRotation
  232. && SpecularLightColorR == other.SpecularLightColorR
  233. && SpecularLightColorG == other.SpecularLightColorG
  234. && SpecularLightColorB == other.SpecularLightColorB
  235. );
  236. }
  237. public OSDMap toOSD()
  238. {
  239. OSDMap mat = new OSDMap();
  240. float scale = 10000f;
  241. mat["NormMap"] = NormalMapID;
  242. mat["NormOffsetX"] = (int)(scale * NormalOffsetX);
  243. mat["NormOffsetY"] = (int)(scale * NormalOffsetY);
  244. mat["NormRepeatX"] = (int)(scale * NormalRepeatX);
  245. mat["NormRepeatY"] = (int)(scale * NormalRepeatY);
  246. mat["NormRotation"] = (int)(scale * NormalRotation);
  247. mat["SpecMap"] = SpecularMapID;
  248. mat["SpecOffsetX"] = (int)(scale * SpecularOffsetX);
  249. mat["SpecOffsetY"] = (int)(scale * SpecularOffsetY);
  250. mat["SpecRepeatX"] = (int)(scale * SpecularRepeatX);
  251. mat["SpecRepeatY"] = (int)(scale * SpecularRepeatY);
  252. mat["SpecRotation"] = (int)(scale * SpecularRotation);
  253. OSDArray carray = new OSDArray(4);
  254. carray.Add(SpecularLightColorR);
  255. carray.Add(SpecularLightColorG);
  256. carray.Add(SpecularLightColorB);
  257. carray.Add(255); // solid color
  258. mat["SpecColor"] = carray;
  259. mat["SpecExp"] = SpecularLightExponent;
  260. mat["EnvIntensity"] = EnvironmentIntensity;
  261. mat["DiffuseAlphaMode"] = DiffuseAlphaMode;
  262. mat["AlphaMaskCutoff"] = AlphaMaskCutoff;
  263. return mat;
  264. }
  265. public byte[] toLLSDxml(osUTF8 sb = null)
  266. {
  267. const float scale = 10000f;
  268. bool fullLLSD = false;
  269. if (sb == null)
  270. {
  271. sb = LLSDxmlEncode2.Start(1024, false);
  272. fullLLSD = true;
  273. }
  274. LLSDxmlEncode2.AddMap(sb);
  275. LLSDxmlEncode2.AddElem("NormMap", NormalMapID, sb);
  276. LLSDxmlEncode2.AddElem("NormOffsetX", (int)(scale * NormalOffsetX + 0.5f), sb);
  277. LLSDxmlEncode2.AddElem("NormOffsetY", (int)(scale * NormalOffsetY + 0.5f), sb);
  278. LLSDxmlEncode2.AddElem("NormRepeatX", (int)(scale * NormalRepeatX + 0.5f), sb);
  279. LLSDxmlEncode2.AddElem("NormRepeatY", (int)(scale * NormalRepeatY + 0.5f), sb);
  280. LLSDxmlEncode2.AddElem("NormRotation", (int)(scale * NormalRotation + 0.5f), sb);
  281. LLSDxmlEncode2.AddElem("SpecMap", SpecularMapID, sb);
  282. LLSDxmlEncode2.AddElem("SpecOffsetX", (int)(scale * SpecularOffsetX + 0.5f), sb);
  283. LLSDxmlEncode2.AddElem("SpecOffsetY", (int)(scale * SpecularOffsetY + 0.5f), sb);
  284. LLSDxmlEncode2.AddElem("SpecRepeatX", (int)(scale * SpecularRepeatX + 0.5f), sb);
  285. LLSDxmlEncode2.AddElem("SpecRepeatY", (int)(scale * SpecularRepeatY + 0.5f), sb);
  286. LLSDxmlEncode2.AddElem("SpecRotation", (int)(scale * SpecularRotation + 0.5f), sb);
  287. LLSDxmlEncode2.AddArray("SpecColor", sb);
  288. LLSDxmlEncode2.AddElem(SpecularLightColorR, sb);
  289. LLSDxmlEncode2.AddElem(SpecularLightColorG, sb);
  290. LLSDxmlEncode2.AddElem(SpecularLightColorB, sb);
  291. LLSDxmlEncode2.AddElem(255, sb);
  292. LLSDxmlEncode2.AddEndArray(sb);
  293. LLSDxmlEncode2.AddElem("SpecExp", SpecularLightExponent, sb);
  294. LLSDxmlEncode2.AddElem("EnvIntensity", EnvironmentIntensity, sb);
  295. LLSDxmlEncode2.AddElem("DiffuseAlphaMode", DiffuseAlphaMode, sb);
  296. LLSDxmlEncode2.AddElem("AlphaMaskCutoff", AlphaMaskCutoff, sb);
  297. LLSDxmlEncode2.AddEndMap(sb);
  298. if (fullLLSD)
  299. {
  300. return LLSDxmlEncode2.EndToBytes(sb);
  301. }
  302. else
  303. return Utils.EmptyBytes; // ignored if appending
  304. }
  305. }
  306. }