PrimitiveBaseShape.cs 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086
  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.Drawing;
  29. using System.Drawing.Imaging;
  30. using System.Reflection;
  31. using System.Xml.Serialization;
  32. using log4net;
  33. using OpenMetaverse;
  34. namespace OpenSim.Framework
  35. {
  36. public enum ProfileShape : byte
  37. {
  38. Circle = 0,
  39. Square = 1,
  40. IsometricTriangle = 2,
  41. EquilateralTriangle = 3,
  42. RightTriangle = 4,
  43. HalfCircle = 5
  44. }
  45. public enum HollowShape : byte
  46. {
  47. Same = 0,
  48. Circle = 16,
  49. Square = 32,
  50. Triangle = 48
  51. }
  52. public enum PCodeEnum : byte
  53. {
  54. Primitive = 9,
  55. Avatar = 47,
  56. Grass = 95,
  57. NewTree = 111,
  58. ParticleSystem = 143,
  59. Tree = 255
  60. }
  61. public enum Extrusion : byte
  62. {
  63. Straight = 16,
  64. Curve1 = 32,
  65. Curve2 = 48,
  66. Flexible = 128
  67. }
  68. [Serializable]
  69. public class PrimitiveBaseShape
  70. {
  71. private static readonly ILog m_log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
  72. private static readonly Primitive.TextureEntry m_defaultTexture;
  73. private byte[] m_textureEntry;
  74. private ushort _pathBegin;
  75. private byte _pathCurve;
  76. private ushort _pathEnd;
  77. private sbyte _pathRadiusOffset;
  78. private byte _pathRevolutions;
  79. private byte _pathScaleX;
  80. private byte _pathScaleY;
  81. private byte _pathShearX;
  82. private byte _pathShearY;
  83. private sbyte _pathSkew;
  84. private sbyte _pathTaperX;
  85. private sbyte _pathTaperY;
  86. private sbyte _pathTwist;
  87. private sbyte _pathTwistBegin;
  88. private byte _pCode;
  89. private ushort _profileBegin;
  90. private ushort _profileEnd;
  91. private ushort _profileHollow;
  92. private Vector3 _scale;
  93. private byte _state;
  94. private ProfileShape _profileShape;
  95. private HollowShape _hollowShape;
  96. // Sculpted
  97. [XmlIgnore] private UUID _sculptTexture = UUID.Zero;
  98. [XmlIgnore] private byte _sculptType = (byte)0;
  99. [XmlIgnore] private byte[] _sculptData = new byte[0];
  100. [XmlIgnore] private Image _sculptBitmap = null;
  101. // Flexi
  102. [XmlIgnore] private int _flexiSoftness = 0;
  103. [XmlIgnore] private float _flexiTension = 0f;
  104. [XmlIgnore] private float _flexiDrag = 0f;
  105. [XmlIgnore] private float _flexiGravity = 0f;
  106. [XmlIgnore] private float _flexiWind = 0f;
  107. [XmlIgnore] private float _flexiForceX = 0f;
  108. [XmlIgnore] private float _flexiForceY = 0f;
  109. [XmlIgnore] private float _flexiForceZ = 0f;
  110. //Bright n sparkly
  111. [XmlIgnore] private float _lightColorR = 0f;
  112. [XmlIgnore] private float _lightColorG = 0f;
  113. [XmlIgnore] private float _lightColorB = 0f;
  114. [XmlIgnore] private float _lightColorA = 1f;
  115. [XmlIgnore] private float _lightRadius = 0f;
  116. [XmlIgnore] private float _lightCutoff = 0f;
  117. [XmlIgnore] private float _lightFalloff = 0f;
  118. [XmlIgnore] private float _lightIntensity = 1f;
  119. [XmlIgnore] private bool _flexiEntry = false;
  120. [XmlIgnore] private bool _lightEntry = false;
  121. [XmlIgnore] private bool _sculptEntry = false;
  122. public byte ProfileCurve
  123. {
  124. get { return (byte)((byte)HollowShape | (byte)ProfileShape); }
  125. set
  126. {
  127. // Handle hollow shape component
  128. byte hollowShapeByte = (byte)(value & 0xf0);
  129. if (!Enum.IsDefined(typeof(HollowShape), hollowShapeByte))
  130. {
  131. m_log.WarnFormat(
  132. "[SHAPE]: Attempt to set a ProfileCurve with a hollow shape value of {0}, which isn't a valid enum. Replacing with default shape.",
  133. hollowShapeByte);
  134. this._hollowShape = HollowShape.Same;
  135. }
  136. else
  137. {
  138. this._hollowShape = (HollowShape)hollowShapeByte;
  139. }
  140. // Handle profile shape component
  141. byte profileShapeByte = (byte)(value & 0xf);
  142. if (!Enum.IsDefined(typeof(ProfileShape), profileShapeByte))
  143. {
  144. m_log.WarnFormat(
  145. "[SHAPE]: Attempt to set a ProfileCurve with a profile shape value of {0}, which isn't a valid enum. Replacing with square.",
  146. profileShapeByte);
  147. this._profileShape = ProfileShape.Square;
  148. }
  149. else
  150. {
  151. this._profileShape = (ProfileShape)profileShapeByte;
  152. }
  153. }
  154. }
  155. static PrimitiveBaseShape()
  156. {
  157. m_defaultTexture =
  158. new Primitive.TextureEntry(new UUID("89556747-24cb-43ed-920b-47caed15465f"));
  159. }
  160. public PrimitiveBaseShape()
  161. {
  162. PCode = (byte) PCodeEnum.Primitive;
  163. ExtraParams = new byte[1];
  164. Textures = m_defaultTexture;
  165. }
  166. public PrimitiveBaseShape(bool noShape)
  167. {
  168. if (noShape)
  169. return;
  170. PCode = (byte)PCodeEnum.Primitive;
  171. ExtraParams = new byte[1];
  172. Textures = m_defaultTexture;
  173. }
  174. [XmlIgnore]
  175. public Primitive.TextureEntry Textures
  176. {
  177. get
  178. {
  179. //m_log.DebugFormat("[PRIMITIVE BASE SHAPE]: get m_textureEntry length {0}", m_textureEntry.Length);
  180. return new Primitive.TextureEntry(m_textureEntry, 0, m_textureEntry.Length);
  181. }
  182. set { m_textureEntry = value.GetBytes(); }
  183. }
  184. public byte[] TextureEntry
  185. {
  186. get { return m_textureEntry; }
  187. set
  188. {
  189. if (value == null)
  190. m_textureEntry = new byte[1];
  191. else
  192. m_textureEntry = value;
  193. }
  194. }
  195. public static PrimitiveBaseShape Default
  196. {
  197. get
  198. {
  199. PrimitiveBaseShape boxShape = CreateBox();
  200. boxShape.SetScale(0.5f);
  201. return boxShape;
  202. }
  203. }
  204. public static PrimitiveBaseShape Create()
  205. {
  206. PrimitiveBaseShape shape = new PrimitiveBaseShape();
  207. return shape;
  208. }
  209. public static PrimitiveBaseShape CreateBox()
  210. {
  211. PrimitiveBaseShape shape = Create();
  212. shape._pathCurve = (byte) Extrusion.Straight;
  213. shape._profileShape = ProfileShape.Square;
  214. shape._pathScaleX = 100;
  215. shape._pathScaleY = 100;
  216. return shape;
  217. }
  218. public static PrimitiveBaseShape CreateSphere()
  219. {
  220. PrimitiveBaseShape shape = Create();
  221. shape._pathCurve = (byte) Extrusion.Curve1;
  222. shape._profileShape = ProfileShape.HalfCircle;
  223. shape._pathScaleX = 100;
  224. shape._pathScaleY = 100;
  225. return shape;
  226. }
  227. public static PrimitiveBaseShape CreateCylinder()
  228. {
  229. PrimitiveBaseShape shape = Create();
  230. shape._pathCurve = (byte) Extrusion.Curve1;
  231. shape._profileShape = ProfileShape.Square;
  232. shape._pathScaleX = 100;
  233. shape._pathScaleY = 100;
  234. return shape;
  235. }
  236. public void SetScale(float side)
  237. {
  238. _scale = new Vector3(side, side, side);
  239. }
  240. public void SetHeigth(float heigth)
  241. {
  242. _scale.Z = heigth;
  243. }
  244. public void SetRadius(float radius)
  245. {
  246. _scale.X = _scale.Y = radius * 2f;
  247. }
  248. // TODO: void returns need to change of course
  249. public virtual void GetMesh()
  250. {
  251. }
  252. public PrimitiveBaseShape Copy()
  253. {
  254. return (PrimitiveBaseShape) MemberwiseClone();
  255. }
  256. public static PrimitiveBaseShape CreateCylinder(float radius, float heigth)
  257. {
  258. PrimitiveBaseShape shape = CreateCylinder();
  259. shape.SetHeigth(heigth);
  260. shape.SetRadius(radius);
  261. return shape;
  262. }
  263. public void SetPathRange(Vector3 pathRange)
  264. {
  265. _pathBegin = Primitive.PackBeginCut(pathRange.X);
  266. _pathEnd = Primitive.PackEndCut(pathRange.Y);
  267. }
  268. public void SetPathRange(float begin, float end)
  269. {
  270. _pathBegin = Primitive.PackBeginCut(begin);
  271. _pathEnd = Primitive.PackEndCut(end);
  272. }
  273. public void SetSculptData(byte sculptType, UUID SculptTextureUUID)
  274. {
  275. _sculptType = sculptType;
  276. _sculptTexture = SculptTextureUUID;
  277. }
  278. public void SetProfileRange(Vector3 profileRange)
  279. {
  280. _profileBegin = Primitive.PackBeginCut(profileRange.X);
  281. _profileEnd = Primitive.PackEndCut(profileRange.Y);
  282. }
  283. public void SetProfileRange(float begin, float end)
  284. {
  285. _profileBegin = Primitive.PackBeginCut(begin);
  286. _profileEnd = Primitive.PackEndCut(end);
  287. }
  288. public byte[] ExtraParams
  289. {
  290. get
  291. {
  292. return ExtraParamsToBytes();
  293. }
  294. set
  295. {
  296. ReadInExtraParamsBytes(value);
  297. }
  298. }
  299. public ushort PathBegin {
  300. get {
  301. return _pathBegin;
  302. }
  303. set {
  304. _pathBegin = value;
  305. }
  306. }
  307. public byte PathCurve {
  308. get {
  309. return _pathCurve;
  310. }
  311. set {
  312. _pathCurve = value;
  313. }
  314. }
  315. public ushort PathEnd {
  316. get {
  317. return _pathEnd;
  318. }
  319. set {
  320. _pathEnd = value;
  321. }
  322. }
  323. public sbyte PathRadiusOffset {
  324. get {
  325. return _pathRadiusOffset;
  326. }
  327. set {
  328. _pathRadiusOffset = value;
  329. }
  330. }
  331. public byte PathRevolutions {
  332. get {
  333. return _pathRevolutions;
  334. }
  335. set {
  336. _pathRevolutions = value;
  337. }
  338. }
  339. public byte PathScaleX {
  340. get {
  341. return _pathScaleX;
  342. }
  343. set {
  344. _pathScaleX = value;
  345. }
  346. }
  347. public byte PathScaleY {
  348. get {
  349. return _pathScaleY;
  350. }
  351. set {
  352. _pathScaleY = value;
  353. }
  354. }
  355. public byte PathShearX {
  356. get {
  357. return _pathShearX;
  358. }
  359. set {
  360. _pathShearX = value;
  361. }
  362. }
  363. public byte PathShearY {
  364. get {
  365. return _pathShearY;
  366. }
  367. set {
  368. _pathShearY = value;
  369. }
  370. }
  371. public sbyte PathSkew {
  372. get {
  373. return _pathSkew;
  374. }
  375. set {
  376. _pathSkew = value;
  377. }
  378. }
  379. public sbyte PathTaperX {
  380. get {
  381. return _pathTaperX;
  382. }
  383. set {
  384. _pathTaperX = value;
  385. }
  386. }
  387. public sbyte PathTaperY {
  388. get {
  389. return _pathTaperY;
  390. }
  391. set {
  392. _pathTaperY = value;
  393. }
  394. }
  395. public sbyte PathTwist {
  396. get {
  397. return _pathTwist;
  398. }
  399. set {
  400. _pathTwist = value;
  401. }
  402. }
  403. public sbyte PathTwistBegin {
  404. get {
  405. return _pathTwistBegin;
  406. }
  407. set {
  408. _pathTwistBegin = value;
  409. }
  410. }
  411. public byte PCode {
  412. get {
  413. return _pCode;
  414. }
  415. set {
  416. _pCode = value;
  417. }
  418. }
  419. public ushort ProfileBegin {
  420. get {
  421. return _profileBegin;
  422. }
  423. set {
  424. _profileBegin = value;
  425. }
  426. }
  427. public ushort ProfileEnd {
  428. get {
  429. return _profileEnd;
  430. }
  431. set {
  432. _profileEnd = value;
  433. }
  434. }
  435. public ushort ProfileHollow {
  436. get {
  437. return _profileHollow;
  438. }
  439. set {
  440. _profileHollow = value;
  441. }
  442. }
  443. public Vector3 Scale {
  444. get {
  445. return _scale;
  446. }
  447. set {
  448. _scale = value;
  449. }
  450. }
  451. public byte State {
  452. get {
  453. return _state;
  454. }
  455. set {
  456. _state = value;
  457. }
  458. }
  459. public ProfileShape ProfileShape {
  460. get {
  461. return _profileShape;
  462. }
  463. set {
  464. _profileShape = value;
  465. }
  466. }
  467. public HollowShape HollowShape {
  468. get {
  469. return _hollowShape;
  470. }
  471. set {
  472. _hollowShape = value;
  473. }
  474. }
  475. public UUID SculptTexture {
  476. get {
  477. return _sculptTexture;
  478. }
  479. set {
  480. _sculptTexture = value;
  481. }
  482. }
  483. public byte SculptType {
  484. get {
  485. return _sculptType;
  486. }
  487. set {
  488. _sculptType = value;
  489. }
  490. }
  491. public byte[] SculptData {
  492. get {
  493. return _sculptData;
  494. }
  495. set {
  496. _sculptData = value;
  497. }
  498. }
  499. public Image SculptBitmap {
  500. get {
  501. return _sculptBitmap;
  502. }
  503. set {
  504. _sculptBitmap = value;
  505. }
  506. }
  507. public int FlexiSoftness {
  508. get {
  509. return _flexiSoftness;
  510. }
  511. set {
  512. _flexiSoftness = value;
  513. }
  514. }
  515. public float FlexiTension {
  516. get {
  517. return _flexiTension;
  518. }
  519. set {
  520. _flexiTension = value;
  521. }
  522. }
  523. public float FlexiDrag {
  524. get {
  525. return _flexiDrag;
  526. }
  527. set {
  528. _flexiDrag = value;
  529. }
  530. }
  531. public float FlexiGravity {
  532. get {
  533. return _flexiGravity;
  534. }
  535. set {
  536. _flexiGravity = value;
  537. }
  538. }
  539. public float FlexiWind {
  540. get {
  541. return _flexiWind;
  542. }
  543. set {
  544. _flexiWind = value;
  545. }
  546. }
  547. public float FlexiForceX {
  548. get {
  549. return _flexiForceX;
  550. }
  551. set {
  552. _flexiForceX = value;
  553. }
  554. }
  555. public float FlexiForceY {
  556. get {
  557. return _flexiForceY;
  558. }
  559. set {
  560. _flexiForceY = value;
  561. }
  562. }
  563. public float FlexiForceZ {
  564. get {
  565. return _flexiForceZ;
  566. }
  567. set {
  568. _flexiForceZ = value;
  569. }
  570. }
  571. public float LightColorR {
  572. get {
  573. return _lightColorR;
  574. }
  575. set {
  576. _lightColorR = value;
  577. }
  578. }
  579. public float LightColorG {
  580. get {
  581. return _lightColorG;
  582. }
  583. set {
  584. _lightColorG = value;
  585. }
  586. }
  587. public float LightColorB {
  588. get {
  589. return _lightColorB;
  590. }
  591. set {
  592. _lightColorB = value;
  593. }
  594. }
  595. public float LightColorA {
  596. get {
  597. return _lightColorA;
  598. }
  599. set {
  600. _lightColorA = value;
  601. }
  602. }
  603. public float LightRadius {
  604. get {
  605. return _lightRadius;
  606. }
  607. set {
  608. _lightRadius = value;
  609. }
  610. }
  611. public float LightCutoff {
  612. get {
  613. return _lightCutoff;
  614. }
  615. set {
  616. _lightCutoff = value;
  617. }
  618. }
  619. public float LightFalloff {
  620. get {
  621. return _lightFalloff;
  622. }
  623. set {
  624. _lightFalloff = value;
  625. }
  626. }
  627. public float LightIntensity {
  628. get {
  629. return _lightIntensity;
  630. }
  631. set {
  632. _lightIntensity = value;
  633. }
  634. }
  635. public bool FlexiEntry {
  636. get {
  637. return _flexiEntry;
  638. }
  639. set {
  640. _flexiEntry = value;
  641. }
  642. }
  643. public bool LightEntry {
  644. get {
  645. return _lightEntry;
  646. }
  647. set {
  648. _lightEntry = value;
  649. }
  650. }
  651. public bool SculptEntry {
  652. get {
  653. return _sculptEntry;
  654. }
  655. set {
  656. _sculptEntry = value;
  657. }
  658. }
  659. public byte[] ExtraParamsToBytes()
  660. {
  661. ushort FlexiEP = 0x10;
  662. ushort LightEP = 0x20;
  663. ushort SculptEP = 0x30;
  664. int i = 0;
  665. uint TotalBytesLength = 1; // ExtraParamsNum
  666. uint ExtraParamsNum = 0;
  667. if (_flexiEntry)
  668. {
  669. ExtraParamsNum++;
  670. TotalBytesLength += 16;// data
  671. TotalBytesLength += 2 + 4; // type
  672. }
  673. if (_lightEntry)
  674. {
  675. ExtraParamsNum++;
  676. TotalBytesLength += 16;// data
  677. TotalBytesLength += 2 + 4; // type
  678. }
  679. if (_sculptEntry)
  680. {
  681. ExtraParamsNum++;
  682. TotalBytesLength += 17;// data
  683. TotalBytesLength += 2 + 4; // type
  684. }
  685. byte[] returnbytes = new byte[TotalBytesLength];
  686. // uint paramlength = ExtraParamsNum;
  687. // Stick in the number of parameters
  688. returnbytes[i++] = (byte)ExtraParamsNum;
  689. if (_flexiEntry)
  690. {
  691. byte[] FlexiData = GetFlexiBytes();
  692. returnbytes[i++] = (byte)(FlexiEP % 256);
  693. returnbytes[i++] = (byte)((FlexiEP >> 8) % 256);
  694. returnbytes[i++] = (byte)(FlexiData.Length % 256);
  695. returnbytes[i++] = (byte)((FlexiData.Length >> 8) % 256);
  696. returnbytes[i++] = (byte)((FlexiData.Length >> 16) % 256);
  697. returnbytes[i++] = (byte)((FlexiData.Length >> 24) % 256);
  698. Array.Copy(FlexiData, 0, returnbytes, i, FlexiData.Length);
  699. i += FlexiData.Length;
  700. }
  701. if (_lightEntry)
  702. {
  703. byte[] LightData = GetLightBytes();
  704. returnbytes[i++] = (byte)(LightEP % 256);
  705. returnbytes[i++] = (byte)((LightEP >> 8) % 256);
  706. returnbytes[i++] = (byte)(LightData.Length % 256);
  707. returnbytes[i++] = (byte)((LightData.Length >> 8) % 256);
  708. returnbytes[i++] = (byte)((LightData.Length >> 16) % 256);
  709. returnbytes[i++] = (byte)((LightData.Length >> 24) % 256);
  710. Array.Copy(LightData, 0, returnbytes, i, LightData.Length);
  711. i += LightData.Length;
  712. }
  713. if (_sculptEntry)
  714. {
  715. byte[] SculptData = GetSculptBytes();
  716. returnbytes[i++] = (byte)(SculptEP % 256);
  717. returnbytes[i++] = (byte)((SculptEP >> 8) % 256);
  718. returnbytes[i++] = (byte)(SculptData.Length % 256);
  719. returnbytes[i++] = (byte)((SculptData.Length >> 8) % 256);
  720. returnbytes[i++] = (byte)((SculptData.Length >> 16) % 256);
  721. returnbytes[i++] = (byte)((SculptData.Length >> 24) % 256);
  722. Array.Copy(SculptData, 0, returnbytes, i, SculptData.Length);
  723. i += SculptData.Length;
  724. }
  725. if (!_flexiEntry && !_lightEntry && !_sculptEntry)
  726. {
  727. byte[] returnbyte = new byte[1];
  728. returnbyte[0] = 0;
  729. return returnbyte;
  730. }
  731. return returnbytes;
  732. //m_log.Info("[EXTRAPARAMS]: Length = " + m_shape.ExtraParams.Length.ToString());
  733. }
  734. public void ReadInUpdateExtraParam(ushort type, bool inUse, byte[] data)
  735. {
  736. const ushort FlexiEP = 0x10;
  737. const ushort LightEP = 0x20;
  738. const ushort SculptEP = 0x30;
  739. switch (type)
  740. {
  741. case FlexiEP:
  742. if (!inUse)
  743. {
  744. _flexiEntry = false;
  745. return;
  746. }
  747. ReadFlexiData(data, 0);
  748. break;
  749. case LightEP:
  750. if (!inUse)
  751. {
  752. _lightEntry = false;
  753. return;
  754. }
  755. ReadLightData(data, 0);
  756. break;
  757. case SculptEP:
  758. if (!inUse)
  759. {
  760. _sculptEntry = false;
  761. return;
  762. }
  763. ReadSculptData(data, 0);
  764. break;
  765. }
  766. }
  767. public void ReadInExtraParamsBytes(byte[] data)
  768. {
  769. if (data == null || data.Length == 1)
  770. return;
  771. const ushort FlexiEP = 0x10;
  772. const ushort LightEP = 0x20;
  773. const ushort SculptEP = 0x30;
  774. bool lGotFlexi = false;
  775. bool lGotLight = false;
  776. bool lGotSculpt = false;
  777. int i = 0;
  778. byte extraParamCount = 0;
  779. if (data.Length > 0)
  780. {
  781. extraParamCount = data[i++];
  782. }
  783. for (int k = 0; k < extraParamCount; k++)
  784. {
  785. ushort epType = Utils.BytesToUInt16(data, i);
  786. i += 2;
  787. // uint paramLength = Helpers.BytesToUIntBig(data, i);
  788. i += 4;
  789. switch (epType)
  790. {
  791. case FlexiEP:
  792. ReadFlexiData(data, i);
  793. i += 16;
  794. lGotFlexi = true;
  795. break;
  796. case LightEP:
  797. ReadLightData(data, i);
  798. i += 16;
  799. lGotLight = true;
  800. break;
  801. case SculptEP:
  802. ReadSculptData(data, i);
  803. i += 17;
  804. lGotSculpt = true;
  805. break;
  806. }
  807. }
  808. if (!lGotFlexi)
  809. _flexiEntry = false;
  810. if (!lGotLight)
  811. _lightEntry = false;
  812. if (!lGotSculpt)
  813. _sculptEntry = false;
  814. }
  815. public void ReadSculptData(byte[] data, int pos)
  816. {
  817. byte[] SculptTextureUUID = new byte[16];
  818. UUID SculptUUID = UUID.Zero;
  819. byte SculptTypel = data[16+pos];
  820. if (data.Length+pos >= 17)
  821. {
  822. _sculptEntry = true;
  823. SculptTextureUUID = new byte[16];
  824. SculptTypel = data[16 + pos];
  825. Array.Copy(data, pos, SculptTextureUUID,0, 16);
  826. SculptUUID = new UUID(SculptTextureUUID, 0);
  827. }
  828. else
  829. {
  830. _sculptEntry = false;
  831. SculptUUID = UUID.Zero;
  832. SculptTypel = 0x00;
  833. }
  834. if (_sculptEntry)
  835. {
  836. if (_sculptType != (byte)1 && _sculptType != (byte)2 && _sculptType != (byte)3 && _sculptType != (byte)4)
  837. _sculptType = 4;
  838. }
  839. _sculptTexture = SculptUUID;
  840. _sculptType = SculptTypel;
  841. //m_log.Info("[SCULPT]:" + SculptUUID.ToString());
  842. }
  843. public byte[] GetSculptBytes()
  844. {
  845. byte[] data = new byte[17];
  846. _sculptTexture.GetBytes().CopyTo(data, 0);
  847. data[16] = (byte)_sculptType;
  848. return data;
  849. }
  850. public void ReadFlexiData(byte[] data, int pos)
  851. {
  852. if (data.Length-pos >= 16)
  853. {
  854. _flexiEntry = true;
  855. _flexiSoftness = ((data[pos] & 0x80) >> 6) | ((data[pos + 1] & 0x80) >> 7);
  856. _flexiTension = (float)(data[pos++] & 0x7F) / 10.0f;
  857. _flexiDrag = (float)(data[pos++] & 0x7F) / 10.0f;
  858. _flexiGravity = (float)(data[pos++] / 10.0f) - 10.0f;
  859. _flexiWind = (float)data[pos++] / 10.0f;
  860. Vector3 lForce = new Vector3(data, pos);
  861. _flexiForceX = lForce.X;
  862. _flexiForceY = lForce.Y;
  863. _flexiForceZ = lForce.Z;
  864. }
  865. else
  866. {
  867. _flexiEntry = false;
  868. _flexiSoftness = 0;
  869. _flexiTension = 0.0f;
  870. _flexiDrag = 0.0f;
  871. _flexiGravity = 0.0f;
  872. _flexiWind = 0.0f;
  873. _flexiForceX = 0f;
  874. _flexiForceY = 0f;
  875. _flexiForceZ = 0f;
  876. }
  877. }
  878. public byte[] GetFlexiBytes()
  879. {
  880. byte[] data = new byte[16];
  881. int i = 0;
  882. // Softness is packed in the upper bits of tension and drag
  883. data[i] = (byte)((_flexiSoftness & 2) << 6);
  884. data[i + 1] = (byte)((_flexiSoftness & 1) << 7);
  885. data[i++] |= (byte)((byte)(_flexiTension * 10.01f) & 0x7F);
  886. data[i++] |= (byte)((byte)(_flexiDrag * 10.01f) & 0x7F);
  887. data[i++] = (byte)((_flexiGravity + 10.0f) * 10.01f);
  888. data[i++] = (byte)(_flexiWind * 10.01f);
  889. Vector3 lForce = new Vector3(_flexiForceX, _flexiForceY, _flexiForceZ);
  890. lForce.GetBytes().CopyTo(data, i);
  891. return data;
  892. }
  893. public void ReadLightData(byte[] data, int pos)
  894. {
  895. if (data.Length - pos >= 16)
  896. {
  897. _lightEntry = true;
  898. Color4 lColor = new Color4(data, pos, false);
  899. _lightIntensity = lColor.A;
  900. _lightColorA = 1f;
  901. _lightColorR = lColor.R;
  902. _lightColorG = lColor.G;
  903. _lightColorB = lColor.B;
  904. _lightRadius = Utils.BytesToFloat(data, pos + 4);
  905. _lightCutoff = Utils.BytesToFloat(data, pos + 8);
  906. _lightFalloff = Utils.BytesToFloat(data, pos + 12);
  907. }
  908. else
  909. {
  910. _lightEntry = false;
  911. _lightColorA = 1f;
  912. _lightColorR = 0f;
  913. _lightColorG = 0f;
  914. _lightColorB = 0f;
  915. _lightRadius = 0f;
  916. _lightCutoff = 0f;
  917. _lightFalloff = 0f;
  918. _lightIntensity = 0f;
  919. }
  920. }
  921. public byte[] GetLightBytes()
  922. {
  923. byte[] data = new byte[16];
  924. // Alpha channel in color is intensity
  925. Color4 tmpColor = new Color4(_lightColorR,_lightColorG,_lightColorB,_lightIntensity);
  926. tmpColor.GetBytes().CopyTo(data, 0);
  927. Utils.FloatToBytes(_lightRadius).CopyTo(data, 4);
  928. Utils.FloatToBytes(_lightCutoff).CopyTo(data, 8);
  929. Utils.FloatToBytes(_lightFalloff).CopyTo(data, 12);
  930. return data;
  931. }
  932. }
  933. }