PrimitiveBaseShape.cs 35 KB

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