Пример #1
0
        /// <summary>
        /// Saves a serialized scene from Scene Understanding to disk as obj files. One obj file is saved per class, i.e. one file for walls, one file for ceilings, etc.
        /// </summary>
        /// <param name="serializedScene">Serialized scene.</param>
        /// <returns>Task.</returns>
        public static async Task SaveObjsToDiskAsync(byte[] serializedScene)
        {
            if (serializedScene == null)
            {
                Logger.LogWarning("SceneUnderstandingSaveDataUtils.SaveObjsToDisk: Nothing to save.");
                return;
            }

            // Deserialize the scene.
            SceneUnderstanding.Scene scene = SceneUnderstanding.Scene.Deserialize(serializedScene);

            // List of all SceneObjectKind enum values.
            List <SceneUnderstanding.SceneObjectKind> sceneObjectKinds = Enum.GetValues(typeof(SceneUnderstanding.SceneObjectKind)).Cast <SceneUnderstanding.SceneObjectKind>().ToList();

            List <Task> tasks = new List <Task>();

            foreach (SceneUnderstanding.SceneObjectKind soKind in sceneObjectKinds)
            {
                tasks.Add(SaveSceneObjectsAsObjAsync(
                              scene.SceneObjects.Where <SceneUnderstanding.SceneObject>(so => so.Kind == soKind),
                              SceneUnderstandingUtils.GetColorForLabel(soKind) == null ? Color.black : SceneUnderstandingUtils.GetColorForLabel(soKind).Value,
                              string.Format("{0}_{1}", GetDefaultFileName(), soKind.ToString())));
            }
            await Task.WhenAll(tasks);
        }
        /// <summary>
        /// Initialization.
        /// </summary>
        private void Start()
        {
            SUDataProvider = SUDataProvider == null?gameObject.GetComponent <SceneUnderstandingDataProvider>() : SUDataProvider;

            SUUtils = SUUtils == null?gameObject.GetComponent <SceneUnderstandingUtils>() : SUUtils;

            SceneRoot = SceneRoot == null?SUUtils.CreateGameObject("SceneRoot", null) : SceneRoot;

            SceneObjectMeshMaterial = SceneObjectMeshMaterial == null?Resources.Load <Material>("Materials/SceneObjectMesh") : SceneObjectMeshMaterial;

            SceneObjectQuadMaterial = SceneObjectQuadMaterial == null?Resources.Load <Material>("Materials/SceneObjectQuad") : SceneObjectQuadMaterial;

            SceneObjectWireframeMaterial = SceneObjectWireframeMaterial == null?Resources.Load <Material>("Materials/WireframeTransparent") : SceneObjectWireframeMaterial;

            WorldMeshMaterial = WorldMeshMaterial == null?Resources.Load <Material>("Materials/WireframeTransparent") : WorldMeshMaterial;

            LabelFont  = LabelFont == null ? (Font)Resources.GetBuiltinResource(typeof(Font), "Arial.ttf") : LabelFont;
            StatusText = StatusText == null?GameObject.Find("StatusText").GetComponent <UITextDisplay>() : StatusText;

            // To ensure that the first update, as part of auto refresh, happens immediately.
            _timeElapsedSinceLastAutoRefresh = AutoRefreshIntervalInSeconds;
        }
        /// <summary>
        /// Displays one individual scene object.
        /// </summary>
        /// <param name="sceneObject">Scene Object to display.</param>
        private bool DisplaySceneObject(SceneUnderstanding.SceneObject sceneObject)
        {
            try
            {
                if (sceneObject == null)
                {
                    Logger.LogWarning("SceneUnderstandingDisplayManager.DisplaySceneObject: Scene Object is null.");
                    return(false);
                }

                // Skip the object, if the setting to display that object is set to false.
                if ((RenderSceneObjects == false && sceneObject.Kind != SceneUnderstanding.SceneObjectKind.World) ||
                    (RenderWorldMesh == false && sceneObject.Kind == SceneUnderstanding.SceneObjectKind.World) ||
                    (RenderPlatformSceneObjects == false && sceneObject.Kind == SceneUnderstanding.SceneObjectKind.Platform) ||
                    (RenderBackgroundSceneObjects == false && sceneObject.Kind == SceneUnderstanding.SceneObjectKind.Background) ||
                    (RenderUnknownSceneObjects == false && sceneObject.Kind == SceneUnderstanding.SceneObjectKind.Unknown) ||
                    (RenderCompletelyInferredSceneObjects == false && sceneObject.Kind == SceneUnderstanding.SceneObjectKind.CompletelyInferred))
                {
                    return(false);
                }

                // Create a game object for the scene object, parent it to the root and set it's transform.
                GameObject soGO = SUUtils.CreateGameObject(sceneObject.Kind.ToString(), SceneRoot.transform);
                TransformUtils.SetUnityTransformFromMatrix4x4(TransformUtils.ConvertRightHandedMatrix4x4ToLeftHanded(sceneObject.GetLocationAsMatrix()), soGO.transform, true);

                // This is the new child game object that will contain the meshes, quads, etc.
                GameObject soChildGO = null;

                // Render the world mesh.
                if (sceneObject.Kind == SceneUnderstanding.SceneObjectKind.World)
                {
                    // Get the meshes from the SU API.
                    IEnumerable <SceneUnderstanding.SceneMesh> meshes = sceneObject.Meshes;

                    // Combine all the world meshes into one unity mesh.
                    Mesh unityMesh = SUUtils.GenerateUnityMeshForSceneObjectMeshes(meshes);

                    // Create a game object with the above unity mesh.
                    soChildGO = SUUtils.CreateGameObjectWithMeshComponents(sceneObject.Kind.ToString(), soGO.transform, unityMesh, WorldMeshMaterial, null);
                }
                // Render all other scene objects.
                else
                {
                    Color?color = SceneUnderstandingUtils.GetColorForLabel(sceneObject.Kind);

                    switch (SceneObjectVisualizationMode)
                    {
                    case VisualizationMode.Quad:
                    case VisualizationMode.QuadWithMask:
                    {
                        // Get the quads from the SU API.
                        IEnumerable <SceneUnderstanding.SceneQuad> quads = sceneObject.Quads;

                        // For each quad, generate the unity mesh, create the game object and apply the invalidation mask, if applicable.
                        foreach (SceneUnderstanding.SceneQuad quad in quads)
                        {
                            // Generate the unity mesh for the quad.
                            Mesh unityMesh = SUUtils.GenerateUnityMeshForSceneObjectQuad(quad);

                            // Create a game object with the above unity mesh.
                            soChildGO = SUUtils.CreateGameObjectWithMeshComponents(
                                sceneObject.Kind.ToString(),
                                soGO.transform,
                                unityMesh,
                                SceneObjectVisualizationMode == VisualizationMode.QuadWithMask ? SceneObjectQuadMaterial : SceneObjectMeshMaterial,
                                color);

                            // Apply the invalidation mask.
                            if (SceneObjectVisualizationMode == VisualizationMode.QuadWithMask)
                            {
                                SUUtils.ApplyQuadRegionMask(quad, soChildGO, color);
                            }
                        }
                    }
                    break;

                    case VisualizationMode.Mesh:
                    case VisualizationMode.Wireframe:
                    {
                        // Get the meshes from the SU API.
                        IEnumerable <SceneUnderstanding.SceneMesh> meshes = sceneObject.Meshes;

                        foreach (SceneUnderstanding.SceneMesh mesh in meshes)
                        {
                            // Generate the unity mesh for the Scene Understanding mesh.
                            Mesh unityMesh = SUUtils.GenerateUnityMeshForSceneObjectMesh(mesh);

                            // Create a game object with the above unity mesh.
                            soChildGO = SUUtils.CreateGameObjectWithMeshComponents(
                                sceneObject.Kind.ToString(),
                                soGO.transform,
                                unityMesh,
                                SceneObjectVisualizationMode == VisualizationMode.Mesh ? SceneObjectMeshMaterial : SceneObjectWireframeMaterial,
                                SceneObjectVisualizationMode == VisualizationMode.Mesh ? color : null);
                        }
                    }
                    break;

                    default:
                        break;
                    }
                }

                if (DisplayTextLabels)
                {
                    // Only add text for the labels below.
                    if (sceneObject.Kind == SceneUnderstanding.SceneObjectKind.Ceiling ||
                        sceneObject.Kind == SceneUnderstanding.SceneObjectKind.Floor ||
                        sceneObject.Kind == SceneUnderstanding.SceneObjectKind.Platform ||
                        sceneObject.Kind == SceneUnderstanding.SceneObjectKind.Wall)
                    {
                        SUUtils.AddTextLabel(soChildGO, sceneObject.Kind.ToString(), LabelFont);
                    }
                }

                // When running on device, add a worldanchor component to keep the scene object aligned to the real world.
                // When running on PC, add a boxcollider component, that is used for the 'Focus' functionality (in CameraMovement.cs).
                if (SUDataProvider.RunOnDevice)
                {
                    soGO.AddComponent <UnityEngine.XR.WSA.WorldAnchor>();
                }
                else
                {
                    soGO.AddComponent <BoxCollider>();
                }
            }
            catch (Exception e)
            {
                Logger.LogException(e);
            }
            return(true);
        }