123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387 |
- /*
- * 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 OpenSim 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.
- */
- //#define SPAM
- using System;
- using System.Collections.Generic;
- using OpenSim.Framework;
- using OpenSim.Region.Physics.Manager;
- using OpenMetaverse;
- using OpenMetaverse.Imaging;
- using System.Drawing;
- using System.Drawing.Imaging;
- using PrimMesher;
- namespace OpenSim.Region.Physics.Meshing
- {
- public class MeshmerizerPlugin : IMeshingPlugin
- {
- public MeshmerizerPlugin()
- {
- }
- public string GetName()
- {
- return "Meshmerizer";
- }
- public IMesher GetMesher()
- {
- return new Meshmerizer();
- }
- }
- public class Meshmerizer : IMesher
- {
- //private static readonly log4net.ILog m_log = log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
- // Setting baseDir to a path will enable the dumping of raw files
- // raw files can be imported by blender so a visual inspection of the results can be done
- #if SPAM
- const string baseDir = "rawFiles";
- #else
- private const string baseDir = null; //"rawFiles";
- #endif
- private float minSizeForComplexMesh = 0.2f; // prims with all dimensions smaller than this will have a bounding box mesh
- /// <summary>
- /// creates a simple box mesh of the specified size
- /// </summary>
- /// <param name="minX"></param>
- /// <param name="maxX"></param>
- /// <param name="minY"></param>
- /// <param name="maxY"></param>
- /// <param name="minZ"></param>
- /// <param name="maxZ"></param>
- /// <returns></returns>
- private static Mesh CreateSimpleBoxMesh(float minX, float maxX, float minY, float maxY, float minZ, float maxZ)
- {
- Mesh box = new Mesh();
- // bottom
- box.Add(new Vertex(minX, maxY, minZ));
- box.Add(new Vertex(maxX, maxY, minZ));
- box.Add(new Vertex(maxX, minY, minZ));
- box.Add(new Vertex(minX, minY, minZ));
- box.Add(new Triangle(box.vertices[0], box.vertices[1], box.vertices[2]));
- box.Add(new Triangle(box.vertices[0], box.vertices[2], box.vertices[3]));
- // top
- box.Add(new Vertex(maxX, maxY, maxZ));
- box.Add(new Vertex(minX, maxY, maxZ));
- box.Add(new Vertex(minX, minY, maxZ));
- box.Add(new Vertex(maxX, minY, maxZ));
- box.Add(new Triangle(box.vertices[4], box.vertices[5], box.vertices[6]));
- box.Add(new Triangle(box.vertices[4], box.vertices[6], box.vertices[7]));
- // sides
- box.Add(new Triangle(box.vertices[5], box.vertices[0], box.vertices[3]));
- box.Add(new Triangle(box.vertices[5], box.vertices[3], box.vertices[6]));
- box.Add(new Triangle(box.vertices[1], box.vertices[0], box.vertices[5]));
- box.Add(new Triangle(box.vertices[1], box.vertices[5], box.vertices[4]));
- box.Add(new Triangle(box.vertices[7], box.vertices[1], box.vertices[4]));
- box.Add(new Triangle(box.vertices[7], box.vertices[2], box.vertices[1]));
- box.Add(new Triangle(box.vertices[3], box.vertices[2], box.vertices[7]));
- box.Add(new Triangle(box.vertices[3], box.vertices[7], box.vertices[6]));
- return box;
- }
- /// <summary>
- /// Creates a simple bounding box mesh for a complex input mesh
- /// </summary>
- /// <param name="meshIn"></param>
- /// <returns></returns>
- private static Mesh CreateBoundingBoxMesh(Mesh meshIn)
- {
- float minX = float.MaxValue;
- float maxX = float.MinValue;
- float minY = float.MaxValue;
- float maxY = float.MinValue;
- float minZ = float.MaxValue;
- float maxZ = float.MinValue;
- foreach (Vertex v in meshIn.vertices)
- {
- if (v != null)
- {
- if (v.X < minX) minX = v.X;
- if (v.Y < minY) minY = v.Y;
- if (v.Z < minZ) minZ = v.Z;
- if (v.X > maxX) maxX = v.X;
- if (v.Y > maxY) maxY = v.Y;
- if (v.Z > maxZ) maxZ = v.Z;
- }
- }
- return CreateSimpleBoxMesh(minX, maxX, minY, maxY, minZ, maxZ);
- }
- private void ReportPrimError(string message, string primName, PrimMesh primMesh)
- {
- Console.WriteLine(message);
- Console.WriteLine("\nPrim Name: " + primName);
- Console.WriteLine("****** PrimMesh Parameters ******\n" + primMesh.ParamsToDisplayString());
- }
- 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;
- if (primShape.SculptEntry)
- {
- if (primShape.SculptData.Length == 0)
- return null;
- try
- {
- ManagedImage managedImage; // we never use this
- OpenJPEG.DecodeToImage(primShape.SculptData, out managedImage, out idata);
- }
- catch (Exception)
- {
- System.Console.WriteLine("[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:
- default:
- sculptType = PrimMesher.SculptMesh.SculptType.sphere;
- break;
- }
- sculptMesh = new PrimMesher.SculptMesh((Bitmap)idata, sculptType, (int)lod, false);
- 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;
- 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);
- 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
- Console.WriteLine("****** 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
- Console.WriteLine("****** 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;
- }
- 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 IMesh CreateMesh(String primName, PrimitiveBaseShape primShape, PhysicsVector size, float lod)
- {
- return CreateMesh(primName, primShape, size, lod, false);
- }
- public IMesh CreateMesh(String primName, PrimitiveBaseShape primShape, PhysicsVector size, float lod, bool isPhysical)
- {
- Mesh mesh = null;
- if (size.X < 0.01f) size.X = 0.01f;
- if (size.Y < 0.01f) size.Y = 0.01f;
- if (size.Z < 0.01f) size.Z = 0.01f;
- mesh = CreateMeshFromPrimMesher(primName, primShape, size, lod);
- if (mesh != null)
- {
- if ((!isPhysical) && size.X < minSizeForComplexMesh && size.Y < minSizeForComplexMesh && size.Z < minSizeForComplexMesh)
- {
- #if SPAM
- Console.WriteLine("Meshmerizer: prim " + primName + " has a size of " + size.ToString() + " which is below threshold of " +
- minSizeForComplexMesh.ToString() + " - creating simple bounding box" );
- #endif
- mesh = CreateBoundingBoxMesh(mesh);
- mesh.DumpRaw(baseDir, primName, "Z extruded");
- }
- // trim the vertex and triangle lists to free up memory
- mesh.vertices.TrimExcess();
- mesh.triangles.TrimExcess();
- }
- return mesh;
- }
- }
- }
|