public IEnumerator LoadItem(string path, int slotid = 0) { Debug.Log("LoadItem:" + path); WWW bundledata = new WWW(path); yield return(bundledata); byte[] bundle_bytes = bundledata.bytes; GCHandle hObject = GCHandle.Alloc(bundle_bytes, GCHandleType.Pinned); IntPtr pObject = hObject.AddrOfPinnedObject(); int itemid = FaceunityWorker.fu_CreateItemFromPackage(pObject, bundle_bytes.Length); hObject.Free(); if (itemid_tosdk[slotid] > 0) { UnLoadItem(slotid); } itemid_tosdk[slotid] = itemid; FaceunityWorker.fu_setItemIds(p_itemsid, SLOTLENGTH, IntPtr.Zero); Debug.Log("LoadItem Finish"); }
//输入数据接口案例 public void UpdateData(IntPtr ptr, int texid, int w, int h, UpdateDataMode mode) { if (mode == UpdateDataMode.NV21) { FaceunityWorker.SetNV21Input(ptr, 0, w, h); } else if (mode == UpdateDataMode.Dual) { FaceunityWorker.SetDualInput(ptr, texid, 0, w, h); } else if (mode == UpdateDataMode.Image) { FaceunityWorker.SetImage(ptr, 0, false, w, h); } else if (mode == UpdateDataMode.ImageTexId) { FaceunityWorker.SetImageTexId(texid, 0, w, h); } if (m_tex_created == false) { m_fu_texid = FaceunityWorker.fu_GetNamaTextureId(); if (m_fu_texid > 0) { m_rendered_tex = Texture2D.CreateExternalTexture(w, h, TextureFormat.RGBA32, false, true, (IntPtr)m_fu_texid); Debug.LogFormat("Texture2D.CreateExternalTexture:{0}\n", m_fu_texid); if (RawImg_BackGroud != null) { RawImg_BackGroud.texture = m_rendered_tex; RawImg_BackGroud.gameObject.SetActive(true); Debug.Log("m_rendered_tex: " + m_rendered_tex.GetNativeTexturePtr()); } m_tex_created = true; } } }
// 初始化摄像头 public IEnumerator InitCamera() { yield return(Application.RequestUserAuthorization(UserAuthorization.WebCam)); if (Application.HasUserAuthorization(UserAuthorization.WebCam)) { WebCamDevice[] devices = WebCamTexture.devices; if (devices == null) { Debug.Log("No Camera detected!"); } else { currentDeviceName = devices[0].name; wtex = new WebCamTexture(currentDeviceName, cameraWidth, cameraHeight, cameraFrameRate); wtex.Play(); FaceunityWorker.FixRotation(!devices[0].isFrontFacing); } } if (img_handle.IsAllocated) { img_handle.Free(); } webtexdata = new Color32[wtex.width * wtex.height]; img_handle = GCHandle.Alloc(webtexdata, GCHandleType.Pinned); p_img_ptr = img_handle.AddrOfPinnedObject(); img_nv21 = new byte[wtex.width * wtex.height * 3 / 2]; img_nv21_handle = GCHandle.Alloc(img_nv21, GCHandleType.Pinned); p_img_nv21_ptr = img_nv21_handle.AddrOfPinnedObject(); }
/// <summary> /// 每帧更新眼球transform /// </summary> void Update() { if (FaceunityWorker.Instance == null || FaceunityWorker.fuIsLibraryInit() == 0) { return; } if (faceid >= FaceunityWorker.Instance.m_need_update_headnum) { return; } if (FaceunityWorker.Instance.m_need_update_headnum > 0) //仅在跟踪到人脸的情况下更新 { //skinnedMeshRenderer.enabled = true; } else { //skinnedMeshRenderer.enabled = false; return; } transform.localRotation = m_rotation0; transform.localPosition = m_position0; var R = FaceunityWorker.Instance.m_eye_rotation[faceid].m_data; RotateAround(transform, m_center, PostProcessRotation(new Quaternion(R[0], R[1], R[2], R[3]))); }
/// <summary> /// 加载道具 /// </summary> /// <param name="path"></param> /// <param name="itemid_tosdk"></param> /// <param name="solt_length"></param> /// <param name="slot_id"></param> /// <returns></returns> private IEnumerator LoadItem(Item item, slot_item[] slot_items, string path, int[] itemid_tosdk, int solt_length, int slot_id = 0) { if (FaceunityWorker.fuIsLibraryInit() == 0) { yield break; } yield return(DownLoadBuff(path, (webRquest) => { byte[] bundle_bytes = webRquest.downloadHandler.data; int itemid = FaceunityWorker.fuCreateItemFromPackage(bundle_bytes, bundle_bytes.Length); var errorcode = FaceunityWorker.fuGetSystemError(); if (errorcode != 0) { Debug.LogErrorFormat("errorcode:{0}, {1}", errorcode, Marshal.PtrToStringAnsi(FaceunityWorker.fuGetSystemErrorString(errorcode))); } if (itemid_tosdk[slot_id] > 0) { UnLoadItem(slot_id); } itemid_tosdk[slot_id] = itemid; slot_items[slot_id].id = itemid; slot_items[slot_id].name = item.name; slot_items[slot_id].item = item; FaceunityWorker.fu_SetItemIds(itemid_tosdk, itemid_tosdk.Length, null); })); }
/// <summary> /// 根据运行环境调整Nama的Fov OnCameraStart /// </summary> public void SelfAdJustNamaFov() { int inputTexWidth = CameraManager.Instance.Width; int inputTexHeight = CameraManager.Instance.Height; float _fov = camerafov; if (!NeedSwitchWidthHeight() && inputTexWidth < inputTexHeight) { float t = Mathf.Tan(_fov / 2.0f * Mathf.Deg2Rad); _fov = 2.0f * Mathf.Atan2(inputTexWidth * t, inputTexHeight) * Mathf.Rad2Deg; } else if (NeedSwitchWidthHeight() && inputTexWidth > inputTexHeight) { float t = Mathf.Tan(_fov / 2.0f * Mathf.Deg2Rad); _fov = 2.0f * Mathf.Atan2(inputTexHeight * t, inputTexWidth) * Mathf.Rad2Deg; } FaceunityWorker.fuSetFaceProcessorFov(_fov); if (FaceunityWorker.fuGetFaceProcessorFov() == _fov) { //Debug.LogFormat("fuSetFaceProcessorFov:{0}", _fov); //Debug.LogFormat("fuGetFaceProcessorFov:{0}", FaceunityWorker.fuGetFaceProcessorFov()); //Debug.LogFormat("camerafov: {0}, inputTexWidth:{1}, inputTexHeight:{2}", camerafov, inputTexWidth, inputTexHeight); } else { Debug.LogErrorFormat("fuSetFaceProcessorFov:{0} != fuGetFaceProcessorFov:{1}", _fov, FaceunityWorker.fuGetFaceProcessorFov()); } }
/// <summary> /// 删除关联道具的纹理 /// </summary> /// <param name="slotid">道具在slot数组中的index</param> /// <param name="paramdname">关联图片的关键词</param> public void DeleteTexForItem(int slotid, string paramdname) { if (slotid >= 0 && slotid < slot_length) { FaceunityWorker.fuDeleteTexForItem(slot_items[slotid].id, paramdname); } }
public void SetItemParamd(int slotid, string paramdname, double value) { if (slotid >= 0 && slotid < SLOTLENGTH) { FaceunityWorker.fuItemSetParamd(itemid_tosdk[slotid], paramdname, value); } }
/// <summary> /// 给道具设置一个double参数 /// </summary> /// <param name="slotid">道具在slot数组中的index</param> /// <param name="paramdname">关联参数的关键词</param> /// <param name="value">要设置的参数</param> public void SetItemParamd(int slotid, string paramdname, double value) { if (slotid >= 0 && slotid < slot_length) { FaceunityWorker.fuItemSetParamd(slot_items[slotid].id, paramdname, value); } }
public void CreateTexForItem(int slotid, string paramdname, IntPtr value, int width, int height) { if (slotid >= 0 && slotid < SLOTLENGTH) { FaceunityWorker.fu_CreateTexForItem(slot_items[slotid].id, paramdname, value, width, height); } }
IEnumerator SetFOV() { if (FaceunityWorker.fu_IsTracking() <= 0) { ResetFOV(); } while (true) { if (!ifTrackPos) { ResetFOV(); break; } if (FaceunityWorker.instance.m_focallength != null && FaceunityWorker.fu_IsTracking() > 0) { var R = FaceunityWorker.instance.m_focallength[0].m_data; if (R != null && R[0] > 0) { if (Mathf.Abs(R[0] - dde_focallength) > 0.0001f) { dde_focallength = R[0]; Vector2 currentResolution = NatCam.Camera.PreviewResolution; float l = 0; float rotz = RawImg_BackGroud.rectTransform.eulerAngles.z; if ((rotz > 45 && rotz < 135) || (rotz > 225 && rotz < 315)) { if (RawImg_BackGroud.rectTransform.sizeDelta.x < RawImg_BackGroud.rectTransform.sizeDelta.y) { l = Mathf.Min(currentResolution.x, currentResolution.y); } else { l = Mathf.Max(currentResolution.x, currentResolution.y); } } else { if (RawImg_BackGroud.rectTransform.sizeDelta.x > RawImg_BackGroud.rectTransform.sizeDelta.y) { l = Mathf.Min(currentResolution.x, currentResolution.y); } else { l = Mathf.Max(currentResolution.x, currentResolution.y); } } camera3d.fieldOfView = (float)(Mathf.Atan(l / (2 * R[0])) * Mathf.Rad2Deg * 2.0); //Debug.Log("fieldOfView@@@"); //txt.text = "m_focallength=" + R[0] + "\n fieldOfView=" + camera3d.fieldOfView+ "\n VerticalBGLength="+ l; } //txt.text = "m_focallength=" + R[0] + "\n fieldOfView=" + camera3d.fieldOfView; Debug.Log("dde_focallength=" + dde_focallength + " R[0]=" + R[0]); break; } } //Debug.Log("SetFOV Running!!!"); yield return(Util._endOfFrame); } fovcor = null; }
public void SelfAdjusFov() { if (NatCam.Camera == null || camera3d == null) { return; } float fov = camera3d.fieldOfView; Vector2 resolution = NatCam.Camera.PreviewResolution; //Debug.Log(resolution); //Debug.LogFormat("Orientation:{0}", (int)DispatchUtility.Orientation); Orientation orientation = DispatchUtility.Orientation; if (currentFov != fov || currentResolution != resolution || currentOrientation != orientation) { if ((DispatchUtility.Orientation == Orientation.Rotation_0 || DispatchUtility.Orientation == Orientation.Rotation_180) && resolution.x < resolution.y) { float t = Mathf.Tan(fov / 2.0f * Mathf.Deg2Rad); fov = 2.0f * Mathf.Atan2(resolution.x * t, resolution.y) * Mathf.Rad2Deg; } else if ((DispatchUtility.Orientation == Orientation.Rotation_90 || DispatchUtility.Orientation == Orientation.Rotation_270) && resolution.x > resolution.y) { float t = Mathf.Tan(fov / 2.0f * Mathf.Deg2Rad); fov = 2.0f * Mathf.Atan2(resolution.y * t, resolution.x) * Mathf.Rad2Deg; } FaceunityWorker.fuSetFaceProcessorFov(fov); currentFov = fov; currentResolution = resolution; currentOrientation = orientation; //Debug.LogFormat("fu_SetFaceProcessorFov:{0}", fov); //Debug.LogFormat("fu_GetFaceProcessorFov:{0}", FaceunityWorker.fu_GetFaceProcessorFov()); } }
private float[] GetJoint3ds() { // 获取当前人数 int index = FaceunityWorker.fuHumanProcessorGetNumResults(); if (index > 0) { Debug.LogFormat("检测到人体!index={0}", index); IntPtr result = FaceunityWorker.fuHumanProcessorGetResultJoint3ds(0, m_joint3dsLength); int length = m_joint3dsLength[0]; if (length > 0) { if (joint3ds == null || joint3ds.Length != length) { joint3ds = new float[length]; } Marshal.Copy(result, joint3ds, 0, length); Debug.LogFormat("joint3ds.Length = {0}, joint3ds[0]={1}, joint3ds[1]={2}", joint3ds.Length, joint3ds[0], joint3ds[1]); return(joint3ds); } else { Debug.LogFormat("空的, length={0}", length); } } else { Debug.Log("没身体"); } return(null); }
public void SetItemParams(int slotid, string paramdname, string value) { if (slotid >= 0 && slotid < SLOTLENGTH) { FaceunityWorker.fu_ItemSetParams(slot_items[slotid].id, paramdname, value); } }
public void DeleteTexForItem(int slotid, string paramdname) { if (slotid >= 0 && slotid < SLOTLENGTH) { FaceunityWorker.fu_DeleteTexForItem(slot_items[slotid].id, paramdname); } }
/// <summary> /// 给道具设置一个数组 /// </summary> /// <param name="slotid">道具在slot数组中的index</param> /// <param name="paramdname">关联数组的关键词</param> /// <param name="value">要设置的数组</param> public void SetItemParamdv(int slotid, string paramdname, double[] value) { if (slotid >= 0 && slotid < slot_length && value != null && value.Length > 0) { FaceunityWorker.fuItemSetParamdv(slot_items[slotid].id, paramdname, value, value.Length); } }
public double GetItemParamd(int slotid, string paramdname) { if (slotid >= 0 && slotid < SLOTLENGTH) { return(FaceunityWorker.fu_ItemGetParamd(slot_items[slotid].id, paramdname)); } return(0); }
public void UnBindItemAll(int slotid_dst) { if (slotid_dst >= 0 && slotid_dst < slot_length) { FaceunityWorker.fuUnbindAllItems(slot_items[slotid_dst].id); Debug.LogFormat("UnBindItemAll: slotid_dst = {0}", slotid_dst); } }
void Update() { #if UNITY_EDITOR || UNITY_STANDALONE if (NatCam.Camera == null) { return; } WebCamTexture tex = (WebCamTexture)NatCam.Preview; if (tex != null && tex.isPlaying) { // pass data by byte buf, if (tex.didUpdateThisFrame) { if (webtexdata.Length != tex.width * tex.height) { if (img_handle.IsAllocated) { img_handle.Free(); } webtexdata = new Color32[tex.width * tex.height]; img_handle = GCHandle.Alloc(webtexdata, GCHandleType.Pinned); p_img_ptr = img_handle.AddrOfPinnedObject(); } tex.GetPixels32(webtexdata); //Debug.LogFormat("data pixels:{0},img_btyes:{1}",data.Length,img_bytes.Length/4); //for (int i = 0; i < webtexdata.Length; i++) //{ // img_bytes[4 * i] = webtexdata[i].b; // img_bytes[4 * i + 1] = webtexdata[i].g; // img_bytes[4 * i + 2] = webtexdata[i].r; // img_bytes[4 * i + 3] = 1; //} FaceunityWorker.SetImage(p_img_ptr, 32, false, (int)NatCam.Camera.PreviewResolution.x, (int)NatCam.Camera.PreviewResolution.y); //传输数据方法之一 } } if (m_tex_created == false) { m_fu_texid = FaceunityWorker.fu_GetNamaTextureId(); if (m_fu_texid > 0) { m_tex_created = true; m_rendered_tex = Texture2D.CreateExternalTexture((int)NatCam.Camera.PreviewResolution.x, (int)NatCam.Camera.PreviewResolution.y, TextureFormat.RGBA32, false, true, (IntPtr)m_fu_texid); Debug.LogFormat("Texture2D.CreateExternalTexture:{0}\n", m_fu_texid); if (RawImg_BackGroud != null) { RawImg_BackGroud.texture = m_rendered_tex; RawImg_BackGroud.gameObject.SetActive(true); Debug.Log("m_rendered_tex: " + m_rendered_tex.GetNativeTexturePtr()); } } } #endif }
public void UnLoadAllItems() { Debug.Log("UnLoadAllItems"); FaceunityWorker.fu_DestroyAllItems(); currentItem = ""; currentItemID = -1; beautyitem = ""; beautyitemID = -1; }
void Update() { #if UNITY_EDITOR || UNITY_STANDALONE if (NatCam.Camera == null) { return; } WebCamTexture tex = (WebCamTexture)NatCam.Preview; if (tex != null && tex.isPlaying) { // pass data by byte buf, if (tex.didUpdateThisFrame) { if (webtexdata.Length != tex.width * tex.height) { if (img_handle.IsAllocated) { img_handle.Free(); } webtexdata = new Color32[tex.width * tex.height]; img_handle = GCHandle.Alloc(webtexdata, GCHandleType.Pinned); p_img_ptr = img_handle.AddrOfPinnedObject(); } tex.GetPixels32(webtexdata); FaceunityWorker.SetImage(p_img_ptr, 0, false, (int)NatCam.Camera.PreviewResolution.x, (int)NatCam.Camera.PreviewResolution.y); //传输数据方法之一 } } if (m_tex_created == false && FaceunityWorker.currentMode == FaceunityWorker.FURuningMode.FU_Mode_RenderItems) { m_fu_texid = FaceunityWorker.fu_GetNamaTextureId(); if (m_fu_texid > 0) { m_tex_created = true; m_rendered_tex = Texture2D.CreateExternalTexture((int)NatCam.Camera.PreviewResolution.x, (int)NatCam.Camera.PreviewResolution.y, TextureFormat.RGBA32, false, true, (IntPtr)m_fu_texid); Debug.LogFormat("Texture2D.CreateExternalTexture:{0}\n", m_fu_texid); if (RawImg_BackGroud != null) { RawImg_BackGroud.texture = m_rendered_tex; RawImg_BackGroud.gameObject.SetActive(true); Debug.Log("m_rendered_tex: " + m_rendered_tex.GetNativeTexturePtr()); } } else { Debug.Log("ERROR!!!m_fu_texid: " + m_fu_texid); } } #endif //if (FaceunityWorker.instance.m_rotation_mode != null) //{ // var R = FaceunityWorker.instance.m_rotation_mode[0].m_data; // if (R != null) // txt.text = "rotation=" + R[0]; //} }
public void UnBindItem(int slotid_dst, int slotid_src) { if (slotid_dst >= 0 && slotid_dst < slot_length && slotid_src >= 0 && slotid_src < slot_length) { int[] value = { slot_items[slotid_src].id }; FaceunityWorker.fuUnbindItems(slot_items[slotid_dst].id, value, value.Length); Debug.LogFormat("UnBindItem: slotid_dst={0}, slotid_src={1}", slotid_dst, slotid_src); } }
/** * \brief 封装过的加载道具用的接口,配合slot的概念和item这个struct控制多个不同类型的道具的加载卸载 * \param item 要加载的道具的item,封装过的道具信息集合,方便业务逻辑,详见itemconfig * \param slotid 道具要加载的位置(slot),默认值为0,即slot数组的第0位 * \param cb 加载道具完毕后会自动执行的回调,可以为空 * \return 无 */ public IEnumerator LoadItem(Item item, int slotid = 0, LoadItemCallback cb = null) { if (LoadingItem == false && item.fullname != null && item.fullname.Length != 0 && slotid >= 0 && slotid < SLOTLENGTH) { LoadingItem = true; int tempslot = GetSlotIDbyName(item.name); if (tempslot < 0) //如果尚未载入道具数据 { var bundledata = Resources.LoadAsync <TextAsset>(item.fullname); yield return(bundledata); var data = bundledata.asset as TextAsset; byte[] bundle_bytes = data != null ? data.bytes : null; Debug.LogFormat("bundledata name:{0}, size:{1}", item.name, bundle_bytes.Length); GCHandle hObject = GCHandle.Alloc(bundle_bytes, GCHandleType.Pinned); IntPtr pObject = hObject.AddrOfPinnedObject(); var itemid = FaceunityWorker.fu_CreateItemFromPackage(pObject, bundle_bytes.Length); hObject.Free(); UnLoadItem(slotid); //卸载上一个在这个slot槽内的道具 itemid_tosdk[slotid] = itemid; slot_items[slotid].id = itemid; slot_items[slotid].name = item.name; slot_items[slotid].item = item; FaceunityWorker.fu_setItemIds(p_itemsid, SLOTLENGTH, IntPtr.Zero); Debug.Log("载入Item:" + item.name + " @slotid=" + slotid); } else if (tempslot != slotid) //道具已载入,但是不在请求的slot槽内 { UnLoadItem(slotid); itemid_tosdk[slotid] = slot_items[tempslot].id; slot_items[slotid] = slot_items[tempslot]; itemid_tosdk[tempslot] = 0; slot_items[tempslot].Reset(); FaceunityWorker.fu_setItemIds(p_itemsid, SLOTLENGTH, IntPtr.Zero); Debug.Log("移动Item:" + item.name + " from tempslot=" + tempslot + " to slotid=" + slotid); } else //tempslot == slotid 即重复载入同一个道具进同一个slot槽,直接跳过 { Debug.Log("重复载入Item:" + item.name + " slotid=" + slotid); } SetItemMirror(slotid); if (cb != null) { cb(item);//触发载入道具完成事件 } LoadingItem = false; } }
void Update() { if (Input.GetKeyDown(KeyCode.Escape)) Application.Quit(); if (FaceunityWorker.fu_IsTracking() > 0) Image_FaceDetect.SetActive(false); else Image_FaceDetect.SetActive(true); }
public string GetItemParams(int slotid, string paramdname) { if (slotid >= 0 && slotid < SLOTLENGTH) { byte[] bytes = new byte[32]; int i = FaceunityWorker.fu_ItemGetParams(slot_items[slotid].id, paramdname, bytes, 32); return(System.Text.Encoding.Default.GetString(bytes).Replace("\0", "")); } return(""); }
/// <summary> /// 获取道具中的某个string参数值 /// </summary> /// <param name="slotid">道具在slot数组中的index</param> /// <param name="paramdname">关联参数的关键词</param> /// <returns>参数值</returns> public string GetItemParams(int slotid, string paramdname) { if (slotid >= 0 && slotid < slot_length) { byte[] bytes = new byte[1024]; int i = FaceunityWorker.fuItemGetParams(slot_items[slotid].id, paramdname, bytes, bytes.Length); return(System.Text.Encoding.Default.GetString(bytes).Replace("\0", "")); } return(""); }
public void SetItemParamdv(int slotid, string paramdname, double[] value) { if (slotid >= 0 && slotid < SLOTLENGTH && value != null && value.Length > 0) { GCHandle vgc = GCHandle.Alloc(value, GCHandleType.Pinned); IntPtr vptr = vgc.AddrOfPinnedObject(); FaceunityWorker.fu_ItemSetParamdv(slot_items[slotid].id, paramdname, vptr, value.Length); vgc.Free(); } }
public void SwitchCamera(int newCamera = -1) { FaceunityWorker.fu_OnCameraChange(); // Select the new camera ID // If no argument is given, switch to the next camera newCamera = newCamera < 0 ? (NatCam.Camera + 1) % DeviceCamera.Cameras.Count : newCamera; // Set the new active camera NatCam.Camera = (DeviceCamera)newCamera; StartCoroutine(delaySetItemMirror()); }
//卸载道具。用于舌头跟踪,详见文档 public bool UnLoadItem(int slotid = 0) { if (slotid >= 0 && slotid < SLOTLENGTH) { FaceunityWorker.fu_DestroyItem(itemid_tosdk[slotid]); itemid_tosdk[slotid] = 0; return(true); } Debug.LogError("UnLoadItem Faild!!!"); return(false); }
//卸载所有道具 public void UnLoadAllItems() { Debug.Log("UnLoadAllItems"); FaceunityWorker.fu_DestroyAllItems(); for (int i = 0; i < SLOTLENGTH; i++) { itemid_tosdk[i] = 0; slot_items[i].Reset(); } }