/// <summary> /// Generate the co-ords and faces necessary to construct a mesh from the shape data the accompanies a prim. /// </summary> /// <param name="primName"></param> /// <param name="primShape"></param> /// <param name="size"></param> /// <param name="coords">Coords are added to this list by the method.</param> /// <param name="faces">Faces are added to this list by the method.</param> /// <returns>true if coords and faces were successfully generated, false if not</returns> private bool GenerateCoordsAndFacesFromPrimShapeData( string primName, PrimitiveBaseShape primShape, Vector3 size, out List<Coord> coords, out List<Face> faces) { PrimMesh primMesh; coords = new List<Coord>(); faces = new List<Face>(); float pathShearX = primShape.PathShearX < 128 ? (float)primShape.PathShearX * 0.01f : (float)(primShape.PathShearX - 256) * 0.01f; float pathShearY = primShape.PathShearY < 128 ? (float)primShape.PathShearY * 0.01f : (float)(primShape.PathShearY - 256) * 0.01f; float pathBegin = (float)primShape.PathBegin * 2.0e-5f; float pathEnd = 1.0f - (float)primShape.PathEnd * 2.0e-5f; float pathScaleX = (float)(primShape.PathScaleX - 100) * 0.01f; float pathScaleY = (float)(primShape.PathScaleY - 100) * 0.01f; float profileBegin = (float)primShape.ProfileBegin * 2.0e-5f; float profileEnd = 1.0f - (float)primShape.ProfileEnd * 2.0e-5f; float profileHollow = (float)primShape.ProfileHollow * 2.0e-5f; if (profileHollow > 0.95f) profileHollow = 0.95f; int sides = 4; if ((primShape.ProfileCurve & 0x07) == (byte)ProfileShape.EquilateralTriangle) sides = 3; else if ((primShape.ProfileCurve & 0x07) == (byte)ProfileShape.Circle) sides = 24; else if ((primShape.ProfileCurve & 0x07) == (byte)ProfileShape.HalfCircle) { // half circle, prim is a sphere sides = 24; profileBegin = 0.5f * profileBegin + 0.5f; profileEnd = 0.5f * profileEnd + 0.5f; } int hollowSides = sides; if (primShape.HollowShape == HollowShape.Circle) hollowSides = 24; else if (primShape.HollowShape == HollowShape.Square) hollowSides = 4; else if (primShape.HollowShape == HollowShape.Triangle) hollowSides = 3; primMesh = new PrimMesh(sides, profileBegin, profileEnd, profileHollow, hollowSides); if (primMesh.errorMessage != null) if (primMesh.errorMessage.Length > 0) m_log.Error("[ERROR] " + primMesh.errorMessage); primMesh.topShearX = pathShearX; primMesh.topShearY = pathShearY; primMesh.pathCutBegin = pathBegin; primMesh.pathCutEnd = pathEnd; if (primShape.PathCurve == (byte)Extrusion.Straight || primShape.PathCurve == (byte) Extrusion.Flexible) { primMesh.twistBegin = primShape.PathTwistBegin * 18 / 10; primMesh.twistEnd = primShape.PathTwist * 18 / 10; primMesh.taperX = pathScaleX; primMesh.taperY = pathScaleY; if (profileBegin < 0.0f || profileBegin >= profileEnd || profileEnd > 1.0f) { ReportPrimError("*** CORRUPT PRIM!! ***", primName, primMesh); if (profileBegin < 0.0f) profileBegin = 0.0f; if (profileEnd > 1.0f) profileEnd = 1.0f; } #if SPAM m_log.Debug("****** PrimMesh Parameters (Linear) ******\n" + primMesh.ParamsToDisplayString()); #endif try { primMesh.ExtrudeLinear(); } catch (Exception ex) { ReportPrimError("Extrusion failure: exception: " + ex.ToString(), primName, primMesh); return false; } } else { primMesh.holeSizeX = (200 - primShape.PathScaleX) * 0.01f; primMesh.holeSizeY = (200 - primShape.PathScaleY) * 0.01f; primMesh.radius = 0.01f * primShape.PathRadiusOffset; primMesh.revolutions = 1.0f + 0.015f * primShape.PathRevolutions; primMesh.skew = 0.01f * primShape.PathSkew; primMesh.twistBegin = primShape.PathTwistBegin * 36 / 10; primMesh.twistEnd = primShape.PathTwist * 36 / 10; primMesh.taperX = primShape.PathTaperX * 0.01f; primMesh.taperY = primShape.PathTaperY * 0.01f; if (profileBegin < 0.0f || profileBegin >= profileEnd || profileEnd > 1.0f) { ReportPrimError("*** CORRUPT PRIM!! ***", primName, primMesh); if (profileBegin < 0.0f) profileBegin = 0.0f; if (profileEnd > 1.0f) profileEnd = 1.0f; } #if SPAM m_log.Debug("****** PrimMesh Parameters (Circular) ******\n" + primMesh.ParamsToDisplayString()); #endif try { primMesh.ExtrudeCircular(); } catch (Exception ex) { ReportPrimError("Extrusion failure: exception: " + ex.ToString(), primName, primMesh); return false; } } primMesh.DumpRaw(baseDir, primName, "primMesh"); primMesh.Scale(size.X, size.Y, size.Z); coords = primMesh.coords; faces = primMesh.faces; return true; }
/// <summary> /// Generate the co-ords and faces necessary to construct a mesh from the shape data the accompanies a prim. /// </summary> /// <param name="primName"></param> /// <param name="primShape"></param> /// <param name="size"></param> /// <param name="coords">Coords are added to this list by the method.</param> /// <param name="faces">Faces are added to this list by the method.</param> /// <returns>true if coords and faces were successfully generated, false if not</returns> private bool GenerateCoordsAndFacesFromPrimShapeData( string primName, PrimitiveBaseShape primShape, float lod, out List<Coord> coords, out List<Face> faces) { PrimMesh primMesh; coords = new List<Coord>(); faces = new List<Face>(); float pathShearX = primShape.PathShearX < 128 ? (float)primShape.PathShearX * 0.01f : (float)(primShape.PathShearX - 256) * 0.01f; float pathShearY = primShape.PathShearY < 128 ? (float)primShape.PathShearY * 0.01f : (float)(primShape.PathShearY - 256) * 0.01f; float pathBegin = (float)primShape.PathBegin * 2.0e-5f; float pathEnd = 1.0f - (float)primShape.PathEnd * 2.0e-5f; float pathScaleX = (float)(primShape.PathScaleX - 100) * 0.01f; float pathScaleY = (float)(primShape.PathScaleY - 100) * 0.01f; float profileBegin = (float)primShape.ProfileBegin * 2.0e-5f; float profileEnd = 1.0f - (float)primShape.ProfileEnd * 2.0e-5f; if (profileBegin < 0.0f) profileBegin = 0.0f; if (profileEnd < 0.02f) profileEnd = 0.02f; else if (profileEnd > 1.0f) profileEnd = 1.0f; if (profileBegin >= profileEnd) profileBegin = profileEnd - 0.02f; float profileHollow = (float)primShape.ProfileHollow * 2.0e-5f; if (profileHollow > 0.95f) profileHollow = 0.95f; int sides = 4; LevelOfDetail iLOD = (LevelOfDetail)lod; byte profshape = (byte)(primShape.ProfileCurve & 0x07); if (profshape == (byte)ProfileShape.EquilateralTriangle || profshape == (byte)ProfileShape.IsometricTriangle || profshape == (byte)ProfileShape.RightTriangle) sides = 3; else if (profshape == (byte)ProfileShape.Circle) { switch (iLOD) { case LevelOfDetail.High: sides = 24; break; case LevelOfDetail.Medium: sides = 12; break; case LevelOfDetail.Low: sides = 6; break; case LevelOfDetail.VeryLow: sides = 3; break; default: sides = 24; break; } } else if (profshape == (byte)ProfileShape.HalfCircle) { // half circle, prim is a sphere switch (iLOD) { case LevelOfDetail.High: sides = 24; break; case LevelOfDetail.Medium: sides = 12; break; case LevelOfDetail.Low: sides = 6; break; case LevelOfDetail.VeryLow: sides = 3; break; default: sides = 24; break; } profileBegin = 0.5f * profileBegin + 0.5f; profileEnd = 0.5f * profileEnd + 0.5f; } int hollowSides = sides; if (primShape.HollowShape == HollowShape.Circle) { switch (iLOD) { case LevelOfDetail.High: hollowSides = 24; break; case LevelOfDetail.Medium: hollowSides = 12; break; case LevelOfDetail.Low: hollowSides = 6; break; case LevelOfDetail.VeryLow: hollowSides = 3; break; default: hollowSides = 24; break; } } else if (primShape.HollowShape == HollowShape.Square) hollowSides = 4; else if (primShape.HollowShape == HollowShape.Triangle) { if (profshape == (byte)ProfileShape.HalfCircle) hollowSides = 6; else hollowSides = 3; } primMesh = new PrimMesh(sides, profileBegin, profileEnd, profileHollow, hollowSides); if (primMesh.errorMessage != null) if (primMesh.errorMessage.Length > 0) m_log.Error("[ERROR] " + primMesh.errorMessage); primMesh.topShearX = pathShearX; primMesh.topShearY = pathShearY; primMesh.pathCutBegin = pathBegin; primMesh.pathCutEnd = pathEnd; if (primShape.PathCurve == (byte)Extrusion.Straight || primShape.PathCurve == (byte) Extrusion.Flexible) { primMesh.twistBegin = (primShape.PathTwistBegin * 18) / 10; primMesh.twistEnd = (primShape.PathTwist * 18) / 10; primMesh.taperX = pathScaleX; primMesh.taperY = pathScaleY; #if SPAM m_log.Debug("****** PrimMesh Parameters (Linear) ******\n" + primMesh.ParamsToDisplayString()); #endif try { primMesh.ExtrudeLinear(); } catch (Exception ex) { ReportPrimError("Extrusion failure: exception: " + ex.ToString(), primName, primMesh); return false; } } else { primMesh.holeSizeX = (200 - primShape.PathScaleX) * 0.01f; primMesh.holeSizeY = (200 - primShape.PathScaleY) * 0.01f; primMesh.radius = 0.01f * primShape.PathRadiusOffset; primMesh.revolutions = 1.0f + 0.015f * primShape.PathRevolutions; primMesh.skew = 0.01f * primShape.PathSkew; primMesh.twistBegin = (primShape.PathTwistBegin * 36) / 10; primMesh.twistEnd = (primShape.PathTwist * 36) / 10; primMesh.taperX = primShape.PathTaperX * 0.01f; primMesh.taperY = primShape.PathTaperY * 0.01f; if(profshape == (byte)ProfileShape.HalfCircle) { if(primMesh.holeSizeY < 0.01f) primMesh.holeSizeY = 0.01f; else if(primMesh.holeSizeY > 1.0f) primMesh.holeSizeY = 1.0f; } #if SPAM m_log.Debug("****** PrimMesh Parameters (Circular) ******\n" + primMesh.ParamsToDisplayString()); #endif try { primMesh.ExtrudeCircular(); } catch (Exception ex) { ReportPrimError("Extrusion failure: exception: " + ex.ToString(), primName, primMesh); return false; } } // primMesh.DumpRaw(baseDir, primName, "primMesh"); coords = primMesh.coords; faces = primMesh.faces; return true; }
private Mesh CreateMeshFromPrimMesher(string primName, PrimitiveBaseShape primShape, Vector3 size, float lod) { // m_log.DebugFormat( // "[MESH]: Creating physics proxy for {0}, shape {1}", // primName, (OpenMetaverse.SculptType)primShape.SculptType); PrimMesh primMesh; PrimMesher.SculptMesh sculptMesh; List<Coord> coords = new List<Coord>(); List<Face> faces = new List<Face>(); Image idata = null; string decodedSculptFileName = ""; if (primShape.SculptEntry) { if (((OpenMetaverse.SculptType)primShape.SculptType) == SculptType.Mesh) { if (!useMeshiesPhysicsMesh) return null; m_log.DebugFormat("[MESH]: experimental mesh proxy generation for {0}", primName); OSD meshOsd = null; if (primShape.SculptData.Length <= 0) { m_log.Error("[MESH]: asset data is zero length"); return null; } long start = 0; using (MemoryStream data = new MemoryStream(primShape.SculptData)) { try { OSD osd = OSDParser.DeserializeLLSDBinary(data); if (osd is OSDMap) meshOsd = (OSDMap)osd; else { m_log.Warn("[Mesh}: unable to cast mesh asset to OSDMap"); return null; } } catch (Exception e) { m_log.Error("[MESH]: Exception deserializing mesh asset header:" + e.ToString()); } start = data.Position; } if (meshOsd is OSDMap) { OSDMap physicsParms = null; OSDMap map = (OSDMap)meshOsd; if (map.ContainsKey("physics_shape")) physicsParms = (OSDMap)map["physics_shape"]; // old asset format else if (map.ContainsKey("physics_mesh")) physicsParms = (OSDMap)map["physics_mesh"]; // new asset format if (physicsParms == null) { m_log.Warn("[MESH]: no recognized physics mesh found in mesh asset"); return null; } int physOffset = physicsParms["offset"].AsInteger() + (int)start; int physSize = physicsParms["size"].AsInteger(); if (physOffset < 0 || physSize == 0) return null; // no mesh data in asset OSD decodedMeshOsd = new OSD(); byte[] meshBytes = new byte[physSize]; System.Buffer.BlockCopy(primShape.SculptData, physOffset, meshBytes, 0, physSize); // byte[] decompressed = new byte[physSize * 5]; try { using (MemoryStream inMs = new MemoryStream(meshBytes)) { using (MemoryStream outMs = new MemoryStream()) { using (ZOutputStream zOut = new ZOutputStream(outMs)) { byte[] readBuffer = new byte[2048]; int readLen = 0; while ((readLen = inMs.Read(readBuffer, 0, readBuffer.Length)) > 0) { zOut.Write(readBuffer, 0, readLen); } zOut.Flush(); outMs.Seek(0, SeekOrigin.Begin); byte[] decompressedBuf = outMs.GetBuffer(); decodedMeshOsd = OSDParser.DeserializeLLSDBinary(decompressedBuf); } } } } catch (Exception e) { m_log.Error("[MESH]: exception decoding physical mesh: " + e.ToString()); return null; } OSDArray decodedMeshOsdArray = null; // physics_shape is an array of OSDMaps, one for each submesh if (decodedMeshOsd is OSDArray) { // Console.WriteLine("decodedMeshOsd for {0} - {1}", primName, Util.GetFormattedXml(decodedMeshOsd)); decodedMeshOsdArray = (OSDArray)decodedMeshOsd; foreach (OSD subMeshOsd in decodedMeshOsdArray) { if (subMeshOsd is OSDMap) AddSubMesh(subMeshOsd as OSDMap, size, coords, faces); } } } } else { if (cacheSculptMaps && primShape.SculptTexture != UUID.Zero) { decodedSculptFileName = System.IO.Path.Combine(decodedSculptMapPath, "smap_" + primShape.SculptTexture.ToString()); try { if (File.Exists(decodedSculptFileName)) { idata = Image.FromFile(decodedSculptFileName); } } catch (Exception e) { m_log.Error("[SCULPT]: unable to load cached sculpt map " + decodedSculptFileName + " " + e.Message); } //if (idata != null) // m_log.Debug("[SCULPT]: loaded cached map asset for map ID: " + primShape.SculptTexture.ToString()); } if (idata == null) { if (primShape.SculptData == null || primShape.SculptData.Length == 0) return null; try { OpenMetaverse.Imaging.ManagedImage unusedData; OpenMetaverse.Imaging.OpenJPEG.DecodeToImage(primShape.SculptData, out unusedData, out idata); unusedData = null; //idata = CSJ2K.J2kImage.FromBytes(primShape.SculptData); if (cacheSculptMaps && idata != null) { try { idata.Save(decodedSculptFileName, ImageFormat.MemoryBmp); } catch (Exception e) { m_log.Error("[SCULPT]: unable to cache sculpt map " + decodedSculptFileName + " " + e.Message); } } } catch (DllNotFoundException) { m_log.Error("[PHYSICS]: OpenJpeg is not installed correctly on this system. Physics Proxy generation failed. Often times this is because of an old version of GLIBC. You must have version 2.4 or above!"); return null; } catch (IndexOutOfRangeException) { m_log.Error("[PHYSICS]: OpenJpeg was unable to decode this. Physics Proxy generation failed"); return null; } catch (Exception ex) { m_log.Error("[PHYSICS]: Unable to generate a Sculpty physics proxy. Sculpty texture decode failed: " + ex.Message); return null; } } PrimMesher.SculptMesh.SculptType sculptType; switch ((OpenMetaverse.SculptType)primShape.SculptType) { case OpenMetaverse.SculptType.Cylinder: sculptType = PrimMesher.SculptMesh.SculptType.cylinder; break; case OpenMetaverse.SculptType.Plane: sculptType = PrimMesher.SculptMesh.SculptType.plane; break; case OpenMetaverse.SculptType.Torus: sculptType = PrimMesher.SculptMesh.SculptType.torus; break; case OpenMetaverse.SculptType.Sphere: sculptType = PrimMesher.SculptMesh.SculptType.sphere; break; default: sculptType = PrimMesher.SculptMesh.SculptType.plane; break; } bool mirror = ((primShape.SculptType & 128) != 0); bool invert = ((primShape.SculptType & 64) != 0); sculptMesh = new PrimMesher.SculptMesh((Bitmap)idata, sculptType, (int)lod, false, mirror, invert); idata.Dispose(); sculptMesh.DumpRaw(baseDir, primName, "primMesh"); sculptMesh.Scale(size.X, size.Y, size.Z); coords = sculptMesh.coords; faces = sculptMesh.faces; } } else { float pathShearX = primShape.PathShearX < 128 ? (float)primShape.PathShearX * 0.01f : (float)(primShape.PathShearX - 256) * 0.01f; float pathShearY = primShape.PathShearY < 128 ? (float)primShape.PathShearY * 0.01f : (float)(primShape.PathShearY - 256) * 0.01f; float pathBegin = (float)primShape.PathBegin * 2.0e-5f; float pathEnd = 1.0f - (float)primShape.PathEnd * 2.0e-5f; float pathScaleX = (float)(primShape.PathScaleX - 100) * 0.01f; float pathScaleY = (float)(primShape.PathScaleY - 100) * 0.01f; float profileBegin = (float)primShape.ProfileBegin * 2.0e-5f; float profileEnd = 1.0f - (float)primShape.ProfileEnd * 2.0e-5f; float profileHollow = (float)primShape.ProfileHollow * 2.0e-5f; if (profileHollow > 0.95f) profileHollow = 0.95f; int sides = 4; if ((primShape.ProfileCurve & 0x07) == (byte)ProfileShape.EquilateralTriangle) sides = 3; else if ((primShape.ProfileCurve & 0x07) == (byte)ProfileShape.Circle) sides = 24; else if ((primShape.ProfileCurve & 0x07) == (byte)ProfileShape.HalfCircle) { // half circle, prim is a sphere sides = 24; profileBegin = 0.5f * profileBegin + 0.5f; profileEnd = 0.5f * profileEnd + 0.5f; } int hollowSides = sides; if (primShape.HollowShape == HollowShape.Circle) hollowSides = 24; else if (primShape.HollowShape == HollowShape.Square) hollowSides = 4; else if (primShape.HollowShape == HollowShape.Triangle) hollowSides = 3; primMesh = new PrimMesh(sides, profileBegin, profileEnd, profileHollow, hollowSides); if (primMesh.errorMessage != null) if (primMesh.errorMessage.Length > 0) m_log.Error("[ERROR] " + primMesh.errorMessage); primMesh.topShearX = pathShearX; primMesh.topShearY = pathShearY; primMesh.pathCutBegin = pathBegin; primMesh.pathCutEnd = pathEnd; if (primShape.PathCurve == (byte)Extrusion.Straight || primShape.PathCurve == (byte) Extrusion.Flexible) { primMesh.twistBegin = primShape.PathTwistBegin * 18 / 10; primMesh.twistEnd = primShape.PathTwist * 18 / 10; primMesh.taperX = pathScaleX; primMesh.taperY = pathScaleY; if (profileBegin < 0.0f || profileBegin >= profileEnd || profileEnd > 1.0f) { ReportPrimError("*** CORRUPT PRIM!! ***", primName, primMesh); if (profileBegin < 0.0f) profileBegin = 0.0f; if (profileEnd > 1.0f) profileEnd = 1.0f; } #if SPAM m_log.Debug("****** PrimMesh Parameters (Linear) ******\n" + primMesh.ParamsToDisplayString()); #endif try { primMesh.ExtrudeLinear(); } catch (Exception ex) { ReportPrimError("Extrusion failure: exception: " + ex.ToString(), primName, primMesh); return null; } } else { primMesh.holeSizeX = (200 - primShape.PathScaleX) * 0.01f; primMesh.holeSizeY = (200 - primShape.PathScaleY) * 0.01f; primMesh.radius = 0.01f * primShape.PathRadiusOffset; primMesh.revolutions = 1.0f + 0.015f * primShape.PathRevolutions; primMesh.skew = 0.01f * primShape.PathSkew; primMesh.twistBegin = primShape.PathTwistBegin * 36 / 10; primMesh.twistEnd = primShape.PathTwist * 36 / 10; primMesh.taperX = primShape.PathTaperX * 0.01f; primMesh.taperY = primShape.PathTaperY * 0.01f; if (profileBegin < 0.0f || profileBegin >= profileEnd || profileEnd > 1.0f) { ReportPrimError("*** CORRUPT PRIM!! ***", primName, primMesh); if (profileBegin < 0.0f) profileBegin = 0.0f; if (profileEnd > 1.0f) profileEnd = 1.0f; } #if SPAM m_log.Debug("****** PrimMesh Parameters (Circular) ******\n" + primMesh.ParamsToDisplayString()); #endif try { primMesh.ExtrudeCircular(); } catch (Exception ex) { ReportPrimError("Extrusion failure: exception: " + ex.ToString(), primName, primMesh); return null; } } primMesh.DumpRaw(baseDir, primName, "primMesh"); primMesh.Scale(size.X, size.Y, size.Z); coords = primMesh.coords; faces = primMesh.faces; } // Remove the reference to any JPEG2000 sculpt data so it can be GCed primShape.SculptData = Utils.EmptyBytes; int numCoords = coords.Count; int numFaces = faces.Count; // Create the list of vertices List<Vertex> vertices = new List<Vertex>(); for (int i = 0; i < numCoords; i++) { Coord c = coords[i]; vertices.Add(new Vertex(c.X, c.Y, c.Z)); } Mesh mesh = new Mesh(); // Add the corresponding triangles to the mesh for (int i = 0; i < numFaces; i++) { Face f = faces[i]; mesh.Add(new Triangle(vertices[f.v1], vertices[f.v2], vertices[f.v3])); } return mesh; }
private bool GeneratePrimTrimesh(string primName, PrimitiveBaseShape primShape, ref OpenMetaverse.Vector3 size, float lod, bool preScale, out List<Coord> coords, out List<Face> faces) { PrimMesh primMesh; float pathShearX = Primitive.UnpackPathShear((sbyte)primShape.PathShearX); float pathShearY = Primitive.UnpackPathShear((sbyte)primShape.PathShearY); float pathBegin = (float)primShape.PathBegin * 2.0e-5f; float pathEnd = 1.0f - (float)primShape.PathEnd * 2.0e-5f; float pathScaleX = (float)(primShape.PathScaleX - 100) * 0.01f; float pathScaleY = (float)(primShape.PathScaleY - 100) * 0.01f; float profileBegin = (float)primShape.ProfileBegin * 2.0e-5f; float profileEnd = 1.0f - (float)primShape.ProfileEnd * 2.0e-5f; float profileHollow = (float)primShape.ProfileHollow * 2.0e-5f; if (profileHollow > 0.95f) profileHollow = 0.95f; int sides = 4; LevelOfDetail iLOD = (LevelOfDetail)lod; if ((primShape.ProfileCurve & 0x07) == (byte)ProfileShape.EquilateralTriangle) sides = 3; else if ((primShape.ProfileCurve & 0x07) == (byte)ProfileShape.Circle) { switch (iLOD) { case LevelOfDetail.High: sides = 24; break; case LevelOfDetail.Medium: sides = 12; break; case LevelOfDetail.Low: sides = 6; break; case LevelOfDetail.VeryLow: sides = 3; break; default: sides = 24; break; } } else if ((primShape.ProfileCurve & 0x07) == (byte)ProfileShape.HalfCircle) { // half circle, prim is a sphere switch (iLOD) { case LevelOfDetail.High: sides = 24; break; case LevelOfDetail.Medium: sides = 12; break; case LevelOfDetail.Low: sides = 6; break; case LevelOfDetail.VeryLow: sides = 3; break; default: sides = 24; break; } profileBegin = 0.5f * profileBegin + 0.5f; profileEnd = 0.5f * profileEnd + 0.5f; } int hollowSides = sides; if (primShape.HollowShape == HollowShape.Circle) { switch (iLOD) { case LevelOfDetail.High: hollowSides = 24; break; case LevelOfDetail.Medium: hollowSides = 12; break; case LevelOfDetail.Low: hollowSides = 6; break; case LevelOfDetail.VeryLow: hollowSides = 3; break; default: hollowSides = 24; break; } } else if (primShape.HollowShape == HollowShape.Square) hollowSides = 4; else if (primShape.HollowShape == HollowShape.Triangle) hollowSides = 3; primMesh = new PrimMesh(sides, profileBegin, profileEnd, profileHollow, hollowSides); if (primMesh.errorMessage != null) if (primMesh.errorMessage.Length > 0) m_log.Error("[ERROR] " + primMesh.errorMessage); primMesh.topShearX = pathShearX; primMesh.topShearY = pathShearY; primMesh.pathCutBegin = pathBegin; primMesh.pathCutEnd = pathEnd; if (primShape.PathCurve == (byte)Extrusion.Straight || primShape.PathCurve == (byte)Extrusion.Flexible) { primMesh.twistBegin = primShape.PathTwistBegin * 18 / 10; primMesh.twistEnd = primShape.PathTwist * 18 / 10; primMesh.taperX = pathScaleX; primMesh.taperY = pathScaleY; if (profileBegin < 0.0f || profileBegin >= profileEnd || profileEnd > 1.0f) { ReportPrimError("*** CORRUPT PRIM!! ***", primName, primMesh); if (profileBegin < 0.0f) profileBegin = 0.0f; if (profileEnd > 1.0f) profileEnd = 1.0f; } //m_log.Debug("****** PrimMesh Parameters (Linear) ******\n" + primMesh.ParamsToDisplayString()); try { primMesh.ExtrudeLinear(); } catch (Exception ex) { ReportPrimError("Extrusion failure: exception: " + ex.ToString(), primName, primMesh); coords = null; faces = null; return false; } } else { primMesh.holeSizeX = (200 - primShape.PathScaleX) * 0.01f; primMesh.holeSizeY = (200 - primShape.PathScaleY) * 0.01f; primMesh.radius = 0.01f * primShape.PathRadiusOffset; primMesh.revolutions = 1.0f + 0.015f * primShape.PathRevolutions; primMesh.skew = 0.01f * primShape.PathSkew; primMesh.twistBegin = primShape.PathTwistBegin * 36 / 10; primMesh.twistEnd = primShape.PathTwist * 36 / 10; primMesh.taperX = primShape.PathTaperX * 0.01f; primMesh.taperY = primShape.PathTaperY * 0.01f; if (profileBegin < 0.0f || profileBegin >= profileEnd || profileEnd > 1.0f) { ReportPrimError("*** CORRUPT PRIM!! ***", primName, primMesh); if (profileBegin < 0.0f) profileBegin = 0.0f; if (profileEnd > 1.0f) profileEnd = 1.0f; } //m_log.Debug("****** PrimMesh Parameters (Circular) ******\n" + primMesh.ParamsToDisplayString()); try { primMesh.ExtrudeCircular(); } catch (Exception ex) { ReportPrimError("Extrusion failure: exception: " + ex.ToString(), primName, primMesh); coords = null; faces = null; return false; } } primMesh.DumpRaw(baseDir, primName, "primMesh"); if (preScale) primMesh.Scale(size.X, size.Y, size.Z); coords = primMesh.coords; faces = primMesh.faces; return true; }
public Mesh CreateMeshFromPrimMesher(string primName, PrimitiveBaseShape primShape, PhysicsVector size, float lod) { Mesh mesh = new Mesh(); PrimMesh primMesh; PrimMesher.SculptMesh sculptMesh; List<Coord> coords; List<Face> faces; Image idata = null; string decodedSculptFileName = ""; if (primShape.SculptEntry) { if (cacheSculptMaps && primShape.SculptTexture != UUID.Zero) { decodedSculptFileName = System.IO.Path.Combine(decodedScultMapPath, "smap_" + primShape.SculptTexture.ToString()); try { if (File.Exists(decodedSculptFileName)) { idata = Image.FromFile(decodedSculptFileName); } } catch (Exception e) { m_log.Error("[SCULPT]: unable to load cached sculpt map " + decodedSculptFileName + " " + e.Message); } //if (idata != null) // m_log.Debug("[SCULPT]: loaded cached map asset for map ID: " + primShape.SculptTexture.ToString()); } if (idata == null) { if (primShape.SculptData.Length == 0) return null; try { ManagedImage managedImage; // we never use this OpenJPEG.DecodeToImage(primShape.SculptData, out managedImage, out idata); if (cacheSculptMaps) { try { idata.Save(decodedSculptFileName, ImageFormat.MemoryBmp); } catch (Exception e) { m_log.Error("[SCULPT]: unable to cache sculpt map " + decodedSculptFileName + " " + e.Message); } } } catch (DllNotFoundException) { m_log.Error("[PHYSICS]: OpenJpeg is not installed correctly on this system. Physics Proxy generation failed. Often times this is because of an old version of GLIBC. You must have version 2.4 or above!"); return null; } catch (IndexOutOfRangeException) { m_log.Error("[PHYSICS]: OpenJpeg was unable to decode this. Physics Proxy generation failed"); return null; } catch (Exception) { m_log.Error("[PHYSICS]: Unable to generate a Sculpty physics proxy. Sculpty texture decode failed!"); return null; } } PrimMesher.SculptMesh.SculptType sculptType; switch ((OpenMetaverse.SculptType)primShape.SculptType) { case OpenMetaverse.SculptType.Cylinder: sculptType = PrimMesher.SculptMesh.SculptType.cylinder; break; case OpenMetaverse.SculptType.Plane: sculptType = PrimMesher.SculptMesh.SculptType.plane; break; case OpenMetaverse.SculptType.Torus: sculptType = PrimMesher.SculptMesh.SculptType.torus; break; case OpenMetaverse.SculptType.Sphere: sculptType = PrimMesher.SculptMesh.SculptType.sphere; break; default: sculptType = PrimMesher.SculptMesh.SculptType.plane; break; } bool mirror = ((primShape.SculptType & 128) != 0); bool invert = ((primShape.SculptType & 64) != 0); sculptMesh = new PrimMesher.SculptMesh((Bitmap)idata, sculptType, (int)lod, false, mirror, invert); idata.Dispose(); sculptMesh.DumpRaw(baseDir, primName, "primMesh"); sculptMesh.Scale(size.X, size.Y, size.Z); coords = sculptMesh.coords; faces = sculptMesh.faces; } else { float pathShearX = primShape.PathShearX < 128 ? (float)primShape.PathShearX * 0.01f : (float)(primShape.PathShearX - 256) * 0.01f; float pathShearY = primShape.PathShearY < 128 ? (float)primShape.PathShearY * 0.01f : (float)(primShape.PathShearY - 256) * 0.01f; float pathBegin = (float)primShape.PathBegin * 2.0e-5f; float pathEnd = 1.0f - (float)primShape.PathEnd * 2.0e-5f; float pathScaleX = (float)(primShape.PathScaleX - 100) * 0.01f; float pathScaleY = (float)(primShape.PathScaleY - 100) * 0.01f; float profileBegin = (float)primShape.ProfileBegin * 2.0e-5f; float profileEnd = 1.0f - (float)primShape.ProfileEnd * 2.0e-5f; float profileHollow = (float)primShape.ProfileHollow * 2.0e-5f; if (profileHollow > 0.95f) profileHollow = 0.95f; int sides = 4; if ((primShape.ProfileCurve & 0x07) == (byte)ProfileShape.EquilateralTriangle) sides = 3; else if ((primShape.ProfileCurve & 0x07) == (byte)ProfileShape.Circle) sides = 24; else if ((primShape.ProfileCurve & 0x07) == (byte)ProfileShape.HalfCircle) { // half circle, prim is a sphere sides = 24; profileBegin = 0.5f * profileBegin + 0.5f; profileEnd = 0.5f * profileEnd + 0.5f; } int hollowSides = sides; if (primShape.HollowShape == HollowShape.Circle) hollowSides = 24; else if (primShape.HollowShape == HollowShape.Square) hollowSides = 4; else if (primShape.HollowShape == HollowShape.Triangle) hollowSides = 3; primMesh = new PrimMesh(sides, profileBegin, profileEnd, profileHollow, hollowSides); if (primMesh.errorMessage != null) if (primMesh.errorMessage.Length > 0) m_log.Error("[ERROR] " + primMesh.errorMessage); primMesh.topShearX = pathShearX; primMesh.topShearY = pathShearY; primMesh.pathCutBegin = pathBegin; primMesh.pathCutEnd = pathEnd; if (primShape.PathCurve == (byte)Extrusion.Straight) { primMesh.twistBegin = primShape.PathTwistBegin * 18 / 10; primMesh.twistEnd = primShape.PathTwist * 18 / 10; primMesh.taperX = pathScaleX; primMesh.taperY = pathScaleY; if (profileBegin < 0.0f || profileBegin >= profileEnd || profileEnd > 1.0f) { ReportPrimError("*** CORRUPT PRIM!! ***", primName, primMesh); if (profileBegin < 0.0f) profileBegin = 0.0f; if (profileEnd > 1.0f) profileEnd = 1.0f; } #if SPAM m_log.Debug("****** PrimMesh Parameters (Linear) ******\n" + primMesh.ParamsToDisplayString()); #endif try { primMesh.ExtrudeLinear(); } catch (Exception ex) { ReportPrimError("Extrusion failure: exception: " + ex.ToString(), primName, primMesh); return null; } } else { primMesh.holeSizeX = (200 - primShape.PathScaleX) * 0.01f; primMesh.holeSizeY = (200 - primShape.PathScaleY) * 0.01f; primMesh.radius = 0.01f * primShape.PathRadiusOffset; primMesh.revolutions = 1.0f + 0.015f * primShape.PathRevolutions; primMesh.skew = 0.01f * primShape.PathSkew; primMesh.twistBegin = primShape.PathTwistBegin * 36 / 10; primMesh.twistEnd = primShape.PathTwist * 36 / 10; primMesh.taperX = primShape.PathTaperX * 0.01f; primMesh.taperY = primShape.PathTaperY * 0.01f; if (profileBegin < 0.0f || profileBegin >= profileEnd || profileEnd > 1.0f) { ReportPrimError("*** CORRUPT PRIM!! ***", primName, primMesh); if (profileBegin < 0.0f) profileBegin = 0.0f; if (profileEnd > 1.0f) profileEnd = 1.0f; } #if SPAM m_log.Debug("****** PrimMesh Parameters (Circular) ******\n" + primMesh.ParamsToDisplayString()); #endif try { primMesh.ExtrudeCircular(); } catch (Exception ex) { ReportPrimError("Extrusion failure: exception: " + ex.ToString(), primName, primMesh); return null; } } primMesh.DumpRaw(baseDir, primName, "primMesh"); primMesh.Scale(size.X, size.Y, size.Z); coords = primMesh.coords; faces = primMesh.faces; mesh.primMesh = primMesh; } int numCoords = coords.Count; int numFaces = faces.Count; for (int i = 0; i < numCoords; i++) { Coord c = coords[i]; mesh.vertices.Add(new Vertex(c.X, c.Y, c.Z)); } List<Vertex> vertices = mesh.vertices; for (int i = 0; i < numFaces; i++) { Face f = faces[i]; mesh.triangles.Add(new Triangle(vertices[f.v1], vertices[f.v2], vertices[f.v3])); } return mesh; }
public static Mesh PrimitiveToIrrMesh(Primitive prim, LevelOfDetail detail) { Primitive.ConstructionData primData = prim.PrimData; int sides = 4; int hollowsides = 4; float profileBegin = primData.ProfileBegin; float profileEnd = primData.ProfileEnd; bool isSphere = false; if ((ProfileCurve)(primData.profileCurve & 0x07) == ProfileCurve.Circle) { switch (detail) { case LevelOfDetail.Low: sides = 6; break; case LevelOfDetail.Medium: sides = 12; break; default: sides = 24; break; } } else if ((ProfileCurve)(primData.profileCurve & 0x07) == ProfileCurve.EqualTriangle) { sides = 3; } // half circle, prim is a sphere else if ((ProfileCurve)(primData.profileCurve & 0x07) == ProfileCurve.HalfCircle) { isSphere = true; switch (detail) { case LevelOfDetail.Low: sides = 6; break; case LevelOfDetail.Medium: sides = 12; break; default: sides = 24; break; } profileBegin = 0.5f * profileBegin + 0.5f; profileEnd = 0.5f * profileEnd + 0.5f; } if ((HoleType)primData.ProfileHole == HoleType.Same) { hollowsides = sides; } else if ((HoleType)primData.ProfileHole == HoleType.Circle) { switch (detail) { case LevelOfDetail.Low: hollowsides = 6; break; case LevelOfDetail.Medium: hollowsides = 12; break; default: hollowsides = 24; break; } } else if ((HoleType)primData.ProfileHole == HoleType.Triangle) hollowsides = 3; PrimMesh newPrim = new PrimMesh(sides, profileBegin, profileEnd, (float)primData.ProfileHollow, hollowsides); newPrim.viewerMode = true; newPrim.holeSizeX = primData.PathScaleX; newPrim.holeSizeY = primData.PathScaleY; newPrim.pathCutBegin = primData.PathBegin; newPrim.pathCutEnd = primData.PathEnd; newPrim.topShearX = primData.PathShearX; newPrim.topShearY = primData.PathShearY; newPrim.radius = primData.PathRadiusOffset; newPrim.revolutions = primData.PathRevolutions; newPrim.skew = primData.PathSkew; switch (detail) { case LevelOfDetail.Low: newPrim.stepsPerRevolution = 6; break; case LevelOfDetail.Medium: newPrim.stepsPerRevolution = 12; break; default: newPrim.stepsPerRevolution = 24; break; } if (primData.PathCurve == PathCurve.Line) { newPrim.taperX = 1.0f - primData.PathScaleX; newPrim.taperY = 1.0f - primData.PathScaleY; newPrim.twistBegin = (int)(180 * primData.PathTwistBegin); newPrim.twistEnd = (int)(180 * primData.PathTwist); newPrim.ExtrudeLinear(); } else if (primData.PathCurve == PathCurve.Flexible) { newPrim.taperX = 1.0f - primData.PathScaleX; newPrim.taperY = 1.0f - primData.PathScaleY; newPrim.twistBegin = (int)(180 * primData.PathTwistBegin); newPrim.twistEnd = (int)(180 * primData.PathTwist); newPrim.ExtrudeLinear(); } else { newPrim.taperX = primData.PathTaperX; newPrim.taperY = primData.PathTaperY; newPrim.twistBegin = (int)(360 * primData.PathTwistBegin); newPrim.twistEnd = (int)(360 * primData.PathTwist); newPrim.ExtrudeCircular(); } int numViewerFaces = newPrim.viewerFaces.Count; for (uint i = 0; i < numViewerFaces; i++) { ViewerFace vf = newPrim.viewerFaces[(int)i]; if (isSphere) { vf.uv1.U = (vf.uv1.U - 0.5f) * 2.0f; vf.uv2.U = (vf.uv2.U - 0.5f) * 2.0f; vf.uv3.U = (vf.uv3.U - 0.5f) * 2.0f; } } return FacesToIrrMesh(newPrim.viewerFaces, newPrim.numPrimFaces); }
private MeshData[] GetMeshsDataFromPrim(Primitive prim, DetailLevel lod) { PrimitiveBaseShape primShape = new PrimitiveBaseShape(prim); float profileBegin = (float)primShape.ProfileBegin * 2.0e-5f; float profileEnd = 1.0f - (float)primShape.ProfileEnd * 2.0e-5f; float profileHollow = (float)primShape.ProfileHollow * 2.0e-5f; int sides = 4; if ((prim.PrimData.profileCurve & 0x07) == (byte)ProfileCurve.EqualTriangle) sides = 3; else if ((prim.PrimData.profileCurve & 0x07) == (byte)ProfileCurve.Circle) sides = 24; else if ((prim.PrimData.profileCurve & 0x07) == (byte)ProfileCurve.HalfCircle) { // half circle, prim is a sphere sides = 24; profileBegin = 0.5f * profileBegin + 0.5f; profileEnd = 0.5f * profileEnd + 0.5f; } int hollowSides = sides; if (prim.PrimData.ProfileHole == HoleType.Circle) hollowSides = 24; else if (prim.PrimData.ProfileHole == HoleType.Square) hollowSides = 4; else if (prim.PrimData.ProfileHole == HoleType.Triangle) hollowSides = 3; PrimMesh primMesh = new PrimMesh(sides, profileBegin, profileEnd, profileHollow, hollowSides); primMesh.viewerMode = true; primMesh.calcVertexNormals = true; primMesh.topShearX = primShape.PathShearX < 128 ? (float)primShape.PathShearX * 0.01f : (float)(primShape.PathShearX - 256) * 0.01f; primMesh.topShearY = primShape.PathShearY < 128 ? (float)primShape.PathShearY * 0.01f : (float)(primShape.PathShearY - 256) * 0.01f; primMesh.pathCutBegin = (float)primShape.PathBegin * 2.0e-5f; primMesh.pathCutEnd = 1.0f - (float)primShape.PathEnd * 2.0e-5f; switch (lod) { case DetailLevel.Highest: case DetailLevel.High: primMesh.stepsPerRevolution = 24; break; case DetailLevel.Medium: primMesh.stepsPerRevolution = 12; break; case DetailLevel.Low: primMesh.stepsPerRevolution = 6; break; } float pathScaleX = (float)(primShape.PathScaleX - 100) * 0.01f; float pathScaleY = (float)(primShape.PathScaleY - 100) * 0.01f; if (prim.PrimData.PathCurve == OpenMetaverse.PathCurve.Line) { primMesh.twistBegin = primShape.PathTwistBegin * 18 / 10; primMesh.twistEnd = primShape.PathTwist * 18 / 10; primMesh.taperX = pathScaleX; primMesh.taperY = pathScaleY; try { primMesh.ExtrudeLinear(); } catch { return null; } } else { primMesh.holeSizeX = (200 - primShape.PathScaleX) * 0.01f; primMesh.holeSizeY = (200 - primShape.PathScaleY) * 0.01f; primMesh.radius = 0.01f * primShape.PathRadiusOffset; primMesh.revolutions = 1.0f + 0.015f * primShape.PathRevolutions; primMesh.skew = 0.01f * primShape.PathSkew; primMesh.twistBegin = primShape.PathTwistBegin * 36 / 10; primMesh.twistEnd = primShape.PathTwist * 36 / 10; primMesh.taperX = primShape.PathTaperX * 0.01f; primMesh.taperY = primShape.PathTaperY * 0.01f; try { primMesh.ExtrudeCircular(); } catch { return null; } } return GetMeshsDataFromPrimFaces(prim, primMesh.viewerFaces.ToArray()); }