프로젝트

일반

사용자정보

통계
| 개정판:

t1 / TFDContents / Assets / KinectScripts / Samples / PointCloudView.cs @ 3

이력 | 보기 | 이력해설 | 다운로드 (4.35 KB)

1
using UnityEngine;
2
using System.Collections;
3

    
4

    
5
public class PointCloudView : MonoBehaviour
6
{
7
	[Tooltip("Target mesh width in meters.")]
8
	public float meshWidth = 5.12f;
9

    
10
	[Tooltip("Target mesh height in meters.")]
11
	public float meshHeight = 4.24f;
12

    
13

    
14
    private Mesh mesh;
15
    private Vector3[] vertices;
16
    private Vector2[] uvs;
17
    private int[] triangles;
18

    
19
	private KinectManager manager = null;
20

    
21
	private Vector2[] colorCoords = null;
22
	private ushort[] depthData = null;
23

    
24
	private int depthWidth = 0;
25
	private int depthHeight = 0;
26
	private int colorWidth = 0;
27
	private int colorHeight = 0;
28

    
29
	private const int SampleSize = 2;
30
	
31

    
32
    void Start()
33
    {
34
		manager = KinectManager.Instance;
35

    
36
		if (manager != null)
37
        {
38
			depthWidth = manager.GetDepthImageWidth();
39
			depthHeight = manager.GetDepthImageHeight();
40
			
41
			colorWidth = manager.GetColorImageWidth();
42
			colorHeight = manager.GetColorImageHeight();
43
			
44
			CreateMesh(depthWidth / SampleSize, depthHeight / SampleSize);
45
        }
46
    }
47

    
48
    void CreateMesh(int width, int height)
49
    {
50
        mesh = new Mesh();
51
        GetComponent<MeshFilter>().mesh = mesh;
52

    
53
        vertices = new Vector3[width * height];
54
        uvs = new Vector2[width * height];
55
        triangles = new int[6 * ((width - 1) * (height - 1))];
56

    
57
		float scaleX = meshWidth / width;
58
		float scaleY = meshHeight / height;
59

    
60
		float centerX = meshWidth / 2;
61
		float centerY = meshHeight / 2;
62

    
63
        int triangleIndex = 0;
64
        for (int y = 0; y < height; y++)
65
        {
66
            for (int x = 0; x < width; x++)
67
            {
68
                int index = (y * width) + x;
69

    
70
				float xScaled = x * scaleX - centerX;
71
				float yScaled = y * scaleY - centerY;
72

    
73
				vertices[index] = new Vector3(xScaled, -yScaled, 0);
74
                uvs[index] = new Vector2(((float)x / (float)width), ((float)y / (float)height));
75

    
76
                // Skip the last row/col
77
                if (x != (width - 1) && y != (height - 1))
78
                {
79
                    int topLeft = index;
80
                    int topRight = topLeft + 1;
81
                    int bottomLeft = topLeft + width;
82
                    int bottomRight = bottomLeft + 1;
83

    
84
                    triangles[triangleIndex++] = topLeft;
85
                    triangles[triangleIndex++] = topRight;
86
                    triangles[triangleIndex++] = bottomLeft;
87
                    triangles[triangleIndex++] = bottomLeft;
88
                    triangles[triangleIndex++] = topRight;
89
                    triangles[triangleIndex++] = bottomRight;
90
                }
91
            }
92
        }
93

    
94
        mesh.vertices = vertices;
95
        mesh.uv = uvs;
96
        mesh.triangles = triangles;
97
        mesh.RecalculateNormals();
98
    }
99
    
100
    void Update()
101
    {
102
        if (manager == null)
103
            return;
104

    
105
		// get color texture
106
		gameObject.GetComponent<Renderer>().material.mainTexture = manager.GetUsersClrTex();
107

    
108
		// update the mesh
109
		UpdateMesh();
110
    }
111
    
112
    private void UpdateMesh()
113
    {
114
		if(manager.MapDepthFrameToColorCoords(ref colorCoords))
115
		{
116
			depthData = manager.GetRawDepthMap();
117

    
118
			for (int y = 0; y < depthHeight; y += SampleSize)
119
			{
120
				for (int x = 0; x < depthWidth; x += SampleSize)
121
				{
122
					int indexX = x / SampleSize;
123
					int indexY = y / SampleSize;
124
					int smallIndex = (indexY * (depthWidth / SampleSize)) + indexX;
125
					
126
					float avg = GetAvg(depthData, x, y);
127
					vertices[smallIndex].z = avg;
128
					
129
					// Update UV mapping with CDRP
130
					Vector2 colorCoord = colorCoords[(y * depthWidth) + x];
131
					uvs[smallIndex] = new Vector2(colorCoord.x / colorWidth, colorCoord.y / colorHeight);
132
				}
133
			}
134
			
135
			mesh.vertices = vertices;
136
			mesh.uv = uvs;
137
			mesh.triangles = triangles;
138
			mesh.RecalculateNormals();
139
		}
140
        
141
    }
142
    
143
    private float GetAvg(ushort[] depthData, int x, int y)
144
    {
145
        float sum = 0f;
146
        
147
		for (int y1 = y; y1 < y + SampleSize; y1++)
148
        {
149
			for (int x1 = x; x1 < x + SampleSize; x1++)
150
            {
151
                int fullIndex = (y1 * depthWidth) + x1;
152
                
153
                if (depthData[fullIndex] == 0)
154
                    sum += 4500;
155
                else
156
                    sum += depthData[fullIndex];
157
            }
158
        }
159

    
160
		return sum / (1000f * SampleSize * SampleSize);
161
    }
162

    
163
}