PrimitiveBaseShape.cs 35 KB

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