在渲染任何ARCore模型之前进行截图

时间:2019-05-05 05:03:31

标签: unity3d augmented-reality arcore

我正在使用屏幕截图代码来捕获屏幕的屏幕截图,该屏幕截图工作正常,但也可以使用arcore模型。有没有一种方法可以在渲染模型之前进行截图?

我尝试SetActive(false)然后截屏然后SetActive(true),它确实可以工作,但是有一个明显的区别,即模型消失了而不是重新出现了。

更新:这是一个应用在ScreenShotCamera上的脚本,在删除所有错误(由于@Shingo)之后会进行更新,请随时使用它,以使其正常运行

using GoogleARCore;
using OpenCVForUnitySample;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.XR;

[RequireComponent(typeof(Camera))]
public class SnapshotCamera : MonoBehaviour
{
    Camera snapCam;
    public UnityEngine.UI.Text text;
    public RenderTexture mRenderTexture;
    int resWidth=480;
    int resHeight=800;

    // Start is called before the first frame update
    public void initialize(ARBackgroundRenderer background, Material material)
    {
        background = new ARBackgroundRenderer();
        snapCam = GetComponent<Camera>();
        background.backgroundMaterial = material;
        background.camera = snapCam;
        background.mode = ARRenderMode.MaterialAsBackground;
        if (snapCam.targetTexture == null)
        {
            snapCam.targetTexture = new RenderTexture(resWidth, resHeight, 24);
        }
        else
        {
            snapCam.targetTexture.height = resHeight;
            snapCam.targetTexture.width = resWidth;
            //resHeight = snapCam.targetTexture.height;
            //resWidth = snapCam.targetTexture.width;
        }
        background.camera.cullingMask = LayerMask.NameToLayer("Default");
        //snapCam.CopyFrom(background.camera);

        snapCam.gameObject.SetActive(false);
    }

    public void TakeSnapShot()
    {
        snapCam.gameObject.SetActive(true);
    }

    void LateUpdate()
    {
        if (snapCam.gameObject.activeInHierarchy)
        {
            snapCam.cullingMask = LayerMask.NameToLayer("Default");
            if (ARCoreBackgroundRenderer.screenShot == null)
                ARCoreBackgroundRenderer.screenShot = new Texture2D(resWidth, resHeight, TextureFormat.RGB24, false);
            snapCam.Render();
            RenderTexture.active = snapCam.targetTexture;
            ARCoreBackgroundRenderer.screenShot.ReadPixels(new Rect(0, 0, resWidth, resHeight), 0, 0);
            ARCoreBackgroundRenderer.screenShot.Apply();

            snapCam.gameObject.SetActive(false);
            HandPoseRecognition.captureTexture = false;

            //string name = string.Format("{0}_Capture{1}_{2}.png", Application.productName, "{0}", System.DateTime.Now.ToString("yyyy-MM-dd_HH-mm-ss"));
            //UnityEngine.Debug.Log("Permission result: " + NativeGallery.SaveImageToGallery(ARCoreBackgroundRenderer.screenShot, Application.productName + " Captures", name));
        }
    }
}

也许我有点模棱两可,感谢您,您在评论中提到的内容已经解决,但是现在的问题是。 我将向您展示这些图像: 这些是我拥有的2部摄像机: enter image description here

这是我的主摄像机(ARCore摄像机)显示的内容

enter image description here

这就是(ScreenShot摄像机)显示的内容 enter image description here

1 个答案:

答案 0 :(得分:1)

您可以使用图层,将每个arcore模型放置在一个图层(例如ARLAYER)中,然后设置相机的剔除蒙版以避免这些模型。

伪代码:

// Set models' layer
foreach (GameObject arcoreModel in arcoreModels)
     arcoreModel.layer = ARLAYER;

// Set camera's culling mask
camera.cullingMask = ~(1 << ARLAYER);
camera.Render();

从其他相机创建屏幕截图相机

var go = new GameObject("screenshotcamera");
// Copy transform
go.transform.position = mainCamera.transform.position.
...
// Copy camera
var screenshotcamera= go.AddComopnent<Camera>();
screenshotcamera.CopyFrom(mainCamera);

使用脚本更新

snapCam = GetComponent<Camera>();

screenshot