/// <summary>
        /// Prompts to save a Timeline of the current session recording and add it to the current synthetic environment
        /// </summary>
        public void TrySaveSyntheticRecording()
        {
            var simulationSettings = SimulationSettings.instance;

            if (simulationSettings.EnvironmentMode != EnvironmentMode.Synthetic)
            {
                return;
            }

            var environmentPrefab = simulationSettings.EnvironmentPrefab;
            var defaultName       = $"{environmentPrefab.name} Recording";
            var recordingPath     = EditorUtility.SaveFilePanelInProject("Save Recording", defaultName, "playable", "");

            if (recordingPath.Length == 0)
            {
                return;
            }

            var recordingInfo = SessionRecordingUtils.CreateSessionRecordingAsset(recordingPath);

            AddDataFromRecordingSession(recordingInfo);
            recordingInfo.AddSyntheticEnvironment(environmentPrefab);
            AssetDatabase.SaveAssets();

            m_CurrentSyntheticRecordings.Add(recordingInfo);
            if (m_SyntheticRecordingsMap.TryGetValue(environmentPrefab, out var recordings))
            {
                recordings.Add(recordingInfo);
            }
            else
            {
                m_SyntheticRecordingsMap[environmentPrefab] = new List <SessionRecordingInfo> {
                    recordingInfo
                }
            };

            var newOptionsCount   = SyntheticRecordingOptionContents.Length + 1;
            var newOptionContents = new GUIContent[newOptionsCount];

            SyntheticRecordingOptionContents.CopyTo(newOptionContents, 0);
            newOptionContents[newOptionsCount - 1] = new GUIContent(recordingInfo.DisplayName);
            SyntheticRecordingOptionContents       = newOptionContents;
        }
