我完全不熟悉Unity3D的更复杂功能集,想知道它是否有能力拍照并对其进行操作。具体来说,我希望用户自拍后在脸部轮廓线上描绘,从而创建一个PNG文件,然后将其贴图映射到模型上。
我知道将脸部映射到模型上很简单,但我想知道我是否需要将拍照/雕刻功能编写到涵盖Chrome应用程序中,还是可以在Unity内部完成所有操作。我不需要教程,只是想问一下这是否可能。
我完全不熟悉Unity3D的更复杂功能集,想知道它是否有能力拍照并对其进行操作。具体来说,我希望用户自拍后在脸部轮廓线上描绘,从而创建一个PNG文件,然后将其贴图映射到模型上。
我知道将脸部映射到模型上很简单,但我想知道我是否需要将拍照/雕刻功能编写到涵盖Chrome应用程序中,还是可以在Unity内部完成所有操作。我不需要教程,只是想问一下这是否可能。
using UnityEngine;
using System.Collections;
using System.IO;
public class WebCamPhotoCamera : MonoBehaviour
{
WebCamTexture webCamTexture;
void Start()
{
webCamTexture = new WebCamTexture();
GetComponent<Renderer>().material.mainTexture = webCamTexture; //Add Mesh Renderer to the GameObject to which this script is attached to
webCamTexture.Play();
}
IEnumerator TakePhoto() // Start this Coroutine on some button click
{
// NOTE - you almost certainly have to do this here:
yield return new WaitForEndOfFrame();
// it's a rare case where the Unity doco is pretty clear,
// http://docs.unity3d.com/ScriptReference/WaitForEndOfFrame.html
// be sure to scroll down to the SECOND long example on that doco page
Texture2D photo = new Texture2D(webCamTexture.width, webCamTexture.height);
photo.SetPixels(webCamTexture.GetPixels());
photo.Apply();
//Encode to a PNG
byte[] bytes = photo.EncodeToPNG();
//Write out the PNG. Of course you have to substitute your_path for something sensible
File.WriteAllBytes(your_path + "photo.png", bytes);
}
}
对于那些想要让相机实时渲染的人,以下是我成功实现的方法。首先,我编辑了巴特的答案,使纹理在Update上分配而不仅仅是在Start上:
void Start()
{
webCamTexture = new WebCamTexture();
webCamTexture.Play();
}
void Update()
{
GetComponent<RawImage>().texture = webCamTexture;
}
然后我将脚本附加到一个具有RawImage组件的GameObject上。您可以通过在Unity编辑器中的层次结构中右键单击-> UI-> RawImage轻松创建一个(这需要Unity 4.6及以上版本)。运行它应该会在您的视图中显示摄像机的实时视频。截至本文撰写时,Unity 5支持在免费个人版Unity 5中使用网络摄像头。
我希望这能帮助任何想要在Unity中捕获实时相机视频的人。
这是可能的。我强烈建议您查看WebcamTexture Unity API。它具有一些有用的功能:
Bart的回答需要进行必要的修改。我使用了他的代码,但是我得到的图片是黑色的。必要的修改是我们需要将TakePhoto转换为协程并添加。
yield return new WaitForEndOfFrame();
您需要在设备列表中搜索并选择您的摄像头设备索引,以便用于播放网络摄像头纹理。 您可以使用以下代码:
using UnityEngine;
using System.Collections;
using System.IO;
using UnityEngine.UI;
using System.Collections.Generic;
public class GetCam : MonoBehaviour
{
WebCamTexture webCam;
string your_path = "C:\\Users\\Jay\\Desktop";// any path you want to save your image
public RawImage display;
public AspectRatioFitter fit;
public void Start()
{
if(WebCamTexture.devices.Length==0)
{
Debug.LogError("can not found any camera!");
return;
}
int index = -1;
for (int i = 0; i < WebCamTexture.devices.Length; i++)
{
if (WebCamTexture.devices[i].name.ToLower().Contains("your webcam name"))
{
Debug.LogError("WebCam Name:" + WebCamTexture.devices[i].name + " Webcam Index:" + i);
index = i;
}
}
if (index == -1)
{
Debug.LogError("can not found your camera name!");
return;
}
WebCamDevice device = WebCamTexture.devices[index];
webCam = new WebCamTexture(device.name);
webCam.Play();
StartCoroutine(TakePhoto());
display.texture = webCam;
}
public void Update()
{
float ratio = (float)webCam.width / (float)webCam.height;
fit.aspectRatio = ratio;
float ScaleY = webCam.videoVerticallyMirrored ? -1f : 1f;
display.rectTransform.localScale = new Vector3(1f, ScaleY, 1f);
int orient = -webCam.videoRotationAngle;
display.rectTransform.localEulerAngles = new Vector3(0, 0, orient);
}
public void callTakePhoto() // call this function in button click event
{
StartCoroutine(TakePhoto());
}
IEnumerator TakePhoto() // Start this Coroutine on some button click
{
// NOTE - you almost certainly have to do this here:
yield return new WaitForEndOfFrame();
// it's a rare case where the Unity doco is pretty clear,
// http://docs.unity3d.com/ScriptReference/WaitForEndOfFrame.html
// be sure to scroll down to the SECOND long example on that doco page
Texture2D photo = new Texture2D(webCam.width, webCam.height);
photo.SetPixels(webCam.GetPixels());
photo.Apply();
//Encode to a PNG
byte[] bytes = photo.EncodeToPNG();
//Write out the PNG. Of course you have to substitute your_path for something sensible
File.WriteAllBytes(your_path + "\\photo.png", bytes);
}
}
针对这种功能,有一个名为Camera Capture Kit的插件可用 - https://www.assetstore.unity3d.com/en/#!/content/56673,虽然提供的功能面向移动设备,但它包含了如何使用WebCamTexture拍摄静态图像的演示。
如果您想在不使用第三方插件的情况下实现此操作,那么@FuntionR的解决方案将会有所帮助。但是,如果您想将拍摄的照片保存到图库(Android和iOS),则无法在Unity中实现,您需要编写本地代码将照片传输到图库,然后从Unity中调用它。
这里有一篇总结性的博客,将指导您实现目标。 http://unitydevelopers.blogspot.com/2018/07/pick-image-from-gallery-in-unity3d.html
编辑:请注意,上述线程描述了从图库选择图像的过程,但同样的过程也适用于将图像保存到图库。