Meshmerizer.cs 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670
  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 OpenSimulator 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.StructuredData;
  34. using System.Drawing;
  35. using System.Drawing.Imaging;
  36. using System.IO.Compression;
  37. using PrimMesher;
  38. using log4net;
  39. using Nini.Config;
  40. using System.Reflection;
  41. using System.IO;
  42. using ComponentAce.Compression.Libs.zlib;
  43. namespace OpenSim.Region.Physics.Meshing
  44. {
  45. public class MeshmerizerPlugin : IMeshingPlugin
  46. {
  47. public MeshmerizerPlugin()
  48. {
  49. }
  50. public string GetName()
  51. {
  52. return "Meshmerizer";
  53. }
  54. public IMesher GetMesher(IConfigSource config)
  55. {
  56. return new Meshmerizer(config);
  57. }
  58. }
  59. public class Meshmerizer : IMesher
  60. {
  61. private static readonly ILog m_log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
  62. // Setting baseDir to a path will enable the dumping of raw files
  63. // raw files can be imported by blender so a visual inspection of the results can be done
  64. #if SPAM
  65. const string baseDir = "rawFiles";
  66. #else
  67. private const string baseDir = null; //"rawFiles";
  68. #endif
  69. private bool cacheSculptMaps = true;
  70. private string decodedSculptMapPath = null;
  71. private bool useMeshiesPhysicsMesh = false;
  72. private float minSizeForComplexMesh = 0.2f; // prims with all dimensions smaller than this will have a bounding box mesh
  73. private Dictionary<ulong, Mesh> m_uniqueMeshes = new Dictionary<ulong, Mesh>();
  74. public Meshmerizer(IConfigSource config)
  75. {
  76. IConfig start_config = config.Configs["Startup"];
  77. IConfig mesh_config = config.Configs["Mesh"];
  78. decodedSculptMapPath = start_config.GetString("DecodedSculptMapPath","j2kDecodeCache");
  79. cacheSculptMaps = start_config.GetBoolean("CacheSculptMaps", cacheSculptMaps);
  80. if(mesh_config != null)
  81. useMeshiesPhysicsMesh = mesh_config.GetBoolean("UseMeshiesPhysicsMesh", useMeshiesPhysicsMesh);
  82. try
  83. {
  84. if (!Directory.Exists(decodedSculptMapPath))
  85. Directory.CreateDirectory(decodedSculptMapPath);
  86. }
  87. catch (Exception e)
  88. {
  89. m_log.WarnFormat("[SCULPT]: Unable to create {0} directory: ", decodedSculptMapPath, e.Message);
  90. }
  91. }
  92. /// <summary>
  93. /// creates a simple box mesh of the specified size. This mesh is of very low vertex count and may
  94. /// be useful as a backup proxy when level of detail is not needed or when more complex meshes fail
  95. /// for some reason
  96. /// </summary>
  97. /// <param name="minX"></param>
  98. /// <param name="maxX"></param>
  99. /// <param name="minY"></param>
  100. /// <param name="maxY"></param>
  101. /// <param name="minZ"></param>
  102. /// <param name="maxZ"></param>
  103. /// <returns></returns>
  104. private static Mesh CreateSimpleBoxMesh(float minX, float maxX, float minY, float maxY, float minZ, float maxZ)
  105. {
  106. Mesh box = new Mesh();
  107. List<Vertex> vertices = new List<Vertex>();
  108. // bottom
  109. vertices.Add(new Vertex(minX, maxY, minZ));
  110. vertices.Add(new Vertex(maxX, maxY, minZ));
  111. vertices.Add(new Vertex(maxX, minY, minZ));
  112. vertices.Add(new Vertex(minX, minY, minZ));
  113. box.Add(new Triangle(vertices[0], vertices[1], vertices[2]));
  114. box.Add(new Triangle(vertices[0], vertices[2], vertices[3]));
  115. // top
  116. vertices.Add(new Vertex(maxX, maxY, maxZ));
  117. vertices.Add(new Vertex(minX, maxY, maxZ));
  118. vertices.Add(new Vertex(minX, minY, maxZ));
  119. vertices.Add(new Vertex(maxX, minY, maxZ));
  120. box.Add(new Triangle(vertices[4], vertices[5], vertices[6]));
  121. box.Add(new Triangle(vertices[4], vertices[6], vertices[7]));
  122. // sides
  123. box.Add(new Triangle(vertices[5], vertices[0], vertices[3]));
  124. box.Add(new Triangle(vertices[5], vertices[3], vertices[6]));
  125. box.Add(new Triangle(vertices[1], vertices[0], vertices[5]));
  126. box.Add(new Triangle(vertices[1], vertices[5], vertices[4]));
  127. box.Add(new Triangle(vertices[7], vertices[1], vertices[4]));
  128. box.Add(new Triangle(vertices[7], vertices[2], vertices[1]));
  129. box.Add(new Triangle(vertices[3], vertices[2], vertices[7]));
  130. box.Add(new Triangle(vertices[3], vertices[7], vertices[6]));
  131. return box;
  132. }
  133. /// <summary>
  134. /// Creates a simple bounding box mesh for a complex input mesh
  135. /// </summary>
  136. /// <param name="meshIn"></param>
  137. /// <returns></returns>
  138. private static Mesh CreateBoundingBoxMesh(Mesh meshIn)
  139. {
  140. float minX = float.MaxValue;
  141. float maxX = float.MinValue;
  142. float minY = float.MaxValue;
  143. float maxY = float.MinValue;
  144. float minZ = float.MaxValue;
  145. float maxZ = float.MinValue;
  146. foreach (Vector3 v in meshIn.getVertexList())
  147. {
  148. if (v != null)
  149. {
  150. if (v.X < minX) minX = v.X;
  151. if (v.Y < minY) minY = v.Y;
  152. if (v.Z < minZ) minZ = v.Z;
  153. if (v.X > maxX) maxX = v.X;
  154. if (v.Y > maxY) maxY = v.Y;
  155. if (v.Z > maxZ) maxZ = v.Z;
  156. }
  157. }
  158. return CreateSimpleBoxMesh(minX, maxX, minY, maxY, minZ, maxZ);
  159. }
  160. private void ReportPrimError(string message, string primName, PrimMesh primMesh)
  161. {
  162. m_log.Error(message);
  163. m_log.Error("\nPrim Name: " + primName);
  164. m_log.Error("****** PrimMesh Parameters ******\n" + primMesh.ParamsToDisplayString());
  165. }
  166. private ulong GetMeshKey(PrimitiveBaseShape pbs, Vector3 size, float lod)
  167. {
  168. ulong hash = 5381;
  169. hash = djb2(hash, pbs.PathCurve);
  170. hash = djb2(hash, (byte)((byte)pbs.HollowShape | (byte)pbs.ProfileShape));
  171. hash = djb2(hash, pbs.PathBegin);
  172. hash = djb2(hash, pbs.PathEnd);
  173. hash = djb2(hash, pbs.PathScaleX);
  174. hash = djb2(hash, pbs.PathScaleY);
  175. hash = djb2(hash, pbs.PathShearX);
  176. hash = djb2(hash, pbs.PathShearY);
  177. hash = djb2(hash, (byte)pbs.PathTwist);
  178. hash = djb2(hash, (byte)pbs.PathTwistBegin);
  179. hash = djb2(hash, (byte)pbs.PathRadiusOffset);
  180. hash = djb2(hash, (byte)pbs.PathTaperX);
  181. hash = djb2(hash, (byte)pbs.PathTaperY);
  182. hash = djb2(hash, pbs.PathRevolutions);
  183. hash = djb2(hash, (byte)pbs.PathSkew);
  184. hash = djb2(hash, pbs.ProfileBegin);
  185. hash = djb2(hash, pbs.ProfileEnd);
  186. hash = djb2(hash, pbs.ProfileHollow);
  187. // TODO: Separate scale out from the primitive shape data (after
  188. // scaling is supported at the physics engine level)
  189. byte[] scaleBytes = size.GetBytes();
  190. for (int i = 0; i < scaleBytes.Length; i++)
  191. hash = djb2(hash, scaleBytes[i]);
  192. // Include LOD in hash, accounting for endianness
  193. byte[] lodBytes = new byte[4];
  194. Buffer.BlockCopy(BitConverter.GetBytes(lod), 0, lodBytes, 0, 4);
  195. if (!BitConverter.IsLittleEndian)
  196. {
  197. Array.Reverse(lodBytes, 0, 4);
  198. }
  199. for (int i = 0; i < lodBytes.Length; i++)
  200. hash = djb2(hash, lodBytes[i]);
  201. // include sculpt UUID
  202. if (pbs.SculptEntry)
  203. {
  204. scaleBytes = pbs.SculptTexture.GetBytes();
  205. for (int i = 0; i < scaleBytes.Length; i++)
  206. hash = djb2(hash, scaleBytes[i]);
  207. }
  208. return hash;
  209. }
  210. private ulong djb2(ulong hash, byte c)
  211. {
  212. return ((hash << 5) + hash) + (ulong)c;
  213. }
  214. private ulong djb2(ulong hash, ushort c)
  215. {
  216. hash = ((hash << 5) + hash) + (ulong)((byte)c);
  217. return ((hash << 5) + hash) + (ulong)(c >> 8);
  218. }
  219. private Mesh CreateMeshFromPrimMesher(string primName, PrimitiveBaseShape primShape, Vector3 size, float lod)
  220. {
  221. PrimMesh primMesh;
  222. PrimMesher.SculptMesh sculptMesh;
  223. List<Coord> coords = new List<Coord>();
  224. List<Face> faces = new List<Face>();
  225. Image idata = null;
  226. string decodedSculptFileName = "";
  227. if (primShape.SculptEntry)
  228. {
  229. if (((OpenMetaverse.SculptType)primShape.SculptType) == SculptType.Mesh)
  230. {
  231. if (!useMeshiesPhysicsMesh)
  232. return null;
  233. m_log.Debug("[MESH]: experimental mesh proxy generation");
  234. OSD meshOsd = null;
  235. if (primShape.SculptData.Length <= 0)
  236. {
  237. m_log.Error("[MESH]: asset data is zero length");
  238. return null;
  239. }
  240. long start = 0;
  241. using (MemoryStream data = new MemoryStream(primShape.SculptData))
  242. {
  243. try
  244. {
  245. meshOsd = (OSDMap)OSDParser.DeserializeLLSDBinary(data);
  246. }
  247. catch (Exception e)
  248. {
  249. m_log.Error("[MESH]: Exception deserializing mesh asset header:" + e.ToString());
  250. }
  251. start = data.Position;
  252. }
  253. if (meshOsd is OSDMap)
  254. {
  255. OSDMap map = (OSDMap)meshOsd;
  256. OSDMap physicsParms = (OSDMap)map["physics_shape"]; // old asset format
  257. if (physicsParms.Count == 0)
  258. physicsParms = (OSDMap)map["physics_mesh"]; // new asset format
  259. int physOffset = physicsParms["offset"].AsInteger() + (int)start;
  260. int physSize = physicsParms["size"].AsInteger();
  261. if (physOffset < 0 || physSize == 0)
  262. return null; // no mesh data in asset
  263. OSD decodedMeshOsd = new OSD();
  264. byte[] meshBytes = new byte[physSize];
  265. System.Buffer.BlockCopy(primShape.SculptData, physOffset, meshBytes, 0, physSize);
  266. // byte[] decompressed = new byte[physSize * 5];
  267. try
  268. {
  269. using (MemoryStream inMs = new MemoryStream(meshBytes))
  270. {
  271. using (MemoryStream outMs = new MemoryStream())
  272. {
  273. using (ZOutputStream zOut = new ZOutputStream(outMs))
  274. {
  275. byte[] readBuffer = new byte[2048];
  276. int readLen = 0;
  277. while ((readLen = inMs.Read(readBuffer, 0, readBuffer.Length)) > 0)
  278. {
  279. zOut.Write(readBuffer, 0, readLen);
  280. }
  281. zOut.Flush();
  282. outMs.Seek(0, SeekOrigin.Begin);
  283. byte[] decompressedBuf = outMs.GetBuffer();
  284. decodedMeshOsd = OSDParser.DeserializeLLSDBinary(decompressedBuf);
  285. }
  286. }
  287. }
  288. }
  289. catch (Exception e)
  290. {
  291. m_log.Error("[MESH]: exception decoding physical mesh: " + e.ToString());
  292. return null;
  293. }
  294. OSDArray decodedMeshOsdArray = null;
  295. // physics_shape is an array of OSDMaps, one for each submesh
  296. if (decodedMeshOsd is OSDArray)
  297. {
  298. decodedMeshOsdArray = (OSDArray)decodedMeshOsd;
  299. foreach (OSD subMeshOsd in decodedMeshOsdArray)
  300. {
  301. if (subMeshOsd is OSDMap)
  302. {
  303. OSDMap subMeshMap = (OSDMap)subMeshOsd;
  304. OpenMetaverse.Vector3 posMax = ((OSDMap)subMeshMap["PositionDomain"])["Max"].AsVector3();
  305. OpenMetaverse.Vector3 posMin = ((OSDMap)subMeshMap["PositionDomain"])["Min"].AsVector3();
  306. ushort faceIndexOffset = (ushort)coords.Count;
  307. byte[] posBytes = subMeshMap["Position"].AsBinary();
  308. for (int i = 0; i < posBytes.Length; i += 6)
  309. {
  310. ushort uX = Utils.BytesToUInt16(posBytes, i);
  311. ushort uY = Utils.BytesToUInt16(posBytes, i + 2);
  312. ushort uZ = Utils.BytesToUInt16(posBytes, i + 4);
  313. Coord c = new Coord(
  314. Utils.UInt16ToFloat(uX, posMin.X, posMax.X) * size.X,
  315. Utils.UInt16ToFloat(uY, posMin.Y, posMax.Y) * size.Y,
  316. Utils.UInt16ToFloat(uZ, posMin.Z, posMax.Z) * size.Z);
  317. coords.Add(c);
  318. }
  319. byte[] triangleBytes = subMeshMap["TriangleList"].AsBinary();
  320. for (int i = 0; i < triangleBytes.Length; i += 6)
  321. {
  322. ushort v1 = (ushort)(Utils.BytesToUInt16(triangleBytes, i) + faceIndexOffset);
  323. ushort v2 = (ushort)(Utils.BytesToUInt16(triangleBytes, i + 2) + faceIndexOffset);
  324. ushort v3 = (ushort)(Utils.BytesToUInt16(triangleBytes, i + 4) + faceIndexOffset);
  325. Face f = new Face(v1, v2, v3);
  326. faces.Add(f);
  327. }
  328. }
  329. }
  330. }
  331. }
  332. }
  333. else
  334. {
  335. if (cacheSculptMaps && primShape.SculptTexture != UUID.Zero)
  336. {
  337. decodedSculptFileName = System.IO.Path.Combine(decodedSculptMapPath, "smap_" + primShape.SculptTexture.ToString());
  338. try
  339. {
  340. if (File.Exists(decodedSculptFileName))
  341. {
  342. idata = Image.FromFile(decodedSculptFileName);
  343. }
  344. }
  345. catch (Exception e)
  346. {
  347. m_log.Error("[SCULPT]: unable to load cached sculpt map " + decodedSculptFileName + " " + e.Message);
  348. }
  349. //if (idata != null)
  350. // m_log.Debug("[SCULPT]: loaded cached map asset for map ID: " + primShape.SculptTexture.ToString());
  351. }
  352. if (idata == null)
  353. {
  354. if (primShape.SculptData == null || primShape.SculptData.Length == 0)
  355. return null;
  356. try
  357. {
  358. OpenMetaverse.Imaging.ManagedImage unusedData;
  359. OpenMetaverse.Imaging.OpenJPEG.DecodeToImage(primShape.SculptData, out unusedData, out idata);
  360. unusedData = null;
  361. //idata = CSJ2K.J2kImage.FromBytes(primShape.SculptData);
  362. if (cacheSculptMaps && idata != null)
  363. {
  364. try { idata.Save(decodedSculptFileName, ImageFormat.MemoryBmp); }
  365. catch (Exception e) { m_log.Error("[SCULPT]: unable to cache sculpt map " + decodedSculptFileName + " " + e.Message); }
  366. }
  367. }
  368. catch (DllNotFoundException)
  369. {
  370. 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!");
  371. return null;
  372. }
  373. catch (IndexOutOfRangeException)
  374. {
  375. m_log.Error("[PHYSICS]: OpenJpeg was unable to decode this. Physics Proxy generation failed");
  376. return null;
  377. }
  378. catch (Exception ex)
  379. {
  380. m_log.Error("[PHYSICS]: Unable to generate a Sculpty physics proxy. Sculpty texture decode failed: " + ex.Message);
  381. return null;
  382. }
  383. }
  384. PrimMesher.SculptMesh.SculptType sculptType;
  385. switch ((OpenMetaverse.SculptType)primShape.SculptType)
  386. {
  387. case OpenMetaverse.SculptType.Cylinder:
  388. sculptType = PrimMesher.SculptMesh.SculptType.cylinder;
  389. break;
  390. case OpenMetaverse.SculptType.Plane:
  391. sculptType = PrimMesher.SculptMesh.SculptType.plane;
  392. break;
  393. case OpenMetaverse.SculptType.Torus:
  394. sculptType = PrimMesher.SculptMesh.SculptType.torus;
  395. break;
  396. case OpenMetaverse.SculptType.Sphere:
  397. sculptType = PrimMesher.SculptMesh.SculptType.sphere;
  398. break;
  399. default:
  400. sculptType = PrimMesher.SculptMesh.SculptType.plane;
  401. break;
  402. }
  403. bool mirror = ((primShape.SculptType & 128) != 0);
  404. bool invert = ((primShape.SculptType & 64) != 0);
  405. sculptMesh = new PrimMesher.SculptMesh((Bitmap)idata, sculptType, (int)lod, false, mirror, invert);
  406. idata.Dispose();
  407. sculptMesh.DumpRaw(baseDir, primName, "primMesh");
  408. sculptMesh.Scale(size.X, size.Y, size.Z);
  409. coords = sculptMesh.coords;
  410. faces = sculptMesh.faces;
  411. }
  412. }
  413. else
  414. {
  415. float pathShearX = primShape.PathShearX < 128 ? (float)primShape.PathShearX * 0.01f : (float)(primShape.PathShearX - 256) * 0.01f;
  416. float pathShearY = primShape.PathShearY < 128 ? (float)primShape.PathShearY * 0.01f : (float)(primShape.PathShearY - 256) * 0.01f;
  417. float pathBegin = (float)primShape.PathBegin * 2.0e-5f;
  418. float pathEnd = 1.0f - (float)primShape.PathEnd * 2.0e-5f;
  419. float pathScaleX = (float)(primShape.PathScaleX - 100) * 0.01f;
  420. float pathScaleY = (float)(primShape.PathScaleY - 100) * 0.01f;
  421. float profileBegin = (float)primShape.ProfileBegin * 2.0e-5f;
  422. float profileEnd = 1.0f - (float)primShape.ProfileEnd * 2.0e-5f;
  423. float profileHollow = (float)primShape.ProfileHollow * 2.0e-5f;
  424. if (profileHollow > 0.95f)
  425. profileHollow = 0.95f;
  426. int sides = 4;
  427. if ((primShape.ProfileCurve & 0x07) == (byte)ProfileShape.EquilateralTriangle)
  428. sides = 3;
  429. else if ((primShape.ProfileCurve & 0x07) == (byte)ProfileShape.Circle)
  430. sides = 24;
  431. else if ((primShape.ProfileCurve & 0x07) == (byte)ProfileShape.HalfCircle)
  432. { // half circle, prim is a sphere
  433. sides = 24;
  434. profileBegin = 0.5f * profileBegin + 0.5f;
  435. profileEnd = 0.5f * profileEnd + 0.5f;
  436. }
  437. int hollowSides = sides;
  438. if (primShape.HollowShape == HollowShape.Circle)
  439. hollowSides = 24;
  440. else if (primShape.HollowShape == HollowShape.Square)
  441. hollowSides = 4;
  442. else if (primShape.HollowShape == HollowShape.Triangle)
  443. hollowSides = 3;
  444. primMesh = new PrimMesh(sides, profileBegin, profileEnd, profileHollow, hollowSides);
  445. if (primMesh.errorMessage != null)
  446. if (primMesh.errorMessage.Length > 0)
  447. m_log.Error("[ERROR] " + primMesh.errorMessage);
  448. primMesh.topShearX = pathShearX;
  449. primMesh.topShearY = pathShearY;
  450. primMesh.pathCutBegin = pathBegin;
  451. primMesh.pathCutEnd = pathEnd;
  452. if (primShape.PathCurve == (byte)Extrusion.Straight || primShape.PathCurve == (byte) Extrusion.Flexible)
  453. {
  454. primMesh.twistBegin = primShape.PathTwistBegin * 18 / 10;
  455. primMesh.twistEnd = primShape.PathTwist * 18 / 10;
  456. primMesh.taperX = pathScaleX;
  457. primMesh.taperY = pathScaleY;
  458. if (profileBegin < 0.0f || profileBegin >= profileEnd || profileEnd > 1.0f)
  459. {
  460. ReportPrimError("*** CORRUPT PRIM!! ***", primName, primMesh);
  461. if (profileBegin < 0.0f) profileBegin = 0.0f;
  462. if (profileEnd > 1.0f) profileEnd = 1.0f;
  463. }
  464. #if SPAM
  465. m_log.Debug("****** PrimMesh Parameters (Linear) ******\n" + primMesh.ParamsToDisplayString());
  466. #endif
  467. try
  468. {
  469. primMesh.ExtrudeLinear();
  470. }
  471. catch (Exception ex)
  472. {
  473. ReportPrimError("Extrusion failure: exception: " + ex.ToString(), primName, primMesh);
  474. return null;
  475. }
  476. }
  477. else
  478. {
  479. primMesh.holeSizeX = (200 - primShape.PathScaleX) * 0.01f;
  480. primMesh.holeSizeY = (200 - primShape.PathScaleY) * 0.01f;
  481. primMesh.radius = 0.01f * primShape.PathRadiusOffset;
  482. primMesh.revolutions = 1.0f + 0.015f * primShape.PathRevolutions;
  483. primMesh.skew = 0.01f * primShape.PathSkew;
  484. primMesh.twistBegin = primShape.PathTwistBegin * 36 / 10;
  485. primMesh.twistEnd = primShape.PathTwist * 36 / 10;
  486. primMesh.taperX = primShape.PathTaperX * 0.01f;
  487. primMesh.taperY = primShape.PathTaperY * 0.01f;
  488. if (profileBegin < 0.0f || profileBegin >= profileEnd || profileEnd > 1.0f)
  489. {
  490. ReportPrimError("*** CORRUPT PRIM!! ***", primName, primMesh);
  491. if (profileBegin < 0.0f) profileBegin = 0.0f;
  492. if (profileEnd > 1.0f) profileEnd = 1.0f;
  493. }
  494. #if SPAM
  495. m_log.Debug("****** PrimMesh Parameters (Circular) ******\n" + primMesh.ParamsToDisplayString());
  496. #endif
  497. try
  498. {
  499. primMesh.ExtrudeCircular();
  500. }
  501. catch (Exception ex)
  502. {
  503. ReportPrimError("Extrusion failure: exception: " + ex.ToString(), primName, primMesh);
  504. return null;
  505. }
  506. }
  507. primMesh.DumpRaw(baseDir, primName, "primMesh");
  508. primMesh.Scale(size.X, size.Y, size.Z);
  509. coords = primMesh.coords;
  510. faces = primMesh.faces;
  511. }
  512. // Remove the reference to any JPEG2000 sculpt data so it can be GCed
  513. primShape.SculptData = Utils.EmptyBytes;
  514. int numCoords = coords.Count;
  515. int numFaces = faces.Count;
  516. // Create the list of vertices
  517. List<Vertex> vertices = new List<Vertex>();
  518. for (int i = 0; i < numCoords; i++)
  519. {
  520. Coord c = coords[i];
  521. vertices.Add(new Vertex(c.X, c.Y, c.Z));
  522. }
  523. Mesh mesh = new Mesh();
  524. // Add the corresponding triangles to the mesh
  525. for (int i = 0; i < numFaces; i++)
  526. {
  527. Face f = faces[i];
  528. mesh.Add(new Triangle(vertices[f.v1], vertices[f.v2], vertices[f.v3]));
  529. }
  530. return mesh;
  531. }
  532. public IMesh CreateMesh(String primName, PrimitiveBaseShape primShape, Vector3 size, float lod)
  533. {
  534. return CreateMesh(primName, primShape, size, lod, false);
  535. }
  536. public IMesh CreateMesh(String primName, PrimitiveBaseShape primShape, Vector3 size, float lod, bool isPhysical)
  537. {
  538. Mesh mesh = null;
  539. ulong key = 0;
  540. // If this mesh has been created already, return it instead of creating another copy
  541. // For large regions with 100k+ prims and hundreds of copies of each, this can save a GB or more of memory
  542. key = GetMeshKey(primShape, size, lod);
  543. if (m_uniqueMeshes.TryGetValue(key, out mesh))
  544. return mesh;
  545. if (size.X < 0.01f) size.X = 0.01f;
  546. if (size.Y < 0.01f) size.Y = 0.01f;
  547. if (size.Z < 0.01f) size.Z = 0.01f;
  548. mesh = CreateMeshFromPrimMesher(primName, primShape, size, lod);
  549. if (mesh != null)
  550. {
  551. if ((!isPhysical) && size.X < minSizeForComplexMesh && size.Y < minSizeForComplexMesh && size.Z < minSizeForComplexMesh)
  552. {
  553. #if SPAM
  554. m_log.Debug("Meshmerizer: prim " + primName + " has a size of " + size.ToString() + " which is below threshold of " +
  555. minSizeForComplexMesh.ToString() + " - creating simple bounding box");
  556. #endif
  557. mesh = CreateBoundingBoxMesh(mesh);
  558. mesh.DumpRaw(baseDir, primName, "Z extruded");
  559. }
  560. // trim the vertex and triangle lists to free up memory
  561. mesh.TrimExcess();
  562. m_uniqueMeshes.Add(key, mesh);
  563. }
  564. return mesh;
  565. }
  566. }
  567. }