我完全不熟悉 Unity3D 更复杂的功能集,并且很好奇它是否有能力拍摄照片然后对其进行操作。具体来说,我的愿望是让用户拍一张自拍照,然后让他们在脸部周围描画以创建一个 PNG,然后将其纹理映射到模型上。
我知道将面部映射到模型上很简单,但我想知道是否需要将照片/雕刻功能写入包含的 Chrome 应用程序中,或者是否可以在 Unity 中完成这一切。我不需要关于如何做到这一点的教程,只是询问这是否可行。
是的,这是可能的。您会想看看网络摄像头纹理功能。
您创建一个 WebCamTexture 并调用它Play()启动相机的函数。 WebCamTexture 与任何纹理一样,允许您通过 GetPixels() 调用获取像素。这使您可以在需要时拍摄快照,并将其保存在Texture2D 中。致电编码为PNG()随后写入文件应该可以到达那里。
请注意,下面的代码是基于文档的快速编写。我还没有测试过。如果有多个可用设备,您可能必须选择正确的设备。
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);
}
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)