diff options
Diffstat (limited to 'OpenSim/Region/Physics/UbitMeshing/Meshmerizer.cs')
-rw-r--r-- | OpenSim/Region/Physics/UbitMeshing/Meshmerizer.cs | 1187 |
1 files changed, 1187 insertions, 0 deletions
diff --git a/OpenSim/Region/Physics/UbitMeshing/Meshmerizer.cs b/OpenSim/Region/Physics/UbitMeshing/Meshmerizer.cs new file mode 100644 index 0000000..4c40175 --- /dev/null +++ b/OpenSim/Region/Physics/UbitMeshing/Meshmerizer.cs | |||
@@ -0,0 +1,1187 @@ | |||
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 | |||
29 | using System; | ||
30 | using System.Collections.Generic; | ||
31 | using OpenSim.Framework; | ||
32 | using OpenSim.Region.Physics.Manager; | ||
33 | using OpenMetaverse; | ||
34 | using OpenMetaverse.StructuredData; | ||
35 | using System.Drawing; | ||
36 | using System.Drawing.Imaging; | ||
37 | using System.IO.Compression; | ||
38 | using PrimMesher; | ||
39 | using log4net; | ||
40 | using Nini.Config; | ||
41 | using System.Reflection; | ||
42 | using System.IO; | ||
43 | using ComponentAce.Compression.Libs.zlib; | ||
44 | using OpenSim.Region.Physics.ConvexDecompositionDotNet; | ||
45 | |||
46 | namespace OpenSim.Region.Physics.Meshing | ||
47 | { | ||
48 | public class MeshmerizerPlugin : IMeshingPlugin | ||
49 | { | ||
50 | public MeshmerizerPlugin() | ||
51 | { | ||
52 | } | ||
53 | |||
54 | public string GetName() | ||
55 | { | ||
56 | return "UbitMeshmerizer"; | ||
57 | } | ||
58 | |||
59 | public IMesher GetMesher(IConfigSource config) | ||
60 | { | ||
61 | return new Meshmerizer(config); | ||
62 | } | ||
63 | } | ||
64 | |||
65 | public class Meshmerizer : IMesher | ||
66 | { | ||
67 | private static readonly ILog m_log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType); | ||
68 | |||
69 | // Setting baseDir to a path will enable the dumping of raw files | ||
70 | // raw files can be imported by blender so a visual inspection of the results can be done | ||
71 | #if SPAM | ||
72 | const string baseDir = "rawFiles"; | ||
73 | #else | ||
74 | private const string baseDir = null; //"rawFiles"; | ||
75 | #endif | ||
76 | |||
77 | private bool cacheSculptMaps = true; | ||
78 | private bool cacheSculptAlphaMaps = true; | ||
79 | |||
80 | private string decodedSculptMapPath = null; | ||
81 | private bool useMeshiesPhysicsMesh = false; | ||
82 | |||
83 | private float minSizeForComplexMesh = 0.2f; // prims with all dimensions smaller than this will have a bounding box mesh | ||
84 | |||
85 | // private Dictionary<ulong, Mesh> m_uniqueMeshes = new Dictionary<ulong, Mesh>(); | ||
86 | // private Dictionary<ulong, Mesh> m_uniqueReleasedMeshes = new Dictionary<ulong, Mesh>(); | ||
87 | private Dictionary<AMeshKey, Mesh> m_uniqueMeshes = new Dictionary<AMeshKey, Mesh>(); | ||
88 | private Dictionary<AMeshKey, Mesh> m_uniqueReleasedMeshes = new Dictionary<AMeshKey, Mesh>(); | ||
89 | |||
90 | public Meshmerizer(IConfigSource config) | ||
91 | { | ||
92 | IConfig start_config = config.Configs["Startup"]; | ||
93 | IConfig mesh_config = config.Configs["Mesh"]; | ||
94 | |||
95 | decodedSculptMapPath = start_config.GetString("DecodedSculptMapPath","j2kDecodeCache"); | ||
96 | |||
97 | cacheSculptMaps = start_config.GetBoolean("CacheSculptMaps", cacheSculptMaps); | ||
98 | |||
99 | if (Environment.OSVersion.Platform == PlatformID.Unix) | ||
100 | { | ||
101 | cacheSculptAlphaMaps = false; | ||
102 | } | ||
103 | else | ||
104 | cacheSculptAlphaMaps = cacheSculptMaps; | ||
105 | |||
106 | if(mesh_config != null) | ||
107 | useMeshiesPhysicsMesh = mesh_config.GetBoolean("UseMeshiesPhysicsMesh", useMeshiesPhysicsMesh); | ||
108 | |||
109 | try | ||
110 | { | ||
111 | if (!Directory.Exists(decodedSculptMapPath)) | ||
112 | Directory.CreateDirectory(decodedSculptMapPath); | ||
113 | } | ||
114 | catch (Exception e) | ||
115 | { | ||
116 | m_log.WarnFormat("[SCULPT]: Unable to create {0} directory: ", decodedSculptMapPath, e.Message); | ||
117 | } | ||
118 | } | ||
119 | |||
120 | /// <summary> | ||
121 | /// creates a simple box mesh of the specified size. This mesh is of very low vertex count and may | ||
122 | /// be useful as a backup proxy when level of detail is not needed or when more complex meshes fail | ||
123 | /// for some reason | ||
124 | /// </summary> | ||
125 | /// <param name="minX"></param> | ||
126 | /// <param name="maxX"></param> | ||
127 | /// <param name="minY"></param> | ||
128 | /// <param name="maxY"></param> | ||
129 | /// <param name="minZ"></param> | ||
130 | /// <param name="maxZ"></param> | ||
131 | /// <returns></returns> | ||
132 | private static Mesh CreateSimpleBoxMesh(float minX, float maxX, float minY, float maxY, float minZ, float maxZ) | ||
133 | { | ||
134 | Mesh box = new Mesh(); | ||
135 | List<Vertex> vertices = new List<Vertex>(); | ||
136 | // bottom | ||
137 | |||
138 | vertices.Add(new Vertex(minX, maxY, minZ)); | ||
139 | vertices.Add(new Vertex(maxX, maxY, minZ)); | ||
140 | vertices.Add(new Vertex(maxX, minY, minZ)); | ||
141 | vertices.Add(new Vertex(minX, minY, minZ)); | ||
142 | |||
143 | box.Add(new Triangle(vertices[0], vertices[1], vertices[2])); | ||
144 | box.Add(new Triangle(vertices[0], vertices[2], vertices[3])); | ||
145 | |||
146 | // top | ||
147 | |||
148 | vertices.Add(new Vertex(maxX, maxY, maxZ)); | ||
149 | vertices.Add(new Vertex(minX, maxY, maxZ)); | ||
150 | vertices.Add(new Vertex(minX, minY, maxZ)); | ||
151 | vertices.Add(new Vertex(maxX, minY, maxZ)); | ||
152 | |||
153 | box.Add(new Triangle(vertices[4], vertices[5], vertices[6])); | ||
154 | box.Add(new Triangle(vertices[4], vertices[6], vertices[7])); | ||
155 | |||
156 | // sides | ||
157 | |||
158 | box.Add(new Triangle(vertices[5], vertices[0], vertices[3])); | ||
159 | box.Add(new Triangle(vertices[5], vertices[3], vertices[6])); | ||
160 | |||
161 | box.Add(new Triangle(vertices[1], vertices[0], vertices[5])); | ||
162 | box.Add(new Triangle(vertices[1], vertices[5], vertices[4])); | ||
163 | |||
164 | box.Add(new Triangle(vertices[7], vertices[1], vertices[4])); | ||
165 | box.Add(new Triangle(vertices[7], vertices[2], vertices[1])); | ||
166 | |||
167 | box.Add(new Triangle(vertices[3], vertices[2], vertices[7])); | ||
168 | box.Add(new Triangle(vertices[3], vertices[7], vertices[6])); | ||
169 | |||
170 | return box; | ||
171 | } | ||
172 | |||
173 | /// <summary> | ||
174 | /// Creates a simple bounding box mesh for a complex input mesh | ||
175 | /// </summary> | ||
176 | /// <param name="meshIn"></param> | ||
177 | /// <returns></returns> | ||
178 | private static Mesh CreateBoundingBoxMesh(Mesh meshIn) | ||
179 | { | ||
180 | float minX = float.MaxValue; | ||
181 | float maxX = float.MinValue; | ||
182 | float minY = float.MaxValue; | ||
183 | float maxY = float.MinValue; | ||
184 | float minZ = float.MaxValue; | ||
185 | float maxZ = float.MinValue; | ||
186 | |||
187 | foreach (Vector3 v in meshIn.getVertexList()) | ||
188 | { | ||
189 | if (v.X < minX) minX = v.X; | ||
190 | if (v.Y < minY) minY = v.Y; | ||
191 | if (v.Z < minZ) minZ = v.Z; | ||
192 | |||
193 | if (v.X > maxX) maxX = v.X; | ||
194 | if (v.Y > maxY) maxY = v.Y; | ||
195 | if (v.Z > maxZ) maxZ = v.Z; | ||
196 | } | ||
197 | |||
198 | return CreateSimpleBoxMesh(minX, maxX, minY, maxY, minZ, maxZ); | ||
199 | } | ||
200 | |||
201 | private void ReportPrimError(string message, string primName, PrimMesh primMesh) | ||
202 | { | ||
203 | m_log.Error(message); | ||
204 | m_log.Error("\nPrim Name: " + primName); | ||
205 | m_log.Error("****** PrimMesh Parameters ******\n" + primMesh.ParamsToDisplayString()); | ||
206 | } | ||
207 | |||
208 | /// <summary> | ||
209 | /// Add a submesh to an existing list of coords and faces. | ||
210 | /// </summary> | ||
211 | /// <param name="subMeshData"></param> | ||
212 | /// <param name="size">Size of entire object</param> | ||
213 | /// <param name="coords"></param> | ||
214 | /// <param name="faces"></param> | ||
215 | private void AddSubMesh(OSDMap subMeshData, Vector3 size, List<Coord> coords, List<Face> faces) | ||
216 | { | ||
217 | // Console.WriteLine("subMeshMap for {0} - {1}", primName, Util.GetFormattedXml((OSD)subMeshMap)); | ||
218 | |||
219 | // As per http://wiki.secondlife.com/wiki/Mesh/Mesh_Asset_Format, some Mesh Level | ||
220 | // of Detail Blocks (maps) contain just a NoGeometry key to signal there is no | ||
221 | // geometry for this submesh. | ||
222 | if (subMeshData.ContainsKey("NoGeometry") && ((OSDBoolean)subMeshData["NoGeometry"])) | ||
223 | return; | ||
224 | |||
225 | OpenMetaverse.Vector3 posMax; | ||
226 | OpenMetaverse.Vector3 posMin; | ||
227 | if (subMeshData.ContainsKey("PositionDomain")) | ||
228 | { | ||
229 | posMax = ((OSDMap)subMeshData["PositionDomain"])["Max"].AsVector3(); | ||
230 | posMin = ((OSDMap)subMeshData["PositionDomain"])["Min"].AsVector3(); | ||
231 | } | ||
232 | else | ||
233 | { | ||
234 | posMax = new Vector3(0.5f, 0.5f, 0.5f); | ||
235 | posMin = new Vector3(-0.5f, -0.5f, -0.5f); | ||
236 | } | ||
237 | |||
238 | ushort faceIndexOffset = (ushort)coords.Count; | ||
239 | |||
240 | byte[] posBytes = subMeshData["Position"].AsBinary(); | ||
241 | for (int i = 0; i < posBytes.Length; i += 6) | ||
242 | { | ||
243 | ushort uX = Utils.BytesToUInt16(posBytes, i); | ||
244 | ushort uY = Utils.BytesToUInt16(posBytes, i + 2); | ||
245 | ushort uZ = Utils.BytesToUInt16(posBytes, i + 4); | ||
246 | |||
247 | Coord c = new Coord( | ||
248 | Utils.UInt16ToFloat(uX, posMin.X, posMax.X) * size.X, | ||
249 | Utils.UInt16ToFloat(uY, posMin.Y, posMax.Y) * size.Y, | ||
250 | Utils.UInt16ToFloat(uZ, posMin.Z, posMax.Z) * size.Z); | ||
251 | |||
252 | coords.Add(c); | ||
253 | } | ||
254 | |||
255 | byte[] triangleBytes = subMeshData["TriangleList"].AsBinary(); | ||
256 | for (int i = 0; i < triangleBytes.Length; i += 6) | ||
257 | { | ||
258 | ushort v1 = (ushort)(Utils.BytesToUInt16(triangleBytes, i) + faceIndexOffset); | ||
259 | ushort v2 = (ushort)(Utils.BytesToUInt16(triangleBytes, i + 2) + faceIndexOffset); | ||
260 | ushort v3 = (ushort)(Utils.BytesToUInt16(triangleBytes, i + 4) + faceIndexOffset); | ||
261 | Face f = new Face(v1, v2, v3); | ||
262 | faces.Add(f); | ||
263 | } | ||
264 | } | ||
265 | |||
266 | /// <summary> | ||
267 | /// Create a physics mesh from data that comes with the prim. The actual data used depends on the prim type. | ||
268 | /// </summary> | ||
269 | /// <param name="primName"></param> | ||
270 | /// <param name="primShape"></param> | ||
271 | /// <param name="size"></param> | ||
272 | /// <param name="lod"></param> | ||
273 | /// <returns></returns> | ||
274 | private Mesh CreateMeshFromPrimMesher(string primName, PrimitiveBaseShape primShape, Vector3 size, float lod, bool convex) | ||
275 | { | ||
276 | // m_log.DebugFormat( | ||
277 | // "[MESH]: Creating physics proxy for {0}, shape {1}", | ||
278 | // primName, (OpenMetaverse.SculptType)primShape.SculptType); | ||
279 | |||
280 | List<Coord> coords; | ||
281 | List<Face> faces; | ||
282 | |||
283 | if (primShape.SculptEntry) | ||
284 | { | ||
285 | if (((OpenMetaverse.SculptType)primShape.SculptType) == SculptType.Mesh) | ||
286 | { | ||
287 | if (!useMeshiesPhysicsMesh) | ||
288 | return null; | ||
289 | |||
290 | if (!GenerateCoordsAndFacesFromPrimMeshData(primName, primShape, size, out coords, out faces, convex)) | ||
291 | return null; | ||
292 | } | ||
293 | else | ||
294 | { | ||
295 | if (!GenerateCoordsAndFacesFromPrimSculptData(primName, primShape, size, lod, out coords, out faces)) | ||
296 | return null; | ||
297 | } | ||
298 | } | ||
299 | else | ||
300 | { | ||
301 | if (!GenerateCoordsAndFacesFromPrimShapeData(primName, primShape, size, lod, out coords, out faces)) | ||
302 | return null; | ||
303 | } | ||
304 | |||
305 | primShape.SculptData = Utils.EmptyBytes; | ||
306 | |||
307 | int numCoords = coords.Count; | ||
308 | int numFaces = faces.Count; | ||
309 | |||
310 | Mesh mesh = new Mesh(); | ||
311 | // Add the corresponding triangles to the mesh | ||
312 | for (int i = 0; i < numFaces; i++) | ||
313 | { | ||
314 | Face f = faces[i]; | ||
315 | mesh.Add(new Triangle(coords[f.v1].X, coords[f.v1].Y, coords[f.v1].Z, | ||
316 | coords[f.v2].X, coords[f.v2].Y, coords[f.v2].Z, | ||
317 | coords[f.v3].X, coords[f.v3].Y, coords[f.v3].Z)); | ||
318 | } | ||
319 | |||
320 | coords.Clear(); | ||
321 | faces.Clear(); | ||
322 | |||
323 | return mesh; | ||
324 | } | ||
325 | |||
326 | /// <summary> | ||
327 | /// Generate the co-ords and faces necessary to construct a mesh from the mesh data the accompanies a prim. | ||
328 | /// </summary> | ||
329 | /// <param name="primName"></param> | ||
330 | /// <param name="primShape"></param> | ||
331 | /// <param name="size"></param> | ||
332 | /// <param name="coords">Coords are added to this list by the method.</param> | ||
333 | /// <param name="faces">Faces are added to this list by the method.</param> | ||
334 | /// <returns>true if coords and faces were successfully generated, false if not</returns> | ||
335 | private bool GenerateCoordsAndFacesFromPrimMeshData( | ||
336 | string primName, PrimitiveBaseShape primShape, Vector3 size, out List<Coord> coords, out List<Face> faces, bool convex) | ||
337 | { | ||
338 | // m_log.DebugFormat("[MESH]: experimental mesh proxy generation for {0}", primName); | ||
339 | |||
340 | bool usemesh = false; | ||
341 | |||
342 | coords = new List<Coord>(); | ||
343 | faces = new List<Face>(); | ||
344 | OSD meshOsd = null; | ||
345 | |||
346 | if (primShape.SculptData.Length <= 0) | ||
347 | { | ||
348 | m_log.InfoFormat("[MESH]: asset data for {0} is zero length", primName); | ||
349 | return false; | ||
350 | } | ||
351 | |||
352 | long start = 0; | ||
353 | using (MemoryStream data = new MemoryStream(primShape.SculptData)) | ||
354 | { | ||
355 | try | ||
356 | { | ||
357 | OSD osd = OSDParser.DeserializeLLSDBinary(data); | ||
358 | if (osd is OSDMap) | ||
359 | meshOsd = (OSDMap)osd; | ||
360 | else | ||
361 | { | ||
362 | m_log.Warn("[Mesh}: unable to cast mesh asset to OSDMap"); | ||
363 | return false; | ||
364 | } | ||
365 | } | ||
366 | catch (Exception e) | ||
367 | { | ||
368 | m_log.Error("[MESH]: Exception deserializing mesh asset header:" + e.ToString()); | ||
369 | } | ||
370 | |||
371 | start = data.Position; | ||
372 | } | ||
373 | |||
374 | if (meshOsd is OSDMap) | ||
375 | { | ||
376 | OSDMap physicsParms = null; | ||
377 | OSDMap map = (OSDMap)meshOsd; | ||
378 | |||
379 | if (!convex) | ||
380 | { | ||
381 | if (map.ContainsKey("physics_shape")) | ||
382 | physicsParms = (OSDMap)map["physics_shape"]; // old asset format | ||
383 | else if (map.ContainsKey("physics_mesh")) | ||
384 | physicsParms = (OSDMap)map["physics_mesh"]; // new asset format | ||
385 | |||
386 | if (physicsParms != null) | ||
387 | usemesh = true; | ||
388 | } | ||
389 | |||
390 | if(!usemesh && (map.ContainsKey("physics_convex"))) | ||
391 | physicsParms = (OSDMap)map["physics_convex"]; | ||
392 | |||
393 | |||
394 | if (physicsParms == null) | ||
395 | { | ||
396 | m_log.Warn("[MESH]: unknown mesh type"); | ||
397 | return false; | ||
398 | } | ||
399 | |||
400 | int physOffset = physicsParms["offset"].AsInteger() + (int)start; | ||
401 | int physSize = physicsParms["size"].AsInteger(); | ||
402 | |||
403 | if (physOffset < 0 || physSize == 0) | ||
404 | return false; // no mesh data in asset | ||
405 | |||
406 | OSD decodedMeshOsd = new OSD(); | ||
407 | byte[] meshBytes = new byte[physSize]; | ||
408 | System.Buffer.BlockCopy(primShape.SculptData, physOffset, meshBytes, 0, physSize); | ||
409 | // byte[] decompressed = new byte[physSize * 5]; | ||
410 | try | ||
411 | { | ||
412 | using (MemoryStream inMs = new MemoryStream(meshBytes)) | ||
413 | { | ||
414 | using (MemoryStream outMs = new MemoryStream()) | ||
415 | { | ||
416 | using (ZOutputStream zOut = new ZOutputStream(outMs)) | ||
417 | { | ||
418 | byte[] readBuffer = new byte[2048]; | ||
419 | int readLen = 0; | ||
420 | while ((readLen = inMs.Read(readBuffer, 0, readBuffer.Length)) > 0) | ||
421 | { | ||
422 | zOut.Write(readBuffer, 0, readLen); | ||
423 | } | ||
424 | zOut.Flush(); | ||
425 | outMs.Seek(0, SeekOrigin.Begin); | ||
426 | |||
427 | byte[] decompressedBuf = outMs.GetBuffer(); | ||
428 | |||
429 | decodedMeshOsd = OSDParser.DeserializeLLSDBinary(decompressedBuf); | ||
430 | } | ||
431 | } | ||
432 | } | ||
433 | } | ||
434 | catch (Exception e) | ||
435 | { | ||
436 | m_log.Error("[MESH]: exception decoding physical mesh: " + e.ToString()); | ||
437 | return false; | ||
438 | } | ||
439 | |||
440 | if (usemesh) | ||
441 | { | ||
442 | OSDArray decodedMeshOsdArray = null; | ||
443 | |||
444 | // physics_shape is an array of OSDMaps, one for each submesh | ||
445 | if (decodedMeshOsd is OSDArray) | ||
446 | { | ||
447 | // Console.WriteLine("decodedMeshOsd for {0} - {1}", primName, Util.GetFormattedXml(decodedMeshOsd)); | ||
448 | |||
449 | decodedMeshOsdArray = (OSDArray)decodedMeshOsd; | ||
450 | foreach (OSD subMeshOsd in decodedMeshOsdArray) | ||
451 | { | ||
452 | if (subMeshOsd is OSDMap) | ||
453 | AddSubMesh(subMeshOsd as OSDMap, size, coords, faces); | ||
454 | } | ||
455 | } | ||
456 | } | ||
457 | else | ||
458 | { | ||
459 | OSDMap cmap = (OSDMap)decodedMeshOsd; | ||
460 | if (cmap == null) | ||
461 | return false; | ||
462 | |||
463 | byte[] data; | ||
464 | |||
465 | List<float3> vs = new List<float3>(); | ||
466 | PHullResult hullr = new PHullResult(); | ||
467 | float3 f3; | ||
468 | Coord c; | ||
469 | Face f; | ||
470 | Vector3 range; | ||
471 | Vector3 min; | ||
472 | |||
473 | const float invMaxU16 = 1.0f / 65535f; | ||
474 | int t1; | ||
475 | int t2; | ||
476 | int t3; | ||
477 | int i; | ||
478 | int nverts; | ||
479 | int nindexs; | ||
480 | |||
481 | if (cmap.ContainsKey("Max")) | ||
482 | range = cmap["Max"].AsVector3(); | ||
483 | else | ||
484 | range = new Vector3(0.5f, 0.5f, 0.5f); | ||
485 | |||
486 | if (cmap.ContainsKey("Min")) | ||
487 | min = cmap["Min"].AsVector3(); | ||
488 | else | ||
489 | min = new Vector3(-0.5f, -0.5f, -0.5f); | ||
490 | |||
491 | range = range - min; | ||
492 | range *= invMaxU16; | ||
493 | |||
494 | if (!convex && cmap.ContainsKey("HullList") && cmap.ContainsKey("Positions")) | ||
495 | { | ||
496 | List<int> hsizes = new List<int>(); | ||
497 | int totalpoints = 0; | ||
498 | data = cmap["HullList"].AsBinary(); | ||
499 | for (i = 0; i < data.Length; i++) | ||
500 | { | ||
501 | t1 = data[i]; | ||
502 | if (t1 == 0) | ||
503 | t1 = 256; | ||
504 | totalpoints += t1; | ||
505 | hsizes.Add(t1); | ||
506 | } | ||
507 | |||
508 | data = cmap["Positions"].AsBinary(); | ||
509 | int ptr = 0; | ||
510 | int vertsoffset = 0; | ||
511 | |||
512 | if (totalpoints == data.Length / 6) // 2 bytes per coord, 3 coords per point | ||
513 | { | ||
514 | foreach (int hullsize in hsizes) | ||
515 | { | ||
516 | for (i = 0; i < hullsize; i++ ) | ||
517 | { | ||
518 | t1 = data[ptr++]; | ||
519 | t1 += data[ptr++] << 8; | ||
520 | t2 = data[ptr++]; | ||
521 | t2 += data[ptr++] << 8; | ||
522 | t3 = data[ptr++]; | ||
523 | t3 += data[ptr++] << 8; | ||
524 | |||
525 | f3 = new float3((t1 * range.X + min.X) * size.X, | ||
526 | (t2 * range.Y + min.Y) * size.Y, | ||
527 | (t3 * range.Z + min.Z) * size.Z); | ||
528 | vs.Add(f3); | ||
529 | } | ||
530 | |||
531 | if(hullsize <3) | ||
532 | { | ||
533 | vs.Clear(); | ||
534 | continue; | ||
535 | } | ||
536 | |||
537 | if (hullsize <5) | ||
538 | { | ||
539 | foreach (float3 point in vs) | ||
540 | { | ||
541 | c.X = point.x; | ||
542 | c.Y = point.y; | ||
543 | c.Z = point.z; | ||
544 | coords.Add(c); | ||
545 | } | ||
546 | f = new Face(vertsoffset, vertsoffset + 1, vertsoffset + 2); | ||
547 | faces.Add(f); | ||
548 | |||
549 | if (hullsize == 4) | ||
550 | { | ||
551 | // not sure about orientation.. | ||
552 | f = new Face(vertsoffset, vertsoffset + 2, vertsoffset + 3); | ||
553 | faces.Add(f); | ||
554 | f = new Face(vertsoffset, vertsoffset + 3, vertsoffset + 1); | ||
555 | faces.Add(f); | ||
556 | f = new Face(vertsoffset + 3, vertsoffset + 2, vertsoffset + 1); | ||
557 | faces.Add(f); | ||
558 | } | ||
559 | vertsoffset += vs.Count; | ||
560 | vs.Clear(); | ||
561 | continue; | ||
562 | } | ||
563 | |||
564 | if (!HullUtils.ComputeHull(vs, ref hullr, 0, 0.0f)) | ||
565 | { | ||
566 | vs.Clear(); | ||
567 | continue; | ||
568 | } | ||
569 | |||
570 | nverts = hullr.Vertices.Count; | ||
571 | nindexs = hullr.Indices.Count; | ||
572 | |||
573 | if (nindexs % 3 != 0) | ||
574 | { | ||
575 | vs.Clear(); | ||
576 | continue; | ||
577 | } | ||
578 | |||
579 | for (i = 0; i < nverts; i++) | ||
580 | { | ||
581 | c.X = hullr.Vertices[i].x; | ||
582 | c.Y = hullr.Vertices[i].y; | ||
583 | c.Z = hullr.Vertices[i].z; | ||
584 | coords.Add(c); | ||
585 | } | ||
586 | |||
587 | for (i = 0; i < nindexs; i += 3) | ||
588 | { | ||
589 | t1 = hullr.Indices[i]; | ||
590 | if (t1 > nverts) | ||
591 | break; | ||
592 | t2 = hullr.Indices[i + 1]; | ||
593 | if (t2 > nverts) | ||
594 | break; | ||
595 | t3 = hullr.Indices[i + 2]; | ||
596 | if (t3 > nverts) | ||
597 | break; | ||
598 | f = new Face(vertsoffset + t1, vertsoffset + t2, vertsoffset + t3); | ||
599 | faces.Add(f); | ||
600 | } | ||
601 | vertsoffset += nverts; | ||
602 | vs.Clear(); | ||
603 | } | ||
604 | } | ||
605 | if (coords.Count > 0 && faces.Count > 0) | ||
606 | return true; | ||
607 | } | ||
608 | |||
609 | vs.Clear(); | ||
610 | |||
611 | if (cmap.ContainsKey("BoundingVerts")) | ||
612 | { | ||
613 | data = cmap["BoundingVerts"].AsBinary(); | ||
614 | |||
615 | for (i = 0; i < data.Length; ) | ||
616 | { | ||
617 | t1 = data[i++]; | ||
618 | t1 += data[i++] << 8; | ||
619 | t2 = data[i++]; | ||
620 | t2 += data[i++] << 8; | ||
621 | t3 = data[i++]; | ||
622 | t3 += data[i++] << 8; | ||
623 | |||
624 | f3 = new float3((t1 * range.X + min.X) * size.X, | ||
625 | (t2 * range.Y + min.Y) * size.Y, | ||
626 | (t3 * range.Z + min.Z) * size.Z); | ||
627 | vs.Add(f3); | ||
628 | } | ||
629 | |||
630 | if (vs.Count < 3) | ||
631 | { | ||
632 | vs.Clear(); | ||
633 | return false; | ||
634 | } | ||
635 | |||
636 | if (vs.Count < 5) | ||
637 | { | ||
638 | foreach (float3 point in vs) | ||
639 | { | ||
640 | c.X = point.x; | ||
641 | c.Y = point.y; | ||
642 | c.Z = point.z; | ||
643 | coords.Add(c); | ||
644 | } | ||
645 | f = new Face(0, 1, 2); | ||
646 | faces.Add(f); | ||
647 | |||
648 | if (vs.Count == 4) | ||
649 | { | ||
650 | f = new Face(0, 2, 3); | ||
651 | faces.Add(f); | ||
652 | f = new Face(0, 3, 1); | ||
653 | faces.Add(f); | ||
654 | f = new Face( 3, 2, 1); | ||
655 | faces.Add(f); | ||
656 | } | ||
657 | vs.Clear(); | ||
658 | return true; | ||
659 | } | ||
660 | |||
661 | if (!HullUtils.ComputeHull(vs, ref hullr, 0, 0.0f)) | ||
662 | return false; | ||
663 | |||
664 | nverts = hullr.Vertices.Count; | ||
665 | nindexs = hullr.Indices.Count; | ||
666 | |||
667 | if (nindexs % 3 != 0) | ||
668 | return false; | ||
669 | |||
670 | for (i = 0; i < nverts; i++) | ||
671 | { | ||
672 | c.X = hullr.Vertices[i].x; | ||
673 | c.Y = hullr.Vertices[i].y; | ||
674 | c.Z = hullr.Vertices[i].z; | ||
675 | coords.Add(c); | ||
676 | } | ||
677 | for (i = 0; i < nindexs; i += 3) | ||
678 | { | ||
679 | t1 = hullr.Indices[i]; | ||
680 | if (t1 > nverts) | ||
681 | break; | ||
682 | t2 = hullr.Indices[i + 1]; | ||
683 | if (t2 > nverts) | ||
684 | break; | ||
685 | t3 = hullr.Indices[i + 2]; | ||
686 | if (t3 > nverts) | ||
687 | break; | ||
688 | f = new Face(t1, t2, t3); | ||
689 | faces.Add(f); | ||
690 | } | ||
691 | |||
692 | if (coords.Count > 0 && faces.Count > 0) | ||
693 | return true; | ||
694 | } | ||
695 | else | ||
696 | return false; | ||
697 | } | ||
698 | } | ||
699 | |||
700 | return true; | ||
701 | } | ||
702 | |||
703 | /// <summary> | ||
704 | /// Generate the co-ords and faces necessary to construct a mesh from the sculpt data the accompanies a prim. | ||
705 | /// </summary> | ||
706 | /// <param name="primName"></param> | ||
707 | /// <param name="primShape"></param> | ||
708 | /// <param name="size"></param> | ||
709 | /// <param name="lod"></param> | ||
710 | /// <param name="coords">Coords are added to this list by the method.</param> | ||
711 | /// <param name="faces">Faces are added to this list by the method.</param> | ||
712 | /// <returns>true if coords and faces were successfully generated, false if not</returns> | ||
713 | private bool GenerateCoordsAndFacesFromPrimSculptData( | ||
714 | string primName, PrimitiveBaseShape primShape, Vector3 size, float lod, out List<Coord> coords, out List<Face> faces) | ||
715 | { | ||
716 | coords = new List<Coord>(); | ||
717 | faces = new List<Face>(); | ||
718 | PrimMesher.SculptMesh sculptMesh; | ||
719 | Image idata = null; | ||
720 | string decodedSculptFileName = ""; | ||
721 | |||
722 | if (cacheSculptMaps && primShape.SculptTexture != UUID.Zero) | ||
723 | { | ||
724 | decodedSculptFileName = System.IO.Path.Combine(decodedSculptMapPath, "smap_" + primShape.SculptTexture.ToString()); | ||
725 | try | ||
726 | { | ||
727 | if (File.Exists(decodedSculptFileName)) | ||
728 | { | ||
729 | idata = Image.FromFile(decodedSculptFileName); | ||
730 | } | ||
731 | } | ||
732 | catch (Exception e) | ||
733 | { | ||
734 | m_log.Error("[SCULPT]: unable to load cached sculpt map " + decodedSculptFileName + " " + e.Message); | ||
735 | |||
736 | } | ||
737 | //if (idata != null) | ||
738 | // m_log.Debug("[SCULPT]: loaded cached map asset for map ID: " + primShape.SculptTexture.ToString()); | ||
739 | } | ||
740 | |||
741 | if (idata == null) | ||
742 | { | ||
743 | if (primShape.SculptData == null || primShape.SculptData.Length == 0) | ||
744 | return false; | ||
745 | |||
746 | try | ||
747 | { | ||
748 | OpenMetaverse.Imaging.ManagedImage unusedData; | ||
749 | OpenMetaverse.Imaging.OpenJPEG.DecodeToImage(primShape.SculptData, out unusedData, out idata); | ||
750 | |||
751 | if (idata == null) | ||
752 | { | ||
753 | // In some cases it seems that the decode can return a null bitmap without throwing | ||
754 | // an exception | ||
755 | m_log.WarnFormat("[PHYSICS]: OpenJPEG decoded sculpt data for {0} to a null bitmap. Ignoring.", primName); | ||
756 | |||
757 | return false; | ||
758 | } | ||
759 | |||
760 | unusedData = null; | ||
761 | |||
762 | //idata = CSJ2K.J2kImage.FromBytes(primShape.SculptData); | ||
763 | |||
764 | if (cacheSculptMaps && (cacheSculptAlphaMaps || (((ImageFlags)(idata.Flags) & ImageFlags.HasAlpha) ==0))) | ||
765 | // don't cache images with alpha channel in linux since mono can't load them correctly) | ||
766 | { | ||
767 | try { idata.Save(decodedSculptFileName, ImageFormat.MemoryBmp); } | ||
768 | catch (Exception e) { m_log.Error("[SCULPT]: unable to cache sculpt map " + decodedSculptFileName + " " + e.Message); } | ||
769 | } | ||
770 | } | ||
771 | catch (DllNotFoundException) | ||
772 | { | ||
773 | 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!"); | ||
774 | return false; | ||
775 | } | ||
776 | catch (IndexOutOfRangeException) | ||
777 | { | ||
778 | m_log.Error("[PHYSICS]: OpenJpeg was unable to decode this. Physics Proxy generation failed"); | ||
779 | return false; | ||
780 | } | ||
781 | catch (Exception ex) | ||
782 | { | ||
783 | m_log.Error("[PHYSICS]: Unable to generate a Sculpty physics proxy. Sculpty texture decode failed: " + ex.Message); | ||
784 | return false; | ||
785 | } | ||
786 | } | ||
787 | |||
788 | PrimMesher.SculptMesh.SculptType sculptType; | ||
789 | // remove mirror and invert bits | ||
790 | OpenMetaverse.SculptType pbsSculptType = ((OpenMetaverse.SculptType)(primShape.SculptType & 0x3f)); | ||
791 | switch (pbsSculptType) | ||
792 | { | ||
793 | case OpenMetaverse.SculptType.Cylinder: | ||
794 | sculptType = PrimMesher.SculptMesh.SculptType.cylinder; | ||
795 | break; | ||
796 | case OpenMetaverse.SculptType.Plane: | ||
797 | sculptType = PrimMesher.SculptMesh.SculptType.plane; | ||
798 | break; | ||
799 | case OpenMetaverse.SculptType.Torus: | ||
800 | sculptType = PrimMesher.SculptMesh.SculptType.torus; | ||
801 | break; | ||
802 | case OpenMetaverse.SculptType.Sphere: | ||
803 | sculptType = PrimMesher.SculptMesh.SculptType.sphere; | ||
804 | break; | ||
805 | default: | ||
806 | sculptType = PrimMesher.SculptMesh.SculptType.plane; | ||
807 | break; | ||
808 | } | ||
809 | |||
810 | bool mirror = ((primShape.SculptType & 128) != 0); | ||
811 | bool invert = ((primShape.SculptType & 64) != 0); | ||
812 | |||
813 | sculptMesh = new PrimMesher.SculptMesh((Bitmap)idata, sculptType, (int)lod, mirror, invert); | ||
814 | |||
815 | idata.Dispose(); | ||
816 | |||
817 | sculptMesh.DumpRaw(baseDir, primName, "primMesh"); | ||
818 | |||
819 | sculptMesh.Scale(size.X, size.Y, size.Z); | ||
820 | |||
821 | coords = sculptMesh.coords; | ||
822 | faces = sculptMesh.faces; | ||
823 | |||
824 | return true; | ||
825 | } | ||
826 | |||
827 | /// <summary> | ||
828 | /// Generate the co-ords and faces necessary to construct a mesh from the shape data the accompanies a prim. | ||
829 | /// </summary> | ||
830 | /// <param name="primName"></param> | ||
831 | /// <param name="primShape"></param> | ||
832 | /// <param name="size"></param> | ||
833 | /// <param name="coords">Coords are added to this list by the method.</param> | ||
834 | /// <param name="faces">Faces are added to this list by the method.</param> | ||
835 | /// <returns>true if coords and faces were successfully generated, false if not</returns> | ||
836 | private bool GenerateCoordsAndFacesFromPrimShapeData( | ||
837 | string primName, PrimitiveBaseShape primShape, Vector3 size, float lod, out List<Coord> coords, out List<Face> faces) | ||
838 | { | ||
839 | PrimMesh primMesh; | ||
840 | coords = new List<Coord>(); | ||
841 | faces = new List<Face>(); | ||
842 | |||
843 | float pathShearX = primShape.PathShearX < 128 ? (float)primShape.PathShearX * 0.01f : (float)(primShape.PathShearX - 256) * 0.01f; | ||
844 | float pathShearY = primShape.PathShearY < 128 ? (float)primShape.PathShearY * 0.01f : (float)(primShape.PathShearY - 256) * 0.01f; | ||
845 | float pathBegin = (float)primShape.PathBegin * 2.0e-5f; | ||
846 | float pathEnd = 1.0f - (float)primShape.PathEnd * 2.0e-5f; | ||
847 | float pathScaleX = (float)(primShape.PathScaleX - 100) * 0.01f; | ||
848 | float pathScaleY = (float)(primShape.PathScaleY - 100) * 0.01f; | ||
849 | |||
850 | float profileBegin = (float)primShape.ProfileBegin * 2.0e-5f; | ||
851 | float profileEnd = 1.0f - (float)primShape.ProfileEnd * 2.0e-5f; | ||
852 | float profileHollow = (float)primShape.ProfileHollow * 2.0e-5f; | ||
853 | if (profileHollow > 0.95f) | ||
854 | profileHollow = 0.95f; | ||
855 | |||
856 | int sides = 4; | ||
857 | LevelOfDetail iLOD = (LevelOfDetail)lod; | ||
858 | if ((primShape.ProfileCurve & 0x07) == (byte)ProfileShape.EquilateralTriangle) | ||
859 | sides = 3; | ||
860 | else if ((primShape.ProfileCurve & 0x07) == (byte)ProfileShape.Circle) | ||
861 | { | ||
862 | switch (iLOD) | ||
863 | { | ||
864 | case LevelOfDetail.High: sides = 24; break; | ||
865 | case LevelOfDetail.Medium: sides = 12; break; | ||
866 | case LevelOfDetail.Low: sides = 6; break; | ||
867 | case LevelOfDetail.VeryLow: sides = 3; break; | ||
868 | default: sides = 24; break; | ||
869 | } | ||
870 | } | ||
871 | else if ((primShape.ProfileCurve & 0x07) == (byte)ProfileShape.HalfCircle) | ||
872 | { // half circle, prim is a sphere | ||
873 | switch (iLOD) | ||
874 | { | ||
875 | case LevelOfDetail.High: sides = 24; break; | ||
876 | case LevelOfDetail.Medium: sides = 12; break; | ||
877 | case LevelOfDetail.Low: sides = 6; break; | ||
878 | case LevelOfDetail.VeryLow: sides = 3; break; | ||
879 | default: sides = 24; break; | ||
880 | } | ||
881 | |||
882 | profileBegin = 0.5f * profileBegin + 0.5f; | ||
883 | profileEnd = 0.5f * profileEnd + 0.5f; | ||
884 | } | ||
885 | |||
886 | int hollowSides = sides; | ||
887 | if (primShape.HollowShape == HollowShape.Circle) | ||
888 | { | ||
889 | switch (iLOD) | ||
890 | { | ||
891 | case LevelOfDetail.High: hollowSides = 24; break; | ||
892 | case LevelOfDetail.Medium: hollowSides = 12; break; | ||
893 | case LevelOfDetail.Low: hollowSides = 6; break; | ||
894 | case LevelOfDetail.VeryLow: hollowSides = 3; break; | ||
895 | default: hollowSides = 24; break; | ||
896 | } | ||
897 | } | ||
898 | else if (primShape.HollowShape == HollowShape.Square) | ||
899 | hollowSides = 4; | ||
900 | else if (primShape.HollowShape == HollowShape.Triangle) | ||
901 | hollowSides = 3; | ||
902 | |||
903 | primMesh = new PrimMesh(sides, profileBegin, profileEnd, profileHollow, hollowSides); | ||
904 | |||
905 | if (primMesh.errorMessage != null) | ||
906 | if (primMesh.errorMessage.Length > 0) | ||
907 | m_log.Error("[ERROR] " + primMesh.errorMessage); | ||
908 | |||
909 | primMesh.topShearX = pathShearX; | ||
910 | primMesh.topShearY = pathShearY; | ||
911 | primMesh.pathCutBegin = pathBegin; | ||
912 | primMesh.pathCutEnd = pathEnd; | ||
913 | |||
914 | if (primShape.PathCurve == (byte)Extrusion.Straight || primShape.PathCurve == (byte) Extrusion.Flexible) | ||
915 | { | ||
916 | primMesh.twistBegin = primShape.PathTwistBegin * 18 / 10; | ||
917 | primMesh.twistEnd = primShape.PathTwist * 18 / 10; | ||
918 | primMesh.taperX = pathScaleX; | ||
919 | primMesh.taperY = pathScaleY; | ||
920 | |||
921 | if (profileBegin < 0.0f || profileBegin >= profileEnd || profileEnd > 1.0f) | ||
922 | { | ||
923 | ReportPrimError("*** CORRUPT PRIM!! ***", primName, primMesh); | ||
924 | if (profileBegin < 0.0f) profileBegin = 0.0f; | ||
925 | if (profileEnd > 1.0f) profileEnd = 1.0f; | ||
926 | } | ||
927 | #if SPAM | ||
928 | m_log.Debug("****** PrimMesh Parameters (Linear) ******\n" + primMesh.ParamsToDisplayString()); | ||
929 | #endif | ||
930 | try | ||
931 | { | ||
932 | primMesh.ExtrudeLinear(); | ||
933 | } | ||
934 | catch (Exception ex) | ||
935 | { | ||
936 | ReportPrimError("Extrusion failure: exception: " + ex.ToString(), primName, primMesh); | ||
937 | return false; | ||
938 | } | ||
939 | } | ||
940 | else | ||
941 | { | ||
942 | primMesh.holeSizeX = (200 - primShape.PathScaleX) * 0.01f; | ||
943 | primMesh.holeSizeY = (200 - primShape.PathScaleY) * 0.01f; | ||
944 | primMesh.radius = 0.01f * primShape.PathRadiusOffset; | ||
945 | primMesh.revolutions = 1.0f + 0.015f * primShape.PathRevolutions; | ||
946 | primMesh.skew = 0.01f * primShape.PathSkew; | ||
947 | primMesh.twistBegin = primShape.PathTwistBegin * 36 / 10; | ||
948 | primMesh.twistEnd = primShape.PathTwist * 36 / 10; | ||
949 | primMesh.taperX = primShape.PathTaperX * 0.01f; | ||
950 | primMesh.taperY = primShape.PathTaperY * 0.01f; | ||
951 | |||
952 | if (profileBegin < 0.0f || profileBegin >= profileEnd || profileEnd > 1.0f) | ||
953 | { | ||
954 | ReportPrimError("*** CORRUPT PRIM!! ***", primName, primMesh); | ||
955 | if (profileBegin < 0.0f) profileBegin = 0.0f; | ||
956 | if (profileEnd > 1.0f) profileEnd = 1.0f; | ||
957 | } | ||
958 | #if SPAM | ||
959 | m_log.Debug("****** PrimMesh Parameters (Circular) ******\n" + primMesh.ParamsToDisplayString()); | ||
960 | #endif | ||
961 | try | ||
962 | { | ||
963 | primMesh.ExtrudeCircular(); | ||
964 | } | ||
965 | catch (Exception ex) | ||
966 | { | ||
967 | ReportPrimError("Extrusion failure: exception: " + ex.ToString(), primName, primMesh); | ||
968 | return false; | ||
969 | } | ||
970 | } | ||
971 | |||
972 | primMesh.DumpRaw(baseDir, primName, "primMesh"); | ||
973 | |||
974 | primMesh.Scale(size.X, size.Y, size.Z); | ||
975 | |||
976 | coords = primMesh.coords; | ||
977 | faces = primMesh.faces; | ||
978 | |||
979 | return true; | ||
980 | } | ||
981 | |||
982 | public AMeshKey GetMeshUniqueKey(PrimitiveBaseShape primShape, Vector3 size, byte lod, bool convex) | ||
983 | { | ||
984 | AMeshKey key = new AMeshKey(); | ||
985 | Byte[] someBytes; | ||
986 | |||
987 | key.hashB = 5181; | ||
988 | ulong hash = 5381; | ||
989 | |||
990 | if (primShape.SculptEntry) | ||
991 | { | ||
992 | key.uuid = primShape.SculptTexture; | ||
993 | key.hashB = mdjb2(key.hashB, primShape.SculptType); | ||
994 | } | ||
995 | else | ||
996 | { | ||
997 | hash = mdjb2(hash, primShape.PathCurve); | ||
998 | hash = mdjb2(hash, (byte)primShape.HollowShape); | ||
999 | hash = mdjb2(hash, (byte)primShape.ProfileShape); | ||
1000 | hash = mdjb2(hash, primShape.PathBegin); | ||
1001 | hash = mdjb2(hash, primShape.PathEnd); | ||
1002 | hash = mdjb2(hash, primShape.PathScaleX); | ||
1003 | hash = mdjb2(hash, primShape.PathScaleY); | ||
1004 | hash = mdjb2(hash, primShape.PathShearX); | ||
1005 | hash = mdjb2(hash, primShape.PathShearY); | ||
1006 | hash = mdjb2(hash, (byte)primShape.PathTwist); | ||
1007 | hash = mdjb2(hash, (byte)primShape.PathTwistBegin); | ||
1008 | hash = mdjb2(hash, (byte)primShape.PathRadiusOffset); | ||
1009 | hash = mdjb2(hash, (byte)primShape.PathTaperX); | ||
1010 | hash = mdjb2(hash, (byte)primShape.PathTaperY); | ||
1011 | hash = mdjb2(hash, primShape.PathRevolutions); | ||
1012 | hash = mdjb2(hash, (byte)primShape.PathSkew); | ||
1013 | hash = mdjb2(hash, primShape.ProfileBegin); | ||
1014 | hash = mdjb2(hash, primShape.ProfileEnd); | ||
1015 | hash = mdjb2(hash, primShape.ProfileHollow); | ||
1016 | key.hashA = hash; | ||
1017 | } | ||
1018 | |||
1019 | hash = key.hashB; | ||
1020 | |||
1021 | someBytes = size.GetBytes(); | ||
1022 | for (int i = 0; i < someBytes.Length; i++) | ||
1023 | hash = mdjb2(hash, someBytes[i]); | ||
1024 | |||
1025 | hash = mdjb2(hash, lod); | ||
1026 | |||
1027 | hash &= 0x3fffffffffffffff; | ||
1028 | |||
1029 | if (convex) | ||
1030 | hash |= 0x4000000000000000; | ||
1031 | |||
1032 | if (primShape.SculptEntry) | ||
1033 | hash |= 0x8000000000000000; | ||
1034 | |||
1035 | key.hashB = hash; | ||
1036 | |||
1037 | return key; | ||
1038 | } | ||
1039 | |||
1040 | private ulong mdjb2(ulong hash, byte c) | ||
1041 | { | ||
1042 | return ((hash << 5) + hash) + (ulong)c; | ||
1043 | } | ||
1044 | |||
1045 | private ulong mdjb2(ulong hash, ushort c) | ||
1046 | { | ||
1047 | hash = ((hash << 5) + hash) + (ulong)((byte)c); | ||
1048 | return ((hash << 5) + hash) + (ulong)(c >> 8); | ||
1049 | } | ||
1050 | |||
1051 | |||
1052 | public IMesh CreateMesh(String primName, PrimitiveBaseShape primShape, Vector3 size, float lod) | ||
1053 | { | ||
1054 | return CreateMesh(primName, primShape, size, lod, false,false); | ||
1055 | } | ||
1056 | |||
1057 | public IMesh CreateMesh(String primName, PrimitiveBaseShape primShape, Vector3 size, float lod, bool isPhysical) | ||
1058 | { | ||
1059 | return CreateMesh(primName, primShape, size, lod, false,false); | ||
1060 | } | ||
1061 | |||
1062 | private static Vector3 m_MeshUnitSize = new Vector3(0.5f, 0.5f, 0.5f); | ||
1063 | |||
1064 | public IMesh CreateMesh(String primName, PrimitiveBaseShape primShape, Vector3 size, float lod, bool isPhysical, bool convex) | ||
1065 | { | ||
1066 | #if SPAM | ||
1067 | m_log.DebugFormat("[MESH]: Creating mesh for {0}", primName); | ||
1068 | #endif | ||
1069 | |||
1070 | Mesh mesh = null; | ||
1071 | // ulong key = 0; | ||
1072 | |||
1073 | |||
1074 | if (size.X < 0.01f) size.X = 0.01f; | ||
1075 | if (size.Y < 0.01f) size.Y = 0.01f; | ||
1076 | if (size.Z < 0.01f) size.Z = 0.01f; | ||
1077 | |||
1078 | // try to find a identical mesh on meshs in use | ||
1079 | // key = primShape.GetMeshKey(size, lod, convex); | ||
1080 | AMeshKey key = GetMeshUniqueKey(primShape,size,(byte)lod, convex); | ||
1081 | |||
1082 | lock (m_uniqueMeshes) | ||
1083 | { | ||
1084 | m_uniqueMeshes.TryGetValue(key, out mesh); | ||
1085 | |||
1086 | if (mesh != null) | ||
1087 | { | ||
1088 | mesh.RefCount++; | ||
1089 | return mesh; | ||
1090 | } | ||
1091 | } | ||
1092 | |||
1093 | // try to find a identical mesh on meshs recently released | ||
1094 | lock (m_uniqueReleasedMeshes) | ||
1095 | { | ||
1096 | m_uniqueReleasedMeshes.TryGetValue(key, out mesh); | ||
1097 | if (mesh != null) | ||
1098 | { | ||
1099 | m_uniqueReleasedMeshes.Remove(key); | ||
1100 | lock (m_uniqueMeshes) | ||
1101 | m_uniqueMeshes.Add(key, mesh); | ||
1102 | mesh.RefCount = 1; | ||
1103 | return mesh; | ||
1104 | } | ||
1105 | } | ||
1106 | |||
1107 | mesh = CreateMeshFromPrimMesher(primName, primShape, size, lod,convex); | ||
1108 | |||
1109 | if (mesh != null) | ||
1110 | { | ||
1111 | if ((!isPhysical) && size.X < minSizeForComplexMesh && size.Y < minSizeForComplexMesh && size.Z < minSizeForComplexMesh) | ||
1112 | { | ||
1113 | #if SPAM | ||
1114 | m_log.Debug("Meshmerizer: prim " + primName + " has a size of " + size.ToString() + " which is below threshold of " + | ||
1115 | minSizeForComplexMesh.ToString() + " - creating simple bounding box"); | ||
1116 | #endif | ||
1117 | mesh = CreateBoundingBoxMesh(mesh); | ||
1118 | mesh.DumpRaw(baseDir, primName, "Z extruded"); | ||
1119 | } | ||
1120 | |||
1121 | // trim the vertex and triangle lists to free up memory | ||
1122 | mesh.TrimExcess(); | ||
1123 | mesh.Key = key; | ||
1124 | mesh.RefCount = 1; | ||
1125 | |||
1126 | lock(m_uniqueMeshes) | ||
1127 | m_uniqueMeshes.Add(key, mesh); | ||
1128 | } | ||
1129 | |||
1130 | return mesh; | ||
1131 | } | ||
1132 | |||
1133 | public void ReleaseMesh(IMesh imesh) | ||
1134 | { | ||
1135 | if (imesh == null) | ||
1136 | return; | ||
1137 | |||
1138 | Mesh mesh = (Mesh)imesh; | ||
1139 | |||
1140 | int curRefCount = mesh.RefCount; | ||
1141 | curRefCount--; | ||
1142 | |||
1143 | if (curRefCount > 0) | ||
1144 | { | ||
1145 | mesh.RefCount = curRefCount; | ||
1146 | return; | ||
1147 | } | ||
1148 | |||
1149 | lock (m_uniqueMeshes) | ||
1150 | { | ||
1151 | mesh.RefCount = 0; | ||
1152 | m_uniqueMeshes.Remove(mesh.Key); | ||
1153 | lock (m_uniqueReleasedMeshes) | ||
1154 | m_uniqueReleasedMeshes.Add(mesh.Key, mesh); | ||
1155 | } | ||
1156 | } | ||
1157 | |||
1158 | public void ExpireReleaseMeshs() | ||
1159 | { | ||
1160 | if (m_uniqueReleasedMeshes.Count == 0) | ||
1161 | return; | ||
1162 | |||
1163 | List<Mesh> meshstodelete = new List<Mesh>(); | ||
1164 | int refcntr; | ||
1165 | |||
1166 | lock (m_uniqueReleasedMeshes) | ||
1167 | { | ||
1168 | foreach (Mesh m in m_uniqueReleasedMeshes.Values) | ||
1169 | { | ||
1170 | refcntr = m.RefCount; | ||
1171 | refcntr--; | ||
1172 | if (refcntr > -6) | ||
1173 | m.RefCount = refcntr; | ||
1174 | else | ||
1175 | meshstodelete.Add(m); | ||
1176 | } | ||
1177 | |||
1178 | foreach (Mesh m in meshstodelete) | ||
1179 | { | ||
1180 | m_uniqueReleasedMeshes.Remove(m.Key); | ||
1181 | m.releaseSourceMeshData(); | ||
1182 | m.releasePinned(); | ||
1183 | } | ||
1184 | } | ||
1185 | } | ||
1186 | } | ||
1187 | } | ||