Beispiel #2
0
        public static void CreateSessionRecordingFromVideoClip()
        {
            var videoClip      = Selection.activeObject as VideoClip;
            var pathAtSelected = GetPathAtSelected();
            var assetPath      = AssetDatabase.GenerateUniqueAssetPath($"{pathAtSelected}{videoClip.name}.{k_PlayableFileTypeName}");
            var recordingInfo  = SessionRecordingUtils.CreateSessionRecordingAsset(assetPath);

            recordingInfo.DefaultExtrapolationMode = DirectorWrapMode.Loop;
            var timeline     = recordingInfo.Timeline;
            var videoTrack   = timeline.CreateTrack <MarsVideoPlayableTrack>(null, "Video");
            var timelineClip = videoTrack.CreateClip <MarsVideoPlayableAsset>();

            timelineClip.displayName = videoClip.name;
            var videoPlayableAsset = timelineClip.asset as MarsVideoPlayableAsset;

            timelineClip.duration        = videoClip.length;
            videoPlayableAsset.VideoClip = videoClip;
            AssetDatabase.SaveAssets();
            ProjectWindowUtil.ShowCreatedAsset(timeline);
        }
        static IEnumerator ImportDataRecording(this IUsesCloudStorage storageUser, string recordingPath,
                                               string recordingKey, string recordingName, string jsonText, Stack <RequestHandle> requests,
                                               Action <UnityObject> callback)
        {
            k_DataRecordings.Clear();
            k_NewAssets.Clear();

            var recording = SceneSerialization.FromJson <CompanionDataRecording>(jsonText);

            var recordingInfo = SessionRecordingUtils.CreateSessionRecordingAsset(recordingPath);
            var timeline      = recordingInfo.Timeline;

            recordingInfo.name            = recordingName + " Info";
            recordingInfo.CloudResourceId = recordingKey;

            var resourceCount = 0;

            if (recording.HasCameraPath)
            {
                resourceCount++;
                requests.Push(storageUser.DownloadCameraPath(recordingKey, (success, list) =>
                {
                    if (success && list != null && list.Count > 0)
                    {
                        k_CameraPoseRecorder.PoseEvents = list;
                        var dataRecording = k_CameraPoseRecorder.TryCreateDataRecording(timeline, k_NewAssets);
                        if (dataRecording != null)
                        {
                            k_DataRecordings.Add(dataRecording);
                        }
                    }

                    // ReSharper disable once AccessToModifiedClosure
                    resourceCount--;
                }));
            }

            if (recording.HasPlaneData)
            {
                resourceCount++;
                requests.Push(storageUser.DownloadPlaneData(recordingKey, (success, list) =>
                {
                    if (success && list != null && list.Count > 0)
                    {
                        k_PlaneFindingRecorder.PlaneEvents = list;
                        var dataRecording = k_PlaneFindingRecorder.TryCreateDataRecording(timeline, k_NewAssets);
                        if (dataRecording != null)
                        {
                            k_DataRecordings.Add(dataRecording);
                        }
                    }

                    resourceCount--;
                }));
            }

            if (recording.HasPointCloud)
            {
                resourceCount++;
                requests.Push(storageUser.DownloadPointCloud(recordingKey, (success, list) =>
                {
                    if (success && list != null && list.Count > 0)
                    {
                        k_PointCloudRecorder.PointCloudEvents = list;
                        var dataRecording = k_PointCloudRecorder.TryCreateDataRecording(timeline, k_NewAssets);
                        if (dataRecording != null)
                        {
                            k_DataRecordings.Add(dataRecording);
                        }
                    }

                    resourceCount--;
                }));
            }

            while (resourceCount > 0)
            {
                yield return(null);
            }

            foreach (var dataRecording in k_DataRecordings)
            {
                recordingInfo.AddDataRecording(dataRecording);
                dataRecording.name = dataRecording.GetType().Name;
                AssetDatabase.AddObjectToAsset(dataRecording, timeline);
            }

            foreach (var newAsset in k_NewAssets)
            {
                AssetDatabase.AddObjectToAsset(newAsset, timeline);
            }

            AssetDatabase.SaveAssets();
            ProjectWindowUtil.ShowCreatedAsset(timeline);

            k_DataRecordings.Clear();
            k_NewAssets.Clear();

            if (!recording.HasVideo)
            {
                FinalizeDataRecordingImport(timeline, recordingKey, recordingPath, callback);
                yield break;
            }

            var videoPath = EditorUtility.SaveFilePanelInProject(k_SaveVideoDialogTitle, recordingName, k_VideoExtension, string.Empty);

            if (string.IsNullOrEmpty(recordingPath))
            {
                FinalizeDataRecordingImport(timeline, recordingKey, recordingPath, callback);
                yield break;
            }

            // TODO: Progress bar
            requests.Push(storageUser.DownloadVideo(recordingKey, (success, path) =>
            {
                try
                {
                    AssetDatabase.StartAssetEditing();

                    if (File.Exists(path))
                    {
                        var destinationPath = Path.Combine(Directory.GetCurrentDirectory(), videoPath);
                        if (File.Exists(destinationPath))
                        {
                            File.Delete(destinationPath);
                        }

                        File.Move(path, destinationPath);
                    }
                    else
                    {
                        Debug.LogError($"Couldn't find temp video file at path {path}");
                    }
                }
                catch (IOException e)
                {
                    Debug.LogException(e);
                }
                finally
                {
                    AssetDatabase.StopAssetEditing();
                }

                AssetDatabase.Refresh();

                var videoClip                      = AssetDatabase.LoadAssetAtPath <VideoClip>(videoPath);
                var videoTrack                     = timeline.CreateTrack <MarsVideoPlayableTrack>(null, k_DefaultVideoTrackName);
                var timelineClip                   = videoTrack.CreateClip <MarsVideoPlayableAsset>();
                timelineClip.displayName           = videoClip.name;
                timelineClip.duration              = videoClip.length;
                var videoPlayableAsset             = (MarsVideoPlayableAsset)timelineClip.asset;
                videoPlayableAsset.VideoClip       = videoClip;
                videoPlayableAsset.FacingDirection = CameraFacingDirection.World;
                timeline.editorSettings.fps        = (float)videoClip.frameRate;

                var videoHalfHeight            = videoClip.height * 0.5f;
                var halfFOV                    = recording.fovRadians * 0.5f;
                videoPlayableAsset.FocalLength = videoHalfHeight / Mathf.Tan(halfFOV);

                videoPlayableAsset.ZRotation = recording.zRotation;
            }));

            FinalizeDataRecordingImport(timeline, recordingKey, recordingPath, callback);
        }