-
Notifications
You must be signed in to change notification settings - Fork 157
/
PointCloudProcessing.cs
116 lines (93 loc) · 3.19 KB
/
PointCloudProcessing.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
using UnityEngine;
using Unity.Collections;
using Unity.Jobs;
using Random = UnityEngine.Random;
public class PointCloudProcessing : MonoBehaviour
{
public int pointCount = 10000;
const float k_PointCloudRadius = 5f;
NativeArray<Vector3> m_PointCloud;
NativeArray<Vector3> m_NormalizedPointCloud;
NativeArray<float> m_SquareMagnitudes;
GeneratePointCloudJob m_GenPointCloudJob;
CalculateDistancesJob m_DistancesJob;
NormalizationJob m_NormalizeJob;
JobHandle m_GeneratePointsJobHandle;
JobHandle m_DistancesJobHandle;
JobHandle m_NormalizeJobHandle;
protected void Start()
{
m_PointCloud = new NativeArray<Vector3>(pointCount, Allocator.Persistent);
m_NormalizedPointCloud = new NativeArray<Vector3>(pointCount, Allocator.Persistent);
m_SquareMagnitudes = new NativeArray<float>(pointCount, Allocator.Persistent);
}
// in most cases we would of course not be generating a point cloud, but
// taking one in. this simulates a large point cloud being constantly updated
struct GeneratePointCloudJob : IJobParallelFor
{
public NativeArray<Vector3> pointCloud;
[ReadOnly]
public Vector3 center;
[ReadOnly]
public float radius;
public void Execute(int i)
{
pointCloud[i] = center + Vector3.one * (i / 100) * radius;
}
}
// turns that point cloud into normalized versions from 0-1
struct NormalizationJob : IJobParallelFor
{
[ReadOnly]
public NativeArray<Vector3> points;
public NativeArray<Vector3> normalizedPoints;
public void Execute(int i)
{
normalizedPoints[i] = points[i].normalized;
}
}
// calculate the square magnitudes of the non-normalized points
struct CalculateDistancesJob : IJobParallelFor
{
[ReadOnly]
public NativeArray<Vector3> points;
public NativeArray<float> squareMagnitudes;
public void Execute(int i)
{
squareMagnitudes[i] = points[i].sqrMagnitude;
}
}
public void LateUpdate()
{
m_DistancesJobHandle.Complete();
m_NormalizeJobHandle.Complete();
}
public void Update()
{
m_GenPointCloudJob = new GeneratePointCloudJob()
{
pointCloud = m_PointCloud,
center = Random.insideUnitSphere * 2,
radius = k_PointCloudRadius
};
m_DistancesJob = new CalculateDistancesJob()
{
points = m_PointCloud,
squareMagnitudes = m_SquareMagnitudes
};
m_NormalizeJob = new NormalizationJob()
{
points = m_PointCloud,
normalizedPoints = m_NormalizedPointCloud,
};
m_GeneratePointsJobHandle = m_GenPointCloudJob.Schedule(m_PointCloud.Length, 64);
m_DistancesJobHandle = m_DistancesJob.Schedule(m_PointCloud.Length, 64, m_GeneratePointsJobHandle);
m_NormalizeJobHandle = m_NormalizeJob.Schedule(m_PointCloud.Length, 64, m_GeneratePointsJobHandle);
}
private void OnDestroy()
{
m_NormalizedPointCloud.Dispose();
m_PointCloud.Dispose();
m_SquareMagnitudes.Dispose();
}
}