Unity3D和Realsense 2.0 D415-深度到地形

时间:2019-08-29 14:47:05

标签: c# unity3d realsense

我正在尝试从RealSense摄像机D415的深度值生成运行时地形。 我有相机可以尝试在夏天做些事情。以前,我在Unity中使用的网络摄像头从未多过,因此即使经过所有示例场景,我也很迷失。

我的地形是500 x 500 x 40 使用RealSense SDK 2.0 下面是地形生成器代码,可处理Perlin噪声

using UnityEngine;
using Intel.RealSense;

/// <summary>
/// this script is made to test the new terrain in unity with intel realsense depth
/// </summary>
public class TerrainGenerator : MonoBehaviour {

    public int depth = 40;
    public int width = 500;
    public int height = 500;

    public float scale = 20f;

    public float xOffset = 10;
    public float yOffset = 10;
    private DepthFrame depthFrame;
    public bool scroll;
    public float scrollSpeed;

    private Pipeline pipe;
    private void Start()
    {
        ///
        pipe = new Pipeline();
        pipe.Start();


        ///


        xOffset = Random.Range(0f, 9999f);
        yOffset = Random.Range(0f, 9999f);

        scroll = false;
        scrollSpeed = 0;
    }
    void Update()
    {
        Terrain terrain = GetComponent<Terrain>();
        terrain.terrainData = GenerateTerrain(terrain.terrainData);

        if(scroll)
            xOffset += Time.deltaTime * scrollSpeed;

    }
    TerrainData GenerateTerrain(TerrainData tData)
    {
        tData.heightmapResolution = width + 1;
        tData.size = new Vector3(width, depth, height);
        tData.SetHeights(0, 0, GenerateHeights());
        return tData;
    }
    float[,] GenerateHeights()
    {
        float[,] heights = new float[width, height];
        for (int x = 0; x < width; x++)
        {
            for (int y = 0; y < height; y++)
            {

                //heights[x, y] = CalculateHeight(x, y);
                var frames = pipe.WaitForFrames();
                var depth = frames.DepthFrame;
                heights[x, y] = depth.GetDistance(x, y);

            }
        }
        return heights;
    }

    float CalculateHeight(int x, int y)
    {
        //float xCoord = (float)x / width * scale + xOffset;
        //float yCoord = (float)y / height * scale + yOffset;
        //return depthFrame.GetDistance(x,y);
        //return Mathf.PerlinNoise(xCoord, yCoord);

        using (var frames = pipe.WaitForFrames())
        using (var depth = frames.DepthFrame)
            return depth.GetDistance(x, y);
    }

}

我正在寻找有关如何正确设置相机深度的指导。我以前从未在Unity中使用过相机。

1 个答案:

答案 0 :(得分:1)

有点题外话,但我想问一下您的上述代码运行得如何?您是否遇到诸如以下错误:

ExternalException: rs2_pipeline_wait_for_frames(pipe:000000006402E3C0) Rethrow as Exception: Frame didn't arrived within 1000

我正在尝试做类似的事情,也遇到了问题。也许我们可以一起解决?

干杯, 凸轮