/// <summary> /// Read text file asynchronously /// </summary> public static AsyncRequest <string> ReadFileAsync(string path) { var request = new AsyncRequestThreaded <string>(() => File.ReadAllText(path)); AvatarSdkMgr.SpawnCoroutine(request.Await()); return(request); }
/// <summary> /// Parsing .ply-encoded 3D points (e.g. "haircut point cloud"). /// </summary> public static AsyncRequest <Vector3[]> PlyToPointsAsync(byte[] plyBytes) { var request = new AsyncRequestThreaded <Vector3[]> (() => { Vector3[] points; PlyReader.ReadPointCloudFromPly(plyBytes, out points); return(points); }, AvatarSdkMgr.Str(Strings.ParsingPoints)); AvatarSdkMgr.SpawnCoroutine(request.Await()); return(request); }
/// <summary> /// Some of the files involved in avatar generation (e.g. textures) may be large. This function helps to /// work around this by saving file in a separate thread, thus not blocking the main thread. /// </summary> /// <param name="bytes">Binary file content.</param> /// <param name="path">Full absolute path.</param> public static AsyncRequest <string> SaveFileAsync(byte[] bytes, string path) { var request = new AsyncRequestThreaded <string> (() => { Directory.CreateDirectory(Path.GetDirectoryName(path)); File.WriteAllBytes(path, bytes); return(path); }); request.State = AvatarSdkMgr.Str(Strings.SavingFiles); AvatarSdkMgr.SpawnCoroutine(request.Await()); return(request); }
/// <summary> /// Parsing .ply data asynchronously into Unity mesh data (vertices, triangles, etc.) /// </summary> /// <param name="plyBytes">Binary content of .ply file.</param> public static AsyncRequest <MeshData> PlyToMeshDataAsync(byte[] plyBytes) { var request = new AsyncRequestThreaded <MeshData> (() => { var meshData = new MeshData(); PlyReader.ReadMeshDataFromPly( plyBytes, out meshData.vertices, out meshData.triangles, out meshData.uv, out meshData.indexMap ); return(meshData); }, AvatarSdkMgr.Str(Strings.ParsingMeshData)); AvatarSdkMgr.SpawnCoroutine(request.Await()); return(request); }
/// <summary> /// Unzips the file asynchronously. /// </summary> /// <param name="path">Absolute path to zip file.</param> /// <param name="location">Unzip location. If null, then files will be unzipped in the location of .zip file.</param> public static AsyncRequest <string> UnzipFileAsync(string path, string location = null) { if (string.IsNullOrEmpty(location)) { location = Path.GetDirectoryName(path); } AsyncRequest <string> request = null; Func <string> unzipFunc = () => { ZipUtils.Unzip(path, location); return(location); }; // unzip asynchronously in a separate thread request = new AsyncRequestThreaded <string> (() => unzipFunc(), AvatarSdkMgr.Str(Strings.UnzippingFile)); AvatarSdkMgr.SpawnCoroutine(request.Await()); return(request); }
/// <summary> /// Read blendshapes from the avatar directory and add them to 3D head mesh. /// </summary> private static IEnumerator AddBlendshapes(string avatarId, Mesh mesh, int[] indexMap, AsyncRequest <Mesh> request) { var blendshapesDirs = AvatarSdkMgr.Storage().GetAvatarBlendshapesDirs(avatarId); var loadBlendshapesRequest = new AsyncRequestThreaded <Dictionary <string, Vector3[]> > ((r) => { var timer = new MeasureTime("Read all blendshapes"); var blendshapes = new Dictionary <string, Vector3[]> (); List <string> blendshapeFiles = new List <string>(); foreach (string dir in blendshapesDirs) { blendshapeFiles.AddRange(Directory.GetFiles(dir)); } var blendshapeReader = new BlendshapeReader(indexMap); for (int i = 0; i < blendshapeFiles.Count; ++i) { var blendshapePath = blendshapeFiles [i]; var filename = Path.GetFileName(blendshapePath); // crude parsing of filenames if (!filename.EndsWith(".bin")) { continue; } var tokens = filename.Split(new [] { ".bin" }, StringSplitOptions.None); if (tokens.Length != 2) { continue; } var blendshapeName = tokens [0]; blendshapes [blendshapeName] = blendshapeReader.ReadVerticesDeltas(blendshapePath); r.Progress = (float)i / blendshapeFiles.Count; } timer.Stop(); return(blendshapes); }, AvatarSdkMgr.Str(Strings.ParsingBlendshapes)); yield return(request.AwaitSubrequest(loadBlendshapesRequest, finalProgress: 0.9f)); if (request.IsError) { yield break; } var addBlendshapesTimer = DateTime.Now; float targetFps = 30.0f; int numBlendshapes = 0, loadedSinceLastPause = 0; var blendshapesDict = loadBlendshapesRequest.Result; foreach (var blendshape in blendshapesDict) { mesh.AddBlendShapeFrame(blendshape.Key, 100.0f, blendshape.Value, null, null); ++numBlendshapes; ++loadedSinceLastPause; if ((DateTime.Now - addBlendshapesTimer).TotalMilliseconds > 1000.0f / targetFps && loadedSinceLastPause >= 5) { // Debug.LogFormat ("Pause after {0} blendshapes to avoid blocking the main thread", numBlendshapes); yield return(null); addBlendshapesTimer = DateTime.Now; loadedSinceLastPause = 0; } } request.Result = mesh; request.IsDone = true; }