Meshmerizer.cs 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387
  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 (Exception)
  153. {
  154. System.Console.WriteLine("[PHYSICS]: Unable to generate a Sculpty physics proxy. Sculpty texture decode failed!");
  155. return null;
  156. }
  157. PrimMesher.SculptMesh.SculptType sculptType;
  158. switch ((OpenMetaverse.SculptType)primShape.SculptType)
  159. {
  160. case OpenMetaverse.SculptType.Cylinder:
  161. sculptType = PrimMesher.SculptMesh.SculptType.cylinder;
  162. break;
  163. case OpenMetaverse.SculptType.Plane:
  164. sculptType = PrimMesher.SculptMesh.SculptType.plane;
  165. break;
  166. case OpenMetaverse.SculptType.Torus:
  167. sculptType = PrimMesher.SculptMesh.SculptType.torus;
  168. break;
  169. case OpenMetaverse.SculptType.Sphere:
  170. default:
  171. sculptType = PrimMesher.SculptMesh.SculptType.sphere;
  172. break;
  173. }
  174. sculptMesh = new PrimMesher.SculptMesh((Bitmap)idata, sculptType, (int)lod, false);
  175. idata.Dispose();
  176. sculptMesh.DumpRaw(baseDir, primName, "primMesh");
  177. sculptMesh.Scale(size.X, size.Y, size.Z);
  178. coords = sculptMesh.coords;
  179. faces = sculptMesh.faces;
  180. }
  181. else
  182. {
  183. float pathShearX = primShape.PathShearX < 128 ? (float)primShape.PathShearX * 0.01f : (float)(primShape.PathShearX - 256) * 0.01f;
  184. float pathShearY = primShape.PathShearY < 128 ? (float)primShape.PathShearY * 0.01f : (float)(primShape.PathShearY - 256) * 0.01f;
  185. float pathBegin = (float)primShape.PathBegin * 2.0e-5f;
  186. float pathEnd = 1.0f - (float)primShape.PathEnd * 2.0e-5f;
  187. float pathScaleX = (float)(primShape.PathScaleX - 100) * 0.01f;
  188. float pathScaleY = (float)(primShape.PathScaleY - 100) * 0.01f;
  189. float profileBegin = (float)primShape.ProfileBegin * 2.0e-5f;
  190. float profileEnd = 1.0f - (float)primShape.ProfileEnd * 2.0e-5f;
  191. float profileHollow = (float)primShape.ProfileHollow * 2.0e-5f;
  192. int sides = 4;
  193. if ((primShape.ProfileCurve & 0x07) == (byte)ProfileShape.EquilateralTriangle)
  194. sides = 3;
  195. else if ((primShape.ProfileCurve & 0x07) == (byte)ProfileShape.Circle)
  196. sides = 24;
  197. else if ((primShape.ProfileCurve & 0x07) == (byte)ProfileShape.HalfCircle)
  198. { // half circle, prim is a sphere
  199. sides = 24;
  200. profileBegin = 0.5f * profileBegin + 0.5f;
  201. profileEnd = 0.5f * profileEnd + 0.5f;
  202. }
  203. int hollowSides = sides;
  204. if (primShape.HollowShape == HollowShape.Circle)
  205. hollowSides = 24;
  206. else if (primShape.HollowShape == HollowShape.Square)
  207. hollowSides = 4;
  208. else if (primShape.HollowShape == HollowShape.Triangle)
  209. hollowSides = 3;
  210. primMesh = new PrimMesh(sides, profileBegin, profileEnd, profileHollow, hollowSides);
  211. primMesh.topShearX = pathShearX;
  212. primMesh.topShearY = pathShearY;
  213. primMesh.pathCutBegin = pathBegin;
  214. primMesh.pathCutEnd = pathEnd;
  215. if (primShape.PathCurve == (byte)Extrusion.Straight)
  216. {
  217. primMesh.twistBegin = primShape.PathTwistBegin * 18 / 10;
  218. primMesh.twistEnd = primShape.PathTwist * 18 / 10;
  219. primMesh.taperX = pathScaleX;
  220. primMesh.taperY = pathScaleY;
  221. if (profileBegin < 0.0f || profileBegin >= profileEnd || profileEnd > 1.0f)
  222. {
  223. ReportPrimError("*** CORRUPT PRIM!! ***", primName, primMesh);
  224. if (profileBegin < 0.0f) profileBegin = 0.0f;
  225. if (profileEnd > 1.0f) profileEnd = 1.0f;
  226. }
  227. #if SPAM
  228. Console.WriteLine("****** PrimMesh Parameters (Linear) ******\n" + primMesh.ParamsToDisplayString());
  229. #endif
  230. try
  231. {
  232. primMesh.ExtrudeLinear();
  233. }
  234. catch (Exception ex)
  235. {
  236. ReportPrimError("Extrusion failure: exception: " + ex.ToString(), primName, primMesh);
  237. return null;
  238. }
  239. }
  240. else
  241. {
  242. primMesh.holeSizeX = (200 - primShape.PathScaleX) * 0.01f;
  243. primMesh.holeSizeY = (200 - primShape.PathScaleY) * 0.01f;
  244. primMesh.radius = 0.01f * primShape.PathRadiusOffset;
  245. primMesh.revolutions = 1.0f + 0.015f * primShape.PathRevolutions;
  246. primMesh.skew = 0.01f * primShape.PathSkew;
  247. primMesh.twistBegin = primShape.PathTwistBegin * 36 / 10;
  248. primMesh.twistEnd = primShape.PathTwist * 36 / 10;
  249. primMesh.taperX = primShape.PathTaperX * 0.01f;
  250. primMesh.taperY = primShape.PathTaperY * 0.01f;
  251. if (profileBegin < 0.0f || profileBegin >= profileEnd || profileEnd > 1.0f)
  252. {
  253. ReportPrimError("*** CORRUPT PRIM!! ***", primName, primMesh);
  254. if (profileBegin < 0.0f) profileBegin = 0.0f;
  255. if (profileEnd > 1.0f) profileEnd = 1.0f;
  256. }
  257. #if SPAM
  258. Console.WriteLine("****** PrimMesh Parameters (Circular) ******\n" + primMesh.ParamsToDisplayString());
  259. #endif
  260. try
  261. {
  262. primMesh.ExtrudeCircular();
  263. }
  264. catch (Exception ex)
  265. {
  266. ReportPrimError("Extrusion failure: exception: " + ex.ToString(), primName, primMesh);
  267. return null;
  268. }
  269. }
  270. primMesh.DumpRaw(baseDir, primName, "primMesh");
  271. primMesh.Scale(size.X, size.Y, size.Z);
  272. coords = primMesh.coords;
  273. faces = primMesh.faces;
  274. }
  275. int numCoords = coords.Count;
  276. int numFaces = faces.Count;
  277. for (int i = 0; i < numCoords; i++)
  278. {
  279. Coord c = coords[i];
  280. mesh.vertices.Add(new Vertex(c.X, c.Y, c.Z));
  281. }
  282. List<Vertex> vertices = mesh.vertices;
  283. for (int i = 0; i < numFaces; i++)
  284. {
  285. Face f = faces[i];
  286. mesh.triangles.Add(new Triangle(vertices[f.v1], vertices[f.v2], vertices[f.v3]));
  287. }
  288. return mesh;
  289. }
  290. public IMesh CreateMesh(String primName, PrimitiveBaseShape primShape, PhysicsVector size, float lod)
  291. {
  292. return CreateMesh(primName, primShape, size, lod, false);
  293. }
  294. public IMesh CreateMesh(String primName, PrimitiveBaseShape primShape, PhysicsVector size, float lod, bool isPhysical)
  295. {
  296. Mesh mesh = null;
  297. if (size.X < 0.01f) size.X = 0.01f;
  298. if (size.Y < 0.01f) size.Y = 0.01f;
  299. if (size.Z < 0.01f) size.Z = 0.01f;
  300. mesh = CreateMeshFromPrimMesher(primName, primShape, size, lod);
  301. if (mesh != null)
  302. {
  303. if ((!isPhysical) && size.X < minSizeForComplexMesh && size.Y < minSizeForComplexMesh && size.Z < minSizeForComplexMesh)
  304. {
  305. #if SPAM
  306. Console.WriteLine("Meshmerizer: prim " + primName + " has a size of " + size.ToString() + " which is below threshold of " +
  307. minSizeForComplexMesh.ToString() + " - creating simple bounding box" );
  308. #endif
  309. mesh = CreateBoundingBoxMesh(mesh);
  310. mesh.DumpRaw(baseDir, primName, "Z extruded");
  311. }
  312. // trim the vertex and triangle lists to free up memory
  313. mesh.vertices.TrimExcess();
  314. mesh.triangles.TrimExcess();
  315. }
  316. return mesh;
  317. }
  318. }
  319. }