Realsense 2.0 D415 - Depth to Terrain - Unity C#
I am trying to generate a runtime terrain from the depth values of the realsense camera D415.
I just got the camera to try and do something for the summer. never used more than webcams in unity before so im very lost even after going through all the sample scenes.
my terrain is 500 x 500 x 40
using realsense sdk 2.0
Here is the code below. IF i run this unity goes full silent treatment on my incompetence im very sure im doing something gravely wrong. any help is greatly appreciated. Right now the code works with Perlin Noise. I just need to find out how to get the depth values (z values) for the xand y position.
-
using UnityEngine;
-
using Intel.RealSense;
-
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.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.SetHeights(0, 0, GenerateHeights());
-
return tData;
-
}
-
float[,] GenerateHeights()
-
{
-
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);
-
}
-
-
}
-
I found a case where a RealSense user generated a depth map in Unity and RealSense SDK 2.0 with C#.
Please sign in to leave a comment.
Comments
1 comment