t1 / TFDContents / Assets / Resources / CoordMapper.compute @ 9
이력 | 보기 | 이력해설 | 다운로드 (2.89 KB)
1 | 3 | KTH | // Each #kernel tells which function to compile; you can have many kernels |
---|---|---|---|
2 | #pragma kernel MapDepthFrame2ColorFrame |
||
3 | #pragma kernel MapColorSpace2DepthFrame |
||
4 | |||
5 | |||
6 | float2 depthFocalLength; |
||
7 | float2 depthPrincipalPoint; |
||
8 | float3 depthRadialDistortion; |
||
9 | |||
10 | float2 colorFocalLength; |
||
11 | float2 colorPrincipalPoint; |
||
12 | float3 colorRadialDistortion; |
||
13 | |||
14 | float4x4 depth2colorMat; |
||
15 | float4x4 color2depthMat; |
||
16 | |||
17 | StructuredBuffer<float2> depthPlaneCoords; |
||
18 | StructuredBuffer<int> depthDepthValues; |
||
19 | RWStructuredBuffer<float2> colorPlaneCoords; |
||
20 | |||
21 | StructuredBuffer<half3> colorSpaceCoords; |
||
22 | RWStructuredBuffer<half2> colorDepthCoords; |
||
23 | |||
24 | |||
25 | [numthreads(64,1,1)] |
||
26 | void MapDepthFrame2ColorFrame (uint3 id : SV_DispatchThreadID) |
||
27 | { |
||
28 | int idx = id.x; |
||
29 | float depth = (float)depthDepthValues[idx] / 1000.0; |
||
30 | |||
31 | // unproject from depth plane |
||
32 | float x = (depthPlaneCoords[idx].x - depthPrincipalPoint.x) / depthFocalLength.x; |
||
33 | float y = (depthPrincipalPoint.y - depthPlaneCoords[idx].y) / depthFocalLength.y; |
||
34 | |||
35 | // undistort |
||
36 | float r = x * x + y * y; |
||
37 | float d = 1 - depthRadialDistortion.x * r - depthRadialDistortion.y * r * r - depthRadialDistortion.z * r * r * r; |
||
38 | |||
39 | // depth space coords |
||
40 | float4 depthSpacePoint = float4(x * d * depth, y * d * depth, depth, 1.0); |
||
41 | |||
42 | // color space coords |
||
43 | float4 colorSpacePoint = mul(depth2colorMat, depthSpacePoint); |
||
44 | |||
45 | // project on color plane |
||
46 | float u = colorFocalLength.x * colorSpacePoint.x / colorSpacePoint.z + colorPrincipalPoint.x; |
||
47 | float v = colorPrincipalPoint.y - (colorFocalLength.y * colorSpacePoint.y / colorSpacePoint.z); |
||
48 | |||
49 | // distort |
||
50 | u = (u - colorPrincipalPoint.x) / colorFocalLength.x; |
||
51 | v = (colorPrincipalPoint.y - v) / colorFocalLength.y; |
||
52 | |||
53 | r = u * u + v * v; |
||
54 | d = 1 + colorRadialDistortion.x * r + colorRadialDistortion.y * r * r + colorRadialDistortion.z * r * r * r; |
||
55 | |||
56 | u = u * d * colorFocalLength.x + colorPrincipalPoint.x; |
||
57 | v = colorPrincipalPoint.y - v * d * colorFocalLength.y; |
||
58 | |||
59 | colorPlaneCoords[idx] = float2(u, v); |
||
60 | } |
||
61 | |||
62 | |||
63 | [numthreads(64,1,1)] |
||
64 | void MapColorSpace2DepthFrame (uint3 id : SV_DispatchThreadID) |
||
65 | { |
||
66 | int idx = id.x; |
||
67 | |||
68 | // color space coords |
||
69 | float4 colorSpacePoint = float4((float3)colorSpaceCoords[idx], 1.0); |
||
70 | |||
71 | // depth space coords |
||
72 | float4 depthSpacePoint = mul(color2depthMat, colorSpacePoint); |
||
73 | |||
74 | // project on depth plane |
||
75 | float u = depthFocalLength.x * depthSpacePoint.x / depthSpacePoint.z + depthPrincipalPoint.x; |
||
76 | float v = depthPrincipalPoint.y - (depthFocalLength.y * depthSpacePoint.y / depthSpacePoint.z); |
||
77 | |||
78 | // distort |
||
79 | u = (u - depthPrincipalPoint.x) / depthFocalLength.x; |
||
80 | v = (depthPrincipalPoint.y - v) / depthFocalLength.y; |
||
81 | |||
82 | float r = u * u + v * v; |
||
83 | float d = 1 + depthRadialDistortion.x * r + depthRadialDistortion.y * r * r + depthRadialDistortion.z * r * r * r; |
||
84 | |||
85 | u = u * d * depthFocalLength.x + depthPrincipalPoint.x; |
||
86 | v = depthPrincipalPoint.y - v * d * depthFocalLength.y; |
||
87 | |||
88 | colorDepthCoords[idx] = half2(u, v); |
||
89 | } |
||
90 |