aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/OpenSim/Region/Physics/Meshing/Meshmerizer.cs
diff options
context:
space:
mode:
Diffstat (limited to 'OpenSim/Region/Physics/Meshing/Meshmerizer.cs')
-rw-r--r--OpenSim/Region/Physics/Meshing/Meshmerizer.cs69
1 files changed, 63 insertions, 6 deletions
diff --git a/OpenSim/Region/Physics/Meshing/Meshmerizer.cs b/OpenSim/Region/Physics/Meshing/Meshmerizer.cs
index f469ad6..0873035 100644
--- a/OpenSim/Region/Physics/Meshing/Meshmerizer.cs
+++ b/OpenSim/Region/Physics/Meshing/Meshmerizer.cs
@@ -76,6 +76,7 @@ namespace OpenSim.Region.Physics.Meshing
76 76
77 private float minSizeForComplexMesh = 0.2f; // prims with all dimensions smaller than this will have a bounding box mesh 77 private float minSizeForComplexMesh = 0.2f; // prims with all dimensions smaller than this will have a bounding box mesh
78 78
79 private Dictionary<ulong, Mesh> m_uniqueMeshes = new Dictionary<ulong, Mesh>();
79 80
80 /// <summary> 81 /// <summary>
81 /// creates a simple box mesh of the specified size. This mesh is of very low vertex count and may 82 /// creates a simple box mesh of the specified size. This mesh is of very low vertex count and may
@@ -170,9 +171,62 @@ namespace OpenSim.Region.Physics.Meshing
170 171
171 } 172 }
172 173
173 public Mesh CreateMeshFromPrimMesher(string primName, PrimitiveBaseShape primShape, PhysicsVector size, float lod) 174 private ulong GetMeshKey( PrimitiveBaseShape pbs, PhysicsVector size, float lod )
175 {
176 ulong hash = 5381;
177
178 hash = djb2(hash, pbs.PathCurve);
179 hash = djb2(hash, (byte)((byte)pbs.HollowShape | (byte)pbs.ProfileShape));
180 hash = djb2(hash, pbs.PathBegin);
181 hash = djb2(hash, pbs.PathEnd);
182 hash = djb2(hash, pbs.PathScaleX);
183 hash = djb2(hash, pbs.PathScaleY);
184 hash = djb2(hash, pbs.PathShearX);
185 hash = djb2(hash, pbs.PathShearY);
186 hash = djb2(hash, (byte)pbs.PathTwist);
187 hash = djb2(hash, (byte)pbs.PathTwistBegin);
188 hash = djb2(hash, (byte)pbs.PathRadiusOffset);
189 hash = djb2(hash, (byte)pbs.PathTaperX);
190 hash = djb2(hash, (byte)pbs.PathTaperY);
191 hash = djb2(hash, pbs.PathRevolutions);
192 hash = djb2(hash, (byte)pbs.PathSkew);
193 hash = djb2(hash, pbs.ProfileBegin);
194 hash = djb2(hash, pbs.ProfileEnd);
195 hash = djb2(hash, pbs.ProfileHollow);
196
197 // TODO: Separate scale out from the primitive shape data (after
198 // scaling is supported at the physics engine level)
199 byte[] scaleBytes = size.GetBytes();
200 for (int i = 0; i < scaleBytes.Length; i++)
201 hash = djb2(hash, scaleBytes[i]);
202
203 // Include LOD in hash, accounting for endianness
204 byte[] lodBytes = new byte[4];
205 Buffer.BlockCopy(BitConverter.GetBytes(lod), 0, lodBytes, 0, 4);
206 if (!BitConverter.IsLittleEndian)
207 {
208 Array.Reverse(lodBytes, 0, 4);
209 }
210 for (int i = 0; i < lodBytes.Length; i++)
211 hash = djb2(hash, lodBytes[i]);
212
213 return hash;
214 }
215
216 private ulong djb2(ulong hash, byte c)
217 {
218 return ((hash << 5) + hash) + (ulong)c;
219 }
220
221 private ulong djb2(ulong hash, ushort c)
222 {
223 hash = ((hash << 5) + hash) + (ulong)((byte)c);
224 return ((hash << 5) + hash) + (ulong)(c >> 8);
225 }
226
227
228 private Mesh CreateMeshFromPrimMesher(string primName, PrimitiveBaseShape primShape, PhysicsVector size, float lod)
174 { 229 {
175 Mesh mesh = new Mesh();
176 PrimMesh primMesh; 230 PrimMesh primMesh;
177 PrimMesher.SculptMesh sculptMesh; 231 PrimMesher.SculptMesh sculptMesh;
178 232
@@ -385,8 +439,6 @@ namespace OpenSim.Region.Physics.Meshing
385 439
386 coords = primMesh.coords; 440 coords = primMesh.coords;
387 faces = primMesh.faces; 441 faces = primMesh.faces;
388
389
390 } 442 }
391 443
392 444
@@ -401,13 +453,13 @@ namespace OpenSim.Region.Physics.Meshing
401 vertices.Add(new Vertex(c.X, c.Y, c.Z)); 453 vertices.Add(new Vertex(c.X, c.Y, c.Z));
402 } 454 }
403 455
456 Mesh mesh = new Mesh();
404 // Add the corresponding triangles to the mesh 457 // Add the corresponding triangles to the mesh
405 for (int i = 0; i < numFaces; i++) 458 for (int i = 0; i < numFaces; i++)
406 { 459 {
407 Face f = faces[i]; 460 Face f = faces[i];
408 mesh.Add(new Triangle(vertices[f.v1], vertices[f.v2], vertices[f.v3])); 461 mesh.Add(new Triangle(vertices[f.v1], vertices[f.v2], vertices[f.v3]));
409 } 462 }
410
411 return mesh; 463 return mesh;
412 } 464 }
413 465
@@ -418,7 +470,12 @@ namespace OpenSim.Region.Physics.Meshing
418 470
419 public IMesh CreateMesh(String primName, PrimitiveBaseShape primShape, PhysicsVector size, float lod, bool isPhysical) 471 public IMesh CreateMesh(String primName, PrimitiveBaseShape primShape, PhysicsVector size, float lod, bool isPhysical)
420 { 472 {
473 // If this mesh has been created already, return it instead of creating another copy
474 // For large regions with 100k+ prims and hundreds of copies of each, this can save a GB or more of memory
475 ulong key = GetMeshKey(primShape, size, lod);
421 Mesh mesh = null; 476 Mesh mesh = null;
477 if (m_uniqueMeshes.TryGetValue(key, out mesh))
478 return mesh;
422 479
423 if (size.X < 0.01f) size.X = 0.01f; 480 if (size.X < 0.01f) size.X = 0.01f;
424 if (size.Y < 0.01f) size.Y = 0.01f; 481 if (size.Y < 0.01f) size.Y = 0.01f;
@@ -441,7 +498,7 @@ namespace OpenSim.Region.Physics.Meshing
441 // trim the vertex and triangle lists to free up memory 498 // trim the vertex and triangle lists to free up memory
442 mesh.TrimExcess(); 499 mesh.TrimExcess();
443 } 500 }
444 501 m_uniqueMeshes.Add(key, mesh);
445 return mesh; 502 return mesh;
446 } 503 }
447 } 504 }