AVProLiveCamrea + ChromaKeyKit實作實時攝像綠幕摳圖
我使用的是Unity2018.4.26
csdn 微元素都能搜得到 淘寶也行 支持正版更好
AVProLiveCamrea 2.6.2
ChromaKeyKit 2.2
原理就是 使用AVProLiveCamrea獲取到的Texture畫面資訊 經過處理(ChromaKeyKit摳圖)賦值到UGUI的RawImage上(當然材質球也是可以的 同理)
參考文章
效果展示
綠幕摳像
準備作業
新建工程 匯入兩個插件 準備一個攝像頭連接在電腦上



自定義 MSKBridgeAVProCam(參考的MSKBridgeWebCamPlayer)
using System;
using System.Collections;
using System.Collections.Generic;
using Nexweron.Core.MSK;
using UnityEngine;
using UnityEngine.Serialization;
using UnityEngine.UI;
using UnityEngine.Video;
public class MSKBridgeAVProCam : MonoBehaviour
{
public AVProLiveCamera avProLiveCam;
public MSKController mskController;
private Texture _texture;
public RawImage uguiImage;
private void UpdateProperties()
{
if (_texture != avProLiveCam.OutputTexture)
{
_texture = avProLiveCam.OutputTexture;
uguiImage.texture = _texture;
mskController.SetSourceTexture(_texture);
}
}
private void OnEnable() {
if (avProLiveCam != null) {
if (mskController != null) {
avProLiveCam.frameReady += WebCamPlayerFrameReady;
} else {
Debug.LogError("AVProLiveCamera | mskController = null");
}
} else {
Debug.LogError("AVProLiveCamera | webCamPlayer = null");
}
}
private void OnDisable() {
if (avProLiveCam != null) {
avProLiveCam.frameReady -= WebCamPlayerFrameReady;
}
}
private void WebCamPlayerFrameReady()
{
if (avProLiveCam.isActiveAndEnabled&&avProLiveCam!=null&&avProLiveCam.OutputTexture != null)
{
Debug.Log("更新texture");
UpdateProperties();
RenderTexture textur = avProLiveCam.OutputTexture as RenderTexture;
mskController.RenderOut(textur);
}
}
}

畫面處理(綠幕摳圖)
摳圖插件 詳細引數可以參考
ChromaKeyKit

AVProLiveCamrea腳本還需要做一些小修改(發現直接在Update里賦值 rawImage會閃爍)
為了在攝像頭獲取到texture資訊后 對texture做操作這里仿照 WebCamPlayer 新建一個事件

盲猜用這個方法就行

測驗效果
創建一個plane 調大些 貼上材質

找一個有綠色部分的畫面 等下攝像頭就對著它

測驗結果


轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/237146.html
標籤:其他
上一篇:Unity NGUI部分講解
下一篇:題解
