Пример #1
0
        protected override void Awake()
        {
            base.Awake();

            ObserverState = ObserverStates.Stopped;

            if (Test)
            {
                // create dummy SurfaceObject
                List <Vector3> testVerts = new List <Vector3>();
                testVerts.Add(new Vector3(-0.5f, -0.5f, -0.5f));
                testVerts.Add(new Vector3(0, 0, 0));
                testVerts.Add(new Vector3(0.5f, 0.5f, 0.5f));
                Mesh testMesh = new Mesh();
                testMesh.SetVertices(testVerts);
                SurfaceObject testSO = CreateSurfaceObject(testMesh, "test", gameObject.transform);
                testSO.Renderer.transform.position = new Vector3(1, 1, 1);

                // create matching dummy SurfacePoints
                Transform      tmpTransform = testSO.Renderer.transform;
                List <Vector3> tmpWvertices = testSO.Filter.sharedMesh.vertices.ToList();
                int            tmpTriCount  = testSO.Filter.sharedMesh.triangles.ToList().Count;
                for (int i = 0; i < tmpWvertices.Count; i++)
                {
                    tmpWvertices[i] = tmpTransform.TransformPoint(tmpWvertices[i]);
                }
                SurfacePoints extras = new SurfacePoints(
                    MeshManager.IntersectionPoints(testSO.Renderer.bounds), tmpWvertices,
                    tmpTriCount, testSO.Renderer.bounds);

                // Log
                UpdateOrAddSurfaceObject(testSO, extras);
            }
        }
Пример #2
0
    /// <summary>
    /// ID's all of SpatialMappingObserver's store meshes as visible or not, updates class metadata.
    /// Renders mesh bounds if VisualizeBounds.
    /// </summary>
    public List <bool> UpdateVertices(Intersector.Frustum SensorView)
    {
        Density = observer.Density;

        // Adjust Sensor.FOV by specified factor...
        FOV            = new Intersector.ViewVector(FOVFactor * SensorView.FOV.Phi, FOVFactor * SensorView.FOV.Phi);
        SensorView.FOV = FOV;

        // create fresh lists, metadata
        List <bool> visiblilty             = new List <bool>();
        List <Visualizer.Content> toRender = new List <Visualizer.Content>();

        TotalMeshCount     = 0;
        TotalTriangleCount = 0;
        TotalVertexCount   = 0;
        MeshesInView       = 0;
        TrianglesInView    = 0;
        VerticesInView     = 0;

        // add colors if necessary
        while (VB && BoundColors.Count < observer.SurfaceObjects.Count)
        {
            BoundColors.Add(Visualizer.RandomColor(BoundsColor1, BoundsColor2));
        }

        // check meshes for visiblity
        for (int i = 0; i < observer.ExtraData.Count; i++)
        {
            SurfacePoints extras    = observer.ExtraData[i];
            bool          isVisible = MeshInter.AnyInView(extras.IntersectPts, SensorView);

            visiblilty.Add(isVisible);

            if (isVisible)
            {
                // update metadata
                MeshesInView++;
                TrianglesInView += extras.TriangleCount;
                VerticesInView  += extras.Wvertices.Count;

                // setup bounding box visualization
                if (VB)
                {
                    toRender.AddRange(Visualizer.CreateMarkers(extras.IntersectPts, MarkerSize, BoundColors[i]));
                    toRender.AddRange(Visualizer.CreateBoundingLines(extras.BoundsBox, LineSize, BoundColors[i]));
                }
            }
            // update metadata totals
            TotalMeshCount++;
            TotalTriangleCount += extras.TriangleCount;
            TotalVertexCount   += extras.Wvertices.Count;
        }

        if (VB)
        {
            BoundsVis.Visualize(toRender);
        }

        return(visiblilty);
    }
