/// <summary> /// 读取本地的Avatar数据列表 /// </summary> public static AvatarData[] ReadAvtarDatas() { AvatarDebug.Log("ReadAvtarDatas start-----"); List <AvatarData> avatats = new List <AvatarData>(); if (Directory.Exists(AvatarTools.AvatarDataFolderPath)) { DirectoryInfo dir = new DirectoryInfo(AvatarTools.AvatarDataFolderPath); FileInfo[] infos = dir.GetFiles(); for (int i = 0; i < infos.Length; i++) { if (infos[i].Extension == AvatarTools.AvatarDataExtension) { byte[] data = new byte[infos[i].Length]; using (FileStream fs = infos[i].Open(FileMode.Open)) { fs.Read(data, 0, data.Length); } avatats.Add((AvatarData)ByteToObject(data)); } } } else { Directory.CreateDirectory(AvatarTools.AvatarDataFolderPath); } AvatarDebug.Log("ReadAvtarDatas done! sum:" + avatats.Count); return(avatats.ToArray()); }
/// <summary> /// 截取当前摄像机在屏幕中的信息 /// </summary> /// <param name="scale">比例值</param> /// <returns>摄像机图片</returns> public static Texture2D GetTexture(float scale = 1f) { if (camTex != null) { AvatarDebug.Log("camTex getting ---"); Texture2D tex = new Texture2D(Screen.width, Screen.height); tex.ReadPixels(new Rect(0, 0, Screen.width, Screen.height), 0, 0); tex.Apply(); int width = (int)(Screen.width * scale); int height = (int)(Screen.height * scale); Texture2D _tex = new Texture2D(width, height); for (int y = 0; y < height; y++) { for (int x = 0; x < width; x++) { _tex.SetPixel(x, y, tex.GetPixelBilinear((float)x / width, (float)y / height)); } } _tex.Apply(); AvatarDebug.Log("camTex get done!"); return(_tex); } else { AvatarDebug.Log("get camTex? camTex is null!", LogType.Warning); return(null); } }
/// <summary> /// 关闭摄像机拍摄 /// </summary> public static void Close() { if (camTex != null) { camTex.Stop(); Screen.orientation = orientation; camTex = null; AvatarDebug.Log("camTex close done!"); } else { AvatarDebug.Log("close camTex? camTex is null!", LogType.Warning); } }
/// <summary> /// 保存Avatar数据到本地 /// </summary> public static void SaveAvatarData(AvatarData data) { AvatarDebug.Log("SaveAvatarData start-----"); if (!Directory.Exists(AvatarTools.AvatarDataFolderPath)) { Directory.CreateDirectory(AvatarTools.AvatarDataFolderPath); } string path = GetAvatarDataFilePath(data.GUID); using (FileStream fs = File.Open(path, FileMode.OpenOrCreate)) { byte[] _data = ObjectToByte(data); fs.Write(_data, 0, _data.Length); } AvatarDebug.Log("SaveAvatarData done!"); }
public SkinnedMeshRenderer GetSkinnedMesh(Dictionary <string, Transform> bones, SkinnedMeshRenderer skin) { Mesh _mesh = new Mesh(); _mesh.vertices = Vec3.GetVectors(vertices); _mesh.normals = Vec3.GetVectors(normals); _mesh.tangents = Vec4.GetVectors(tangents); _mesh.uv = Vec2.GetVectors(uv); _mesh.boneWeights = BoneWeight_.GetBoneWeights(boneWeights); _mesh.bindposes = Matrix4x4_.GetMatrix4x4s(bindposes); _mesh.triangles = triangles; _mesh.RecalculateBounds(); skin.sharedMesh = _mesh; Texture2D tex = new Texture2D(0, 0); if (texture != null) { tex.LoadImage(texture); } else { tex = null; } skin.material.mainTexture = tex; skin.material.color = color; skin.name = name; skin.rootBone.name = rootBoneName; List <Transform> _bones = new List <Transform>(); for (int i = 0; i < bonesName.Length; i++) { string _name = bonesName[i]; if (bones.ContainsKey(_name)) { _bones.Add(bones[_name]); } else { AvatarDebug.Log("bones name is null!"); } } skin.bones = _bones.ToArray(); return(skin); }
public SkinnedInfo(SkinnedMeshRenderer skin) { Mesh _mesh = skin.sharedMesh; vertices = Vec3.SetVectors(_mesh.vertices); normals = Vec3.SetVectors(_mesh.normals); tangents = Vec4.SetVectors(_mesh.tangents); uv = Vec2.SetVectors(_mesh.uv); boneWeights = BoneWeight_.SetBoneWeights(_mesh.boneWeights); bindposes = Matrix4x4_.SetMatrix4x4s(_mesh.bindposes); triangles = _mesh.triangles; _mesh.RecalculateBounds(); skin.sharedMesh = _mesh; color = skin.material.color; name = skin.name; rootBoneName = skin.rootBone.name; bonesName = new string[skin.bones.Length]; for (int i = 0; i < bonesName.Length; i++) { bonesName[i] = skin.bones[i].name; } Texture2D matTex = (Texture2D)skin.material.mainTexture; if (matTex) { Texture2D tex = new Texture2D(matTex.width, matTex.height); try { tex.SetPixels(matTex.GetPixels()); tex.Apply(); texture = tex.EncodeToPNG(); } catch { texture = null; AvatarDebug.Log("mainTexture is not read/write!"); } } else { texture = null; AvatarDebug.Log("mainTexture is null!"); } }
/// <summary> /// 打开摄像机拍摄功能 /// </summary> /// <param name="id">设备ID</param> public static void Open(int id = 0) { AvatarDebug.Log("camTex opening ---"); if (id < WebCamTexture.devices.Length) { Application.RequestUserAuthorization(UserAuthorization.WebCam); if (Application.HasUserAuthorization(UserAuthorization.WebCam)) { camTex = new WebCamTexture(WebCamTexture.devices[id].name, Screen.height, Screen.width, 24); orientation = Screen.orientation; Screen.orientation = ScreenOrientation.Portrait; camTex.Play(); CamAngle = camTex.videoRotationAngle; AvatarDebug.Log("camTex open done!"); return; } } AvatarDebug.Log("camTex open fail!", LogType.Error); }
public void LoadSkinnedMesh(SkinnedMeshRenderer origin, SkinnedMeshRenderer target) { if (origin && target) { Transform[] bones = new Transform[target.bones.Length]; for (int i = 0; i < bones.Length; i++) { bones[i] = Bones[target.bones[i].name]; } origin.sharedMesh = target.sharedMesh; origin.sharedMaterial = target.sharedMaterial; origin.bones = bones; origin.rootBone = Bones[target.rootBone.name]; } else { AvatarDebug.Log("origin & target SkinnedMeshRenderer is null!", LogType.Error); } }
/// <summary> /// 读取一个Avatar数据 /// </summary> /// <param name="data">Avatar数据</param> public void SetAvatarData(AvatarData data) { Texture2D tex = new Texture2D(0, 0); if (data.FaceImageData != null) { tex.LoadImage(data.FaceImageData); } else { tex = null; } FaceData = new FaceData(transform, Face, data.FaceBonesData, false, tex); SetFaceImage(FaceData.DataImage); Hair = data.Hair.GetSkinnedMesh(Bones, Hair); UpperBody = data.UpperBody.GetSkinnedMesh(Bones, UpperBody); UpperCloth = data.UpperCloth.GetSkinnedMesh(Bones, UpperCloth); LowerBody = data.LowerBody.GetSkinnedMesh(Bones, LowerBody); LowerCloth = data.LowerCloth.GetSkinnedMesh(Bones, LowerCloth); Shoes = data.Shoes.GetSkinnedMesh(Bones, Shoes); StartCoroutine(BoneChange(changeTime)); AvatarDebug.Log("SetAvatarData done!"); }
internal static IEnumerator PostTex(Texture2D tex, PostCallBack func) { if (Application.internetReachability != NetworkReachability.NotReachable) { if (AvatarTools.API_Key != null && AvatarTools.API_Secret != null) { WWWForm form = new WWWForm(); form.AddField("api_key", AvatarTools.API_Key); form.AddField("api_secret", AvatarTools.API_Secret); form.AddBinaryData("image_file", tex.EncodeToPNG()); form.AddField("return_landmark", 1); form.AddField("return_attributes", "gender,age,smiling,glass,headpose"); WWW www = new WWW(FaceURL, form); AvatarDebug.Log("http=>post:start!"); yield return(www); if (www.error != null) { AvatarDebug.Log("http=>post:error!\n" + www.error, LogType.Error); func(false, www.error, tex); } else { AvatarDebug.Log("http=>post:done!\n" + www.text); func(true, www.text, tex); } } else { AvatarDebug.Log("http=>post:fild! API_Key & API_Secret is null!", LogType.Error); } } else { AvatarDebug.Log("http=>post:fild! internet disable!", LogType.Error); } }
private void JsonManager(bool isDone, string json, Texture2D tex) { if (LoadResultEvent != null) { LoadResultEvent(isDone); } if (!isDone) { return; } IJsonFaceData jsonData = new JsonFaceData(AvatarTools.ReadFaceJson(json)); Gender = jsonData.Gender; Age = jsonData.Age; FaceData = new FaceData(transform, Face); Dictionary <string, BoneInfo> dataBone = new JsonFaceDataBone(jsonData); IFaceImageData imageData = new JsonFaceDataImage(jsonData, tex); FaceData = new FaceData(transform, Face, dataBone, true, null); AvatarTools.FaceTexMapper(imageData, FaceData); SetFaceImage(FaceData.DataImage); AvatarDebug.Log("LoadFaceTex done!"); StartCoroutine(BoneChange(changeTime)); }
/// <summary> /// 删除本地的Avatar数据 /// </summary> public static void DeleteAvatarDatas(AvatarData data) { File.Delete(AvatarTools.GetAvatarDataFilePath(data.GUID)); AvatarDebug.Log("DeleteAvatarDatas done! avatarName:" + data.AvatarName); }
/// <summary> /// 读取贴图到Face++,然后根据数据变化 /// </summary> /// <param name="tex"></param> public void LoadFaceTex(Texture2D tex) { AvatarDebug.Log("start load texture:" + "width:" + tex.width + ",height:" + tex.height); StartCoroutine(AvatarTools.PostTex(tex, JsonManager)); }