This commit is contained in:
Takashi Yoshinaga
2019-09-01 17:11:07 +09:00
parent a4250062ec
commit 40bc3dc596
669 changed files with 157 additions and 240 deletions
+66 -78
View File
@@ -1,7 +1,6 @@
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Microsoft.Azure.Kinect.Sensor.WinForms;
using Microsoft.Azure.Kinect.Sensor;
using System.Runtime.InteropServices;
using System;
@@ -11,16 +10,17 @@ public class KinectScript : MonoBehaviour
{
int depthWidth;
int depthHeight;
Transformation transformation;
int pointNum;
int nearClip = 300;
Mesh mesh;
int visNum;
Vector3[] vertices;
int[] vertexIndeces;
int[] indeces;
Color32[] col;
Texture2D tex;
Texture2D texture;
Transformation transformation;
Device device;
int nearThreshold = 300;
private void OnDestroy()
{
@@ -29,13 +29,8 @@ public class KinectScript : MonoBehaviour
void Start()
{
InitKinect();
InitRenderingInfo();
InitKinect();
InitMesh();
Task t = KinectLoop(device);
}
void InitKinect()
@@ -44,7 +39,7 @@ public class KinectScript : MonoBehaviour
device.StartCameras(new DeviceConfiguration
{
ColorFormat = ImageFormat.ColorBGRA32,
ColorResolution = ColorResolution.R1080p,
ColorResolution = ColorResolution.R720p,
DepthMode = DepthMode.NFOV_Unbinned,
SynchronizedImagesOnly = true,
CameraFPS = FPS.FPS30,
@@ -52,30 +47,29 @@ public class KinectScript : MonoBehaviour
transformation = device.GetCalibration().CreateTransformation();
depthWidth = device.GetCalibration().DepthCameraCalibration.ResolutionWidth;
depthHeight = device.GetCalibration().DepthCameraCalibration.ResolutionHeight;
depthWidth = device.GetCalibration().depth_camera_calibration.resolution_width;
depthHeight = device.GetCalibration().depth_camera_calibration.resolution_height;
pointNum = depthWidth * depthHeight;
}
void InitRenderingInfo()
void InitMesh()
{
mesh = new Mesh();
mesh.indexFormat = UnityEngine.Rendering.IndexFormat.UInt32;
visNum = depthWidth * depthHeight;
tex = new Texture2D(depthWidth, depthHeight);
col = new Color32[visNum];
texture = new Texture2D(depthWidth, depthHeight);
col = new Color32[pointNum];
vertices = new Vector3[visNum];
Vector2[] uv = new Vector2[visNum];
vertices = new Vector3[pointNum];
Vector2[] uv = new Vector2[pointNum];
Vector3[] normals = new Vector3[visNum];
vertexIndeces = new int[6 * (depthWidth - 1) * (depthHeight - 1)];
Vector3[] normals = new Vector3[pointNum];
indeces = new int[6 * (depthWidth - 1) * (depthHeight - 1)];
int index = 0;
for (int y = 0; y < depthHeight; y++)
{
for (int x = 0; x < depthWidth; x++)
{
int index = (y * depthWidth) + x;
uv[index] = new Vector2(((float)(x+0.5f) / (float)(depthWidth)), ((float)(y+0.5f) / ((float)(depthHeight))));
normals[index] = new Vector3(0, -1, 0);
index++;
@@ -85,7 +79,7 @@ public class KinectScript : MonoBehaviour
mesh.vertices = vertices;
mesh.uv = uv;
mesh.normals = normals;
gameObject.GetComponent<MeshRenderer>().materials[0].mainTexture = tex;
gameObject.GetComponent<MeshRenderer>().materials[0].mainTexture = texture;
gameObject.GetComponent<MeshFilter>().mesh = mesh;
}
@@ -100,80 +94,74 @@ public class KinectScript : MonoBehaviour
{
Image modifiedColor = transformation.ColorImageToDepthCamera(capture);
BGRA[] colorArray = modifiedColor.GetPixels<BGRA>().ToArray();
Image hoge = transformation.DepthImageToPointCloud(capture.Depth);
Short3[] PointCloud = hoge.GetPixels<Short3>().ToArray();
int width = depthWidth;
int height = depthHeight;
Image cloudImage = transformation.DepthImageToPointCloud(capture.Depth);
Short3[] PointCloud = cloudImage.GetPixels<Short3>().ToArray();
int triangleIndex = 0;
for (int y = 0; y < height; y++)
int pointIndex = 0;
int topLeft, topRight, bottomLeft, bottomRight;
int tl, tr, bl, br;
for (int y = 0; y < depthHeight; y++)
{
for (int x = 0; x < width; x++)
for (int x = 0; x < depthWidth; x++)
{
int index = (y * width) + x;
vertices[pointIndex].x = PointCloud[pointIndex].X * 0.001f;
vertices[pointIndex].y = -PointCloud[pointIndex].Y * 0.001f;
vertices[pointIndex].z = PointCloud[pointIndex].Z * 0.001f;
vertices[index].x = PointCloud[index].X * 0.001f;
vertices[index].y = -PointCloud[index].Y * 0.001f;
vertices[index].z = PointCloud[index].Z * 0.001f;
col[pointIndex].a = 255;
col[pointIndex].b = colorArray[pointIndex].B;
col[pointIndex].g = colorArray[pointIndex].G;
col[pointIndex].r = colorArray[pointIndex].R;
col[index].a = 255;
col[index].b = colorArray[index].B;
col[index].g = colorArray[index].G;
col[index].r = colorArray[index].R;
if (x != (width - 1) && y != (height - 1))
if (x != (depthWidth - 1) && y != (depthHeight - 1))
{
int topLeft = index;
int topRight = topLeft + 1;
int bottomLeft = topLeft + width;
int bottomRight = bottomLeft + 1;
int tl = PointCloud[index].Z;
int tr = PointCloud[index + 1].Z;
int bl = PointCloud[index + depthWidth].Z;
int br = PointCloud[index + 1 + depthWidth].Z;
if (tl > nearThreshold && tr > nearThreshold && bl > nearThreshold)
topLeft = pointIndex;
topRight = topLeft + 1;
bottomLeft = topLeft + depthWidth;
bottomRight = bottomLeft + 1;
tl = PointCloud[topLeft].Z;
tr = PointCloud[topRight].Z;
bl = PointCloud[bottomLeft].Z;
br = PointCloud[bottomRight].Z;
if (tl > nearClip && tr > nearClip && bl > nearClip)
{
vertexIndeces[triangleIndex++] = topLeft;
vertexIndeces[triangleIndex++] = topRight;
vertexIndeces[triangleIndex++] = bottomLeft;
indeces[triangleIndex++] = topLeft;
indeces[triangleIndex++] = topRight;
indeces[triangleIndex++] = bottomLeft;
}
else
{
vertexIndeces[triangleIndex++] = 0;
vertexIndeces[triangleIndex++] = 0;
vertexIndeces[triangleIndex++] = 0;
indeces[triangleIndex++] = 0;
indeces[triangleIndex++] = 0;
indeces[triangleIndex++] = 0;
}
if (bl > nearThreshold && tr > nearThreshold && br > nearThreshold)
if (bl > nearClip && tr > nearClip && br > nearClip)
{
vertexIndeces[triangleIndex++] = bottomLeft;
vertexIndeces[triangleIndex++] = topRight;
vertexIndeces[triangleIndex++] = bottomRight;
indeces[triangleIndex++] = bottomLeft;
indeces[triangleIndex++] = topRight;
indeces[triangleIndex++] = bottomRight;
}
else
{
vertexIndeces[triangleIndex++] = 0;
vertexIndeces[triangleIndex++] = 0;
vertexIndeces[triangleIndex++] = 0;
indeces[triangleIndex++] = 0;
indeces[triangleIndex++] = 0;
indeces[triangleIndex++] = 0;
}
}
pointIndex++;
}
}
tex.SetPixels32(col);
tex.Apply();
texture.SetPixels32(col);
texture.Apply();
mesh.vertices = vertices;
mesh.triangles = vertexIndeces;
mesh.triangles = indeces;
mesh.RecalculateBounds();
}
}