Skip to content

Commit 96b1ebb

Browse files
committed
Intensity calc
1 parent ed668fb commit 96b1ebb

File tree

1 file changed

+31
-9
lines changed

1 file changed

+31
-9
lines changed

Assets/UnitySensors/Runtime/Scripts/Sensors/LiDAR/Livox/LivoxSensor.cs

Lines changed: 31 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,11 @@ public class LivoxSensor : Sensor
2222
private int _scanSeparation = 40;
2323

2424
[SerializeField]
25-
private float _minRange = 0.1f;
25+
private float _minDistance = 0.1f;
2626
[SerializeField]
27-
private float _maxRange = 100.0f;
27+
private float _maxDistance = 100.0f;
28+
[SerializeField]
29+
private float _maxIntensity = 255.0f;
2830
[SerializeField]
2931
private float _gaussianNoiseSigma = 0.0f;
3032

@@ -45,6 +47,7 @@ public class LivoxSensor : Sensor
4547
private UpdateGaussianNoisesJob _updateGaussianNoisesJob;
4648
private Random _random;
4749
public NativeArray<Vector3> points;
50+
public NativeArray<float> intensities;
4851
private NativeArray<Vector3> _directions;
4952
private NativeArray<int> _pixelIndices;
5053
private NativeArray<float> _noises;
@@ -89,8 +92,8 @@ private void SetupCamera()
8992

9093
_cam.targetTexture = _rt;
9194
_cam.fieldOfView = fov;
92-
_cam.nearClipPlane = _minRange;
93-
_cam.farClipPlane = _maxRange;
95+
_cam.nearClipPlane = _minDistance;
96+
_cam.farClipPlane = _maxDistance;
9497
_cam.gameObject.AddComponent<DepthCamera>();
9598
_cam.clearFlags = CameraClearFlags.SolidColor;
9699

@@ -118,6 +121,7 @@ private void SetupIndicesAndDirections()
118121
private void SetupJob()
119122
{
120123
points = new NativeArray<Vector3>(_pointsNum, Allocator.Persistent);
124+
intensities = new NativeArray<float>(_pointsNum, Allocator.Persistent);
121125
_randomSeed = (uint)Environment.TickCount;
122126
_random = new Random(_randomSeed);
123127

@@ -132,14 +136,18 @@ private void SetupJob()
132136

133137
_textureToPointsJob = new TextureToPointsJob()
134138
{
135-
far = _maxRange,
136139
scanSeparation = _scanSeparation,
137140
separationCounter = 0,
141+
minDistance = _minDistance,
142+
minDistance_sqr = _minDistance * _minDistance,
143+
maxDistance = _maxDistance,
144+
maxIntensity = _maxIntensity,
138145
pixelIndices = _pixelIndices,
139146
directions = _directions,
140147
pixels = _texture.GetPixelData<Color>(0),
141148
noises = _noises,
142-
points = points
149+
points = points,
150+
intensities = intensities
143151
};
144152
}
145153

@@ -183,6 +191,7 @@ private void OnDestroy()
183191
_pixelIndices.Dispose();
184192
_directions.Dispose();
185193
points.Dispose();
194+
intensities.Dispose();
186195

187196
_rt.Release();
188197
}
@@ -208,10 +217,18 @@ public void Execute(int index)
208217
[BurstCompile]
209218
private struct TextureToPointsJob : IJobParallelFor
210219
{
211-
public float far;
212220
public int scanSeparation;
213221
public int separationCounter;
214222

223+
[ReadOnly]
224+
public float minDistance;
225+
[ReadOnly]
226+
public float minDistance_sqr;
227+
[ReadOnly]
228+
public float maxDistance;
229+
[ReadOnly]
230+
public float maxIntensity;
231+
215232
[ReadOnly]
216233
public NativeArray<int> pixelIndices;
217234
[ReadOnly]
@@ -223,13 +240,18 @@ private struct TextureToPointsJob : IJobParallelFor
223240
public NativeArray<float> noises;
224241

225242
public NativeArray<Vector3> points;
243+
public NativeArray<float> intensities;
226244

227245
public void Execute(int index)
228246
{
229247
int offset = points.Length * separationCounter / scanSeparation;
230248
int pixelIndex = pixelIndices.AsReadOnly()[index + offset];
231-
float distance = pixels.AsReadOnly()[pixelIndex].r;
232-
points[index] = directions.AsReadOnly()[index + offset] * (far * Mathf.Clamp01(1.0f - distance) + noises[index]);
249+
float distance = maxDistance * Mathf.Clamp01(1.0f - pixels.AsReadOnly()[pixelIndex].r) + noises[index];
250+
bool isValid = (minDistance <= distance && distance <= maxDistance);
251+
if (!isValid) distance = 0;
252+
points[index] = directions.AsReadOnly()[index + offset] * distance;
253+
float distance_sqr = distance * distance;
254+
intensities[index] = isValid ? maxIntensity * minDistance_sqr / distance_sqr : 0;
233255
}
234256
}
235257
}

0 commit comments

Comments
 (0)