PrimitiveBaseShape.cs 55 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692
  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.Collections.Generic;
  29. using System.IO;
  30. using System.Reflection;
  31. using System.Xml;
  32. using System.Xml.Schema;
  33. using System.Xml.Serialization;
  34. using System.Runtime.CompilerServices;
  35. using log4net;
  36. using OpenMetaverse;
  37. using OpenMetaverse.StructuredData;
  38. namespace OpenSim.Framework
  39. {
  40. public enum ProfileShape : byte
  41. {
  42. Circle = 0,
  43. Square = 1,
  44. IsometricTriangle = 2,
  45. EquilateralTriangle = 3,
  46. RightTriangle = 4,
  47. HalfCircle = 5
  48. }
  49. public enum HollowShape : byte
  50. {
  51. Same = 0,
  52. Circle = 16,
  53. Square = 32,
  54. Triangle = 48
  55. }
  56. public enum PCodeEnum : byte
  57. {
  58. Primitive = 9,
  59. Avatar = 47,
  60. Grass = 95,
  61. NewTree = 111,
  62. ParticleSystem = 143,
  63. Tree = 255
  64. }
  65. public enum Extrusion : byte
  66. {
  67. Straight = 16,
  68. Curve1 = 32,
  69. Curve2 = 48,
  70. Flexible = 128
  71. }
  72. [Serializable]
  73. public class PrimitiveBaseShape
  74. {
  75. private static readonly ILog m_log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
  76. private static readonly byte[] DEFAULT_TEXTURE = new Primitive.TextureEntry(new UUID("89556747-24cb-43ed-920b-47caed15465f")).GetBytes();
  77. private byte[] m_textureEntry;
  78. private ushort _pathBegin;
  79. private byte _pathCurve;
  80. private ushort _pathEnd;
  81. private sbyte _pathRadiusOffset;
  82. private byte _pathRevolutions;
  83. private byte _pathScaleX;
  84. private byte _pathScaleY;
  85. private byte _pathShearX;
  86. private byte _pathShearY;
  87. private sbyte _pathSkew;
  88. private sbyte _pathTaperX;
  89. private sbyte _pathTaperY;
  90. private sbyte _pathTwist;
  91. private sbyte _pathTwistBegin;
  92. private byte _pCode;
  93. private ushort _profileBegin;
  94. private ushort _profileEnd;
  95. private ushort _profileHollow;
  96. private Vector3 _scale;
  97. private byte _state;
  98. private byte _lastattach;
  99. private ProfileShape _profileShape;
  100. private HollowShape _hollowShape;
  101. //extra parameters Sculpted
  102. [XmlIgnore] private UUID _sculptTexture;
  103. [XmlIgnore] private byte _sculptType;
  104. [XmlIgnore] private byte[] _sculptData = Utils.EmptyBytes;
  105. //extra parameters Flexi
  106. [XmlIgnore] private int _flexiSoftness;
  107. [XmlIgnore] private float _flexiTension;
  108. [XmlIgnore] private float _flexiDrag;
  109. [XmlIgnore] private float _flexiGravity;
  110. [XmlIgnore] private float _flexiWind;
  111. [XmlIgnore] private float _flexiForceX;
  112. [XmlIgnore] private float _flexiForceY;
  113. [XmlIgnore] private float _flexiForceZ;
  114. //extra parameters light
  115. [XmlIgnore] private float _lightColorR;
  116. [XmlIgnore] private float _lightColorG;
  117. [XmlIgnore] private float _lightColorB;
  118. [XmlIgnore] private float _lightColorA = 1.0f;
  119. [XmlIgnore] private float _lightRadius;
  120. [XmlIgnore] private float _lightCutoff;
  121. [XmlIgnore] private float _lightFalloff;
  122. [XmlIgnore] private float _lightIntensity = 1.0f;
  123. //extra parameters Projection
  124. [XmlIgnore] private UUID _projectionTextureID;
  125. [XmlIgnore] private float _projectionFOV;
  126. [XmlIgnore] private float _projectionFocus;
  127. [XmlIgnore] private float _projectionAmb;
  128. //extra parameters extramesh/flag
  129. [XmlIgnore] private uint _meshFlags;
  130. [XmlIgnore] private bool _flexiEntry;
  131. [XmlIgnore] private bool _lightEntry;
  132. [XmlIgnore] private bool _sculptEntry;
  133. [XmlIgnore] private bool _projectionEntry;
  134. [XmlIgnore] private bool _meshFlagsEntry;
  135. [XmlIgnore]
  136. public Primitive.ReflectionProbe ReflectionProbe = null;
  137. [XmlIgnore]
  138. public Primitive.RenderMaterials RenderMaterials = null;
  139. public bool MeshFlagEntry
  140. {
  141. get { return _meshFlagsEntry; }
  142. }
  143. public bool AnimeshEnabled
  144. {
  145. get
  146. {
  147. return(_meshFlagsEntry &&
  148. (_meshFlags & 0x01) != 0 &&
  149. (_sculptType & 0x07) == (int)OpenMetaverse.SculptType.Mesh);
  150. }
  151. set
  152. {
  153. if((_sculptType & 0x07) != (int)OpenMetaverse.SculptType.Mesh)
  154. {
  155. _meshFlagsEntry = false;
  156. _meshFlags = 0;
  157. return;
  158. }
  159. if (value)
  160. {
  161. _meshFlagsEntry = true;
  162. _meshFlags |= 1;
  163. }
  164. else
  165. _meshFlags &= 0xfe;
  166. }
  167. }
  168. public byte ProfileCurve
  169. {
  170. get { return (byte)((byte)HollowShape | (byte)ProfileShape); }
  171. set
  172. {
  173. // Handle hollow shape component
  174. byte hollowShapeByte = (byte)(value & 0xf0);
  175. if (!Enum.IsDefined(typeof(HollowShape), hollowShapeByte))
  176. {
  177. m_log.WarnFormat(
  178. "[SHAPE]: Attempt to set a ProfileCurve with a hollow shape value of {0}, which isn't a valid enum. Replacing with default shape.",
  179. hollowShapeByte);
  180. _hollowShape = HollowShape.Same;
  181. }
  182. else
  183. {
  184. _hollowShape = (HollowShape)hollowShapeByte;
  185. }
  186. // Handle profile shape component
  187. byte profileShapeByte = (byte)(value & 0xf);
  188. if (!Enum.IsDefined(typeof(ProfileShape), profileShapeByte))
  189. {
  190. m_log.WarnFormat(
  191. "[SHAPE]: Attempt to set a ProfileCurve with a profile shape value of {0}, which isn't a valid enum. Replacing with square.",
  192. profileShapeByte);
  193. _profileShape = ProfileShape.Square;
  194. }
  195. else
  196. {
  197. _profileShape = (ProfileShape)profileShapeByte;
  198. }
  199. }
  200. }
  201. /// <summary>
  202. /// Entries to store media textures on each face
  203. /// </summary>
  204. /// Do not change this value directly - always do it through an IMoapModule.
  205. /// Lock before manipulating.
  206. public MediaList Media { get; set; }
  207. public PrimitiveBaseShape()
  208. {
  209. PCode = (byte)PCodeEnum.Primitive;
  210. m_textureEntry = DEFAULT_TEXTURE;
  211. }
  212. /// <summary>
  213. /// Construct a PrimitiveBaseShape object from a OpenMetaverse.Primitive object
  214. /// </summary>
  215. /// <param name="prim"></param>
  216. public PrimitiveBaseShape(Primitive prim)
  217. {
  218. //m_log.DebugFormat("[PRIMITIVE BASE SHAPE]: Creating from {0}", prim.ID);
  219. PCode = (byte)prim.PrimData.PCode;
  220. State = prim.PrimData.State;
  221. LastAttachPoint = prim.PrimData.State;
  222. PathBegin = Primitive.PackBeginCut(prim.PrimData.PathBegin);
  223. PathEnd = Primitive.PackEndCut(prim.PrimData.PathEnd);
  224. PathScaleX = Primitive.PackPathScale(prim.PrimData.PathScaleX);
  225. PathScaleY = Primitive.PackPathScale(prim.PrimData.PathScaleY);
  226. PathShearX = (byte)Primitive.PackPathShear(prim.PrimData.PathShearX);
  227. PathShearY = (byte)Primitive.PackPathShear(prim.PrimData.PathShearY);
  228. PathSkew = Primitive.PackPathTwist(prim.PrimData.PathSkew);
  229. ProfileBegin = Primitive.PackBeginCut(prim.PrimData.ProfileBegin);
  230. ProfileEnd = Primitive.PackEndCut(prim.PrimData.ProfileEnd);
  231. Scale = prim.Scale;
  232. PathCurve = (byte)prim.PrimData.PathCurve;
  233. ProfileCurve = (byte)prim.PrimData.ProfileCurve;
  234. ProfileHollow = Primitive.PackProfileHollow(prim.PrimData.ProfileHollow);
  235. PathRadiusOffset = Primitive.PackPathTwist(prim.PrimData.PathRadiusOffset);
  236. PathRevolutions = Primitive.PackPathRevolutions(prim.PrimData.PathRevolutions);
  237. PathTaperX = Primitive.PackPathTaper(prim.PrimData.PathTaperX);
  238. PathTaperY = Primitive.PackPathTaper(prim.PrimData.PathTaperY);
  239. PathTwist = Primitive.PackPathTwist(prim.PrimData.PathTwist);
  240. PathTwistBegin = Primitive.PackPathTwist(prim.PrimData.PathTwistBegin);
  241. m_textureEntry = prim.Textures.GetBytes();
  242. if (prim.Sculpt != null)
  243. {
  244. SculptEntry = (prim.Sculpt.Type != OpenMetaverse.SculptType.None);
  245. SculptData = prim.Sculpt.GetBytes();
  246. SculptTexture = prim.Sculpt.SculptTexture;
  247. SculptType = (byte)prim.Sculpt.Type;
  248. }
  249. else
  250. {
  251. SculptType = (byte)OpenMetaverse.SculptType.None;
  252. }
  253. }
  254. [XmlIgnore]
  255. public Primitive.TextureEntry Textures
  256. {
  257. get
  258. {
  259. //m_log.DebugFormat("[SHAPE]: get m_textureEntry length {0}", m_textureEntry.Length);
  260. try { return new Primitive.TextureEntry(m_textureEntry, 0, m_textureEntry.Length); }
  261. catch { }
  262. m_log.Warn("[SHAPE]: Failed to decode texture, length=" + ((m_textureEntry != null) ? m_textureEntry.Length : 0));
  263. return new Primitive.TextureEntry(UUID.Zero);
  264. }
  265. set { m_textureEntry = value.GetBytes(); }
  266. }
  267. public byte[] TextureEntry
  268. {
  269. get { return m_textureEntry; }
  270. set
  271. {
  272. if (value == null)
  273. m_textureEntry = new byte[1];
  274. else
  275. m_textureEntry = value;
  276. }
  277. }
  278. public static PrimitiveBaseShape Default
  279. {
  280. get
  281. {
  282. PrimitiveBaseShape boxShape = CreateBox();
  283. boxShape.SetScale(0.5f);
  284. return boxShape;
  285. }
  286. }
  287. public static PrimitiveBaseShape Create()
  288. {
  289. return new PrimitiveBaseShape();
  290. }
  291. public static PrimitiveBaseShape CreateBox()
  292. {
  293. PrimitiveBaseShape shape = new()
  294. {
  295. _pathCurve = (byte)Extrusion.Straight,
  296. _profileShape = ProfileShape.Square,
  297. _pathScaleX = 100,
  298. _pathScaleY = 100
  299. };
  300. return shape;
  301. }
  302. public static PrimitiveBaseShape CreateSphere()
  303. {
  304. PrimitiveBaseShape shape = new()
  305. {
  306. _pathCurve = (byte)Extrusion.Curve1,
  307. _profileShape = ProfileShape.HalfCircle,
  308. _pathScaleX = 100,
  309. _pathScaleY = 100
  310. };
  311. return shape;
  312. }
  313. public static PrimitiveBaseShape CreateCylinder()
  314. {
  315. PrimitiveBaseShape shape = new()
  316. {
  317. _pathCurve = (byte)Extrusion.Curve1,
  318. _profileShape = ProfileShape.Square,
  319. _pathScaleX = 100,
  320. _pathScaleY = 100
  321. };
  322. return shape;
  323. }
  324. public static PrimitiveBaseShape CreateMesh(int numberOfFaces, UUID meshAssetID)
  325. {
  326. PrimitiveBaseShape shape = new()
  327. {
  328. _pathScaleX = 100,
  329. _pathScaleY = 100
  330. };
  331. if (numberOfFaces <= 0) // oops ?
  332. numberOfFaces = 1;
  333. switch(numberOfFaces)
  334. {
  335. case 1: // torus
  336. shape.ProfileCurve = (byte)ProfileShape.Circle | (byte)HollowShape.Triangle;
  337. shape.PathCurve = (byte)Extrusion.Curve1;
  338. shape._pathScaleY = 150;
  339. break;
  340. case 2: // torus with hollow (a sl viewer whould see 4 faces on a hollow sphere)
  341. shape.ProfileCurve = (byte)ProfileShape.Circle | (byte)HollowShape.Triangle;
  342. shape.PathCurve = (byte)Extrusion.Curve1;
  343. shape.ProfileHollow = 27500;
  344. shape._pathScaleY = 150;
  345. break;
  346. case 3: // cylinder
  347. shape.ProfileCurve = (byte)ProfileShape.Circle | (byte)HollowShape.Triangle;
  348. shape.PathCurve = (byte)Extrusion.Straight;
  349. break;
  350. case 4: // cylinder with hollow
  351. shape.ProfileCurve = (byte)ProfileShape.Circle | (byte)HollowShape.Triangle;
  352. shape.PathCurve = (byte)Extrusion.Straight;
  353. shape.ProfileHollow = 27500;
  354. break;
  355. case 5: // prism
  356. shape.ProfileCurve = (byte)ProfileShape.EquilateralTriangle | (byte)HollowShape.Triangle;
  357. shape.PathCurve = (byte)Extrusion.Straight;
  358. break;
  359. case 6: // box
  360. shape.ProfileCurve = (byte)ProfileShape.Square | (byte)HollowShape.Triangle;
  361. shape.PathCurve = (byte)Extrusion.Straight;
  362. break;
  363. case 7: // box with hollow
  364. shape.ProfileCurve = (byte)ProfileShape.Square | (byte)HollowShape.Triangle;
  365. shape.PathCurve = (byte)Extrusion.Straight;
  366. shape.ProfileHollow = 27500;
  367. break;
  368. default: // 8 faces box with cut
  369. shape.ProfileCurve = (byte)ProfileShape.Square | (byte)HollowShape.Triangle;
  370. shape.PathCurve = (byte)Extrusion.Straight;
  371. shape.ProfileBegin = 9375;
  372. break;
  373. }
  374. shape.SculptEntry = true;
  375. shape.SculptType = (byte)OpenMetaverse.SculptType.Mesh;
  376. shape.SculptTexture = meshAssetID;
  377. return shape;
  378. }
  379. public void SetScale(float side)
  380. {
  381. _scale = new Vector3(side, side, side);
  382. }
  383. public void SetHeigth(float height)
  384. {
  385. _scale.Z = height;
  386. }
  387. public void SetRadius(float radius)
  388. {
  389. _scale.X = _scale.Y = radius * 2f;
  390. }
  391. // TODO: void returns need to change of course
  392. public virtual void GetMesh()
  393. {
  394. }
  395. public PrimitiveBaseShape Copy()
  396. {
  397. return (PrimitiveBaseShape) MemberwiseClone();
  398. }
  399. public static PrimitiveBaseShape CreateCylinder(float radius, float heigth)
  400. {
  401. PrimitiveBaseShape shape = CreateCylinder();
  402. shape.SetHeigth(heigth);
  403. shape.SetRadius(radius);
  404. return shape;
  405. }
  406. public void SetPathRange(Vector3 pathRange)
  407. {
  408. _pathBegin = Primitive.PackBeginCut(pathRange.X);
  409. _pathEnd = Primitive.PackEndCut(pathRange.Y);
  410. }
  411. public void SetPathRange(float begin, float end)
  412. {
  413. _pathBegin = Primitive.PackBeginCut(begin);
  414. _pathEnd = Primitive.PackEndCut(end);
  415. }
  416. public void SetSculptProperties(byte sculptType, UUID SculptTextureUUID)
  417. {
  418. _sculptType = sculptType;
  419. _sculptTexture = SculptTextureUUID;
  420. }
  421. public void SetProfileRange(Vector3 profileRange)
  422. {
  423. _profileBegin = Primitive.PackBeginCut(profileRange.X);
  424. _profileEnd = Primitive.PackEndCut(profileRange.Y);
  425. }
  426. public void SetProfileRange(float begin, float end)
  427. {
  428. _profileBegin = Primitive.PackBeginCut(begin);
  429. _profileEnd = Primitive.PackEndCut(end);
  430. }
  431. public byte[] ExtraParams
  432. {
  433. get
  434. {
  435. return ExtraParamsToBytes();
  436. }
  437. set
  438. {
  439. ReadInExtraParamsBytes(value);
  440. }
  441. }
  442. public ushort PathBegin
  443. {
  444. get { return _pathBegin; }
  445. set { _pathBegin = value; }
  446. }
  447. public byte PathCurve
  448. {
  449. get { return _pathCurve; }
  450. set { _pathCurve = value; }
  451. }
  452. public ushort PathEnd
  453. {
  454. get { return _pathEnd; }
  455. set { _pathEnd = value; }
  456. }
  457. public sbyte PathRadiusOffset
  458. {
  459. get { return _pathRadiusOffset; }
  460. set { _pathRadiusOffset = value; }
  461. }
  462. public byte PathRevolutions
  463. {
  464. get { return _pathRevolutions; }
  465. set { _pathRevolutions = value; }
  466. }
  467. public byte PathScaleX
  468. {
  469. get { return _pathScaleX; }
  470. set { _pathScaleX = value; }
  471. }
  472. public byte PathScaleY
  473. {
  474. get { return _pathScaleY; }
  475. set { _pathScaleY = value; }
  476. }
  477. public byte PathShearX
  478. {
  479. get { return _pathShearX; }
  480. set { _pathShearX = value; }
  481. }
  482. public byte PathShearY
  483. {
  484. get { return _pathShearY; }
  485. set { _pathShearY = value; }
  486. }
  487. public sbyte PathSkew
  488. {
  489. get { return _pathSkew; }
  490. set { _pathSkew = value; }
  491. }
  492. public sbyte PathTaperX
  493. {
  494. get { return _pathTaperX; }
  495. set { _pathTaperX = value; }
  496. }
  497. public sbyte PathTaperY
  498. {
  499. get { return _pathTaperY; }
  500. set { _pathTaperY = value; }
  501. }
  502. public sbyte PathTwist
  503. {
  504. get { return _pathTwist; }
  505. set { _pathTwist = value; }
  506. }
  507. public sbyte PathTwistBegin
  508. {
  509. get { return _pathTwistBegin; }
  510. set { _pathTwistBegin = value; }
  511. }
  512. public byte PCode
  513. {
  514. get { return _pCode; }
  515. set { _pCode = value; }
  516. }
  517. public ushort ProfileBegin
  518. {
  519. get { return _profileBegin; }
  520. set { _profileBegin = value; }
  521. }
  522. public ushort ProfileEnd
  523. {
  524. get { return _profileEnd; }
  525. set { _profileEnd = value; }
  526. }
  527. public ushort ProfileHollow
  528. {
  529. get { return _profileHollow; }
  530. set { _profileHollow = value; }
  531. }
  532. public Vector3 Scale
  533. {
  534. get { return _scale; }
  535. set { _scale = value; }
  536. }
  537. public byte State
  538. {
  539. get { return _state; }
  540. set { _state = value; }
  541. }
  542. public byte LastAttachPoint
  543. {
  544. get { return _lastattach; }
  545. set { _lastattach = value; }
  546. }
  547. public ProfileShape ProfileShape
  548. {
  549. get { return _profileShape; }
  550. set { _profileShape = value; }
  551. }
  552. public HollowShape HollowShape
  553. {
  554. get { return _hollowShape; }
  555. set { _hollowShape = value; }
  556. }
  557. public UUID SculptTexture
  558. {
  559. get { return _sculptTexture; }
  560. set { _sculptTexture = value; }
  561. }
  562. public byte SculptType
  563. {
  564. get { return _sculptType; }
  565. set { _sculptType = value; }
  566. }
  567. // This is only used at runtime. For sculpties this holds the texture data, and for meshes
  568. // the mesh data.
  569. public byte[] SculptData
  570. {
  571. get { return _sculptData; }
  572. set { _sculptData = value; }
  573. }
  574. public int FlexiSoftness
  575. {
  576. get { return _flexiSoftness; }
  577. set { _flexiSoftness = value; }
  578. }
  579. public float FlexiTension
  580. {
  581. get { return _flexiTension; }
  582. set { _flexiTension = value; }
  583. }
  584. public float FlexiDrag
  585. {
  586. get { return _flexiDrag; }
  587. set { _flexiDrag = value; }
  588. }
  589. public float FlexiGravity
  590. {
  591. get { return _flexiGravity; }
  592. set { _flexiGravity = value; }
  593. }
  594. public float FlexiWind
  595. {
  596. get { return _flexiWind; }
  597. set { _flexiWind = value; }
  598. }
  599. public float FlexiForceX
  600. {
  601. get { return _flexiForceX; }
  602. set { _flexiForceX = value; }
  603. }
  604. public float FlexiForceY
  605. {
  606. get { return _flexiForceY; }
  607. set { _flexiForceY = value; }
  608. }
  609. public float FlexiForceZ
  610. {
  611. get { return _flexiForceZ; }
  612. set { _flexiForceZ = value; }
  613. }
  614. public float LightColorR
  615. {
  616. get { return _lightColorR; }
  617. set
  618. {
  619. if (value < 0)
  620. _lightColorR = 0;
  621. else if (value > 1.0f)
  622. _lightColorR = 1.0f;
  623. else
  624. _lightColorR = value;
  625. }
  626. }
  627. public float LightColorG
  628. {
  629. get { return _lightColorG; }
  630. set
  631. {
  632. if (value < 0)
  633. _lightColorG = 0;
  634. else if (value > 1.0f)
  635. _lightColorG = 1.0f;
  636. else
  637. _lightColorG = value;
  638. }
  639. }
  640. public float LightColorB
  641. {
  642. get { return _lightColorB; }
  643. set
  644. {
  645. if (value < 0)
  646. _lightColorB = 0;
  647. else if (value > 1.0f)
  648. _lightColorB = 1.0f;
  649. else
  650. _lightColorB = value;
  651. }
  652. }
  653. public float LightColorA
  654. {
  655. get { return _lightColorA; }
  656. set
  657. {
  658. if (value < 0)
  659. _lightColorA = 0;
  660. else if (value > 1.0f)
  661. _lightColorA = 1.0f;
  662. else
  663. _lightColorA = value;
  664. }
  665. }
  666. public float LightRadius
  667. {
  668. get { return _lightRadius; }
  669. set { _lightRadius = value; }
  670. }
  671. public float LightCutoff
  672. {
  673. get { return _lightCutoff; }
  674. set { _lightCutoff = value; }
  675. }
  676. public float LightFalloff
  677. {
  678. get { return _lightFalloff; }
  679. set { _lightFalloff = value; }
  680. }
  681. public float LightIntensity
  682. {
  683. get { return _lightIntensity; }
  684. set { _lightIntensity = value; }
  685. }
  686. // only means we do have flexi data
  687. public bool FlexiEntry
  688. {
  689. get { return _flexiEntry; }
  690. set { _flexiEntry = value; }
  691. }
  692. public bool LightEntry
  693. {
  694. get { return _lightEntry; }
  695. set { _lightEntry = value; }
  696. }
  697. public bool SculptEntry
  698. {
  699. get { return _sculptEntry; }
  700. set { _sculptEntry = value; }
  701. }
  702. public bool ProjectionEntry
  703. {
  704. get { return _projectionEntry; }
  705. set { _projectionEntry = value; }
  706. }
  707. public UUID ProjectionTextureUUID
  708. {
  709. get { return _projectionTextureID; }
  710. set { _projectionTextureID = value; }
  711. }
  712. public float ProjectionFOV
  713. {
  714. get { return _projectionFOV; }
  715. set { _projectionFOV = value; }
  716. }
  717. public float ProjectionFocus
  718. {
  719. get { return _projectionFocus; }
  720. set { _projectionFocus = value; }
  721. }
  722. public float ProjectionAmbiance
  723. {
  724. get { return _projectionAmb; }
  725. set { _projectionAmb = value; }
  726. }
  727. public ulong GetMeshKey(Vector3 size, float lod)
  728. {
  729. return GetMeshKey(size, lod, false);
  730. }
  731. public ulong GetMeshKey(Vector3 size, float lod, bool convex)
  732. {
  733. ulong hash = 5381;
  734. hash = djb2(hash, PathCurve);
  735. hash = djb2(hash, (byte)((byte)HollowShape | (byte)ProfileShape));
  736. hash = djb2(hash, PathBegin);
  737. hash = djb2(hash, PathEnd);
  738. hash = djb2(hash, PathScaleX);
  739. hash = djb2(hash, PathScaleY);
  740. hash = djb2(hash, PathShearX);
  741. hash = djb2(hash, PathShearY);
  742. hash = djb2(hash, (byte)PathTwist);
  743. hash = djb2(hash, (byte)PathTwistBegin);
  744. hash = djb2(hash, (byte)PathRadiusOffset);
  745. hash = djb2(hash, (byte)PathTaperX);
  746. hash = djb2(hash, (byte)PathTaperY);
  747. hash = djb2(hash, PathRevolutions);
  748. hash = djb2(hash, (byte)PathSkew);
  749. hash = djb2(hash, ProfileBegin);
  750. hash = djb2(hash, ProfileEnd);
  751. hash = djb2(hash, ProfileHollow);
  752. // TODO: Separate scale out from the primitive shape data (after
  753. // scaling is supported at the physics engine level)
  754. hash = djb2(hash, size.X);
  755. hash = djb2(hash, size.Y);
  756. hash = djb2(hash, size.Z);
  757. hash = djb2(hash, lod);
  758. // include sculpt UUID
  759. if (SculptEntry)
  760. {
  761. byte[] scaleBytes = this.SculptTexture.GetBytes();
  762. for (int i = 0; i < scaleBytes.Length; i++)
  763. hash = djb2(hash, scaleBytes[i]);
  764. }
  765. if(convex)
  766. hash = djb2(hash, 0xa5);
  767. return hash;
  768. }
  769. private static ulong djb2(ulong hash, byte c)
  770. {
  771. //return ((hash << 5) + hash) + (ulong)c;
  772. return 33 * hash + (ulong)c;
  773. }
  774. private static ulong djb2(ulong hash, ushort c)
  775. {
  776. //hash = ((hash << 5) + hash) + (ulong)((byte)c);
  777. //return ((hash << 5) + hash) + (ulong)(c >> 8);
  778. return 33 * hash + c;
  779. }
  780. private static ulong djb2(ulong hash, float c)
  781. {
  782. //hash = ((hash << 5) + hash) + (ulong)((byte)c);
  783. //return ((hash << 5) + hash) + (ulong)(c >> 8);
  784. return 33 * hash + (ulong)c.GetHashCode();
  785. }
  786. public unsafe byte[] ExtraParamsToBytes()
  787. {
  788. //m_log.DebugFormat("[EXTRAPARAMS]: Called ExtraParamsToBytes()");
  789. const byte FlexiEP = 0x10;
  790. const byte LightEP = 0x20;
  791. const byte SculptEP = 0x30;
  792. const byte ProjectionEP = 0x40;
  793. //const byte MeshEP = 0x60;
  794. const byte MeshFlagsEP = 0x70;
  795. const byte MaterialsEP = 0x80;
  796. const byte ReflectionProbeEP = 0x90;
  797. int TotalBytesLength = 1; // ExtraParamsNum
  798. uint ExtraParamsNum = 0;
  799. if (_flexiEntry)
  800. {
  801. ExtraParamsNum++;
  802. TotalBytesLength += 16 + 2 + 4;// data
  803. }
  804. if (_lightEntry)
  805. {
  806. ExtraParamsNum++;
  807. TotalBytesLength += 16 + 2 + 4; // data
  808. }
  809. if (_sculptEntry)
  810. {
  811. ExtraParamsNum++;
  812. TotalBytesLength += 17 + 2 + 4;// data
  813. }
  814. if (_projectionEntry)
  815. {
  816. ExtraParamsNum++;
  817. TotalBytesLength += 28 + 2 + 4; // data
  818. }
  819. if (_meshFlagsEntry)
  820. {
  821. ExtraParamsNum++;
  822. TotalBytesLength += 4 + 2 + 4; // data
  823. }
  824. if (ReflectionProbe != null)
  825. {
  826. ExtraParamsNum++;
  827. TotalBytesLength += 9 + 2 + 4; // data
  828. }
  829. bool hasRenderMaterials = RenderMaterials is not null && RenderMaterials.entries is not null && RenderMaterials.entries.Length > 0;
  830. if (hasRenderMaterials)
  831. {
  832. ExtraParamsNum++;
  833. TotalBytesLength += 1 + 17 * RenderMaterials.entries.Length + 2 + 4; // data
  834. }
  835. byte[] safeReturnBytes = new byte[TotalBytesLength];
  836. if(TotalBytesLength == 1)
  837. {
  838. safeReturnBytes[0] = 0;
  839. return safeReturnBytes;
  840. }
  841. fixed(byte* breturnBytes = &safeReturnBytes[0])
  842. {
  843. byte* returnBytes = breturnBytes;
  844. *returnBytes++ = (byte)ExtraParamsNum;
  845. if (_flexiEntry)
  846. {
  847. *returnBytes = FlexiEP; returnBytes += 2;// 2 bytes id code
  848. *returnBytes = 16; returnBytes += 4;// 4 bytes size
  849. // Softness is packed in the upper bits of tension and drag
  850. *returnBytes++ = (byte)(((_flexiSoftness & 2) << 6) | ((byte)(_flexiTension * 10.01f) & 0x7F));
  851. *returnBytes++ = (byte)(((_flexiSoftness & 1) << 7) | ((byte)(_flexiDrag * 10.01f) & 0x7F));
  852. *returnBytes++ = (byte)((_flexiGravity + 10.0f) * 10.01f);
  853. *returnBytes++ = (byte)(_flexiWind * 10.01f);
  854. Utils.FloatToBytes(_flexiForceX, returnBytes); returnBytes += 4;
  855. Utils.FloatToBytes(_flexiForceY, returnBytes); returnBytes += 4;
  856. Utils.FloatToBytes(_flexiForceZ, returnBytes); returnBytes += 4;
  857. }
  858. if (_lightEntry)
  859. {
  860. *returnBytes = LightEP; returnBytes += 2;
  861. *returnBytes = 16; returnBytes += 4;
  862. // Alpha channel in color is intensity
  863. *returnBytes++ = Utils.FloatZeroOneToByte(_lightColorR);
  864. *returnBytes++ = Utils.FloatZeroOneToByte(_lightColorG);
  865. *returnBytes++ = Utils.FloatZeroOneToByte(_lightColorB);
  866. *returnBytes++ = Utils.FloatZeroOneToByte(_lightIntensity);
  867. Utils.FloatToBytes(_lightRadius, returnBytes); returnBytes += 4;
  868. Utils.FloatToBytes(_lightCutoff, returnBytes); returnBytes += 4;
  869. Utils.FloatToBytes(_lightFalloff, returnBytes); returnBytes += 4;
  870. }
  871. if (_sculptEntry)
  872. {
  873. //if(_sculptType == 5)
  874. // *returnBytes = MeshEP; returnBytes += 2;
  875. //else
  876. *returnBytes = SculptEP; returnBytes += 2;
  877. *returnBytes = 17; returnBytes += 4;
  878. _sculptTexture.ToBytes(returnBytes); returnBytes += 16;
  879. *returnBytes++ = _sculptType;
  880. }
  881. if (_projectionEntry)
  882. {
  883. *returnBytes = ProjectionEP; returnBytes += 2;
  884. *returnBytes = 28; returnBytes += 4;
  885. _projectionTextureID.ToBytes(returnBytes); returnBytes += 16;
  886. Utils.FloatToBytes(_projectionFOV, returnBytes); returnBytes += 4;
  887. Utils.FloatToBytes(_projectionFocus, returnBytes); returnBytes += 4;
  888. Utils.FloatToBytes(_projectionAmb, returnBytes); returnBytes += 4;
  889. }
  890. if (_meshFlagsEntry)
  891. {
  892. *returnBytes = MeshFlagsEP; returnBytes += 2;
  893. *returnBytes = 4; returnBytes += 4;
  894. Utils.UIntToBytes(_meshFlags, returnBytes); returnBytes += 4;
  895. }
  896. if (ReflectionProbe != null)
  897. {
  898. *returnBytes = ReflectionProbeEP; returnBytes += 2;
  899. *returnBytes = 9; returnBytes += 4;
  900. Utils.FloatToBytes(ReflectionProbe.Ambiance, returnBytes); returnBytes += 4;
  901. Utils.FloatToBytes(ReflectionProbe.ClipDistance, returnBytes); returnBytes += 4;
  902. *returnBytes++ = ReflectionProbe.Flags;
  903. }
  904. if (hasRenderMaterials)
  905. {
  906. *returnBytes = MaterialsEP; returnBytes += 2;
  907. int len = 1 + 17 * RenderMaterials.entries.Length;
  908. *returnBytes++ = (byte)len;
  909. *returnBytes++ = (byte)(len >> 8);
  910. *returnBytes++ = (byte)(len >> 16);
  911. *returnBytes++ = (byte)(len >> 24);
  912. *returnBytes++ = (byte)RenderMaterials.entries.Length;
  913. for (int j = 0; j < RenderMaterials.entries.Length; ++j)
  914. {
  915. *returnBytes++ = RenderMaterials.entries[j].te_index;
  916. RenderMaterials.entries[j].id.ToBytes(returnBytes); returnBytes += 16;
  917. }
  918. }
  919. }
  920. return safeReturnBytes;
  921. }
  922. public void ReadInUpdateExtraParam(ushort type, bool inUse, byte[] data)
  923. {
  924. const ushort FlexiEP = 0x10;
  925. const ushort LightEP = 0x20;
  926. const ushort SculptEP = 0x30;
  927. const ushort ProjectionEP = 0x40;
  928. const ushort MeshEP = 0x60;
  929. const ushort MeshFlagsEP = 0x70;
  930. const ushort MaterialsEP = 0x80;
  931. const ushort ReflectionProbeEP = 0x90;
  932. switch (type)
  933. {
  934. case FlexiEP:
  935. if (!inUse)
  936. {
  937. _flexiEntry = false;
  938. return;
  939. }
  940. ReadFlexiData(data, 0);
  941. break;
  942. case LightEP:
  943. if (!inUse)
  944. {
  945. _lightEntry = false;
  946. return;
  947. }
  948. ReadLightData(data, 0);
  949. break;
  950. case MeshEP:
  951. case SculptEP:
  952. if (!inUse)
  953. {
  954. _sculptEntry = false;
  955. return;
  956. }
  957. ReadSculptData(data, 0);
  958. break;
  959. case ProjectionEP:
  960. if (!inUse)
  961. {
  962. _projectionEntry = false;
  963. return;
  964. }
  965. ReadProjectionData(data, 0);
  966. break;
  967. case MeshFlagsEP:
  968. if (!inUse)
  969. {
  970. _meshFlagsEntry = false;
  971. _meshFlags = 0;
  972. return;
  973. }
  974. ReadMeshFlagsData(data, 0);
  975. break;
  976. case ReflectionProbeEP:
  977. if (!inUse)
  978. {
  979. ReflectionProbe = null;
  980. return;
  981. }
  982. ReadReflectionProbe(data, 0);
  983. break;
  984. case MaterialsEP:
  985. if (!inUse)
  986. {
  987. RenderMaterials = null;
  988. return;
  989. }
  990. ReadRenderMaterials(data, 0, data.Length);
  991. break;
  992. }
  993. }
  994. public void ReadInExtraParamsBytes(byte[] data)
  995. {
  996. if (data == null)
  997. return;
  998. _flexiEntry = false;
  999. _lightEntry = false;
  1000. _sculptEntry = false;
  1001. _projectionEntry = false;
  1002. _meshFlagsEntry = false;
  1003. RenderMaterials = null;
  1004. ReflectionProbe = null;
  1005. if (data.Length == 1)
  1006. return;
  1007. const byte FlexiEP = 0x10;
  1008. const byte LightEP = 0x20;
  1009. const byte SculptEP = 0x30;
  1010. const byte ProjectionEP = 0x40;
  1011. const byte MeshEP = 0x60;
  1012. const byte MeshFlagsEP = 0x70;
  1013. const byte MaterialsEP = 0x80;
  1014. const byte ReflectionProbeEP = 0x90;
  1015. byte extraParamCount = data[0];
  1016. int i = 1;
  1017. for (int k = 0; k < extraParamCount; ++k)
  1018. {
  1019. byte epType = data[i];
  1020. switch (epType)
  1021. {
  1022. case FlexiEP:
  1023. i += 6;
  1024. ReadFlexiData(data, i);
  1025. i += 16;
  1026. break;
  1027. case LightEP:
  1028. i += 6;
  1029. ReadLightData(data, i);
  1030. i += 16;
  1031. break;
  1032. case MeshEP:
  1033. case SculptEP:
  1034. i += 6;
  1035. ReadSculptData(data, i);
  1036. i += 17;
  1037. break;
  1038. case ProjectionEP:
  1039. i += 6;
  1040. ReadProjectionData(data, i);
  1041. i += 28;
  1042. break;
  1043. case MeshFlagsEP:
  1044. i += 6;
  1045. ReadMeshFlagsData(data, i);
  1046. i += 4;
  1047. break;
  1048. case ReflectionProbeEP:
  1049. i += 6;
  1050. ReadReflectionProbe(data, i);
  1051. i += 9;
  1052. break;
  1053. case MaterialsEP:
  1054. i += 2;
  1055. if (data.Length - i >= 4)
  1056. {
  1057. int size = Utils.BytesToInt(data, i);
  1058. i += 4;
  1059. i += ReadRenderMaterials(data, i, size);
  1060. }
  1061. break;
  1062. }
  1063. }
  1064. }
  1065. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  1066. public void ReadSculptData(byte[] data, int pos)
  1067. {
  1068. if (data.Length-pos >= 17)
  1069. {
  1070. _sculptTexture = new UUID(data, pos);
  1071. _sculptType = data[pos + 16];
  1072. _sculptEntry = true;
  1073. }
  1074. else
  1075. {
  1076. _sculptEntry = false;
  1077. _sculptTexture = UUID.Zero;
  1078. _sculptType = 0x00;
  1079. }
  1080. }
  1081. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  1082. public void ReadFlexiData(byte[] data, int pos)
  1083. {
  1084. if (data.Length-pos >= 16)
  1085. {
  1086. _flexiEntry = true;
  1087. _flexiSoftness = ((data[pos] & 0x80) >> 6) | ((data[pos + 1] & 0x80) >> 7);
  1088. _flexiTension = (float)(data[pos++] & 0x7F) / 10.0f;
  1089. _flexiDrag = (float)(data[pos++] & 0x7F) / 10.0f;
  1090. _flexiGravity = (float)(data[pos++] / 10.0f) - 10.0f;
  1091. _flexiWind = (float)data[pos++] / 10.0f;
  1092. _flexiForceX = Utils.BytesToFloat(data, pos);
  1093. _flexiForceY = Utils.BytesToFloat(data, pos + 4);
  1094. _flexiForceZ = Utils.BytesToFloat(data, pos + 8);
  1095. }
  1096. else
  1097. {
  1098. _flexiEntry = false;
  1099. _flexiSoftness = 0;
  1100. _flexiTension = 0.0f;
  1101. _flexiDrag = 0.0f;
  1102. _flexiGravity = 0.0f;
  1103. _flexiWind = 0.0f;
  1104. _flexiForceX = 0f;
  1105. _flexiForceY = 0f;
  1106. _flexiForceZ = 0f;
  1107. }
  1108. }
  1109. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  1110. public void ReadLightData(byte[] data, int pos)
  1111. {
  1112. if (data.Length - pos >= 16)
  1113. {
  1114. _lightEntry = true;
  1115. Color4 lColor = new(data, pos, false);
  1116. _lightIntensity = lColor.A;
  1117. _lightColorA = 1f;
  1118. _lightColorR = lColor.R;
  1119. _lightColorG = lColor.G;
  1120. _lightColorB = lColor.B;
  1121. _lightRadius = Utils.BytesToFloat(data, pos + 4);
  1122. _lightCutoff = Utils.BytesToFloat(data, pos + 8);
  1123. _lightFalloff = Utils.BytesToFloat(data, pos + 12);
  1124. }
  1125. else
  1126. {
  1127. _lightEntry = false;
  1128. _lightColorA = 1f;
  1129. _lightColorR = 0f;
  1130. _lightColorG = 0f;
  1131. _lightColorB = 0f;
  1132. _lightRadius = 0f;
  1133. _lightCutoff = 0f;
  1134. _lightFalloff = 0f;
  1135. _lightIntensity = 0f;
  1136. }
  1137. }
  1138. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  1139. public void ReadProjectionData(byte[] data, int pos)
  1140. {
  1141. if (data.Length - pos >= 28)
  1142. {
  1143. _projectionEntry = true;
  1144. _projectionTextureID = new UUID(data, pos);
  1145. _projectionFOV = Utils.BytesToFloat(data, pos + 16);
  1146. _projectionFocus = Utils.BytesToFloat(data, pos + 20);
  1147. _projectionAmb = Utils.BytesToFloat(data, pos + 24);
  1148. }
  1149. else
  1150. {
  1151. _projectionEntry = false;
  1152. _projectionTextureID = UUID.Zero;
  1153. _projectionFOV = 0f;
  1154. _projectionFocus = 0f;
  1155. _projectionAmb = 0f;
  1156. }
  1157. }
  1158. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  1159. public void ReadMeshFlagsData(byte[] data, int pos)
  1160. {
  1161. _meshFlagsEntry = true;
  1162. _meshFlags = data.Length - pos >= 4 ? Utils.BytesToUInt(data, pos) : 0;
  1163. }
  1164. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  1165. public void ReadReflectionProbe(byte[] data, int pos)
  1166. {
  1167. if (data.Length - pos >= 9)
  1168. {
  1169. ReflectionProbe = new Primitive.ReflectionProbe
  1170. {
  1171. Ambiance = Utils.Clamp(Utils.BytesToFloat(data, pos), 0, 1.0f),
  1172. ClipDistance = Utils.Clamp(Utils.BytesToFloat(data, pos + 4), 0, 1024f),
  1173. Flags = data[pos + 8]
  1174. };
  1175. }
  1176. }
  1177. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  1178. public int ReadRenderMaterials(byte[] data, int pos, int size)
  1179. {
  1180. if (size > 17)
  1181. {
  1182. int count = data[pos];
  1183. ++pos;
  1184. if (size >= 1 + 17 * count)
  1185. {
  1186. var entries = new Primitive.RenderMaterials.RenderMaterialEntry[count];
  1187. for (int i = 0; i < count; ++i)
  1188. {
  1189. entries[i].te_index = data[pos++];
  1190. entries[i].id = new UUID(data, pos);
  1191. pos += 16;
  1192. }
  1193. RenderMaterials ??= new Primitive.RenderMaterials();
  1194. RenderMaterials.entries = entries;
  1195. }
  1196. }
  1197. return size + 4;
  1198. }
  1199. /// <summary>
  1200. /// Creates a OpenMetaverse.Primitive and populates it with converted PrimitiveBaseShape values
  1201. /// </summary>
  1202. /// <returns></returns>
  1203. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  1204. public Primitive ToOmvPrimitive()
  1205. {
  1206. // position and rotation defaults here since they are not available in PrimitiveBaseShape
  1207. return ToOmvPrimitive(new Vector3(0.0f, 0.0f, 0.0f),
  1208. new Quaternion(0.0f, 0.0f, 0.0f, 1.0f));
  1209. }
  1210. /// <summary>
  1211. /// Creates a OpenMetaverse.Primitive and populates it with converted PrimitiveBaseShape values
  1212. /// </summary>
  1213. /// <param name="position"></param>
  1214. /// <param name="rotation"></param>
  1215. /// <returns></returns>
  1216. public Primitive ToOmvPrimitive(Vector3 position, Quaternion rotation)
  1217. {
  1218. OpenMetaverse.Primitive prim = new()
  1219. {
  1220. Scale = this.Scale,
  1221. Position = position,
  1222. Rotation = rotation
  1223. };
  1224. if (SculptEntry)
  1225. {
  1226. prim.Sculpt = new Primitive.SculptData
  1227. {
  1228. Type = (SculptType)SculptType,
  1229. SculptTexture = SculptTexture
  1230. };
  1231. }
  1232. prim.PrimData.PathShearX = PathShearX < 128 ? (float)PathShearX * 0.01f : (float)(PathShearX - 256) * 0.01f;
  1233. prim.PrimData.PathShearY = PathShearY < 128 ? (float)PathShearY * 0.01f : (float)(PathShearY - 256) * 0.01f;
  1234. prim.PrimData.PathBegin = (float)PathBegin * 2.0e-5f;
  1235. prim.PrimData.PathEnd = 1.0f - (float)PathEnd * 2.0e-5f;
  1236. prim.PrimData.PathScaleX = (200 - PathScaleX) * 0.01f;
  1237. prim.PrimData.PathScaleY = (200 - PathScaleY) * 0.01f;
  1238. prim.PrimData.PathTaperX = PathTaperX * 0.01f;
  1239. prim.PrimData.PathTaperY = PathTaperY * 0.01f;
  1240. prim.PrimData.PathTwistBegin = PathTwistBegin * 0.01f;
  1241. prim.PrimData.PathTwist = PathTwist * 0.01f;
  1242. prim.PrimData.ProfileBegin = (float)ProfileBegin * 2.0e-5f;
  1243. prim.PrimData.ProfileEnd = 1.0f - (float)ProfileEnd * 2.0e-5f;
  1244. prim.PrimData.ProfileHollow = (float)ProfileHollow * 2.0e-5f;
  1245. prim.PrimData.profileCurve = ProfileCurve;
  1246. prim.PrimData.ProfileHole = (HoleType)HollowShape;
  1247. prim.PrimData.PathCurve = (PathCurve)PathCurve;
  1248. prim.PrimData.PathRadiusOffset = 0.01f * PathRadiusOffset;
  1249. prim.PrimData.PathRevolutions = 1.0f + 0.015f * PathRevolutions;
  1250. prim.PrimData.PathSkew = 0.01f * PathSkew;
  1251. prim.PrimData.PCode = OpenMetaverse.PCode.Prim;
  1252. prim.PrimData.State = 0;
  1253. if (FlexiEntry)
  1254. {
  1255. prim.Flexible = new Primitive.FlexibleData
  1256. {
  1257. Drag = FlexiDrag,
  1258. Force = new Vector3(FlexiForceX, FlexiForceY, FlexiForceZ),
  1259. Gravity = FlexiGravity,
  1260. Softness = FlexiSoftness,
  1261. Tension = FlexiTension,
  1262. Wind = FlexiWind
  1263. };
  1264. }
  1265. if (LightEntry)
  1266. {
  1267. prim.Light = new Primitive.LightData
  1268. {
  1269. Color = new Color4(LightColorR, LightColorG, LightColorB, LightColorA),
  1270. Cutoff = LightCutoff,
  1271. Falloff = LightFalloff,
  1272. Intensity = LightIntensity,
  1273. Radius = LightRadius
  1274. };
  1275. }
  1276. prim.Textures = Textures;
  1277. prim.Properties = new Primitive.ObjectProperties
  1278. {
  1279. Name = "Object",
  1280. Description = "",
  1281. CreatorID = UUID.Zero,
  1282. GroupID = UUID.Zero,
  1283. OwnerID = UUID.Zero,
  1284. Permissions = new Permissions(),
  1285. SalePrice = 10,
  1286. SaleType = new SaleType()
  1287. };
  1288. return prim;
  1289. }
  1290. public byte[] RenderMaterialsOvrToRawBin()
  1291. {
  1292. // byte: number of entries
  1293. // repeat:
  1294. // byte; entry face index
  1295. // byte; low entry override utf8 length
  1296. // byte: high entry override utf8 length
  1297. // utf8 bytes: override
  1298. if (RenderMaterials is null)
  1299. return null;
  1300. if (RenderMaterials.overrides is null || RenderMaterials.overrides.Length == 0)
  1301. return new byte[] { 0 }; // store so outdated viewer caches can be updated
  1302. int nentries = 0;
  1303. for (int i = 0; i < RenderMaterials.overrides.Length; i++)
  1304. {
  1305. if (!string.IsNullOrEmpty(RenderMaterials.overrides[i].data))
  1306. nentries++;
  1307. }
  1308. if(nentries == 0)
  1309. return new byte[] { 0 };
  1310. osUTF8 sb = OSUTF8Cached.Acquire();
  1311. sb.Append((byte)nentries);
  1312. for (int i = 0; i < RenderMaterials.overrides.Length; i++)
  1313. {
  1314. if (string.IsNullOrEmpty(RenderMaterials.overrides[i].data))
  1315. continue;
  1316. sb.Append(RenderMaterials.overrides[i].te_index);
  1317. int len = RenderMaterials.overrides[i].data.Length;
  1318. sb.Append((byte)(len & 0xff));
  1319. sb.Append((byte)((len >> 8) & 0xff));
  1320. sb.Append(RenderMaterials.overrides[i].data);
  1321. }
  1322. return OSUTF8Cached.GetArrayAndRelease(sb);
  1323. }
  1324. public void RenderMaterialsOvrFromRawBin(byte[] data)
  1325. {
  1326. if (RenderMaterials is not null && RenderMaterials.overrides is not null)
  1327. RenderMaterials.overrides = null;
  1328. if (data is null || data.Length < 1)
  1329. return;
  1330. int nentries = data[0];
  1331. if (nentries > 128)
  1332. return;
  1333. if (nentries == 0) // for outdated viewer caches
  1334. {
  1335. RenderMaterials ??= new Primitive.RenderMaterials();
  1336. return;
  1337. }
  1338. int indx = 1;
  1339. Primitive.RenderMaterials.RenderMaterialOverrideEntry[] overrides = new Primitive.RenderMaterials.RenderMaterialOverrideEntry[nentries];
  1340. try
  1341. {
  1342. for(int i = 0; i < overrides.Length; i++)
  1343. {
  1344. overrides[i].te_index = data[indx++];
  1345. int ovrlen = data[indx++];
  1346. ovrlen += data[indx++] << 8;
  1347. overrides[i].data = Utils.BytesToString(data,indx, ovrlen);
  1348. if(overrides[i].data.StartsWith("{\"asset")) // ignore old test data
  1349. return;
  1350. indx += ovrlen;
  1351. }
  1352. }
  1353. catch
  1354. {
  1355. return;
  1356. }
  1357. RenderMaterials ??= new Primitive.RenderMaterials();
  1358. RenderMaterials.overrides = overrides;
  1359. }
  1360. /// <summary>
  1361. /// Encapsulates a list of media entries.
  1362. /// </summary>
  1363. /// This class is necessary because we want to replace auto-serialization of MediaEntry with something more
  1364. /// OSD like and less vulnerable to change.
  1365. public class MediaList : List<MediaEntry>, IXmlSerializable
  1366. {
  1367. public const string MEDIA_TEXTURE_TYPE = "sl";
  1368. public MediaList() : base() {}
  1369. public MediaList(IEnumerable<MediaEntry> collection) : base(collection) {}
  1370. public MediaList(int capacity) : base(capacity) {}
  1371. public XmlSchema GetSchema()
  1372. {
  1373. return null;
  1374. }
  1375. public string ToXml()
  1376. {
  1377. lock (this)
  1378. {
  1379. using (StringWriter sw = new())
  1380. {
  1381. using (XmlTextWriter xtw = new(sw))
  1382. {
  1383. xtw.WriteStartElement("OSMedia");
  1384. xtw.WriteAttributeString("type", MEDIA_TEXTURE_TYPE);
  1385. xtw.WriteAttributeString("version", "0.1");
  1386. OSDArray meArray = new();
  1387. foreach (MediaEntry me in this)
  1388. {
  1389. OSD osd = (null == me ? new OSD() : me.GetOSD());
  1390. meArray.Add(osd);
  1391. }
  1392. xtw.WriteStartElement("OSData");
  1393. xtw.WriteRaw(OSDParser.SerializeLLSDXmlString(meArray));
  1394. xtw.WriteEndElement();
  1395. xtw.WriteEndElement();
  1396. xtw.Flush();
  1397. return sw.ToString();
  1398. }
  1399. }
  1400. }
  1401. }
  1402. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  1403. public void WriteXml(XmlWriter writer)
  1404. {
  1405. writer.WriteRaw(ToXml());
  1406. }
  1407. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  1408. public static MediaList FromXml(string rawXml)
  1409. {
  1410. MediaList ml = new();
  1411. ml.ReadXml(rawXml);
  1412. if(ml.Count == 0)
  1413. return null;
  1414. return ml;
  1415. }
  1416. public void ReadXml(string rawXml)
  1417. {
  1418. try
  1419. {
  1420. using (StringReader sr = new(rawXml))
  1421. {
  1422. using (XmlTextReader xtr = new(sr))
  1423. {
  1424. xtr.DtdProcessing = DtdProcessing.Ignore;
  1425. xtr.MoveToContent();
  1426. string type = xtr.GetAttribute("type");
  1427. //m_log.DebugFormat("[MOAP]: Loaded media texture entry with type {0}", type);
  1428. if (type != MEDIA_TEXTURE_TYPE)
  1429. return;
  1430. xtr.ReadStartElement("OSMedia");
  1431. OSD osdp = OSDParser.DeserializeLLSDXml(xtr.ReadInnerXml());
  1432. if(osdp is not OSDArray osdMeArray)
  1433. return;
  1434. if(osdMeArray.Count == 0)
  1435. return;
  1436. foreach (OSD osdMe in osdMeArray)
  1437. {
  1438. MediaEntry me = (osdMe is OSDMap ? MediaEntry.FromOSD(osdMe) : new MediaEntry());
  1439. Add(me);
  1440. }
  1441. }
  1442. }
  1443. }
  1444. catch
  1445. {
  1446. m_log.Debug("PrimitiveBaseShape] error decoding MOAP xml" );
  1447. }
  1448. }
  1449. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  1450. public void ReadXml(XmlReader reader)
  1451. {
  1452. if (reader.IsEmptyElement)
  1453. return;
  1454. ReadXml(reader.ReadInnerXml());
  1455. }
  1456. }
  1457. }
  1458. }