PrimitiveBaseShape.cs 54 KB

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