Пример #3
0
        /// <summary>
        /// Update the first surface with a matching ID if one exists in <see cref="SurfaceObjects"/>, otherwise add the surface as new.
        /// </summary>
        /// <param name="toUpdateOrAdd">The surface to be updated or added.</param>
        /// <param name="destroyGameObjectIfReplaced">If a surface is updated, and a game object is being replaced, pass true to destroy the outgoing game object or false otherwise.</param>
        /// <param name="destroyMeshesIfReplaced">If a surface is updated, and new meshes are replacing old meshes, pass true to destroy the outgoing meshes or false otherwise.</param>
        /// <returns>The surface object that was updated or null if one was not found meaning a new surface was added.</returns>
        protected SurfaceObject?UpdateOrAddSurfaceObject(SurfaceObject toUpdateOrAdd, SurfacePoints extras,
                                                         bool destroyGameObjectIfReplaced = true, bool destroyMeshesIfReplaced = true)
        {
            /*
             * // debug
             * Debug.Log(string.Format("Attempting to Add/Update surface to SMS. Wvertices Length: {0}, TriCount: {1}. " +
             *  "Currently, SurfaceObject Count: {2}, ExtraData Count: {3}",
             *  extras.Wvertices.Count, extras.TriangleCount, SurfaceObjects.Count, ExtraData.Count));
             */

            SurfaceObject?replaced = null;

            // verify correct material
            toUpdateOrAdd.Renderer.sharedMaterial = RenderMaterial;

            for (int iSurface = 0; iSurface < surfaceObjectsWriteable.Count; iSurface++)
            {
                SurfaceObject existing = surfaceObjectsWriteable[iSurface];

                if (existing.ID == toUpdateOrAdd.ID)
                {
                    surfaceObjectsWriteable[iSurface] = toUpdateOrAdd;
                    ExtraData[iSurface] = extras;

                    var handlers = SurfaceUpdated;
                    if (handlers != null)
                    {
                        handlers(this, DataEventArgs.Create(new SurfaceUpdate {
                            Old = existing, New = toUpdateOrAdd
                        }));
                    }

                    CleanUpSurface(
                        existing,
                        destroyGameObjectIfReplaced,
                        destroyMeshesIfReplaced,
                        objectToPreserve: toUpdateOrAdd.Object,
                        meshToPreserveA: toUpdateOrAdd.Filter.sharedMesh,
                        meshToPreserveB: toUpdateOrAdd.Collider.sharedMesh
                        );

                    replaced = existing;
                    break;
                }
            }

            if (replaced == null)
            {
                AddSurfaceObject(toUpdateOrAdd, extras);
            }

            return(replaced);
        }
Пример #4
0
        /// <summary>
        /// Add the surface to <see cref="SurfaceObjects"/>.
        /// </summary>
        /// <param name="toAdd">The surface to add.</param>
        protected void AddSurfaceObject(SurfaceObject toAdd, SurfacePoints extras)
        {
            surfaceObjectsWriteable.Add(toAdd);
            ExtraData.Add(extras);

            var handlers = SurfaceAdded;

            if (handlers != null)
            {
                handlers(this, DataEventArgs.Create(toAdd));
            }
        }
        protected override void Awake()
        {
            base.Awake();

            ObserverState = ObserverStates.Stopped;
            density       = DefaultTrianglesPerCubicMeter;

            // debug
            StartObserving();

            if (Test)
            {
                // create dummy SurfaceObject
                List <Vector3> testVerts = new List <Vector3>();
                testVerts.Add(new Vector3(-0.5f, -0.5f, -0.5f));
                testVerts.Add(new Vector3(0, 0, 0));
                testVerts.Add(new Vector3(0.5f, 0.5f, 0.5f));
                Mesh testMesh = new Mesh();
                testMesh.SetVertices(testVerts);
                SurfaceObject testSO1 = CreateSurfaceObject(testMesh, "test", gameObject.transform, 0);
                testSO1.Renderer.transform.position = new Vector3(1, 1, 1);
                SurfaceObject testSO2 = CreateSurfaceObject(testMesh, "test", gameObject.transform, 1);
                testSO2.Renderer.transform.position = new Vector3(-1, -1, -1);

                // create matching dummy SurfacePoints: 1
                Transform      tmpTransform1 = testSO1.Renderer.transform;
                List <Vector3> tmpWvertices1 = testSO1.Filter.sharedMesh.vertices.ToList();
                int            tmpTriCount1  = testSO1.Filter.sharedMesh.triangles.ToList().Count;
                for (int i = 0; i < tmpWvertices1.Count; i++)
                {
                    tmpWvertices1[i] = tmpTransform1.TransformPoint(tmpWvertices1[i]);
                }
                SurfacePoints extras1 = new SurfacePoints(
                    MeshManager.IntersectionPoints(testSO1.Renderer.bounds), tmpWvertices1,
                    tmpTriCount1, testSO1.Renderer.bounds, Density);
                // create matching dummy SurfacePoints: 2
                Transform      tmpTransform2 = testSO2.Renderer.transform;
                List <Vector3> tmpWvertices2 = testSO2.Filter.sharedMesh.vertices.ToList();
                int            tmpTriCount2  = testSO2.Filter.sharedMesh.triangles.ToList().Count;
                for (int i = 0; i < tmpWvertices2.Count; i++)
                {
                    tmpWvertices2[i] = tmpTransform2.TransformPoint(tmpWvertices2[i]);
                }
                SurfacePoints extras2 = new SurfacePoints(
                    MeshManager.IntersectionPoints(testSO2.Renderer.bounds), tmpWvertices2,
                    tmpTriCount1, testSO2.Renderer.bounds, Density);

                // Log
                UpdateOrAddSurfaceObject(testSO1, extras1);
                UpdateOrAddSurfaceObject(testSO2, extras2);
            }
        }
