SOPMaterial.cs 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331
  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. inthash ^= NormalMapID.GetHashCode();
  194. inthash ^= SpecularMapID.GetHashCode();
  195. }
  196. validinthash = true;
  197. }
  198. return inthash;
  199. }
  200. public override bool Equals(Object o)
  201. {
  202. if (o == null || !(o is FaceMaterial))
  203. return false;
  204. FaceMaterial other = (FaceMaterial)o;
  205. return (
  206. DiffuseAlphaMode == other.DiffuseAlphaMode
  207. && AlphaMaskCutoff == other.AlphaMaskCutoff
  208. && SpecularLightExponent == other.SpecularLightExponent
  209. && EnvironmentIntensity == other.EnvironmentIntensity
  210. && NormalMapID == other.NormalMapID
  211. && NormalOffsetX == other.NormalOffsetX
  212. && NormalOffsetY == other.NormalOffsetY
  213. && NormalRepeatX == other.NormalRepeatX
  214. && NormalRepeatY == other.NormalRepeatY
  215. && NormalRotation == other.NormalRotation
  216. && SpecularMapID == other.SpecularMapID
  217. && SpecularOffsetX == other.SpecularOffsetX
  218. && SpecularOffsetY == other.SpecularOffsetY
  219. && SpecularRepeatX == other.SpecularRepeatX
  220. && SpecularRepeatY == other.SpecularRepeatY
  221. && SpecularRotation == other.SpecularRotation
  222. && SpecularLightColorR == other.SpecularLightColorR
  223. && SpecularLightColorG == other.SpecularLightColorG
  224. && SpecularLightColorB == other.SpecularLightColorB
  225. );
  226. }
  227. public OSDMap toOSD()
  228. {
  229. OSDMap mat = new OSDMap();
  230. float scale = 10000f;
  231. mat["NormMap"] = NormalMapID;
  232. mat["NormOffsetX"] = (int)(scale * NormalOffsetX);
  233. mat["NormOffsetY"] = (int)(scale * NormalOffsetY);
  234. mat["NormRepeatX"] = (int)(scale * NormalRepeatX);
  235. mat["NormRepeatY"] = (int)(scale * NormalRepeatY);
  236. mat["NormRotation"] = (int)(scale * NormalRotation);
  237. mat["SpecMap"] = SpecularMapID;
  238. mat["SpecOffsetX"] = (int)(scale * SpecularOffsetX);
  239. mat["SpecOffsetY"] = (int)(scale * SpecularOffsetY);
  240. mat["SpecRepeatX"] = (int)(scale * SpecularRepeatX);
  241. mat["SpecRepeatY"] = (int)(scale * SpecularRepeatY);
  242. mat["SpecRotation"] = (int)(scale * SpecularRotation);
  243. OSDArray carray = new OSDArray(4);
  244. carray.Add(SpecularLightColorR);
  245. carray.Add(SpecularLightColorG);
  246. carray.Add(SpecularLightColorB);
  247. carray.Add(255); // solid color
  248. mat["SpecColor"] = carray;
  249. mat["SpecExp"] = SpecularLightExponent;
  250. mat["EnvIntensity"] = EnvironmentIntensity;
  251. mat["DiffuseAlphaMode"] = DiffuseAlphaMode;
  252. mat["AlphaMaskCutoff"] = AlphaMaskCutoff;
  253. return mat;
  254. }
  255. public byte[] toLLSDxml(osUTF8 sb = null)
  256. {
  257. const float scale = 10000f;
  258. bool fullLLSD = false;
  259. if (sb == null)
  260. {
  261. sb = LLSDxmlEncode2.Start(1024, false);
  262. fullLLSD = true;
  263. }
  264. LLSDxmlEncode2.AddMap(sb);
  265. LLSDxmlEncode2.AddElem("NormMap", NormalMapID, sb);
  266. LLSDxmlEncode2.AddElem("NormOffsetX", (int)(scale * NormalOffsetX + 0.5f), sb);
  267. LLSDxmlEncode2.AddElem("NormOffsetY", (int)(scale * NormalOffsetY + 0.5f), sb);
  268. LLSDxmlEncode2.AddElem("NormRepeatX", (int)(scale * NormalRepeatX + 0.5f), sb);
  269. LLSDxmlEncode2.AddElem("NormRepeatY", (int)(scale * NormalRepeatY + 0.5f), sb);
  270. LLSDxmlEncode2.AddElem("NormRotation", (int)(scale * NormalRotation + 0.5f), sb);
  271. LLSDxmlEncode2.AddElem("SpecMap", SpecularMapID, sb);
  272. LLSDxmlEncode2.AddElem("SpecOffsetX", (int)(scale * SpecularOffsetX + 0.5f), sb);
  273. LLSDxmlEncode2.AddElem("SpecOffsetY", (int)(scale * SpecularOffsetY + 0.5f), sb);
  274. LLSDxmlEncode2.AddElem("SpecRepeatX", (int)(scale * SpecularRepeatX + 0.5f), sb);
  275. LLSDxmlEncode2.AddElem("SpecRepeatY", (int)(scale * SpecularRepeatY + 0.5f), sb);
  276. LLSDxmlEncode2.AddElem("SpecRotation", (int)(scale * SpecularRotation + 0.5f), sb);
  277. LLSDxmlEncode2.AddArray("SpecColor", sb);
  278. LLSDxmlEncode2.AddElem(SpecularLightColorR, sb);
  279. LLSDxmlEncode2.AddElem(SpecularLightColorG, sb);
  280. LLSDxmlEncode2.AddElem(SpecularLightColorB, sb);
  281. LLSDxmlEncode2.AddElem(255, sb);
  282. LLSDxmlEncode2.AddEndArray(sb);
  283. LLSDxmlEncode2.AddElem("SpecExp", SpecularLightExponent, sb);
  284. LLSDxmlEncode2.AddElem("EnvIntensity", EnvironmentIntensity, sb);
  285. LLSDxmlEncode2.AddElem("DiffuseAlphaMode", DiffuseAlphaMode, sb);
  286. LLSDxmlEncode2.AddElem("AlphaMaskCutoff", AlphaMaskCutoff, sb);
  287. LLSDxmlEncode2.AddEndMap(sb);
  288. if (fullLLSD)
  289. {
  290. return LLSDxmlEncode2.EndToBytes(sb);
  291. }
  292. else
  293. return Utils.EmptyBytes; // ignored if appending
  294. }
  295. }
  296. }