Exemplo n.º 1
0
        public IMesh CreateMesh(String primName, PrimitiveBaseShape primShape, Vector3 size, float lod, bool isPhysical, bool shouldCache)
        {
#if SPAM
            m_log.DebugFormat("[MESH]: Creating mesh for {0}", primName);
#endif

            Mesh  mesh = null;
            ulong key  = 0;

            // If this mesh has been created already, return it instead of creating another copy
            // For large regions with 100k+ prims and hundreds of copies of each, this can save a GB or more of memory
            if (shouldCache)
            {
                key = primShape.GetMeshKey(size, lod);
                if (m_uniqueMeshes.TryGetValue(key, out mesh))
                {
                    return(mesh);
                }
            }

            if (size.X < 0.01f)
            {
                size.X = 0.01f;
            }
            if (size.Y < 0.01f)
            {
                size.Y = 0.01f;
            }
            if (size.Z < 0.01f)
            {
                size.Z = 0.01f;
            }

            mesh = CreateMeshFromPrimMesher(primName, primShape, size, lod);

            if (mesh != null)
            {
                if ((!isPhysical) && size.X < minSizeForComplexMesh && size.Y < minSizeForComplexMesh && size.Z < minSizeForComplexMesh)
                {
#if SPAM
                    m_log.Debug("Meshmerizer: prim " + primName + " has a size of " + size.ToString() + " which is below threshold of " +
                                minSizeForComplexMesh.ToString() + " - creating simple bounding box");
#endif
                    mesh = CreateBoundingBoxMesh(mesh);
                    mesh.DumpRaw(baseDir, primName, "Z extruded");
                }

                // trim the vertex and triangle lists to free up memory
                mesh.TrimExcess();

                if (shouldCache)
                {
                    m_uniqueMeshes.Add(key, mesh);
                }
            }

            return(mesh);
        }
Exemplo n.º 2
0
        public IMesh CreateMesh(String primName, PrimitiveBaseShape primShape, Vector3 size, float lod, bool isPhysical, bool shouldCache, out List <List <Vector3> > hulls, out List <Vector3> boundingHull)
        {
#if SPAM
            m_log.DebugFormat("[MESH]: Creating mesh for {0}", primName);
#endif

            Mesh  mesh = null;
            ulong key  = 0;

            // If this mesh has been created already, return it instead of creating another copy
            // For large regions with 100k+ prims and hundreds of copies of each, this can save a GB or more of memory
            m_uniqueMeshesRwLock.AcquireReaderLock(-1);
            try
            {
                if (shouldCache)
                {
                    key = primShape.GetMeshKey(size, lod);
                    if (m_uniqueMeshes.TryGetValue(key, out mesh))
                    {
                        m_uniqueMeshesBoundingHulls.TryGetValue(key, out boundingHull);
                        m_uniqueMeshesHulls.TryGetValue(key, out hulls);
                        return(mesh);
                    }
                }

                LockCookie lc = m_uniqueMeshesRwLock.UpgradeToWriterLock(-1);
                try
                {
                    /* recheck since we allow a lot of threading here */
                    if (shouldCache)
                    {
                        if (m_uniqueMeshes.TryGetValue(key, out mesh))
                        {
                            m_uniqueMeshesBoundingHulls.TryGetValue(key, out boundingHull);
                            m_uniqueMeshesHulls.TryGetValue(key, out hulls);
                            return(mesh);
                        }
                    }
                    if (size.X < 0.01f)
                    {
                        size.X = 0.01f;
                    }
                    if (size.Y < 0.01f)
                    {
                        size.Y = 0.01f;
                    }
                    if (size.Z < 0.01f)
                    {
                        size.Z = 0.01f;
                    }

                    List <List <Vector3> > inhulls;
                    List <Vector3>         inboundingHull;
                    mesh = CreateMeshFromPrimMesher(primName, primShape, size, lod, out inhulls, out inboundingHull);

                    if (inhulls != null)
                    {
                        hulls = new List <List <Vector3> >();
                        foreach (var hull in inhulls)
                        {
                            List <Vector3> verts = new List <Vector3>();
                            foreach (var vert in hull)
                            {
                                verts.Add(vert * size);
                            }
                            hulls.Add(verts);
                        }
                    }
                    else
                    {
                        hulls = null;
                    }

                    if (inboundingHull != null)
                    {
                        boundingHull = new List <Vector3>();
                        foreach (var vert in inboundingHull)
                        {
                            boundingHull.Add(vert * size);
                        }
                    }
                    else
                    {
                        boundingHull = null;
                    }

                    if (mesh != null)
                    {
                        if ((!isPhysical) && size.X < minSizeForComplexMesh && size.Y < minSizeForComplexMesh && size.Z < minSizeForComplexMesh)
                        {
#if SPAM
                            m_log.Debug("Meshmerizer: prim " + primName + " has a size of " + size.ToString() + " which is below threshold of " +
                                        minSizeForComplexMesh.ToString() + " - creating simple bounding box");
#endif
                            mesh = CreateBoundingBoxMesh(mesh);
                            mesh.DumpRaw(baseDir, primName, "Z extruded");
                        }

                        // trim the vertex and triangle lists to free up memory
                        mesh.TrimExcess();

                        if (shouldCache)
                        {
                            m_uniqueMeshes.Add(key, mesh);
                            m_uniqueMeshesHulls.Add(key, hulls);
                            m_uniqueMeshesBoundingHulls.Add(key, boundingHull);
                        }
                    }
                }
                finally
                {
                    m_uniqueMeshesRwLock.DowngradeFromWriterLock(ref lc);
                }
            }
            finally
            {
                m_uniqueMeshesRwLock.ReleaseReaderLock();
            }
            return(mesh);
        }