Пример #6
0
    /// <summary>
    /// ID's all of SpatialMappingObserver's store meshes as visible or not, updates class metadata.
    /// Renders mesh bounds if VisualizeBounds.
    /// </summary>
    public List <bool> UpdateVertices(Intersector.Frustum SensorView)
    {
        Density = observer.Density;

        // Adjust Sensor.FOV by specified factor...
        FOV            = new Intersector.ViewVector(FOVFactor * SensorView.FOV.Phi, FOVFactor * SensorView.FOV.Phi);
        SensorView.FOV = FOV;

        // create fresh lists, metadata
        List <bool> visiblilty = new List <bool>();

        TotalMeshCount     = 0;
        TotalTriangleCount = 0;
        TotalVertexCount   = 0;
        MeshesInView       = 0;
        TrianglesInView    = 0;
        VerticesInView     = 0;

        // check meshes for visiblity
        for (int i = 0; i < observer.ExtraData.Count; i++)
        {
            SurfacePoints extras    = observer.ExtraData[i];
            bool          isVisible = MeshInter.AnyInView(extras.IntersectPts, SensorView);

            visiblilty.Add(isVisible);

            if (isVisible)
            {
                // update metadata
                MeshesInView++;
                TrianglesInView += extras.TriangleCount;
                VerticesInView  += extras.Wvertices.Count;
            }
            // update metadata totals
            TotalMeshCount++;
            TotalTriangleCount += extras.TriangleCount;
            TotalVertexCount   += extras.Wvertices.Count;
        }

        return(visiblilty);
    }
        /// <summary>
        /// Handles the SurfaceObserver's OnDataReady event.
        /// </summary>
        /// <param name="cookedData">Struct containing output data.</param>
        /// <param name="outputWritten">Set to true if output has been written.</param>
        /// <param name="elapsedCookTimeSeconds">Seconds between mesh cook request and propagation of this event.</param>
        private void SurfaceObserver_OnDataReady(SurfaceData cookedData, bool outputWritten, float elapsedCookTimeSeconds)
        {
            if (outstandingMeshRequest == null)
            {
                Debug.LogErrorFormat("Got OnDataReady for surface {0} while no request was outstanding.",
                                     cookedData.id.handle
                                     );

                return;
            }

            if (!IsMatchingSurface(outstandingMeshRequest.Value, cookedData))
            {
                Debug.LogErrorFormat("Got mismatched OnDataReady for surface {0} while request for surface {1} was outstanding.",
                                     cookedData.id.handle,
                                     outstandingMeshRequest.Value.ID
                                     );

                ReclaimSurface(outstandingMeshRequest.Value);
                outstandingMeshRequest = null;

                return;
            }

            if (ObserverState != ObserverStates.Running)
            {
                Debug.LogFormat("Got OnDataReady for surface {0}, but observer was no longer running.",
                                cookedData.id.handle
                                );

                ReclaimSurface(outstandingMeshRequest.Value);
                outstandingMeshRequest = null;

                return;
            }

            if (!outputWritten)
            {
                ReclaimSurface(outstandingMeshRequest.Value);
                outstandingMeshRequest = null;

                return;
            }

            Debug.Assert(outstandingMeshRequest.Value.Object.activeSelf);

            // alters properites of mesh as desired: first sets renderer enabled field
            outstandingMeshRequest.Value.Renderer.enabled = SpatialMappingManager.Instance.DrawVisualMeshes;

            // create extra data
            SurfaceObject tmpSO        = outstandingMeshRequest.Value;
            Transform     tmpTransform = tmpSO.Renderer.transform;
            // Wvertices
            List <Vector3> tmpWvertices = tmpSO.Filter.sharedMesh.vertices.ToList();

            for (int i = 0; i < tmpWvertices.Count; i++)
            {
                tmpWvertices[i] = tmpTransform.TransformPoint(tmpWvertices[i]);
            }
            // TriCount
            int tmpTriCount = tmpSO.Filter.sharedMesh.triangles.ToList().Count;
            // SurfacePoints obj
            SurfacePoints extras = new SurfacePoints(
                MeshManager.IntersectionPoints(outstandingMeshRequest.Value.Renderer.bounds), tmpWvertices,
                tmpTriCount, tmpSO.Renderer.bounds, cookedData.trianglesPerCubicMeter);

            // updates/adds mesh in source stored list of SurfaceObjects.
            SurfaceObject?replacedSurface = UpdateOrAddSurfaceObject(outstandingMeshRequest.Value, extras, destroyGameObjectIfReplaced: false);

            outstandingMeshRequest = null;

            if (replacedSurface != null)
            {
                ReclaimSurface(replacedSurface.Value);
            }
        }