コード例 #1
0
 /// <summary>
 /// Adds new bounding objects to the end of the Queue.
 /// </summary>
 /// <param name="boundingObjects">Collection of GameObjects which define the bounds where spatial mesh vertices should be removed.</param>
 private void AddBoundingObjectsToQueue(IEnumerable <GameObject> boundingObjects)
 {
     foreach (GameObject item in boundingObjects)
     {
         BoxCollider boxCollider = item.GetComponent <BoxCollider>();
         if (boxCollider != null)
         {
             // Expand the bounds, if requested.
             if (BoundsExpansion > 0.0f)
             {
                 Debug.Log("Bounds expansion in world coordinates not yet supported!");
             }
             WorldSpaceBounds bounds = new WorldSpaceBounds(boxCollider);
             boundingObjectsQueue.Enqueue(bounds);
         }
     }
 }
コード例 #2
0
    /// <summary>
    /// Iterator block, analyzes surface meshes to find vertices existing within the bounds of any boundingObject and removes them.
    /// </summary>
    /// <returns>Yield result.</returns>
    private IEnumerator RemoveSurfaceVerticesWithinBoundsRoutine()
    {
        List <MeshFilter> meshFilters = HoloToolkit.Unity.SpatialMapping.SpatialMappingManager.Instance.GetMeshFilters();
        float             start       = Time.realtimeSinceStartup;

        while (boundingObjectsQueue.Count > 0)
        {
            // Get the current boundingObject.
            WorldSpaceBounds worldSpaceBounds = boundingObjectsQueue.Dequeue();

            foreach (MeshFilter filter in meshFilters)
            {
                // Since this is amortized across frames, the filter can be destroyed by the time
                // we get here.
                if (filter == null)
                {
                    continue;
                }

                Mesh mesh = filter.sharedMesh;

                /*
                 * if (mesh != null && !worldSpaceBounds.AABBIntersects(mesh.bounds))
                 * {
                 * // We don't need to do anything to this mesh, move to the next one.
                 * continue;
                 * }
                 */

                // Remove vertices from any mesh that intersects with the bounds.
                Vector3[]  verts         = mesh.vertices;
                List <int> vertsToRemove = new List <int>();

                // Find which mesh vertices are within the bounds.
                for (int i = 0; i < verts.Length; ++i)
                {
                    if (worldSpaceBounds.ContainsPoint(verts[i]))
                    {
                        // These vertices are within bounds, so mark them for removal.
                        vertsToRemove.Add(i);
                    }

                    // If too much time has passed, we need to return control to the main game loop.
                    if ((Time.realtimeSinceStartup - start) > FrameTime)
                    {
                        // Pause our work here, and continue finding vertices to remove on the next frame.
                        yield return(null);

                        start = Time.realtimeSinceStartup;
                    }
                }

                if (vertsToRemove.Count == 0)
                {
                    // We did not find any vertices to remove, so move to the next mesh.
                    continue;
                }

                // We found vertices to remove, so now we need to remove any triangles that reference these vertices.
                int[]      indices        = mesh.GetTriangles(0);
                List <int> updatedIndices = new List <int>();

                for (int index = 0; index < indices.Length; index += 3)
                {
                    // Each triangle utilizes three slots in the index buffer, check to see if any of the
                    // triangle indices contain a vertex that should be removed.
                    if (vertsToRemove.Contains(indices[index]) ||
                        vertsToRemove.Contains(indices[index + 1]) ||
                        vertsToRemove.Contains(indices[index + 2]))
                    {
                        // Do nothing, we don't want to save this triangle...
                    }
                    else
                    {
                        // Every vertex in this triangle is good, so let's save it.
                        updatedIndices.Add(indices[index]);
                        updatedIndices.Add(indices[index + 1]);
                        updatedIndices.Add(indices[index + 2]);
                    }

                    // If too much time has passed, we need to return control to the main game loop.
                    if ((Time.realtimeSinceStartup - start) > FrameTime)
                    {
                        // Pause our work, and continue making additional planes on the next frame.
                        yield return(null);

                        start = Time.realtimeSinceStartup;
                    }
                }

                if (indices.Length == updatedIndices.Count)
                {
                    // None of the verts to remove were being referenced in the triangle list.
                    continue;
                }

                m_triangles_removed += (indices.Length - updatedIndices.Count) / 3;

                // Update mesh to use the new triangles.
                mesh.SetTriangles(updatedIndices.ToArray(), 0);
                mesh.RecalculateBounds();
                yield return(null);

                start = Time.realtimeSinceStartup;

                // Reset the mesh collider to fit the new mesh.

                /*
                 * MeshCollider collider = filter.gameObject.GetComponent<MeshCollider>();
                 * if (collider != null)
                 * {
                 * collider.sharedMesh = null;
                 * collider.sharedMesh = mesh;
                 * }
                 */
            }
        }

        float pct_removed = 100f * (float)m_triangles_removed / (float)m_total_triangles;

        Debug.Log("Finished removing " + m_triangles_removed + " triangles (" + pct_removed + "%).");

        // We are done removing vertices, trigger an event.
        EventHandler handler = RemoveVerticesComplete;

        if (handler != null)
        {
            handler(this, EventArgs.Empty);
        }

        removingVerts = false;
    }