【问题标题】:WebCamTexture not displayed in Unity3D?WebCamTexture 未在 Unity3D 中显示?
【发布时间】:2014-08-16 12:08:59
【问题描述】:

我正在尝试使用WebCamTexture 在场景中显示网络摄像头输入。我创建了一个带有一些默认纹理的 Sprite,并为其附加了以下脚本:

public class CameraTexture : MonoBehaviour {

    void Start () {
        WebCamTexture webcamTexture = new WebCamTexture();
        renderer.material.mainTexture = webcamTexture;
        webcamTexture.Play();
    }
}

但是,当我(在 PC 上)运行场景时,不显示相机输入,只显示默认纹理。 以下是我所知道的:

  • webcamTexture 不为空,设备已正确找到
  • 摄像头设备工作正常
  • 其他应用没有挡住摄像头

为什么不显示摄像头输入?

【问题讨论】:

    标签: c# camera unity3d


    【解决方案1】:

    我自己找到了解决方案。

    webcamTexture 无法在 Sprite 上显示。 我不得不创建一个 GUITexture,然后我附加了相同的脚本,一切正常。

    【讨论】:

      【解决方案2】:

      实际上并不完全正确,我创建了一个基于 Camera Capture Kit 的小示例,它是渲染 Native Camera feed (iOS/Android) 并将快照/照片拍摄到纹理的资产 - 这使得可以渲染到 Sprite。

      这里是如何渲染到精灵的示例

      http://stackoverflow.com/questions/30190333/add-renderable-webcamtexture-in-spriterender-unity2d/36889806#36889806
      

      基本上你要做的是确保相机的输入设置在自定义着色器中的辅助材质纹理单元上,然后创建一个具有空白纹理的精灵,并为精灵渲染器提供参考到纹理。

      【讨论】:

        【解决方案3】:
        Texture2D texty = new Texture2D (webcamTextures.width, webcamTextures.height, TextureFormat.RGB24, false);
        texty.SetPixels (webcamTextures.GetPixels());
        texty.Apply ();
        
        Rect newRect = new Rect (0f, 0f, texty.width, texty.height);
        Vector2 pivot = new Vector2 (0.5f, 0.5f);
        imageUI.sprite = Sprite.Create (texty, newRect, pivot, 100f);
        

        【讨论】:

          猜你喜欢
          • 2021-06-16
          • 2013-12-06
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2019-07-18
          • 2017-06-11
          • 2014-09-13
          相关资源
          最近更新 更多