From 66a5b4d1205058cb66a7dc2d02f52a93175bcc57 Mon Sep 17 00:00:00 2001 From: UbitUmarov Date: Fri, 21 Jul 2017 14:11:03 +0100 Subject: ubOde suport convex shapes on all normal prims and sculpts. Since code is experimental this is controled by options ConvexPrims and ConvexSculpts, on section Mesh, that can be set to false in case of problems --- .../PhysicsModules/ubOdeMeshing/Meshmerizer.cs | 133 ++++++++++++++++++--- 1 file changed, 115 insertions(+), 18 deletions(-) (limited to 'OpenSim/Region/PhysicsModules') diff --git a/OpenSim/Region/PhysicsModules/ubOdeMeshing/Meshmerizer.cs b/OpenSim/Region/PhysicsModules/ubOdeMeshing/Meshmerizer.cs index 0117800..c14abd0 100644 --- a/OpenSim/Region/PhysicsModules/ubOdeMeshing/Meshmerizer.cs +++ b/OpenSim/Region/PhysicsModules/ubOdeMeshing/Meshmerizer.cs @@ -43,8 +43,6 @@ using log4net; using Nini.Config; using System.Reflection; using System.IO; -using System.Runtime.Serialization; -using System.Runtime.Serialization.Formatters.Binary; using Mono.Addins; @@ -72,6 +70,8 @@ namespace OpenSim.Region.PhysicsModule.ubODEMeshing private const string baseDir = null; //"rawFiles"; private bool useMeshiesPhysicsMesh = false; + private bool doConvexPrims = true; + private bool doConvexSculpts = true; private float minSizeForComplexMesh = 0.2f; // prims with all dimensions smaller than this will have a bounding box mesh @@ -103,18 +103,14 @@ namespace OpenSim.Region.PhysicsModule.ubODEMeshing if (mesh_config != null) { useMeshiesPhysicsMesh = mesh_config.GetBoolean("UseMeshiesPhysicsMesh", useMeshiesPhysicsMesh); - if (useMeshiesPhysicsMesh) - { - doMeshFileCache = mesh_config.GetBoolean("MeshFileCache", doMeshFileCache); - cachePath = mesh_config.GetString("MeshFileCachePath", cachePath); - fcache = mesh_config.GetFloat("MeshFileCacheExpireHours", fcache); - doCacheExpire = mesh_config.GetBoolean("MeshFileCacheDoExpire", doCacheExpire); - } - else - { - doMeshFileCache = false; - doCacheExpire = false; - } + + doConvexPrims = mesh_config.GetBoolean("ConvexPrims",doConvexPrims); + doConvexSculpts = mesh_config.GetBoolean("ConvexSculpts",doConvexPrims); + + doMeshFileCache = mesh_config.GetBoolean("MeshFileCache", doMeshFileCache); + cachePath = mesh_config.GetString("MeshFileCachePath", cachePath); + fcache = mesh_config.GetFloat("MeshFileCacheExpireHours", fcache); + doCacheExpire = mesh_config.GetBoolean("MeshFileCacheDoExpire", doCacheExpire); m_Enabled = true; } @@ -330,6 +326,7 @@ namespace OpenSim.Region.PhysicsModule.ubODEMeshing List coords; List faces; + bool needsConvexProcessing = convex; if (primShape.SculptEntry) { @@ -340,23 +337,49 @@ namespace OpenSim.Region.PhysicsModule.ubODEMeshing if (!GenerateCoordsAndFacesFromPrimMeshData(primName, primShape, out coords, out faces, convex)) return null; + needsConvexProcessing = false; } else { if (!GenerateCoordsAndFacesFromPrimSculptData(primName, primShape, lod, out coords, out faces)) return null; + needsConvexProcessing &= doConvexSculpts; } } else { if (!GenerateCoordsAndFacesFromPrimShapeData(primName, primShape, lod, convex, out coords, out faces)) return null; + needsConvexProcessing &= doConvexPrims; } - int numCoords = coords.Count; int numFaces = faces.Count; + if(numCoords < 3 || (!needsConvexProcessing && numFaces < 1)) + { + m_log.ErrorFormat("[MESH]: invalid degenerated mesh for prim {0} ignored", primName); + return null; + } + + if(needsConvexProcessing) + { + List convexcoords; + List convexfaces; + if(CreateHull(coords, out convexcoords, out convexfaces) && convexcoords != null && convexfaces != null) + { + coords.Clear(); + coords = convexcoords; + numCoords = coords.Count; + + faces.Clear(); + faces = convexfaces; + numFaces = faces.Count; + } + else + m_log.ErrorFormat("[ubMESH]: failed to create convex for {0} using normal mesh", primName); + } + Mesh mesh = new Mesh(true); // Add the corresponding triangles to the mesh for (int i = 0; i < numFaces; i++) @@ -371,10 +394,10 @@ namespace OpenSim.Region.PhysicsModule.ubODEMeshing faces.Clear(); if(mesh.numberVertices() < 3 || mesh.numberTriangles() < 1) - { - m_log.ErrorFormat("[MESH]: invalid degenerated mesh for prim " + primName + " ignored"); + { + m_log.ErrorFormat("[MESH]: invalid degenerated mesh for prim {0} ignored", primName); return null; - } + } primShape.SculptData = Utils.EmptyBytes; @@ -1583,5 +1606,79 @@ namespace OpenSim.Region.PhysicsModule.ubODEMeshing catch { } } } + + public bool CreateHull(List inputVertices, out List convexcoords, out List newfaces) + { + convexcoords = null; + newfaces = null; + HullDesc desc = new HullDesc(); + HullResult result = new HullResult(); + + int nInputVerts = inputVertices.Count; + int i; + + List vs = new List(nInputVerts); + float3 f3; + + //useless copy + for(i = 0 ; i < nInputVerts; i++) + { + f3 = new float3(inputVertices[i].X, inputVertices[i].Y, inputVertices[i].Z); + vs.Add(f3); + } + + desc.Vertices = vs; + desc.Flags = HullFlag.QF_TRIANGLES; + desc.MaxVertices = 256; + + try + { + HullError ret = HullUtils.CreateConvexHull(desc, ref result); + if (ret != HullError.QE_OK) + return false; + int nverts = result.OutputVertices.Count; + int nindx = result.Indices.Count; + if(nverts < 3 || nindx< 3) + return false; + if(nindx % 3 != 0) + return false; + + convexcoords = new List(nverts); + Coord c; + vs = result.OutputVertices; + + for(i = 0 ; i < nverts; i++) + { + c = new Coord(vs[i].x, vs[i].y, vs[i].z); + convexcoords.Add(c); + } + + newfaces = new List(nindx / 3); + List indxs = result.Indices; + int k, l, m; + Face f; + for(i = 0 ; i < nindx;) + { + k = indxs[i++]; + l = indxs[i++]; + m = indxs[i++]; + if(k > nInputVerts) + continue; + if(l > nInputVerts) + continue; + if(m > nInputVerts) + continue; + f = new Face(k,l,m); + newfaces.Add(f); + } + return true; + } + catch + { + + return false; + } + return false; + } } } -- cgit v1.1