Skip to content

Commit ed668fb

Browse files
committed
Add noise
1 parent 47d584f commit ed668fb

File tree

1 file changed

+53
-6
lines changed

1 file changed

+53
-6
lines changed

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

Lines changed: 53 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
using System;
12
using System.Collections;
23
using System.Collections.Generic;
34
using UnityEngine;
@@ -8,6 +9,8 @@
89
using UnityEngine.Jobs;
910
using Unity.Jobs;
1011

12+
using Random = Unity.Mathematics.Random;
13+
1114
namespace UnitySensors
1215
{
1316
public class LivoxSensor : Sensor
@@ -22,6 +25,8 @@ public class LivoxSensor : Sensor
2225
private float _minRange = 0.1f;
2326
[SerializeField]
2427
private float _maxRange = 100.0f;
28+
[SerializeField]
29+
private float _gaussianNoiseSigma = 0.0f;
2530

2631
[SerializeField]
2732
private int _resolution = 100;
@@ -36,11 +41,15 @@ public class LivoxSensor : Sensor
3641
private Texture2D _texture;
3742

3843
private JobHandle _handle;
39-
private TextureToPointsJob _job;
44+
private TextureToPointsJob _textureToPointsJob;
45+
private UpdateGaussianNoisesJob _updateGaussianNoisesJob;
46+
private Random _random;
4047
public NativeArray<Vector3> points;
4148
private NativeArray<Vector3> _directions;
4249
private NativeArray<int> _pixelIndices;
50+
private NativeArray<float> _noises;
4351

52+
private uint _randomSeed;
4453
private int _pointsNum;
4554
public uint pointsNum { get=>(uint)(_pointsNum);}
4655

@@ -109,23 +118,39 @@ private void SetupIndicesAndDirections()
109118
private void SetupJob()
110119
{
111120
points = new NativeArray<Vector3>(_pointsNum, Allocator.Persistent);
112-
_job = new TextureToPointsJob()
121+
_randomSeed = (uint)Environment.TickCount;
122+
_random = new Random(_randomSeed);
123+
124+
_noises = new NativeArray<float>(_pointsNum, Allocator.Persistent);
125+
126+
_updateGaussianNoisesJob = new UpdateGaussianNoisesJob()
127+
{
128+
sigma = _gaussianNoiseSigma,
129+
random = _random,
130+
noises = _noises
131+
};
132+
133+
_textureToPointsJob = new TextureToPointsJob()
113134
{
114135
far = _maxRange,
115136
scanSeparation = _scanSeparation,
116137
separationCounter = 0,
117138
pixelIndices = _pixelIndices,
118139
directions = _directions,
119140
pixels = _texture.GetPixelData<Color>(0),
141+
noises = _noises,
120142
points = points
121143
};
122144
}
123145

124146
protected override void UpdateSensor()
125147
{
126148
_handle.Complete();
127-
_job.separationCounter++;
128-
if (_job.separationCounter >= _scanSeparation) _job.separationCounter = 0;
149+
if (_randomSeed++ == 0) _randomSeed = 1;
150+
_updateGaussianNoisesJob.random.InitState(_randomSeed);
151+
152+
_textureToPointsJob.separationCounter++;
153+
if (_textureToPointsJob.separationCounter >= _scanSeparation) _textureToPointsJob.separationCounter = 0;
129154

130155
AsyncGPUReadback.Request(_rt, 0, request => {
131156
if (request.hasError)
@@ -140,7 +165,8 @@ protected override void UpdateSensor()
140165
}
141166
});
142167

143-
_handle = _job.Schedule(_pointsNum, 1);
168+
JobHandle updateGaussianNoisesJobHandle = _updateGaussianNoisesJob.Schedule(_pointsNum, 1);
169+
_handle = _textureToPointsJob.Schedule(_pointsNum, 1, updateGaussianNoisesJobHandle);
144170

145171
JobHandle.ScheduleBatchedJobs();
146172
}
@@ -153,13 +179,32 @@ public void CompleteJob()
153179
private void OnDestroy()
154180
{
155181
_handle.Complete();
182+
_noises.Dispose();
156183
_pixelIndices.Dispose();
157184
_directions.Dispose();
158185
points.Dispose();
159186

160187
_rt.Release();
161188
}
162189

190+
[BurstCompile]
191+
private struct UpdateGaussianNoisesJob : IJobParallelFor
192+
{
193+
public float sigma;
194+
public Random random;
195+
public NativeArray<float> noises;
196+
197+
public void Execute(int index)
198+
{
199+
var rand2 = random.NextFloat();
200+
var rand3 = random.NextFloat();
201+
float normrand =
202+
(float)Math.Sqrt(-2.0f * Math.Log(rand2)) *
203+
(float)Math.Cos(2.0f * Math.PI * rand3);
204+
noises[index] = sigma * normrand;
205+
}
206+
}
207+
163208
[BurstCompile]
164209
private struct TextureToPointsJob : IJobParallelFor
165210
{
@@ -174,6 +219,8 @@ private struct TextureToPointsJob : IJobParallelFor
174219

175220
[ReadOnly]
176221
public NativeArray<Color> pixels;
222+
[ReadOnly]
223+
public NativeArray<float> noises;
177224

178225
public NativeArray<Vector3> points;
179226

@@ -182,7 +229,7 @@ public void Execute(int index)
182229
int offset = points.Length * separationCounter / scanSeparation;
183230
int pixelIndex = pixelIndices.AsReadOnly()[index + offset];
184231
float distance = pixels.AsReadOnly()[pixelIndex].r;
185-
points[index] = directions.AsReadOnly()[index + offset] * far * Mathf.Clamp01(1.0f - distance);
232+
points[index] = directions.AsReadOnly()[index + offset] * (far * Mathf.Clamp01(1.0f - distance) + noises[index]);
186233
}
187234
}
188235
}

0 commit comments

Comments
 (0)