untiy 实时人像抠图
生活随笔
收集整理的這篇文章主要介紹了
untiy 实时人像抠图
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
最近公司要求做一個摳圖程序,作者想到接一個百度AI來實現摳圖,做出來之后效果不是太好,然后領導說想要實時摳圖,讓用戶現場體驗更好些,沒辦法,只能推倒重來,然后在網上找個shader,然后自己改改,就實現了。
?
?
首先是shader代碼
Shader "UniversalChromaKey" {Properties {_MainTex ("Base (RGB)", 2D) = "white" {}_Sens ("Sensibilidad", Range (0,.9)) = .3_Cutoff ("Recorte", Range (0,.9)) = .2_Color ("Chroma", Color) = (0, 1.0, 0)}SubShader {Tags { "Queue"="Transparent" "RenderType"="Transparent" }LOD 200CGPROGRAM#pragma surface surf Lambert alphasampler2D _MainTex;float _Cutoff;float _Sens;half3 _Color;struct Input {float2 uv_MainTex;};void surf (Input IN, inout SurfaceOutput o) {half4 c = tex2D (_MainTex, IN.uv_MainTex);o.Emission = c.rgb;float aR = abs(c.r - _Color.r) < _Sens ? abs(c.r - _Color.r) : 1;float aG = abs(c.g - _Color.g) < _Sens ? abs(c.g - _Color.g) : 1;float aB = abs(c.b - _Color.b) < _Sens ? abs(c.b - _Color.b) : 1; float a = (aR + aG + aB) / 3; if(a < _Cutoff){o.Alpha = 0;}else{o.Alpha = 1;}}ENDCG}FallBack "Diffuse" }控制shader腳本
using UnityEngine; using System.Collections; using UnityEngine.UI;public class ShaderControls : MonoBehaviour {float sensS, cutoffS;Color colS;// Use this for initializationvoid Start () {sensS = GetComponent<RawImage>().material.GetFloat("_Sens");cutoffS = GetComponent<RawImage>().material.GetFloat("_Cutoff");colS = GetComponent<RawImage>().material.GetColor("_Color");sens = sensS;cutoff = cutoffS; }// Update is called once per framevoid Update () {}public float sens, cutoff;public string r = "99", g = "205", b ="77";//void OnGUI()//{// sens = GUI.HorizontalSlider(new Rect(25, 25, 100, 30), sens, 0.0f, 1.0f);// cutoff = GUI.HorizontalSlider(new Rect(25, 70, 100, 30), cutoff, 0.0f, 1.0f);// r = GUI.TextField(new Rect(25, 120, 40, 20), r);// g = GUI.TextField(new Rect(70, 120, 40, 20), g);// b = GUI.TextField(new Rect(120, 120, 40, 20), b);// if (GUI.Button(new Rect(25, 160, 100, 30), "Reset"))// {// sens = sensS;// cutoff = cutoffS;// r = (colS.r * 255f).ToString();// g = (colS.g * 255f).ToString();// b = (colS.b * 255f).ToString();// }// GetComponent<RawImage>().material.SetFloat("_Sens", sens);// GetComponent<RawImage>().material.SetFloat("_Cutoff", cutoff);// try// {// Color col = new Color(int.Parse(r) / 255f, int.Parse(g) / 255f, int.Parse(b) / 255f);// //print (col);// GetComponent<RawImage>().material.color = col;// }// catch (UnityException e)// {// }//} }控制外部攝像機腳本
using UnityEngine; using System.Collections; using UnityEngine.Video; using UnityEngine.UI; using System.IO;public class CameraControl : MonoBehaviour {/// <summary>/// 外部攝像頭/// </summary>private WebCamTexture webTex;/// <summary>/// UI父物體/// </summary>private Canvas canvas;/// <summary>/// 攝像頭映射畫面/// </summary>private RawImage Camera_image;private void Start(){canvas = GameObject.Find("Canvas").GetComponent<Canvas>();Camera_image = canvas.transform.Find("Camera_screen").GetComponent<RawImage>();StartCoroutine(CallCamera());}/// <summary>/// 打開攝像頭/// </summary>/// <returns></returns>IEnumerator CallCamera(){//等待用戶允許訪問yield return Application.RequestUserAuthorization(UserAuthorization.WebCam);//如果用戶允許訪問,開始獲取圖像 if (Application.HasUserAuthorization(UserAuthorization.WebCam)){WebCamDevice[] devices = WebCamTexture.devices;string devicename = devices[0].name;webTex = new WebCamTexture(devicename, Screen.width, Screen.height);Camera_image.texture = webTex;webTex.Play();}}}控制shader腳本和打開攝像頭腳本掛載到一個RawImage上面,shader腳本放在材質球上,然后把材質球也放在RawImage上面,
最后效果
?
由于綠幕不平和有陰影導致的綠幕沒有扣掉(測試用的綠幕),如果現場綠幕是平的,效果會更好,作者把項目工程源文件分享出來了,想要的可以下載,如果有不明白的地方可以加QQ群:636926481(互相交流)。
Demo:https://download.csdn.net/download/qq_39954479/12330999
?
?
總結
以上是生活随笔為你收集整理的untiy 实时人像抠图的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: InnoDB Adaptive Hash
- 下一篇: 说说域名、二级域名和主机名的联系区别