Gen*_* S. 19 photo unity-game-engine
我完全不熟悉Unity3D更复杂的功能集,如果它有能力拍照然后操纵它,我很好奇.具体来说,我的愿望是让用户自拍,然后让他们在他们的脸上追踪以创建一个PNG,然后将纹理映射到模型上.
我知道将面部映射到模型上很简单,但我想知道是否需要将照片/雕刻功能写入包含在内的Chrome应用程序中,或者是否可以在Unity中完成.我不需要有关如何做到这一点的教程,只是询问它是否可能.
Bar*_*art 21
是的,这是可能的.您将需要查看WebCamTexture功能.
您创建一个WebCamTexture并调用其Play()函数启动相机.WebCamTexture与任何纹理一样,允许您通过GetPixels()调用获取像素.这允许您在需要时拍摄快照,并且可以将其保存在Texture2D中.调用EncodeToPNG()并随后写入文件应该可以实现.
请注意,下面的代码是基于文档的快速编写.我没有测试过.如果有多个可用设备,则可能必须选择正确的设备.
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);
}
}
Run Code Online (Sandbox Code Playgroud)
对于那些试图让相机渲染实时反馈的人来说,这就是我设法将它拉下来的方法.首先,我编辑了Bart的答案,因此纹理将在Update上分配,而不仅仅在Start上:
void Start()
{
webCamTexture = new WebCamTexture();
webCamTexture.Play();
}
void Update()
{
GetComponent<RawImage>().texture = webCamTexture;
}
Run Code Online (Sandbox Code Playgroud)
然后我将脚本附加到带有RawImage组件的GameObject.您可以在Unity Editor的层次结构中通过右键单击 - > UI - > RawImage轻松创建一个(这需要Unity 4.6及更高版本).运行它应该在您的视图中显示相机的实时馈送.在撰写本文时,Unity 5支持在Unity 5的免费个人版中使用网络摄像头.
我希望这可以帮助任何寻找在Unity中捕获实时摄像头的好方法的人.
有可能的.我强烈建议您查看WebcamTexture Unity API.它有一些有用的功能:
| 归档时间: |
|
| 查看次数: |
42428 次 |
| 最近记录: |