【问题标题】:Second camera doesn't render 3D modle in unity第二台相机不能统一渲染 3D 模型
【发布时间】:2020-05-13 08:23:53
【问题描述】:

我正在使用 Unity 和 Vuforia 创建一个 AR 应用。在我的场景中,我有两个相机。第一个摄像头是ARCamera,第二个摄像头是SecondCamera。第二个Camera 从 AR 相机获取其旋转和位置。它只是移动和旋转比 AR 相机更流畅。

我有一个立方体作为ImageTarget 的孩子。我想只用第二台相机渲染这个立方体。我定义了一个新层为ARCamIgnoreLayer 并将第二个摄像机的Culling Mask 设置为它。另外,我从 AR 相机的 Culling Mask 字段中取消选中ARCamIgnoreLayer。 另外,我在第二台摄像机的target texture 字段中添加了RenderTexture。我将此渲染纹理设置为平面材质的main texture。以这种方式,我将第二台摄像机可以看到的所有东西都投影在这个平面上。

我将这架飞机放置在 Vuforia 的 BackgroundPlane 和 AR 相机之间。这样,我终于可以在 game 窗口(游戏视图)中看到背景中渲染的 AR 相机和前景中渲染的第二个相机。

现在我有两个问题。溶解一个意味着溶解另一个。

  1. 理论上这两个相机的渲染必须相同;因为我将第二个摄像头的field of view 设置为与AR 摄像头相同的编号。此外,正如我所说,这两个相机具有相同的位置和旋转。但他们的渲染不匹配。我不明白它有什么问题。

  2. 为了防止出现第一个问题,我从 AR 摄像头复制了 Camera 组件及其值并将其粘贴到第二个摄像头。我想确保第二个摄像头的 Camera 组件的所有字段与 AR 摄像头的摄像头组件的所有字段具有相同的值。然后我在运行时通过脚本更改了一些值(比如剔除掩码等)。现在第二个相机不渲染任何东西。它只是返回纯色。我也不知道这是怎么回事。

代码如下:

1:

void CreateSecondCam()
{
    CopyComponent(_aRCam.GetComponent<Camera>(), gameObject);

}



T CopyComponent<T>(T original, GameObject destination) where T : Component
{
    System.Type type = original.GetType();
    var dst = destination.GetComponent(type) as T;
    if (!dst) dst = destination.AddComponent(type) as T;
    var fields = type.GetFields();
    foreach (var field in fields)
    {
        if (field.IsStatic) continue;
        field.SetValue(dst, field.GetValue(original));
    }
    var props = type.GetProperties();
    foreach (var prop in props)
    {
        if (!prop.CanWrite || prop.Name == "name") continue;
        prop.SetValue(dst, prop.GetValue(original, null), null);
    }
    return dst as T;
}

这部分脚本从 AR 摄像头复制摄像头组件并将其粘贴到第二个摄像头。

2:

void AssignSomeValues()
    {
        gameObject.GetComponent<Camera>().targetTexture = _chromaKeyRenderTexrture;
        gameObject.GetComponent<Camera>().backgroundColor = _defaultCameraBackgroundColor;
        gameObject.GetComponent<Camera>().cullingMask = 9 << 8;
    }

此函数为第二个相机的相机组件的某些字段分配一些值。

3:

void Update()
{
    gameObject.transform.position = Vector3.Lerp(gameObject.transform.position, _aRCam.transform.position, _softness * Time.deltaTime);
    gameObject.transform.rotation = Quaternion.Slerp(gameObject.transform.rotation, _aRCam.transform.rotation, _softness * Time.deltaTime);
}

我已经在Update 函数中根据 AR 摄像头位置和旋转移动和旋转了第二个摄像头。

以下是截图:

1:

增强现实相机

2:

第二个摄像头

3:

立方体

【问题讨论】:

    标签: c# unity3d camera augmented-reality vuforia


    【解决方案1】:

    我解决了。我更改了我的 AR 相机的Depth。我将 AR 相机的深度设置为 -10。现在第二个摄像头视口出现在 AR 摄像头视口的前面。它完美地工作。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-02-07
      • 1970-01-01
      • 1970-01-01
      • 2015-11-15
      • 1970-01-01
      • 2017-08-05
      • 2013-03-25
      相关资源
      最近更新 更多