效果:在相机照画面中改变指定像素的透明度,实现抠图效果
(前提:电脑有摄像头)
1.打开设备相机using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class MT_Camera : MonoBehaviour
{
public GameObject raw_Image;//记住这个raw_Image
RawImage cam_Video;
//Toggle tog_togCamera;
WebCamTexture camTexture;
public int w_cam = 640;
public int h_cam = 480;
private void Awake()
{
//uiRoot = GameObject.Find("Canvas_UI").transform;
//camRoot = GameObject.Find("Canvas_WebCam").transform;
}
private void OnEnable()
{
//cam_Video = raw_Image.GetComponent();
//changeCam(false);
}
private void Start()
{
cam_Video = raw_Image.GetComponent();
changeCam(false);
//tog_togCamera = uiRoot.Find("tog_ChangeCam").GetComponent();
//transform.GetComponent().referenceResolution = new Vector2(Screen.width, Screen.height);
//tog_togCamera.onValueChanged.AddListener(changeCam);
//tog_togCamera.isOn = true;
///自适应屏幕分辨率显示摄像头数据
//宽度不变,缩放高度自适应显示摄像头数据
//cam_Video.rectTransform.sizeDelta = new Vector2(h_cam * Screen.height / w_cam, Screen.width);
//宽度不变,缩放宽度自适应显示摄像头数据
//cam_Video.rectTransform.sizeDelta = new Vector2(Screen.height, w_cam * Screen.width / h_cam);
}
void changeCam(bool isOn)
{
StartCoroutine(CallCamera(isOn));
}
IEnumerator CallCamera(bool isOn)
{
yield return Application.RequestUserAuthorization(UserAuthorization.WebCam);
if (Application.HasUserAuthorization(UserAuthorization.WebCam))
{
if (camTexture != null)
camTexture.Stop();
WebCamDevice[] cameraDevices = WebCamTexture.devices;
string deviceName = "";
for (int i = 0; i < cameraDevices.Length; i++)
{
//如果是前置摄像机
if (WebCamTexture.devices[i].isFrontFacing && isOn)
{
deviceName = WebCamTexture.devices[i].name;
TurnCam(isOn);
break;
}
//如果是后置摄像机
else if (!WebCamTexture.devices[i].isFrontFacing && !isOn)
{
deviceName = WebCamTexture.devices[i].name;
TurnCam(isOn);
break;
}
}
camTexture = new WebCamTexture(deviceName, w_cam, h_cam, 12);
cam_Video.texture = camTexture;
print("**********"+cam_Video.texture.width+ "**********" + cam_Video.texture.height);
camTexture.Play();
}
}
///
/// 翻转plane,正确显示摄像头数据
///
/// If set to true is turn.
public void TurnCam(bool isOn)
{
#if UNITY_IOS || UNITY_IPHONE
if (!isOn)
cam_Video.rectTransform.localEulerAngles = new Vector3(180, 0, 90);
else cam_Video.rectTransform.localEulerAngles = new Vector3(0, 0, -90);
#elif UNITY_ANDROID
if (!isOn)
cam_Video.rectTransform.localEulerAngles = new Vector3(180, 180, 90);
else cam_Video.rectTransform.localEulerAngles = new Vector3(0, 180, 90);
#endif
}
}
2.在场景中创建raw_Image
把这个raw_Image拖放到上一个脚本组件上
3.把使用了下方shader的材质给到raw_image Shader
Shader "Demo/SpriteShader"
{
Properties
{
[PerRendererData]_MainTex ("Sprite Texture", 2D) = "white" {} // 当前的Sprite图(添加[PerRendererData]后在属性面板中不可见)
_Color ("Alpha Color Key", Color) = (0,0,0,1) // 用于比较的基色(想过滤掉什么颜色,这个颜色就设置为那种颜色)
_Range("Range",Range (0, 1.01))=0.1 // 决定抠图范围的域
[MaterialToggle] PixelSnap ("Pixel snap", Float) = 0 // 在属性面板中以按钮形式显示(在此Shader中没用到)
}
SubShader
{
//Sprite图一般均为透明贴图,需要做以下处理
Tags
{
"Queue"="Transparent"
"IgnoreProjector"="True"
"RenderType"="Transparent"
"PreviewType"="Plane"
"CanUseSpriteAtlas"="True"
}
Pass
{
//Sprite图一般均为透明贴图,需要做以下处理
Cull Off
Lighting Off
ZWrite Off
Fog { Mode Off }
Blend SrcAlpha OneMinusSrcAlpha //Sprite图一般均为透明贴图,需要做以下处理
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
sampler2D _MainTex;
float4 _Color;
half _Range;
struct Vertex
{
float4 vertex : POSITION;
float2 uv_MainTex : TEXCOORD0;
float2 uv2 : TEXCOORD1;
};
struct Fragment
{
float4 vertex : POSITION;
float2 uv_MainTex : TEXCOORD0;
};
Fragment vert(Vertex v)
{
Fragment o;
o.vertex = mul(UNITY_MATRIX_MVP, v.vertex);
o.uv_MainTex = v.uv_MainTex;
return o;
}
float4 frag(Fragment IN) : COLOR
{
float4 o = float4(1, 1, 1, 1);
half4 c = tex2D (_MainTex, IN.uv_MainTex);
o.rgb = c.rgb;
//使用当前像素颜色与基色相减,然后与域相比较以决定是否将其显示出来
if(abs(c.r-_Color.r)
关注
打赏
最近更新
- 深拷贝和浅拷贝的区别(重点)
- 【Vue】走进Vue框架世界
- 【云服务器】项目部署—搭建网站—vue电商后台管理系统
- 【React介绍】 一文带你深入React
- 【React】React组件实例的三大属性之state,props,refs(你学废了吗)
- 【脚手架VueCLI】从零开始,创建一个VUE项目
- 【React】深入理解React组件生命周期----图文详解(含代码)
- 【React】DOM的Diffing算法是什么?以及DOM中key的作用----经典面试题
- 【React】1_使用React脚手架创建项目步骤--------详解(含项目结构说明)
- 【React】2_如何使用react脚手架写一个简单的页面?