Meshmerizer.cs 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398
  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 OpenSim 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. //#define SPAM
  28. using System;
  29. using System.Collections.Generic;
  30. using OpenSim.Framework;
  31. using OpenSim.Region.Physics.Manager;
  32. using OpenMetaverse;
  33. using OpenMetaverse.Imaging;
  34. using System.Drawing;
  35. using System.Drawing.Imaging;
  36. using PrimMesher;
  37. namespace OpenSim.Region.Physics.Meshing
  38. {
  39. public class MeshmerizerPlugin : IMeshingPlugin
  40. {
  41. public MeshmerizerPlugin()
  42. {
  43. }
  44. public string GetName()
  45. {
  46. return "Meshmerizer";
  47. }
  48. public IMesher GetMesher()
  49. {
  50. return new Meshmerizer();
  51. }
  52. }
  53. public class Meshmerizer : IMesher
  54. {
  55. //private static readonly log4net.ILog m_log = log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
  56. // Setting baseDir to a path will enable the dumping of raw files
  57. // raw files can be imported by blender so a visual inspection of the results can be done
  58. #if SPAM
  59. const string baseDir = "rawFiles";
  60. #else
  61. private const string baseDir = null; //"rawFiles";
  62. #endif
  63. private float minSizeForComplexMesh = 0.2f; // prims with all dimensions smaller than this will have a bounding box mesh
  64. /// <summary>
  65. /// creates a simple box mesh of the specified size
  66. /// </summary>
  67. /// <param name="minX"></param>
  68. /// <param name="maxX"></param>
  69. /// <param name="minY"></param>
  70. /// <param name="maxY"></param>
  71. /// <param name="minZ"></param>
  72. /// <param name="maxZ"></param>
  73. /// <returns></returns>
  74. private static Mesh CreateSimpleBoxMesh(float minX, float maxX, float minY, float maxY, float minZ, float maxZ)
  75. {
  76. Mesh box = new Mesh();
  77. // bottom
  78. box.Add(new Vertex(minX, maxY, minZ));
  79. box.Add(new Vertex(maxX, maxY, minZ));
  80. box.Add(new Vertex(maxX, minY, minZ));
  81. box.Add(new Vertex(minX, minY, minZ));
  82. box.Add(new Triangle(box.vertices[0], box.vertices[1], box.vertices[2]));
  83. box.Add(new Triangle(box.vertices[0], box.vertices[2], box.vertices[3]));
  84. // top
  85. box.Add(new Vertex(maxX, maxY, maxZ));
  86. box.Add(new Vertex(minX, maxY, maxZ));
  87. box.Add(new Vertex(minX, minY, maxZ));
  88. box.Add(new Vertex(maxX, minY, maxZ));
  89. box.Add(new Triangle(box.vertices[4], box.vertices[5], box.vertices[6]));
  90. box.Add(new Triangle(box.vertices[4], box.vertices[6], box.vertices[7]));
  91. // sides
  92. box.Add(new Triangle(box.vertices[5], box.vertices[0], box.vertices[3]));
  93. box.Add(new Triangle(box.vertices[5], box.vertices[3], box.vertices[6]));
  94. box.Add(new Triangle(box.vertices[1], box.vertices[0], box.vertices[5]));
  95. box.Add(new Triangle(box.vertices[1], box.vertices[5], box.vertices[4]));
  96. box.Add(new Triangle(box.vertices[7], box.vertices[1], box.vertices[4]));
  97. box.Add(new Triangle(box.vertices[7], box.vertices[2], box.vertices[1]));
  98. box.Add(new Triangle(box.vertices[3], box.vertices[2], box.vertices[7]));
  99. box.Add(new Triangle(box.vertices[3], box.vertices[7], box.vertices[6]));
  100. return box;
  101. }
  102. /// <summary>
  103. /// Creates a simple bounding box mesh for a complex input mesh
  104. /// </summary>
  105. /// <param name="meshIn"></param>
  106. /// <returns></returns>
  107. private static Mesh CreateBoundingBoxMesh(Mesh meshIn)
  108. {
  109. float minX = float.MaxValue;
  110. float maxX = float.MinValue;
  111. float minY = float.MaxValue;
  112. float maxY = float.MinValue;
  113. float minZ = float.MaxValue;
  114. float maxZ = float.MinValue;
  115. foreach (Vertex v in meshIn.vertices)
  116. {
  117. if (v != null)
  118. {
  119. if (v.X < minX) minX = v.X;
  120. if (v.Y < minY) minY = v.Y;
  121. if (v.Z < minZ) minZ = v.Z;
  122. if (v.X > maxX) maxX = v.X;
  123. if (v.Y > maxY) maxY = v.Y;
  124. if (v.Z > maxZ) maxZ = v.Z;
  125. }
  126. }
  127. return CreateSimpleBoxMesh(minX, maxX, minY, maxY, minZ, maxZ);
  128. }
  129. private void ReportPrimError(string message, string primName, PrimMesh primMesh)
  130. {
  131. Console.WriteLine(message);
  132. Console.WriteLine("\nPrim Name: " + primName);
  133. Console.WriteLine("****** PrimMesh Parameters ******\n" + primMesh.ParamsToDisplayString());
  134. }
  135. public Mesh CreateMeshFromPrimMesher(string primName, PrimitiveBaseShape primShape, PhysicsVector size, float lod)
  136. {
  137. Mesh mesh = new Mesh();
  138. PrimMesh primMesh;
  139. PrimMesher.SculptMesh sculptMesh;
  140. List<Coord> coords;
  141. List<Face> faces;
  142. Image idata = null;
  143. if (primShape.SculptEntry)
  144. {
  145. if (primShape.SculptData.Length == 0)
  146. return null;
  147. try
  148. {
  149. ManagedImage managedImage; // we never use this
  150. OpenJPEG.DecodeToImage(primShape.SculptData, out managedImage, out idata);
  151. }
  152. catch (DllNotFoundException)
  153. {
  154. System.Console.WriteLine("[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!");
  155. return null;
  156. }
  157. catch (IndexOutOfRangeException)
  158. {
  159. System.Console.WriteLine("[PHYSICS]: OpenJpeg was unable to decode this. Physics Proxy generation failed");
  160. return null;
  161. }
  162. catch (Exception)
  163. {
  164. System.Console.WriteLine("[PHYSICS]: Unable to generate a Sculpty physics proxy. Sculpty texture decode failed!");
  165. return null;
  166. }
  167. PrimMesher.SculptMesh.SculptType sculptType;
  168. switch ((OpenMetaverse.SculptType)primShape.SculptType)
  169. {
  170. case OpenMetaverse.SculptType.Cylinder:
  171. sculptType = PrimMesher.SculptMesh.SculptType.cylinder;
  172. break;
  173. case OpenMetaverse.SculptType.Plane:
  174. sculptType = PrimMesher.SculptMesh.SculptType.plane;
  175. break;
  176. case OpenMetaverse.SculptType.Torus:
  177. sculptType = PrimMesher.SculptMesh.SculptType.torus;
  178. break;
  179. case OpenMetaverse.SculptType.Sphere:
  180. default:
  181. sculptType = PrimMesher.SculptMesh.SculptType.sphere;
  182. break;
  183. }
  184. sculptMesh = new PrimMesher.SculptMesh((Bitmap)idata, sculptType, (int)lod, false);
  185. idata.Dispose();
  186. sculptMesh.DumpRaw(baseDir, primName, "primMesh");
  187. sculptMesh.Scale(size.X, size.Y, size.Z);
  188. coords = sculptMesh.coords;
  189. faces = sculptMesh.faces;
  190. }
  191. else
  192. {
  193. float pathShearX = primShape.PathShearX < 128 ? (float)primShape.PathShearX * 0.01f : (float)(primShape.PathShearX - 256) * 0.01f;
  194. float pathShearY = primShape.PathShearY < 128 ? (float)primShape.PathShearY * 0.01f : (float)(primShape.PathShearY - 256) * 0.01f;
  195. float pathBegin = (float)primShape.PathBegin * 2.0e-5f;
  196. float pathEnd = 1.0f - (float)primShape.PathEnd * 2.0e-5f;
  197. float pathScaleX = (float)(primShape.PathScaleX - 100) * 0.01f;
  198. float pathScaleY = (float)(primShape.PathScaleY - 100) * 0.01f;
  199. float profileBegin = (float)primShape.ProfileBegin * 2.0e-5f;
  200. float profileEnd = 1.0f - (float)primShape.ProfileEnd * 2.0e-5f;
  201. float profileHollow = (float)primShape.ProfileHollow * 2.0e-5f;
  202. int sides = 4;
  203. if ((primShape.ProfileCurve & 0x07) == (byte)ProfileShape.EquilateralTriangle)
  204. sides = 3;
  205. else if ((primShape.ProfileCurve & 0x07) == (byte)ProfileShape.Circle)
  206. sides = 24;
  207. else if ((primShape.ProfileCurve & 0x07) == (byte)ProfileShape.HalfCircle)
  208. { // half circle, prim is a sphere
  209. sides = 24;
  210. profileBegin = 0.5f * profileBegin + 0.5f;
  211. profileEnd = 0.5f * profileEnd + 0.5f;
  212. }
  213. int hollowSides = sides;
  214. if (primShape.HollowShape == HollowShape.Circle)
  215. hollowSides = 24;
  216. else if (primShape.HollowShape == HollowShape.Square)
  217. hollowSides = 4;
  218. else if (primShape.HollowShape == HollowShape.Triangle)
  219. hollowSides = 3;
  220. primMesh = new PrimMesh(sides, profileBegin, profileEnd, profileHollow, hollowSides);
  221. primMesh.topShearX = pathShearX;
  222. primMesh.topShearY = pathShearY;
  223. primMesh.pathCutBegin = pathBegin;
  224. primMesh.pathCutEnd = pathEnd;
  225. if (primShape.PathCurve == (byte)Extrusion.Straight)
  226. {
  227. primMesh.twistBegin = primShape.PathTwistBegin * 18 / 10;
  228. primMesh.twistEnd = primShape.PathTwist * 18 / 10;
  229. primMesh.taperX = pathScaleX;
  230. primMesh.taperY = pathScaleY;
  231. if (profileBegin < 0.0f || profileBegin >= profileEnd || profileEnd > 1.0f)
  232. {
  233. ReportPrimError("*** CORRUPT PRIM!! ***", primName, primMesh);
  234. if (profileBegin < 0.0f) profileBegin = 0.0f;
  235. if (profileEnd > 1.0f) profileEnd = 1.0f;
  236. }
  237. #if SPAM
  238. Console.WriteLine("****** PrimMesh Parameters (Linear) ******\n" + primMesh.ParamsToDisplayString());
  239. #endif
  240. try
  241. {
  242. primMesh.ExtrudeLinear();
  243. }
  244. catch (Exception ex)
  245. {
  246. ReportPrimError("Extrusion failure: exception: " + ex.ToString(), primName, primMesh);
  247. return null;
  248. }
  249. }
  250. else
  251. {
  252. primMesh.holeSizeX = (200 - primShape.PathScaleX) * 0.01f;
  253. primMesh.holeSizeY = (200 - primShape.PathScaleY) * 0.01f;
  254. primMesh.radius = 0.01f * primShape.PathRadiusOffset;
  255. primMesh.revolutions = 1.0f + 0.015f * primShape.PathRevolutions;
  256. primMesh.skew = 0.01f * primShape.PathSkew;
  257. primMesh.twistBegin = primShape.PathTwistBegin * 36 / 10;
  258. primMesh.twistEnd = primShape.PathTwist * 36 / 10;
  259. primMesh.taperX = primShape.PathTaperX * 0.01f;
  260. primMesh.taperY = primShape.PathTaperY * 0.01f;
  261. if (profileBegin < 0.0f || profileBegin >= profileEnd || profileEnd > 1.0f)
  262. {
  263. ReportPrimError("*** CORRUPT PRIM!! ***", primName, primMesh);
  264. if (profileBegin < 0.0f) profileBegin = 0.0f;
  265. if (profileEnd > 1.0f) profileEnd = 1.0f;
  266. }
  267. #if SPAM
  268. Console.WriteLine("****** PrimMesh Parameters (Circular) ******\n" + primMesh.ParamsToDisplayString());
  269. #endif
  270. try
  271. {
  272. primMesh.ExtrudeCircular();
  273. }
  274. catch (Exception ex)
  275. {
  276. ReportPrimError("Extrusion failure: exception: " + ex.ToString(), primName, primMesh);
  277. return null;
  278. }
  279. }
  280. primMesh.DumpRaw(baseDir, primName, "primMesh");
  281. primMesh.Scale(size.X, size.Y, size.Z);
  282. coords = primMesh.coords;
  283. faces = primMesh.faces;
  284. }
  285. int numCoords = coords.Count;
  286. int numFaces = faces.Count;
  287. for (int i = 0; i < numCoords; i++)
  288. {
  289. Coord c = coords[i];
  290. mesh.vertices.Add(new Vertex(c.X, c.Y, c.Z));
  291. }
  292. List<Vertex> vertices = mesh.vertices;
  293. for (int i = 0; i < numFaces; i++)
  294. {
  295. Face f = faces[i];
  296. mesh.triangles.Add(new Triangle(vertices[f.v1], vertices[f.v2], vertices[f.v3]));
  297. }
  298. return mesh;
  299. }
  300. public IMesh CreateMesh(String primName, PrimitiveBaseShape primShape, PhysicsVector size, float lod)
  301. {
  302. return CreateMesh(primName, primShape, size, lod, false);
  303. }
  304. public IMesh CreateMesh(String primName, PrimitiveBaseShape primShape, PhysicsVector size, float lod, bool isPhysical)
  305. {
  306. Mesh mesh = null;
  307. if (size.X < 0.01f) size.X = 0.01f;
  308. if (size.Y < 0.01f) size.Y = 0.01f;
  309. if (size.Z < 0.01f) size.Z = 0.01f;
  310. mesh = CreateMeshFromPrimMesher(primName, primShape, size, lod);
  311. if (mesh != null)
  312. {
  313. if ((!isPhysical) && size.X < minSizeForComplexMesh && size.Y < minSizeForComplexMesh && size.Z < minSizeForComplexMesh)
  314. {
  315. #if SPAM
  316. Console.WriteLine("Meshmerizer: prim " + primName + " has a size of " + size.ToString() + " which is below threshold of " +
  317. minSizeForComplexMesh.ToString() + " - creating simple bounding box" );
  318. #endif
  319. mesh = CreateBoundingBoxMesh(mesh);
  320. mesh.DumpRaw(baseDir, primName, "Z extruded");
  321. }
  322. // trim the vertex and triangle lists to free up memory
  323. mesh.vertices.TrimExcess();
  324. mesh.triangles.TrimExcess();
  325. }
  326. return mesh;
  327. }
  328. }
  329. }