PrimitiveBaseShape.cs 40 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322
  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.Drawing;
  30. using System.Drawing.Imaging;
  31. using System.IO;
  32. using System.Reflection;
  33. using System.Xml;
  34. using System.Xml.Schema;
  35. using System.Xml.Serialization;
  36. using log4net;
  37. using OpenMetaverse;
  38. using OpenMetaverse.StructuredData;
  39. namespace OpenSim.Framework
  40. {
  41. public enum ProfileShape : byte
  42. {
  43. Circle = 0,
  44. Square = 1,
  45. IsometricTriangle = 2,
  46. EquilateralTriangle = 3,
  47. RightTriangle = 4,
  48. HalfCircle = 5
  49. }
  50. public enum HollowShape : byte
  51. {
  52. Same = 0,
  53. Circle = 16,
  54. Square = 32,
  55. Triangle = 48
  56. }
  57. public enum PCodeEnum : byte
  58. {
  59. Primitive = 9,
  60. Avatar = 47,
  61. Grass = 95,
  62. NewTree = 111,
  63. ParticleSystem = 143,
  64. Tree = 255
  65. }
  66. public enum Extrusion : byte
  67. {
  68. Straight = 16,
  69. Curve1 = 32,
  70. Curve2 = 48,
  71. Flexible = 128
  72. }
  73. [Serializable]
  74. public class PrimitiveBaseShape
  75. {
  76. private static readonly ILog m_log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
  77. private static readonly byte[] DEFAULT_TEXTURE = new Primitive.TextureEntry(new UUID("89556747-24cb-43ed-920b-47caed15465f")).GetBytes();
  78. private byte[] m_textureEntry;
  79. private ushort _pathBegin;
  80. private byte _pathCurve;
  81. private ushort _pathEnd;
  82. private sbyte _pathRadiusOffset;
  83. private byte _pathRevolutions;
  84. private byte _pathScaleX;
  85. private byte _pathScaleY;
  86. private byte _pathShearX;
  87. private byte _pathShearY;
  88. private sbyte _pathSkew;
  89. private sbyte _pathTaperX;
  90. private sbyte _pathTaperY;
  91. private sbyte _pathTwist;
  92. private sbyte _pathTwistBegin;
  93. private byte _pCode;
  94. private ushort _profileBegin;
  95. private ushort _profileEnd;
  96. private ushort _profileHollow;
  97. private Vector3 _scale;
  98. private byte _state;
  99. private ProfileShape _profileShape;
  100. private HollowShape _hollowShape;
  101. // Sculpted
  102. [XmlIgnore] private UUID _sculptTexture;
  103. [XmlIgnore] private byte _sculptType;
  104. [XmlIgnore] private byte[] _sculptData = Utils.EmptyBytes;
  105. // 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. //Bright n sparkly
  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. [XmlIgnore] private bool _flexiEntry;
  124. [XmlIgnore] private bool _lightEntry;
  125. [XmlIgnore] private bool _sculptEntry;
  126. public byte ProfileCurve
  127. {
  128. get { return (byte)((byte)HollowShape | (byte)ProfileShape); }
  129. set
  130. {
  131. // Handle hollow shape component
  132. byte hollowShapeByte = (byte)(value & 0xf0);
  133. if (!Enum.IsDefined(typeof(HollowShape), hollowShapeByte))
  134. {
  135. m_log.WarnFormat(
  136. "[SHAPE]: Attempt to set a ProfileCurve with a hollow shape value of {0}, which isn't a valid enum. Replacing with default shape.",
  137. hollowShapeByte);
  138. this._hollowShape = HollowShape.Same;
  139. }
  140. else
  141. {
  142. this._hollowShape = (HollowShape)hollowShapeByte;
  143. }
  144. // Handle profile shape component
  145. byte profileShapeByte = (byte)(value & 0xf);
  146. if (!Enum.IsDefined(typeof(ProfileShape), profileShapeByte))
  147. {
  148. m_log.WarnFormat(
  149. "[SHAPE]: Attempt to set a ProfileCurve with a profile shape value of {0}, which isn't a valid enum. Replacing with square.",
  150. profileShapeByte);
  151. this._profileShape = ProfileShape.Square;
  152. }
  153. else
  154. {
  155. this._profileShape = (ProfileShape)profileShapeByte;
  156. }
  157. }
  158. }
  159. /// <summary>
  160. /// Entries to store media textures on each face
  161. /// </summary>
  162. /// Do not change this value directly - always do it through an IMoapModule.
  163. /// Lock before manipulating.
  164. public MediaList Media { get; set; }
  165. public PrimitiveBaseShape()
  166. {
  167. PCode = (byte) PCodeEnum.Primitive;
  168. ExtraParams = new byte[1];
  169. m_textureEntry = DEFAULT_TEXTURE;
  170. }
  171. public PrimitiveBaseShape(bool noShape)
  172. {
  173. if (noShape)
  174. return;
  175. PCode = (byte)PCodeEnum.Primitive;
  176. ExtraParams = new byte[1];
  177. m_textureEntry = DEFAULT_TEXTURE;
  178. }
  179. /// <summary>
  180. /// Construct a PrimitiveBaseShape object from a OpenMetaverse.Primitive object
  181. /// </summary>
  182. /// <param name="prim"></param>
  183. public PrimitiveBaseShape(Primitive prim)
  184. {
  185. PCode = (byte)prim.PrimData.PCode;
  186. ExtraParams = new byte[1];
  187. State = prim.PrimData.State;
  188. PathBegin = Primitive.PackBeginCut(prim.PrimData.PathBegin);
  189. PathEnd = Primitive.PackEndCut(prim.PrimData.PathEnd);
  190. PathScaleX = Primitive.PackPathScale(prim.PrimData.PathScaleX);
  191. PathScaleY = Primitive.PackPathScale(prim.PrimData.PathScaleY);
  192. PathShearX = (byte)Primitive.PackPathShear(prim.PrimData.PathShearX);
  193. PathShearY = (byte)Primitive.PackPathShear(prim.PrimData.PathShearY);
  194. PathSkew = Primitive.PackPathTwist(prim.PrimData.PathSkew);
  195. ProfileBegin = Primitive.PackBeginCut(prim.PrimData.ProfileBegin);
  196. ProfileEnd = Primitive.PackEndCut(prim.PrimData.ProfileEnd);
  197. Scale = prim.Scale;
  198. PathCurve = (byte)prim.PrimData.PathCurve;
  199. ProfileCurve = (byte)prim.PrimData.ProfileCurve;
  200. ProfileHollow = Primitive.PackProfileHollow(prim.PrimData.ProfileHollow);
  201. PathRadiusOffset = Primitive.PackPathTwist(prim.PrimData.PathRadiusOffset);
  202. PathRevolutions = Primitive.PackPathRevolutions(prim.PrimData.PathRevolutions);
  203. PathTaperX = Primitive.PackPathTaper(prim.PrimData.PathTaperX);
  204. PathTaperY = Primitive.PackPathTaper(prim.PrimData.PathTaperY);
  205. PathTwist = Primitive.PackPathTwist(prim.PrimData.PathTwist);
  206. PathTwistBegin = Primitive.PackPathTwist(prim.PrimData.PathTwistBegin);
  207. m_textureEntry = prim.Textures.GetBytes();
  208. SculptEntry = (prim.Sculpt.Type != OpenMetaverse.SculptType.None);
  209. SculptData = prim.Sculpt.GetBytes();
  210. SculptTexture = prim.Sculpt.SculptTexture;
  211. SculptType = (byte)prim.Sculpt.Type;
  212. }
  213. [XmlIgnore]
  214. public Primitive.TextureEntry Textures
  215. {
  216. get
  217. {
  218. //m_log.DebugFormat("[SHAPE]: get m_textureEntry length {0}", m_textureEntry.Length);
  219. try { return new Primitive.TextureEntry(m_textureEntry, 0, m_textureEntry.Length); }
  220. catch { }
  221. m_log.Warn("[SHAPE]: Failed to decode texture, length=" + ((m_textureEntry != null) ? m_textureEntry.Length : 0));
  222. return new Primitive.TextureEntry(UUID.Zero);
  223. }
  224. set { m_textureEntry = value.GetBytes(); }
  225. }
  226. public byte[] TextureEntry
  227. {
  228. get { return m_textureEntry; }
  229. set
  230. {
  231. if (value == null)
  232. m_textureEntry = new byte[1];
  233. else
  234. m_textureEntry = value;
  235. }
  236. }
  237. public static PrimitiveBaseShape Default
  238. {
  239. get
  240. {
  241. PrimitiveBaseShape boxShape = CreateBox();
  242. boxShape.SetScale(0.5f);
  243. return boxShape;
  244. }
  245. }
  246. public static PrimitiveBaseShape Create()
  247. {
  248. PrimitiveBaseShape shape = new PrimitiveBaseShape();
  249. return shape;
  250. }
  251. public static PrimitiveBaseShape CreateBox()
  252. {
  253. PrimitiveBaseShape shape = Create();
  254. shape._pathCurve = (byte) Extrusion.Straight;
  255. shape._profileShape = ProfileShape.Square;
  256. shape._pathScaleX = 100;
  257. shape._pathScaleY = 100;
  258. return shape;
  259. }
  260. public static PrimitiveBaseShape CreateSphere()
  261. {
  262. PrimitiveBaseShape shape = Create();
  263. shape._pathCurve = (byte) Extrusion.Curve1;
  264. shape._profileShape = ProfileShape.HalfCircle;
  265. shape._pathScaleX = 100;
  266. shape._pathScaleY = 100;
  267. return shape;
  268. }
  269. public static PrimitiveBaseShape CreateCylinder()
  270. {
  271. PrimitiveBaseShape shape = Create();
  272. shape._pathCurve = (byte) Extrusion.Curve1;
  273. shape._profileShape = ProfileShape.Square;
  274. shape._pathScaleX = 100;
  275. shape._pathScaleY = 100;
  276. return shape;
  277. }
  278. public void SetScale(float side)
  279. {
  280. _scale = new Vector3(side, side, side);
  281. }
  282. public void SetHeigth(float heigth)
  283. {
  284. _scale.Z = heigth;
  285. }
  286. public void SetRadius(float radius)
  287. {
  288. _scale.X = _scale.Y = radius * 2f;
  289. }
  290. // TODO: void returns need to change of course
  291. public virtual void GetMesh()
  292. {
  293. }
  294. public PrimitiveBaseShape Copy()
  295. {
  296. return (PrimitiveBaseShape) MemberwiseClone();
  297. }
  298. public static PrimitiveBaseShape CreateCylinder(float radius, float heigth)
  299. {
  300. PrimitiveBaseShape shape = CreateCylinder();
  301. shape.SetHeigth(heigth);
  302. shape.SetRadius(radius);
  303. return shape;
  304. }
  305. public void SetPathRange(Vector3 pathRange)
  306. {
  307. _pathBegin = Primitive.PackBeginCut(pathRange.X);
  308. _pathEnd = Primitive.PackEndCut(pathRange.Y);
  309. }
  310. public void SetPathRange(float begin, float end)
  311. {
  312. _pathBegin = Primitive.PackBeginCut(begin);
  313. _pathEnd = Primitive.PackEndCut(end);
  314. }
  315. public void SetSculptData(byte sculptType, UUID SculptTextureUUID)
  316. {
  317. _sculptType = sculptType;
  318. _sculptTexture = SculptTextureUUID;
  319. }
  320. public void SetProfileRange(Vector3 profileRange)
  321. {
  322. _profileBegin = Primitive.PackBeginCut(profileRange.X);
  323. _profileEnd = Primitive.PackEndCut(profileRange.Y);
  324. }
  325. public void SetProfileRange(float begin, float end)
  326. {
  327. _profileBegin = Primitive.PackBeginCut(begin);
  328. _profileEnd = Primitive.PackEndCut(end);
  329. }
  330. public byte[] ExtraParams
  331. {
  332. get
  333. {
  334. return ExtraParamsToBytes();
  335. }
  336. set
  337. {
  338. ReadInExtraParamsBytes(value);
  339. }
  340. }
  341. public ushort PathBegin {
  342. get {
  343. return _pathBegin;
  344. }
  345. set {
  346. _pathBegin = value;
  347. }
  348. }
  349. public byte PathCurve {
  350. get {
  351. return _pathCurve;
  352. }
  353. set {
  354. _pathCurve = value;
  355. }
  356. }
  357. public ushort PathEnd {
  358. get {
  359. return _pathEnd;
  360. }
  361. set {
  362. _pathEnd = value;
  363. }
  364. }
  365. public sbyte PathRadiusOffset {
  366. get {
  367. return _pathRadiusOffset;
  368. }
  369. set {
  370. _pathRadiusOffset = value;
  371. }
  372. }
  373. public byte PathRevolutions {
  374. get {
  375. return _pathRevolutions;
  376. }
  377. set {
  378. _pathRevolutions = value;
  379. }
  380. }
  381. public byte PathScaleX {
  382. get {
  383. return _pathScaleX;
  384. }
  385. set {
  386. _pathScaleX = value;
  387. }
  388. }
  389. public byte PathScaleY {
  390. get {
  391. return _pathScaleY;
  392. }
  393. set {
  394. _pathScaleY = value;
  395. }
  396. }
  397. public byte PathShearX {
  398. get {
  399. return _pathShearX;
  400. }
  401. set {
  402. _pathShearX = value;
  403. }
  404. }
  405. public byte PathShearY {
  406. get {
  407. return _pathShearY;
  408. }
  409. set {
  410. _pathShearY = value;
  411. }
  412. }
  413. public sbyte PathSkew {
  414. get {
  415. return _pathSkew;
  416. }
  417. set {
  418. _pathSkew = value;
  419. }
  420. }
  421. public sbyte PathTaperX {
  422. get {
  423. return _pathTaperX;
  424. }
  425. set {
  426. _pathTaperX = value;
  427. }
  428. }
  429. public sbyte PathTaperY {
  430. get {
  431. return _pathTaperY;
  432. }
  433. set {
  434. _pathTaperY = value;
  435. }
  436. }
  437. public sbyte PathTwist {
  438. get {
  439. return _pathTwist;
  440. }
  441. set {
  442. _pathTwist = value;
  443. }
  444. }
  445. public sbyte PathTwistBegin {
  446. get {
  447. return _pathTwistBegin;
  448. }
  449. set {
  450. _pathTwistBegin = value;
  451. }
  452. }
  453. public byte PCode {
  454. get {
  455. return _pCode;
  456. }
  457. set {
  458. _pCode = value;
  459. }
  460. }
  461. public ushort ProfileBegin {
  462. get {
  463. return _profileBegin;
  464. }
  465. set {
  466. _profileBegin = value;
  467. }
  468. }
  469. public ushort ProfileEnd {
  470. get {
  471. return _profileEnd;
  472. }
  473. set {
  474. _profileEnd = value;
  475. }
  476. }
  477. public ushort ProfileHollow {
  478. get {
  479. return _profileHollow;
  480. }
  481. set {
  482. _profileHollow = value;
  483. }
  484. }
  485. public Vector3 Scale {
  486. get {
  487. return _scale;
  488. }
  489. set {
  490. _scale = value;
  491. }
  492. }
  493. public byte State {
  494. get {
  495. return _state;
  496. }
  497. set {
  498. _state = value;
  499. }
  500. }
  501. public ProfileShape ProfileShape {
  502. get {
  503. return _profileShape;
  504. }
  505. set {
  506. _profileShape = value;
  507. }
  508. }
  509. public HollowShape HollowShape {
  510. get {
  511. return _hollowShape;
  512. }
  513. set {
  514. _hollowShape = value;
  515. }
  516. }
  517. public UUID SculptTexture {
  518. get {
  519. return _sculptTexture;
  520. }
  521. set {
  522. _sculptTexture = value;
  523. }
  524. }
  525. public byte SculptType {
  526. get {
  527. return _sculptType;
  528. }
  529. set {
  530. _sculptType = value;
  531. }
  532. }
  533. public byte[] SculptData {
  534. get {
  535. return _sculptData;
  536. }
  537. set {
  538. _sculptData = value;
  539. }
  540. }
  541. public int FlexiSoftness {
  542. get {
  543. return _flexiSoftness;
  544. }
  545. set {
  546. _flexiSoftness = value;
  547. }
  548. }
  549. public float FlexiTension {
  550. get {
  551. return _flexiTension;
  552. }
  553. set {
  554. _flexiTension = value;
  555. }
  556. }
  557. public float FlexiDrag {
  558. get {
  559. return _flexiDrag;
  560. }
  561. set {
  562. _flexiDrag = value;
  563. }
  564. }
  565. public float FlexiGravity {
  566. get {
  567. return _flexiGravity;
  568. }
  569. set {
  570. _flexiGravity = value;
  571. }
  572. }
  573. public float FlexiWind {
  574. get {
  575. return _flexiWind;
  576. }
  577. set {
  578. _flexiWind = value;
  579. }
  580. }
  581. public float FlexiForceX {
  582. get {
  583. return _flexiForceX;
  584. }
  585. set {
  586. _flexiForceX = value;
  587. }
  588. }
  589. public float FlexiForceY {
  590. get {
  591. return _flexiForceY;
  592. }
  593. set {
  594. _flexiForceY = value;
  595. }
  596. }
  597. public float FlexiForceZ {
  598. get {
  599. return _flexiForceZ;
  600. }
  601. set {
  602. _flexiForceZ = value;
  603. }
  604. }
  605. public float LightColorR {
  606. get {
  607. return _lightColorR;
  608. }
  609. set {
  610. _lightColorR = value;
  611. }
  612. }
  613. public float LightColorG {
  614. get {
  615. return _lightColorG;
  616. }
  617. set {
  618. _lightColorG = value;
  619. }
  620. }
  621. public float LightColorB {
  622. get {
  623. return _lightColorB;
  624. }
  625. set {
  626. _lightColorB = value;
  627. }
  628. }
  629. public float LightColorA {
  630. get {
  631. return _lightColorA;
  632. }
  633. set {
  634. _lightColorA = value;
  635. }
  636. }
  637. public float LightRadius {
  638. get {
  639. return _lightRadius;
  640. }
  641. set {
  642. _lightRadius = value;
  643. }
  644. }
  645. public float LightCutoff {
  646. get {
  647. return _lightCutoff;
  648. }
  649. set {
  650. _lightCutoff = value;
  651. }
  652. }
  653. public float LightFalloff {
  654. get {
  655. return _lightFalloff;
  656. }
  657. set {
  658. _lightFalloff = value;
  659. }
  660. }
  661. public float LightIntensity {
  662. get {
  663. return _lightIntensity;
  664. }
  665. set {
  666. _lightIntensity = value;
  667. }
  668. }
  669. public bool FlexiEntry {
  670. get {
  671. return _flexiEntry;
  672. }
  673. set {
  674. _flexiEntry = value;
  675. }
  676. }
  677. public bool LightEntry {
  678. get {
  679. return _lightEntry;
  680. }
  681. set {
  682. _lightEntry = value;
  683. }
  684. }
  685. public bool SculptEntry {
  686. get {
  687. return _sculptEntry;
  688. }
  689. set {
  690. _sculptEntry = value;
  691. }
  692. }
  693. public byte[] ExtraParamsToBytes()
  694. {
  695. ushort FlexiEP = 0x10;
  696. ushort LightEP = 0x20;
  697. ushort SculptEP = 0x30;
  698. int i = 0;
  699. uint TotalBytesLength = 1; // ExtraParamsNum
  700. uint ExtraParamsNum = 0;
  701. if (_flexiEntry)
  702. {
  703. ExtraParamsNum++;
  704. TotalBytesLength += 16;// data
  705. TotalBytesLength += 2 + 4; // type
  706. }
  707. if (_lightEntry)
  708. {
  709. ExtraParamsNum++;
  710. TotalBytesLength += 16;// data
  711. TotalBytesLength += 2 + 4; // type
  712. }
  713. if (_sculptEntry)
  714. {
  715. ExtraParamsNum++;
  716. TotalBytesLength += 17;// data
  717. TotalBytesLength += 2 + 4; // type
  718. }
  719. byte[] returnbytes = new byte[TotalBytesLength];
  720. // uint paramlength = ExtraParamsNum;
  721. // Stick in the number of parameters
  722. returnbytes[i++] = (byte)ExtraParamsNum;
  723. if (_flexiEntry)
  724. {
  725. byte[] FlexiData = GetFlexiBytes();
  726. returnbytes[i++] = (byte)(FlexiEP % 256);
  727. returnbytes[i++] = (byte)((FlexiEP >> 8) % 256);
  728. returnbytes[i++] = (byte)(FlexiData.Length % 256);
  729. returnbytes[i++] = (byte)((FlexiData.Length >> 8) % 256);
  730. returnbytes[i++] = (byte)((FlexiData.Length >> 16) % 256);
  731. returnbytes[i++] = (byte)((FlexiData.Length >> 24) % 256);
  732. Array.Copy(FlexiData, 0, returnbytes, i, FlexiData.Length);
  733. i += FlexiData.Length;
  734. }
  735. if (_lightEntry)
  736. {
  737. byte[] LightData = GetLightBytes();
  738. returnbytes[i++] = (byte)(LightEP % 256);
  739. returnbytes[i++] = (byte)((LightEP >> 8) % 256);
  740. returnbytes[i++] = (byte)(LightData.Length % 256);
  741. returnbytes[i++] = (byte)((LightData.Length >> 8) % 256);
  742. returnbytes[i++] = (byte)((LightData.Length >> 16) % 256);
  743. returnbytes[i++] = (byte)((LightData.Length >> 24) % 256);
  744. Array.Copy(LightData, 0, returnbytes, i, LightData.Length);
  745. i += LightData.Length;
  746. }
  747. if (_sculptEntry)
  748. {
  749. byte[] SculptData = GetSculptBytes();
  750. returnbytes[i++] = (byte)(SculptEP % 256);
  751. returnbytes[i++] = (byte)((SculptEP >> 8) % 256);
  752. returnbytes[i++] = (byte)(SculptData.Length % 256);
  753. returnbytes[i++] = (byte)((SculptData.Length >> 8) % 256);
  754. returnbytes[i++] = (byte)((SculptData.Length >> 16) % 256);
  755. returnbytes[i++] = (byte)((SculptData.Length >> 24) % 256);
  756. Array.Copy(SculptData, 0, returnbytes, i, SculptData.Length);
  757. i += SculptData.Length;
  758. }
  759. if (!_flexiEntry && !_lightEntry && !_sculptEntry)
  760. {
  761. byte[] returnbyte = new byte[1];
  762. returnbyte[0] = 0;
  763. return returnbyte;
  764. }
  765. return returnbytes;
  766. //m_log.Info("[EXTRAPARAMS]: Length = " + m_shape.ExtraParams.Length.ToString());
  767. }
  768. public void ReadInUpdateExtraParam(ushort type, bool inUse, byte[] data)
  769. {
  770. const ushort FlexiEP = 0x10;
  771. const ushort LightEP = 0x20;
  772. const ushort SculptEP = 0x30;
  773. switch (type)
  774. {
  775. case FlexiEP:
  776. if (!inUse)
  777. {
  778. _flexiEntry = false;
  779. return;
  780. }
  781. ReadFlexiData(data, 0);
  782. break;
  783. case LightEP:
  784. if (!inUse)
  785. {
  786. _lightEntry = false;
  787. return;
  788. }
  789. ReadLightData(data, 0);
  790. break;
  791. case SculptEP:
  792. if (!inUse)
  793. {
  794. _sculptEntry = false;
  795. return;
  796. }
  797. ReadSculptData(data, 0);
  798. break;
  799. }
  800. }
  801. public void ReadInExtraParamsBytes(byte[] data)
  802. {
  803. if (data == null || data.Length == 1)
  804. return;
  805. const ushort FlexiEP = 0x10;
  806. const ushort LightEP = 0x20;
  807. const ushort SculptEP = 0x30;
  808. bool lGotFlexi = false;
  809. bool lGotLight = false;
  810. bool lGotSculpt = false;
  811. int i = 0;
  812. byte extraParamCount = 0;
  813. if (data.Length > 0)
  814. {
  815. extraParamCount = data[i++];
  816. }
  817. for (int k = 0; k < extraParamCount; k++)
  818. {
  819. ushort epType = Utils.BytesToUInt16(data, i);
  820. i += 2;
  821. // uint paramLength = Helpers.BytesToUIntBig(data, i);
  822. i += 4;
  823. switch (epType)
  824. {
  825. case FlexiEP:
  826. ReadFlexiData(data, i);
  827. i += 16;
  828. lGotFlexi = true;
  829. break;
  830. case LightEP:
  831. ReadLightData(data, i);
  832. i += 16;
  833. lGotLight = true;
  834. break;
  835. case SculptEP:
  836. ReadSculptData(data, i);
  837. i += 17;
  838. lGotSculpt = true;
  839. break;
  840. }
  841. }
  842. if (!lGotFlexi)
  843. _flexiEntry = false;
  844. if (!lGotLight)
  845. _lightEntry = false;
  846. if (!lGotSculpt)
  847. _sculptEntry = false;
  848. }
  849. public void ReadSculptData(byte[] data, int pos)
  850. {
  851. byte[] SculptTextureUUID = new byte[16];
  852. UUID SculptUUID = UUID.Zero;
  853. byte SculptTypel = data[16+pos];
  854. if (data.Length+pos >= 17)
  855. {
  856. _sculptEntry = true;
  857. SculptTextureUUID = new byte[16];
  858. SculptTypel = data[16 + pos];
  859. Array.Copy(data, pos, SculptTextureUUID,0, 16);
  860. SculptUUID = new UUID(SculptTextureUUID, 0);
  861. }
  862. else
  863. {
  864. _sculptEntry = false;
  865. SculptUUID = UUID.Zero;
  866. SculptTypel = 0x00;
  867. }
  868. if (_sculptEntry)
  869. {
  870. if (_sculptType != (byte)1 && _sculptType != (byte)2 && _sculptType != (byte)3 && _sculptType != (byte)4)
  871. _sculptType = 4;
  872. }
  873. _sculptTexture = SculptUUID;
  874. _sculptType = SculptTypel;
  875. //m_log.Info("[SCULPT]:" + SculptUUID.ToString());
  876. }
  877. public byte[] GetSculptBytes()
  878. {
  879. byte[] data = new byte[17];
  880. _sculptTexture.GetBytes().CopyTo(data, 0);
  881. data[16] = (byte)_sculptType;
  882. return data;
  883. }
  884. public void ReadFlexiData(byte[] data, int pos)
  885. {
  886. if (data.Length-pos >= 16)
  887. {
  888. _flexiEntry = true;
  889. _flexiSoftness = ((data[pos] & 0x80) >> 6) | ((data[pos + 1] & 0x80) >> 7);
  890. _flexiTension = (float)(data[pos++] & 0x7F) / 10.0f;
  891. _flexiDrag = (float)(data[pos++] & 0x7F) / 10.0f;
  892. _flexiGravity = (float)(data[pos++] / 10.0f) - 10.0f;
  893. _flexiWind = (float)data[pos++] / 10.0f;
  894. Vector3 lForce = new Vector3(data, pos);
  895. _flexiForceX = lForce.X;
  896. _flexiForceY = lForce.Y;
  897. _flexiForceZ = lForce.Z;
  898. }
  899. else
  900. {
  901. _flexiEntry = false;
  902. _flexiSoftness = 0;
  903. _flexiTension = 0.0f;
  904. _flexiDrag = 0.0f;
  905. _flexiGravity = 0.0f;
  906. _flexiWind = 0.0f;
  907. _flexiForceX = 0f;
  908. _flexiForceY = 0f;
  909. _flexiForceZ = 0f;
  910. }
  911. }
  912. public byte[] GetFlexiBytes()
  913. {
  914. byte[] data = new byte[16];
  915. int i = 0;
  916. // Softness is packed in the upper bits of tension and drag
  917. data[i] = (byte)((_flexiSoftness & 2) << 6);
  918. data[i + 1] = (byte)((_flexiSoftness & 1) << 7);
  919. data[i++] |= (byte)((byte)(_flexiTension * 10.01f) & 0x7F);
  920. data[i++] |= (byte)((byte)(_flexiDrag * 10.01f) & 0x7F);
  921. data[i++] = (byte)((_flexiGravity + 10.0f) * 10.01f);
  922. data[i++] = (byte)(_flexiWind * 10.01f);
  923. Vector3 lForce = new Vector3(_flexiForceX, _flexiForceY, _flexiForceZ);
  924. lForce.GetBytes().CopyTo(data, i);
  925. return data;
  926. }
  927. public void ReadLightData(byte[] data, int pos)
  928. {
  929. if (data.Length - pos >= 16)
  930. {
  931. _lightEntry = true;
  932. Color4 lColor = new Color4(data, pos, false);
  933. _lightIntensity = lColor.A;
  934. _lightColorA = 1f;
  935. _lightColorR = lColor.R;
  936. _lightColorG = lColor.G;
  937. _lightColorB = lColor.B;
  938. _lightRadius = Utils.BytesToFloat(data, pos + 4);
  939. _lightCutoff = Utils.BytesToFloat(data, pos + 8);
  940. _lightFalloff = Utils.BytesToFloat(data, pos + 12);
  941. }
  942. else
  943. {
  944. _lightEntry = false;
  945. _lightColorA = 1f;
  946. _lightColorR = 0f;
  947. _lightColorG = 0f;
  948. _lightColorB = 0f;
  949. _lightRadius = 0f;
  950. _lightCutoff = 0f;
  951. _lightFalloff = 0f;
  952. _lightIntensity = 0f;
  953. }
  954. }
  955. public byte[] GetLightBytes()
  956. {
  957. byte[] data = new byte[16];
  958. // Alpha channel in color is intensity
  959. Color4 tmpColor = new Color4(_lightColorR,_lightColorG,_lightColorB,_lightIntensity);
  960. tmpColor.GetBytes().CopyTo(data, 0);
  961. Utils.FloatToBytes(_lightRadius).CopyTo(data, 4);
  962. Utils.FloatToBytes(_lightCutoff).CopyTo(data, 8);
  963. Utils.FloatToBytes(_lightFalloff).CopyTo(data, 12);
  964. return data;
  965. }
  966. /// <summary>
  967. /// Creates a OpenMetaverse.Primitive and populates it with converted PrimitiveBaseShape values
  968. /// </summary>
  969. /// <returns></returns>
  970. public Primitive ToOmvPrimitive()
  971. {
  972. // position and rotation defaults here since they are not available in PrimitiveBaseShape
  973. return ToOmvPrimitive(new Vector3(0.0f, 0.0f, 0.0f),
  974. new Quaternion(0.0f, 0.0f, 0.0f, 1.0f));
  975. }
  976. /// <summary>
  977. /// Creates a OpenMetaverse.Primitive and populates it with converted PrimitiveBaseShape values
  978. /// </summary>
  979. /// <param name="position"></param>
  980. /// <param name="rotation"></param>
  981. /// <returns></returns>
  982. public Primitive ToOmvPrimitive(Vector3 position, Quaternion rotation)
  983. {
  984. OpenMetaverse.Primitive prim = new OpenMetaverse.Primitive();
  985. prim.Scale = this.Scale;
  986. prim.Position = position;
  987. prim.Rotation = rotation;
  988. if (this.SculptEntry)
  989. {
  990. prim.Sculpt = new Primitive.SculptData();
  991. prim.Sculpt.Type = (OpenMetaverse.SculptType)this.SculptType;
  992. prim.Sculpt.SculptTexture = this.SculptTexture;
  993. }
  994. prim.PrimData.PathShearX = this.PathShearX < 128 ? (float)this.PathShearX * 0.01f : (float)(this.PathShearX - 256) * 0.01f;
  995. prim.PrimData.PathShearY = this.PathShearY < 128 ? (float)this.PathShearY * 0.01f : (float)(this.PathShearY - 256) * 0.01f;
  996. prim.PrimData.PathBegin = (float)this.PathBegin * 2.0e-5f;
  997. prim.PrimData.PathEnd = 1.0f - (float)this.PathEnd * 2.0e-5f;
  998. prim.PrimData.PathScaleX = (200 - this.PathScaleX) * 0.01f;
  999. prim.PrimData.PathScaleY = (200 - this.PathScaleY) * 0.01f;
  1000. prim.PrimData.PathTaperX = this.PathTaperX * 0.01f;
  1001. prim.PrimData.PathTaperY = this.PathTaperY * 0.01f;
  1002. prim.PrimData.PathTwistBegin = this.PathTwistBegin * 0.01f;
  1003. prim.PrimData.PathTwist = this.PathTwist * 0.01f;
  1004. prim.PrimData.ProfileBegin = (float)this.ProfileBegin * 2.0e-5f;
  1005. prim.PrimData.ProfileEnd = 1.0f - (float)this.ProfileEnd * 2.0e-5f;
  1006. prim.PrimData.ProfileHollow = (float)this.ProfileHollow * 2.0e-5f;
  1007. prim.PrimData.profileCurve = this.ProfileCurve;
  1008. prim.PrimData.ProfileHole = (HoleType)this.HollowShape;
  1009. prim.PrimData.PathCurve = (PathCurve)this.PathCurve;
  1010. prim.PrimData.PathRadiusOffset = 0.01f * this.PathRadiusOffset;
  1011. prim.PrimData.PathRevolutions = 1.0f + 0.015f * this.PathRevolutions;
  1012. prim.PrimData.PathSkew = 0.01f * this.PathSkew;
  1013. prim.PrimData.PCode = OpenMetaverse.PCode.Prim;
  1014. prim.PrimData.State = 0;
  1015. if (this.FlexiEntry)
  1016. {
  1017. prim.Flexible = new Primitive.FlexibleData();
  1018. prim.Flexible.Drag = this.FlexiDrag;
  1019. prim.Flexible.Force = new Vector3(this.FlexiForceX, this.FlexiForceY, this.FlexiForceZ);
  1020. prim.Flexible.Gravity = this.FlexiGravity;
  1021. prim.Flexible.Softness = this.FlexiSoftness;
  1022. prim.Flexible.Tension = this.FlexiTension;
  1023. prim.Flexible.Wind = this.FlexiWind;
  1024. }
  1025. if (this.LightEntry)
  1026. {
  1027. prim.Light = new Primitive.LightData();
  1028. prim.Light.Color = new Color4(this.LightColorR, this.LightColorG, this.LightColorB, this.LightColorA);
  1029. prim.Light.Cutoff = this.LightCutoff;
  1030. prim.Light.Falloff = this.LightFalloff;
  1031. prim.Light.Intensity = this.LightIntensity;
  1032. prim.Light.Radius = this.LightRadius;
  1033. }
  1034. prim.Textures = this.Textures;
  1035. prim.Properties = new Primitive.ObjectProperties();
  1036. prim.Properties.Name = "Primitive";
  1037. prim.Properties.Description = "";
  1038. prim.Properties.CreatorID = UUID.Zero;
  1039. prim.Properties.GroupID = UUID.Zero;
  1040. prim.Properties.OwnerID = UUID.Zero;
  1041. prim.Properties.Permissions = new Permissions();
  1042. prim.Properties.SalePrice = 10;
  1043. prim.Properties.SaleType = new SaleType();
  1044. return prim;
  1045. }
  1046. /// <summary>
  1047. /// Encapsulates a list of media entries.
  1048. /// </summary>
  1049. /// This class is necessary because we want to replace auto-serialization of MediaEntry with something more
  1050. /// OSD like and less vulnerable to change.
  1051. public class MediaList : List<MediaEntry>, IXmlSerializable
  1052. {
  1053. public const string MEDIA_TEXTURE_TYPE = "sl";
  1054. public MediaList() : base() {}
  1055. public MediaList(IEnumerable<MediaEntry> collection) : base(collection) {}
  1056. public MediaList(int capacity) : base(capacity) {}
  1057. public XmlSchema GetSchema()
  1058. {
  1059. return null;
  1060. }
  1061. public string ToXml()
  1062. {
  1063. lock (this)
  1064. {
  1065. using (StringWriter sw = new StringWriter())
  1066. {
  1067. using (XmlTextWriter xtw = new XmlTextWriter(sw))
  1068. {
  1069. xtw.WriteStartElement("OSMedia");
  1070. xtw.WriteAttributeString("type", MEDIA_TEXTURE_TYPE);
  1071. xtw.WriteAttributeString("version", "0.1");
  1072. OSDArray meArray = new OSDArray();
  1073. foreach (MediaEntry me in this)
  1074. {
  1075. OSD osd = (null == me ? new OSD() : me.GetOSD());
  1076. meArray.Add(osd);
  1077. }
  1078. xtw.WriteStartElement("OSData");
  1079. xtw.WriteRaw(OSDParser.SerializeLLSDXmlString(meArray));
  1080. xtw.WriteEndElement();
  1081. xtw.WriteEndElement();
  1082. xtw.Flush();
  1083. return sw.ToString();
  1084. }
  1085. }
  1086. }
  1087. }
  1088. public void WriteXml(XmlWriter writer)
  1089. {
  1090. writer.WriteRaw(ToXml());
  1091. }
  1092. public static MediaList FromXml(string rawXml)
  1093. {
  1094. MediaList ml = new MediaList();
  1095. ml.ReadXml(rawXml);
  1096. return ml;
  1097. }
  1098. public void ReadXml(string rawXml)
  1099. {
  1100. using (StringReader sr = new StringReader(rawXml))
  1101. {
  1102. using (XmlTextReader xtr = new XmlTextReader(sr))
  1103. {
  1104. xtr.MoveToContent();
  1105. string type = xtr.GetAttribute("type");
  1106. //m_log.DebugFormat("[MOAP]: Loaded media texture entry with type {0}", type);
  1107. if (type != MEDIA_TEXTURE_TYPE)
  1108. return;
  1109. xtr.ReadStartElement("OSMedia");
  1110. OSDArray osdMeArray = (OSDArray)OSDParser.DeserializeLLSDXml(xtr.ReadInnerXml());
  1111. foreach (OSD osdMe in osdMeArray)
  1112. {
  1113. MediaEntry me = (osdMe is OSDMap ? MediaEntry.FromOSD(osdMe) : new MediaEntry());
  1114. Add(me);
  1115. }
  1116. xtr.ReadEndElement();
  1117. }
  1118. }
  1119. }
  1120. public void ReadXml(XmlReader reader)
  1121. {
  1122. if (reader.IsEmptyElement)
  1123. return;
  1124. ReadXml(reader.ReadInnerXml());
  1125. }
  1126. }
  1127. }
  1128. }