12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535 |
- /*
- * Copyright (c) Contributors, http://opensimulator.org/
- * See CONTRIBUTORS.TXT for a full list of copyright holders.
- *
- * Redistribution and use in source and binary forms, with or without
- * modification, are permitted provided that the following conditions are met:
- * * Redistributions of source code must retain the above copyright
- * notice, this list of conditions and the following disclaimer.
- * * Redistributions in binary form must reproduce the above copyright
- * notice, this list of conditions and the following disclaimer in the
- * documentation and/or other materials provided with the distribution.
- * * Neither the name of the OpenSimulator Project nor the
- * names of its contributors may be used to endorse or promote products
- * derived from this software without specific prior written permission.
- *
- * THIS SOFTWARE IS PROVIDED BY THE DEVELOPERS ``AS IS'' AND ANY
- * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
- * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
- * DISCLAIMED. IN NO EVENT SHALL THE CONTRIBUTORS BE LIABLE FOR ANY
- * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
- * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
- * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
- * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
- * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
- * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
- */
- using System;
- using System.Collections.Generic;
- using System.Drawing;
- using System.Drawing.Imaging;
- using System.IO;
- using System.Reflection;
- using System.Xml;
- using System.Xml.Schema;
- using System.Xml.Serialization;
- using log4net;
- using OpenMetaverse;
- using OpenMetaverse.StructuredData;
- namespace OpenSim.Framework
- {
- public enum ProfileShape : byte
- {
- Circle = 0,
- Square = 1,
- IsometricTriangle = 2,
- EquilateralTriangle = 3,
- RightTriangle = 4,
- HalfCircle = 5
- }
- public enum HollowShape : byte
- {
- Same = 0,
- Circle = 16,
- Square = 32,
- Triangle = 48
- }
- public enum PCodeEnum : byte
- {
- Primitive = 9,
- Avatar = 47,
- Grass = 95,
- NewTree = 111,
- ParticleSystem = 143,
- Tree = 255
- }
- public enum Extrusion : byte
- {
- Straight = 16,
- Curve1 = 32,
- Curve2 = 48,
- Flexible = 128
- }
- [Serializable]
- public class PrimitiveBaseShape
- {
- private static readonly ILog m_log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
- private static readonly byte[] DEFAULT_TEXTURE = new Primitive.TextureEntry(new UUID("89556747-24cb-43ed-920b-47caed15465f")).GetBytes();
- private byte[] m_textureEntry;
- private ushort _pathBegin;
- private byte _pathCurve;
- private ushort _pathEnd;
- private sbyte _pathRadiusOffset;
- private byte _pathRevolutions;
- private byte _pathScaleX;
- private byte _pathScaleY;
- private byte _pathShearX;
- private byte _pathShearY;
- private sbyte _pathSkew;
- private sbyte _pathTaperX;
- private sbyte _pathTaperY;
- private sbyte _pathTwist;
- private sbyte _pathTwistBegin;
- private byte _pCode;
- private ushort _profileBegin;
- private ushort _profileEnd;
- private ushort _profileHollow;
- private Vector3 _scale;
- private byte _state;
- private byte _lastattach;
- private ProfileShape _profileShape;
- private HollowShape _hollowShape;
- // Sculpted
- [XmlIgnore] private UUID _sculptTexture;
- [XmlIgnore] private byte _sculptType;
- [XmlIgnore] private byte[] _sculptData = Utils.EmptyBytes;
- // Flexi
- [XmlIgnore] private int _flexiSoftness;
- [XmlIgnore] private float _flexiTension;
- [XmlIgnore] private float _flexiDrag;
- [XmlIgnore] private float _flexiGravity;
- [XmlIgnore] private float _flexiWind;
- [XmlIgnore] private float _flexiForceX;
- [XmlIgnore] private float _flexiForceY;
- [XmlIgnore] private float _flexiForceZ;
- //Bright n sparkly
- [XmlIgnore] private float _lightColorR;
- [XmlIgnore] private float _lightColorG;
- [XmlIgnore] private float _lightColorB;
- [XmlIgnore] private float _lightColorA = 1.0f;
- [XmlIgnore] private float _lightRadius;
- [XmlIgnore] private float _lightCutoff;
- [XmlIgnore] private float _lightFalloff;
- [XmlIgnore] private float _lightIntensity = 1.0f;
- [XmlIgnore] private bool _flexiEntry;
- [XmlIgnore] private bool _lightEntry;
- [XmlIgnore] private bool _sculptEntry;
- // Light Projection Filter
- [XmlIgnore] private bool _projectionEntry;
- [XmlIgnore] private UUID _projectionTextureID;
- [XmlIgnore] private float _projectionFOV;
- [XmlIgnore] private float _projectionFocus;
- [XmlIgnore] private float _projectionAmb;
- public byte ProfileCurve
- {
- get { return (byte)((byte)HollowShape | (byte)ProfileShape); }
- set
- {
- // Handle hollow shape component
- byte hollowShapeByte = (byte)(value & 0xf0);
- if (!Enum.IsDefined(typeof(HollowShape), hollowShapeByte))
- {
- m_log.WarnFormat(
- "[SHAPE]: Attempt to set a ProfileCurve with a hollow shape value of {0}, which isn't a valid enum. Replacing with default shape.",
- hollowShapeByte);
- this._hollowShape = HollowShape.Same;
- }
- else
- {
- this._hollowShape = (HollowShape)hollowShapeByte;
- }
- // Handle profile shape component
- byte profileShapeByte = (byte)(value & 0xf);
- if (!Enum.IsDefined(typeof(ProfileShape), profileShapeByte))
- {
- m_log.WarnFormat(
- "[SHAPE]: Attempt to set a ProfileCurve with a profile shape value of {0}, which isn't a valid enum. Replacing with square.",
- profileShapeByte);
- this._profileShape = ProfileShape.Square;
- }
- else
- {
- this._profileShape = (ProfileShape)profileShapeByte;
- }
- }
- }
- /// <summary>
- /// Entries to store media textures on each face
- /// </summary>
- /// Do not change this value directly - always do it through an IMoapModule.
- /// Lock before manipulating.
- public MediaList Media { get; set; }
- public PrimitiveBaseShape()
- {
- PCode = (byte)PCodeEnum.Primitive;
- m_textureEntry = DEFAULT_TEXTURE;
- }
- /// <summary>
- /// Construct a PrimitiveBaseShape object from a OpenMetaverse.Primitive object
- /// </summary>
- /// <param name="prim"></param>
- public PrimitiveBaseShape(Primitive prim)
- {
- // m_log.DebugFormat("[PRIMITIVE BASE SHAPE]: Creating from {0}", prim.ID);
- PCode = (byte)prim.PrimData.PCode;
- State = prim.PrimData.State;
- LastAttachPoint = prim.PrimData.State;
- PathBegin = Primitive.PackBeginCut(prim.PrimData.PathBegin);
- PathEnd = Primitive.PackEndCut(prim.PrimData.PathEnd);
- PathScaleX = Primitive.PackPathScale(prim.PrimData.PathScaleX);
- PathScaleY = Primitive.PackPathScale(prim.PrimData.PathScaleY);
- PathShearX = (byte)Primitive.PackPathShear(prim.PrimData.PathShearX);
- PathShearY = (byte)Primitive.PackPathShear(prim.PrimData.PathShearY);
- PathSkew = Primitive.PackPathTwist(prim.PrimData.PathSkew);
- ProfileBegin = Primitive.PackBeginCut(prim.PrimData.ProfileBegin);
- ProfileEnd = Primitive.PackEndCut(prim.PrimData.ProfileEnd);
- Scale = prim.Scale;
- PathCurve = (byte)prim.PrimData.PathCurve;
- ProfileCurve = (byte)prim.PrimData.ProfileCurve;
- ProfileHollow = Primitive.PackProfileHollow(prim.PrimData.ProfileHollow);
- PathRadiusOffset = Primitive.PackPathTwist(prim.PrimData.PathRadiusOffset);
- PathRevolutions = Primitive.PackPathRevolutions(prim.PrimData.PathRevolutions);
- PathTaperX = Primitive.PackPathTaper(prim.PrimData.PathTaperX);
- PathTaperY = Primitive.PackPathTaper(prim.PrimData.PathTaperY);
- PathTwist = Primitive.PackPathTwist(prim.PrimData.PathTwist);
- PathTwistBegin = Primitive.PackPathTwist(prim.PrimData.PathTwistBegin);
- m_textureEntry = prim.Textures.GetBytes();
- if (prim.Sculpt != null)
- {
- SculptEntry = (prim.Sculpt.Type != OpenMetaverse.SculptType.None);
- SculptData = prim.Sculpt.GetBytes();
- SculptTexture = prim.Sculpt.SculptTexture;
- SculptType = (byte)prim.Sculpt.Type;
- }
- else
- {
- SculptType = (byte)OpenMetaverse.SculptType.None;
- }
- }
- [XmlIgnore]
- public Primitive.TextureEntry Textures
- {
- get
- {
- // m_log.DebugFormat("[SHAPE]: get m_textureEntry length {0}", m_textureEntry.Length);
- try { return new Primitive.TextureEntry(m_textureEntry, 0, m_textureEntry.Length); }
- catch { }
- m_log.Warn("[SHAPE]: Failed to decode texture, length=" + ((m_textureEntry != null) ? m_textureEntry.Length : 0));
- return new Primitive.TextureEntry(UUID.Zero);
- }
- set { m_textureEntry = value.GetBytes(); }
- }
- public byte[] TextureEntry
- {
- get { return m_textureEntry; }
- set
- {
- if (value == null)
- m_textureEntry = new byte[1];
- else
- m_textureEntry = value;
- }
- }
- public static PrimitiveBaseShape Default
- {
- get
- {
- PrimitiveBaseShape boxShape = CreateBox();
- boxShape.SetScale(0.5f);
- return boxShape;
- }
- }
- public static PrimitiveBaseShape Create()
- {
- PrimitiveBaseShape shape = new PrimitiveBaseShape();
- return shape;
- }
- public static PrimitiveBaseShape CreateBox()
- {
- PrimitiveBaseShape shape = Create();
- shape._pathCurve = (byte) Extrusion.Straight;
- shape._profileShape = ProfileShape.Square;
- shape._pathScaleX = 100;
- shape._pathScaleY = 100;
- return shape;
- }
- public static PrimitiveBaseShape CreateSphere()
- {
- PrimitiveBaseShape shape = Create();
- shape._pathCurve = (byte) Extrusion.Curve1;
- shape._profileShape = ProfileShape.HalfCircle;
- shape._pathScaleX = 100;
- shape._pathScaleY = 100;
- return shape;
- }
- public static PrimitiveBaseShape CreateCylinder()
- {
- PrimitiveBaseShape shape = Create();
- shape._pathCurve = (byte) Extrusion.Curve1;
- shape._profileShape = ProfileShape.Square;
- shape._pathScaleX = 100;
- shape._pathScaleY = 100;
- return shape;
- }
- public static PrimitiveBaseShape CreateMesh(int numberOfFaces, UUID meshAssetID)
- {
- PrimitiveBaseShape shape = new PrimitiveBaseShape();
- shape._pathScaleX = 100;
- shape._pathScaleY = 100;
- if(numberOfFaces <= 0) // oops ?
- numberOfFaces = 1;
- switch(numberOfFaces)
- {
- case 1: // torus
- shape.ProfileCurve = (byte)ProfileShape.Circle | (byte)HollowShape.Triangle;
- shape.PathCurve = (byte)Extrusion.Curve1;
- shape._pathScaleY = 150;
- break;
- case 2: // torus with hollow (a sl viewer whould see 4 faces on a hollow sphere)
- shape.ProfileCurve = (byte)ProfileShape.Circle | (byte)HollowShape.Triangle;
- shape.PathCurve = (byte)Extrusion.Curve1;
- shape.ProfileHollow = 27500;
- shape._pathScaleY = 150;
- break;
- case 3: // cylinder
- shape.ProfileCurve = (byte)ProfileShape.Circle | (byte)HollowShape.Triangle;
- shape.PathCurve = (byte)Extrusion.Straight;
- break;
- case 4: // cylinder with hollow
- shape.ProfileCurve = (byte)ProfileShape.Circle | (byte)HollowShape.Triangle;
- shape.PathCurve = (byte)Extrusion.Straight;
- shape.ProfileHollow = 27500;
- break;
- case 5: // prism
- shape.ProfileCurve = (byte)ProfileShape.EquilateralTriangle | (byte)HollowShape.Triangle;
- shape.PathCurve = (byte)Extrusion.Straight;
- break;
- case 6: // box
- shape.ProfileCurve = (byte)ProfileShape.Square | (byte)HollowShape.Triangle;
- shape.PathCurve = (byte)Extrusion.Straight;
- break;
- case 7: // box with hollow
- shape.ProfileCurve = (byte)ProfileShape.Square | (byte)HollowShape.Triangle;
- shape.PathCurve = (byte)Extrusion.Straight;
- shape.ProfileHollow = 27500;
- break;
- default: // 8 faces box with cut
- shape.ProfileCurve = (byte)ProfileShape.Square | (byte)HollowShape.Triangle;
- shape.PathCurve = (byte)Extrusion.Straight;
- shape.ProfileBegin = 9375;
- break;
- }
- shape.SculptEntry = true;
- shape.SculptType = (byte)OpenMetaverse.SculptType.Mesh;
- shape.SculptTexture = meshAssetID;
- return shape;
- }
- public void SetScale(float side)
- {
- _scale = new Vector3(side, side, side);
- }
- public void SetHeigth(float height)
- {
- _scale.Z = height;
- }
- public void SetRadius(float radius)
- {
- _scale.X = _scale.Y = radius * 2f;
- }
- // TODO: void returns need to change of course
- public virtual void GetMesh()
- {
- }
- public PrimitiveBaseShape Copy()
- {
- return (PrimitiveBaseShape) MemberwiseClone();
- }
- public static PrimitiveBaseShape CreateCylinder(float radius, float heigth)
- {
- PrimitiveBaseShape shape = CreateCylinder();
- shape.SetHeigth(heigth);
- shape.SetRadius(radius);
- return shape;
- }
- public void SetPathRange(Vector3 pathRange)
- {
- _pathBegin = Primitive.PackBeginCut(pathRange.X);
- _pathEnd = Primitive.PackEndCut(pathRange.Y);
- }
- public void SetPathRange(float begin, float end)
- {
- _pathBegin = Primitive.PackBeginCut(begin);
- _pathEnd = Primitive.PackEndCut(end);
- }
- public void SetSculptProperties(byte sculptType, UUID SculptTextureUUID)
- {
- _sculptType = sculptType;
- _sculptTexture = SculptTextureUUID;
- }
- public void SetProfileRange(Vector3 profileRange)
- {
- _profileBegin = Primitive.PackBeginCut(profileRange.X);
- _profileEnd = Primitive.PackEndCut(profileRange.Y);
- }
- public void SetProfileRange(float begin, float end)
- {
- _profileBegin = Primitive.PackBeginCut(begin);
- _profileEnd = Primitive.PackEndCut(end);
- }
- public byte[] ExtraParams
- {
- get
- {
- return ExtraParamsToBytes();
- }
- set
- {
- ReadInExtraParamsBytes(value);
- }
- }
- public ushort PathBegin {
- get {
- return _pathBegin;
- }
- set {
- _pathBegin = value;
- }
- }
- public byte PathCurve {
- get {
- return _pathCurve;
- }
- set {
- _pathCurve = value;
- }
- }
- public ushort PathEnd {
- get {
- return _pathEnd;
- }
- set {
- _pathEnd = value;
- }
- }
- public sbyte PathRadiusOffset {
- get {
- return _pathRadiusOffset;
- }
- set {
- _pathRadiusOffset = value;
- }
- }
- public byte PathRevolutions {
- get {
- return _pathRevolutions;
- }
- set {
- _pathRevolutions = value;
- }
- }
- public byte PathScaleX {
- get {
- return _pathScaleX;
- }
- set {
- _pathScaleX = value;
- }
- }
- public byte PathScaleY {
- get {
- return _pathScaleY;
- }
- set {
- _pathScaleY = value;
- }
- }
- public byte PathShearX {
- get {
- return _pathShearX;
- }
- set {
- _pathShearX = value;
- }
- }
- public byte PathShearY {
- get {
- return _pathShearY;
- }
- set {
- _pathShearY = value;
- }
- }
- public sbyte PathSkew {
- get {
- return _pathSkew;
- }
- set {
- _pathSkew = value;
- }
- }
- public sbyte PathTaperX {
- get {
- return _pathTaperX;
- }
- set {
- _pathTaperX = value;
- }
- }
- public sbyte PathTaperY {
- get {
- return _pathTaperY;
- }
- set {
- _pathTaperY = value;
- }
- }
- public sbyte PathTwist {
- get {
- return _pathTwist;
- }
- set {
- _pathTwist = value;
- }
- }
- public sbyte PathTwistBegin {
- get {
- return _pathTwistBegin;
- }
- set {
- _pathTwistBegin = value;
- }
- }
- public byte PCode {
- get {
- return _pCode;
- }
- set {
- _pCode = value;
- }
- }
- public ushort ProfileBegin {
- get {
- return _profileBegin;
- }
- set {
- _profileBegin = value;
- }
- }
- public ushort ProfileEnd {
- get {
- return _profileEnd;
- }
- set {
- _profileEnd = value;
- }
- }
- public ushort ProfileHollow {
- get {
- return _profileHollow;
- }
- set {
- _profileHollow = value;
- }
- }
- public Vector3 Scale {
- get {
- return _scale;
- }
- set {
- _scale = value;
- }
- }
- public byte State {
- get {
- return _state;
- }
- set {
- _state = value;
- }
- }
- public byte LastAttachPoint {
- get {
- return _lastattach;
- }
- set {
- _lastattach = value;
- }
- }
- public ProfileShape ProfileShape {
- get {
- return _profileShape;
- }
- set {
- _profileShape = value;
- }
- }
- public HollowShape HollowShape {
- get {
- return _hollowShape;
- }
- set {
- _hollowShape = value;
- }
- }
- public UUID SculptTexture {
- get {
- return _sculptTexture;
- }
- set {
- _sculptTexture = value;
- }
- }
- public byte SculptType
- {
- get
- {
- return _sculptType;
- }
- set
- {
- _sculptType = value;
- }
- }
- // This is only used at runtime. For sculpties this holds the texture data, and for meshes
- // the mesh data.
- public byte[] SculptData
- {
- get
- {
- return _sculptData;
- }
- set
- {
- // m_log.DebugFormat("[PRIMITIVE BASE SHAPE]: Setting SculptData to data with length {0}", value.Length);
- _sculptData = value;
- }
- }
- public int FlexiSoftness
- {
- get
- {
- return _flexiSoftness;
- }
- set
- {
- _flexiSoftness = value;
- }
- }
- public float FlexiTension {
- get {
- return _flexiTension;
- }
- set {
- _flexiTension = value;
- }
- }
- public float FlexiDrag {
- get {
- return _flexiDrag;
- }
- set {
- _flexiDrag = value;
- }
- }
- public float FlexiGravity {
- get {
- return _flexiGravity;
- }
- set {
- _flexiGravity = value;
- }
- }
- public float FlexiWind {
- get {
- return _flexiWind;
- }
- set {
- _flexiWind = value;
- }
- }
- public float FlexiForceX {
- get {
- return _flexiForceX;
- }
- set {
- _flexiForceX = value;
- }
- }
- public float FlexiForceY {
- get {
- return _flexiForceY;
- }
- set {
- _flexiForceY = value;
- }
- }
- public float FlexiForceZ {
- get {
- return _flexiForceZ;
- }
- set {
- _flexiForceZ = value;
- }
- }
- public float LightColorR {
- get {
- return _lightColorR;
- }
- set {
- if (value < 0)
- _lightColorR = 0;
- else if (value > 1.0f)
- _lightColorR = 1.0f;
- else
- _lightColorR = value;
- }
- }
- public float LightColorG {
- get {
- return _lightColorG;
- }
- set {
- if (value < 0)
- _lightColorG = 0;
- else if (value > 1.0f)
- _lightColorG = 1.0f;
- else
- _lightColorG = value;
- }
- }
- public float LightColorB {
- get {
- return _lightColorB;
- }
- set {
- if (value < 0)
- _lightColorB = 0;
- else if (value > 1.0f)
- _lightColorB = 1.0f;
- else
- _lightColorB = value;
- }
- }
- public float LightColorA {
- get {
- return _lightColorA;
- }
- set {
- if (value < 0)
- _lightColorA = 0;
- else if (value > 1.0f)
- _lightColorA = 1.0f;
- else
- _lightColorA = value;
- }
- }
- public float LightRadius {
- get {
- return _lightRadius;
- }
- set {
- _lightRadius = value;
- }
- }
- public float LightCutoff {
- get {
- return _lightCutoff;
- }
- set {
- _lightCutoff = value;
- }
- }
- public float LightFalloff {
- get {
- return _lightFalloff;
- }
- set {
- _lightFalloff = value;
- }
- }
- public float LightIntensity {
- get {
- return _lightIntensity;
- }
- set {
- _lightIntensity = value;
- }
- }
- public bool FlexiEntry {
- get {
- return _flexiEntry;
- }
- set {
- _flexiEntry = value;
- }
- }
- public bool LightEntry {
- get {
- return _lightEntry;
- }
- set {
- _lightEntry = value;
- }
- }
- public bool SculptEntry {
- get {
- return _sculptEntry;
- }
- set {
- _sculptEntry = value;
- }
- }
- public bool ProjectionEntry {
- get {
- return _projectionEntry;
- }
- set {
- _projectionEntry = value;
- }
- }
- public UUID ProjectionTextureUUID {
- get {
- return _projectionTextureID;
- }
- set {
- _projectionTextureID = value;
- }
- }
- public float ProjectionFOV {
- get {
- return _projectionFOV;
- }
- set {
- _projectionFOV = value;
- }
- }
- public float ProjectionFocus {
- get {
- return _projectionFocus;
- }
- set {
- _projectionFocus = value;
- }
- }
- public float ProjectionAmbiance {
- get {
- return _projectionAmb;
- }
- set {
- _projectionAmb = value;
- }
- }
- public ulong GetMeshKey(Vector3 size, float lod)
- {
- return GetMeshKey(size, lod, false);
- }
- public ulong GetMeshKey(Vector3 size, float lod, bool convex)
- {
- ulong hash = 5381;
- hash = djb2(hash, this.PathCurve);
- hash = djb2(hash, (byte)((byte)this.HollowShape | (byte)this.ProfileShape));
- hash = djb2(hash, this.PathBegin);
- hash = djb2(hash, this.PathEnd);
- hash = djb2(hash, this.PathScaleX);
- hash = djb2(hash, this.PathScaleY);
- hash = djb2(hash, this.PathShearX);
- hash = djb2(hash, this.PathShearY);
- hash = djb2(hash, (byte)this.PathTwist);
- hash = djb2(hash, (byte)this.PathTwistBegin);
- hash = djb2(hash, (byte)this.PathRadiusOffset);
- hash = djb2(hash, (byte)this.PathTaperX);
- hash = djb2(hash, (byte)this.PathTaperY);
- hash = djb2(hash, this.PathRevolutions);
- hash = djb2(hash, (byte)this.PathSkew);
- hash = djb2(hash, this.ProfileBegin);
- hash = djb2(hash, this.ProfileEnd);
- hash = djb2(hash, this.ProfileHollow);
- // TODO: Separate scale out from the primitive shape data (after
- // scaling is supported at the physics engine level)
- byte[] scaleBytes = size.GetBytes();
- for (int i = 0; i < scaleBytes.Length; i++)
- hash = djb2(hash, scaleBytes[i]);
- // Include LOD in hash, accounting for endianness
- byte[] lodBytes = new byte[4];
- Buffer.BlockCopy(BitConverter.GetBytes(lod), 0, lodBytes, 0, 4);
- if (!BitConverter.IsLittleEndian)
- {
- Array.Reverse(lodBytes, 0, 4);
- }
- for (int i = 0; i < lodBytes.Length; i++)
- hash = djb2(hash, lodBytes[i]);
- // include sculpt UUID
- if (this.SculptEntry)
- {
- scaleBytes = this.SculptTexture.GetBytes();
- for (int i = 0; i < scaleBytes.Length; i++)
- hash = djb2(hash, scaleBytes[i]);
- }
- if(convex)
- hash = djb2(hash, 0xa5);
- return hash;
- }
- private ulong djb2(ulong hash, byte c)
- {
- return ((hash << 5) + hash) + (ulong)c;
- }
- private ulong djb2(ulong hash, ushort c)
- {
- hash = ((hash << 5) + hash) + (ulong)((byte)c);
- return ((hash << 5) + hash) + (ulong)(c >> 8);
- }
- public byte[] ExtraParamsToBytes()
- {
- // m_log.DebugFormat("[EXTRAPARAMS]: Called ExtraParamsToBytes()");
- const byte FlexiEP = 0x10;
- const byte LightEP = 0x20;
- const byte SculptEP = 0x30;
- const byte ProjectionEP = 0x40;
- int TotalBytesLength = 1; // ExtraParamsNum
- uint ExtraParamsNum = 0;
- if (_flexiEntry)
- {
- ExtraParamsNum++;
- TotalBytesLength += 16 + 2 + 4;// data
- }
- if (_lightEntry)
- {
- ExtraParamsNum++;
- TotalBytesLength += 16 + 2 + 4; // data
- }
- if (_sculptEntry)
- {
- ExtraParamsNum++;
- TotalBytesLength += 17 + 2 + 4;// data
- }
- if (_projectionEntry)
- {
- ExtraParamsNum++;
- TotalBytesLength += 28 + 2 + 4; // data
- }
- byte[] returnBytes = new byte[TotalBytesLength];
- returnBytes[0] = (byte)ExtraParamsNum;
- if(ExtraParamsNum == 0)
- return returnBytes;
- int i = 1;
- if (_flexiEntry)
- {
- returnBytes[i] = FlexiEP; // 2 bytes id code
- i += 2;
- returnBytes[i] = 16; // 4 bytes size
- i += 4;
- // Softness is packed in the upper bits of tension and drag
- returnBytes[i] = (byte)((_flexiSoftness & 2) << 6);
- returnBytes[i + 1] = (byte)((_flexiSoftness & 1) << 7);
- returnBytes[i++] |= (byte)((byte)(_flexiTension * 10.01f) & 0x7F);
- returnBytes[i++] |= (byte)((byte)(_flexiDrag * 10.01f) & 0x7F);
- returnBytes[i++] = (byte)((_flexiGravity + 10.0f) * 10.01f);
- returnBytes[i++] = (byte)(_flexiWind * 10.01f);
- Utils.FloatToBytes(_flexiForceX, returnBytes, i);
- Utils.FloatToBytes(_flexiForceY, returnBytes, i + 4);
- Utils.FloatToBytes(_flexiForceZ, returnBytes, i + 8);
- i += 12;
- }
- if (_lightEntry)
- {
- returnBytes[i] = LightEP;
- i += 2;
- returnBytes[i] = 16;
- i += 4;
- // Alpha channel in color is intensity
- Color4 tmpColor = new Color4(_lightColorR, _lightColorG, _lightColorB, _lightIntensity);
- tmpColor.GetBytes().CopyTo(returnBytes, i);
- Utils.FloatToBytes(_lightRadius, returnBytes, i + 4);
- Utils.FloatToBytes(_lightCutoff, returnBytes, i + 8);
- Utils.FloatToBytes(_lightFalloff, returnBytes, i + 12);
- i += 16;
- }
- if (_sculptEntry)
- {
- returnBytes[i] = SculptEP;
- i += 2;
- returnBytes[i] = 17;
- i += 4;
- _sculptTexture.GetBytes().CopyTo(returnBytes, i);
- i += 16;
- returnBytes[i++] = _sculptType;
- }
- if (_projectionEntry)
- {
- returnBytes[i] = ProjectionEP;
- i += 2;
- returnBytes[i] = 28;
- i += 4;
- _projectionTextureID.GetBytes().CopyTo(returnBytes, i);
- Utils.FloatToBytes(_projectionFOV).CopyTo(returnBytes, i + 16);
- Utils.FloatToBytes(_projectionFocus).CopyTo(returnBytes, i + 20);
- Utils.FloatToBytes(_projectionAmb).CopyTo(returnBytes, i + 24);
- }
- return returnBytes;
- }
- public void ReadInUpdateExtraParam(ushort type, bool inUse, byte[] data)
- {
- const ushort FlexiEP = 0x10;
- const ushort LightEP = 0x20;
- const ushort SculptEP = 0x30;
- const ushort ProjectionEP = 0x40;
- switch (type)
- {
- case FlexiEP:
- if (!inUse)
- {
- _flexiEntry = false;
- return;
- }
- ReadFlexiData(data, 0);
- break;
- case LightEP:
- if (!inUse)
- {
- _lightEntry = false;
- return;
- }
- ReadLightData(data, 0);
- break;
- case SculptEP:
- if (!inUse)
- {
- _sculptEntry = false;
- return;
- }
- ReadSculptData(data, 0);
- break;
- case ProjectionEP:
- if (!inUse)
- {
- _projectionEntry = false;
- return;
- }
- ReadProjectionData(data, 0);
- break;
- }
- }
- public void ReadInExtraParamsBytes(byte[] data)
- {
- if (data == null)
- return;
- _flexiEntry = false;
- _lightEntry = false;
- _sculptEntry = false;
- _projectionEntry = false;
- if (data.Length == 1)
- return;
- const byte FlexiEP = 0x10;
- const byte LightEP = 0x20;
- const byte SculptEP = 0x30;
- const byte ProjectionEP = 0x40;
- byte extraParamCount = data[0];
- int i = 1;
- for (int k = 0; k < extraParamCount; k++)
- {
- byte epType = data[i];
- i += 6;
- switch (epType)
- {
- case FlexiEP:
- ReadFlexiData(data, i);
- i += 16;
- break;
- case LightEP:
- ReadLightData(data, i);
- i += 16;
- break;
- case SculptEP:
- ReadSculptData(data, i);
- i += 17;
- break;
- case ProjectionEP:
- ReadProjectionData(data, i);
- i += 28;
- break;
- }
- }
- }
- public void ReadSculptData(byte[] data, int pos)
- {
- if (data.Length-pos >= 17)
- {
- _sculptTexture = new UUID(data, pos);
- _sculptType = data[pos + 16];
- _sculptEntry = true;
- }
- else
- {
- _sculptEntry = false;
- _sculptTexture = UUID.Zero;
- _sculptType = 0x00;
- }
- }
- public void ReadFlexiData(byte[] data, int pos)
- {
- if (data.Length-pos >= 16)
- {
- _flexiEntry = true;
- _flexiSoftness = ((data[pos] & 0x80) >> 6) | ((data[pos + 1] & 0x80) >> 7);
- _flexiTension = (float)(data[pos++] & 0x7F) / 10.0f;
- _flexiDrag = (float)(data[pos++] & 0x7F) / 10.0f;
- _flexiGravity = (float)(data[pos++] / 10.0f) - 10.0f;
- _flexiWind = (float)data[pos++] / 10.0f;
- _flexiForceX = Utils.BytesToFloat(data, pos);
- _flexiForceY = Utils.BytesToFloat(data, pos + 4);
- _flexiForceZ = Utils.BytesToFloat(data, pos + 8);
- }
- else
- {
- _flexiEntry = false;
- _flexiSoftness = 0;
- _flexiTension = 0.0f;
- _flexiDrag = 0.0f;
- _flexiGravity = 0.0f;
- _flexiWind = 0.0f;
- _flexiForceX = 0f;
- _flexiForceY = 0f;
- _flexiForceZ = 0f;
- }
- }
- public void ReadLightData(byte[] data, int pos)
- {
- if (data.Length - pos >= 16)
- {
- _lightEntry = true;
- Color4 lColor = new Color4(data, pos, false);
- _lightIntensity = lColor.A;
- _lightColorA = 1f;
- _lightColorR = lColor.R;
- _lightColorG = lColor.G;
- _lightColorB = lColor.B;
- _lightRadius = Utils.BytesToFloat(data, pos + 4);
- _lightCutoff = Utils.BytesToFloat(data, pos + 8);
- _lightFalloff = Utils.BytesToFloat(data, pos + 12);
- }
- else
- {
- _lightEntry = false;
- _lightColorA = 1f;
- _lightColorR = 0f;
- _lightColorG = 0f;
- _lightColorB = 0f;
- _lightRadius = 0f;
- _lightCutoff = 0f;
- _lightFalloff = 0f;
- _lightIntensity = 0f;
- }
- }
- public void ReadProjectionData(byte[] data, int pos)
- {
- if (data.Length - pos >= 28)
- {
- _projectionEntry = true;
- _projectionTextureID = new UUID(data, pos);
- _projectionFOV = Utils.BytesToFloat(data, pos + 16);
- _projectionFocus = Utils.BytesToFloat(data, pos + 20);
- _projectionAmb = Utils.BytesToFloat(data, pos + 24);
- }
- else
- {
- _projectionEntry = false;
- _projectionTextureID = UUID.Zero;
- _projectionFOV = 0f;
- _projectionFocus = 0f;
- _projectionAmb = 0f;
- }
- }
- /// <summary>
- /// Creates a OpenMetaverse.Primitive and populates it with converted PrimitiveBaseShape values
- /// </summary>
- /// <returns></returns>
- public Primitive ToOmvPrimitive()
- {
- // position and rotation defaults here since they are not available in PrimitiveBaseShape
- return ToOmvPrimitive(new Vector3(0.0f, 0.0f, 0.0f),
- new Quaternion(0.0f, 0.0f, 0.0f, 1.0f));
- }
- /// <summary>
- /// Creates a OpenMetaverse.Primitive and populates it with converted PrimitiveBaseShape values
- /// </summary>
- /// <param name="position"></param>
- /// <param name="rotation"></param>
- /// <returns></returns>
- public Primitive ToOmvPrimitive(Vector3 position, Quaternion rotation)
- {
- OpenMetaverse.Primitive prim = new OpenMetaverse.Primitive();
- prim.Scale = this.Scale;
- prim.Position = position;
- prim.Rotation = rotation;
- if (this.SculptEntry)
- {
- prim.Sculpt = new Primitive.SculptData();
- prim.Sculpt.Type = (OpenMetaverse.SculptType)this.SculptType;
- prim.Sculpt.SculptTexture = this.SculptTexture;
- }
- prim.PrimData.PathShearX = this.PathShearX < 128 ? (float)this.PathShearX * 0.01f : (float)(this.PathShearX - 256) * 0.01f;
- prim.PrimData.PathShearY = this.PathShearY < 128 ? (float)this.PathShearY * 0.01f : (float)(this.PathShearY - 256) * 0.01f;
- prim.PrimData.PathBegin = (float)this.PathBegin * 2.0e-5f;
- prim.PrimData.PathEnd = 1.0f - (float)this.PathEnd * 2.0e-5f;
- prim.PrimData.PathScaleX = (200 - this.PathScaleX) * 0.01f;
- prim.PrimData.PathScaleY = (200 - this.PathScaleY) * 0.01f;
- prim.PrimData.PathTaperX = this.PathTaperX * 0.01f;
- prim.PrimData.PathTaperY = this.PathTaperY * 0.01f;
- prim.PrimData.PathTwistBegin = this.PathTwistBegin * 0.01f;
- prim.PrimData.PathTwist = this.PathTwist * 0.01f;
- prim.PrimData.ProfileBegin = (float)this.ProfileBegin * 2.0e-5f;
- prim.PrimData.ProfileEnd = 1.0f - (float)this.ProfileEnd * 2.0e-5f;
- prim.PrimData.ProfileHollow = (float)this.ProfileHollow * 2.0e-5f;
- prim.PrimData.profileCurve = this.ProfileCurve;
- prim.PrimData.ProfileHole = (HoleType)this.HollowShape;
- prim.PrimData.PathCurve = (PathCurve)this.PathCurve;
- prim.PrimData.PathRadiusOffset = 0.01f * this.PathRadiusOffset;
- prim.PrimData.PathRevolutions = 1.0f + 0.015f * this.PathRevolutions;
- prim.PrimData.PathSkew = 0.01f * this.PathSkew;
- prim.PrimData.PCode = OpenMetaverse.PCode.Prim;
- prim.PrimData.State = 0;
- if (this.FlexiEntry)
- {
- prim.Flexible = new Primitive.FlexibleData();
- prim.Flexible.Drag = this.FlexiDrag;
- prim.Flexible.Force = new Vector3(this.FlexiForceX, this.FlexiForceY, this.FlexiForceZ);
- prim.Flexible.Gravity = this.FlexiGravity;
- prim.Flexible.Softness = this.FlexiSoftness;
- prim.Flexible.Tension = this.FlexiTension;
- prim.Flexible.Wind = this.FlexiWind;
- }
- if (this.LightEntry)
- {
- prim.Light = new Primitive.LightData();
- prim.Light.Color = new Color4(this.LightColorR, this.LightColorG, this.LightColorB, this.LightColorA);
- prim.Light.Cutoff = this.LightCutoff;
- prim.Light.Falloff = this.LightFalloff;
- prim.Light.Intensity = this.LightIntensity;
- prim.Light.Radius = this.LightRadius;
- }
- prim.Textures = this.Textures;
- prim.Properties = new Primitive.ObjectProperties();
- prim.Properties.Name = "Object";
- prim.Properties.Description = "";
- prim.Properties.CreatorID = UUID.Zero;
- prim.Properties.GroupID = UUID.Zero;
- prim.Properties.OwnerID = UUID.Zero;
- prim.Properties.Permissions = new Permissions();
- prim.Properties.SalePrice = 10;
- prim.Properties.SaleType = new SaleType();
- return prim;
- }
- /// <summary>
- /// Encapsulates a list of media entries.
- /// </summary>
- /// This class is necessary because we want to replace auto-serialization of MediaEntry with something more
- /// OSD like and less vulnerable to change.
- public class MediaList : List<MediaEntry>, IXmlSerializable
- {
- public const string MEDIA_TEXTURE_TYPE = "sl";
- public MediaList() : base() {}
- public MediaList(IEnumerable<MediaEntry> collection) : base(collection) {}
- public MediaList(int capacity) : base(capacity) {}
- public XmlSchema GetSchema()
- {
- return null;
- }
- public string ToXml()
- {
- lock (this)
- {
- using (StringWriter sw = new StringWriter())
- {
- using (XmlTextWriter xtw = new XmlTextWriter(sw))
- {
- xtw.WriteStartElement("OSMedia");
- xtw.WriteAttributeString("type", MEDIA_TEXTURE_TYPE);
- xtw.WriteAttributeString("version", "0.1");
- OSDArray meArray = new OSDArray();
- foreach (MediaEntry me in this)
- {
- OSD osd = (null == me ? new OSD() : me.GetOSD());
- meArray.Add(osd);
- }
- xtw.WriteStartElement("OSData");
- xtw.WriteRaw(OSDParser.SerializeLLSDXmlString(meArray));
- xtw.WriteEndElement();
- xtw.WriteEndElement();
- xtw.Flush();
- return sw.ToString();
- }
- }
- }
- }
- public void WriteXml(XmlWriter writer)
- {
- writer.WriteRaw(ToXml());
- }
- public static MediaList FromXml(string rawXml)
- {
- MediaList ml = new MediaList();
- ml.ReadXml(rawXml);
- if(ml.Count == 0)
- return null;
- return ml;
- }
- public void ReadXml(string rawXml)
- {
- try
- {
- using (StringReader sr = new StringReader(rawXml))
- {
- using (XmlTextReader xtr = new XmlTextReader(sr))
- {
- xtr.MoveToContent();
- string type = xtr.GetAttribute("type");
- //m_log.DebugFormat("[MOAP]: Loaded media texture entry with type {0}", type);
- if (type != MEDIA_TEXTURE_TYPE)
- return;
- xtr.ReadStartElement("OSMedia");
- OSD osdp = OSDParser.DeserializeLLSDXml(xtr.ReadInnerXml());
- if(osdp == null || !(osdp is OSDArray))
- return;
- OSDArray osdMeArray = osdp as OSDArray;
- if(osdMeArray.Count == 0)
- return;
- foreach (OSD osdMe in osdMeArray)
- {
- MediaEntry me = (osdMe is OSDMap ? MediaEntry.FromOSD(osdMe) : new MediaEntry());
- Add(me);
- }
- }
- }
- }
- catch
- {
- m_log.Debug("PrimitiveBaseShape] error decoding MOAP xml" );
- }
- }
- public void ReadXml(XmlReader reader)
- {
- if (reader.IsEmptyElement)
- return;
- ReadXml(reader.ReadInnerXml());
- }
- }
- }
- }
|