1
+ using System ;
1
2
using System . Collections ;
2
3
using System . Collections . Generic ;
3
4
using UnityEngine ;
8
9
using UnityEngine . Jobs ;
9
10
using Unity . Jobs ;
10
11
12
+ using Random = Unity . Mathematics . Random ;
13
+
11
14
namespace UnitySensors
12
15
{
13
16
public class LivoxSensor : Sensor
@@ -22,6 +25,8 @@ public class LivoxSensor : Sensor
22
25
private float _minRange = 0.1f ;
23
26
[ SerializeField ]
24
27
private float _maxRange = 100.0f ;
28
+ [ SerializeField ]
29
+ private float _gaussianNoiseSigma = 0.0f ;
25
30
26
31
[ SerializeField ]
27
32
private int _resolution = 100 ;
@@ -36,11 +41,15 @@ public class LivoxSensor : Sensor
36
41
private Texture2D _texture ;
37
42
38
43
private JobHandle _handle ;
39
- private TextureToPointsJob _job ;
44
+ private TextureToPointsJob _textureToPointsJob ;
45
+ private UpdateGaussianNoisesJob _updateGaussianNoisesJob ;
46
+ private Random _random ;
40
47
public NativeArray < Vector3 > points ;
41
48
private NativeArray < Vector3 > _directions ;
42
49
private NativeArray < int > _pixelIndices ;
50
+ private NativeArray < float > _noises ;
43
51
52
+ private uint _randomSeed ;
44
53
private int _pointsNum ;
45
54
public uint pointsNum { get => ( uint ) ( _pointsNum ) ; }
46
55
@@ -109,23 +118,39 @@ private void SetupIndicesAndDirections()
109
118
private void SetupJob ( )
110
119
{
111
120
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 ( )
113
134
{
114
135
far = _maxRange ,
115
136
scanSeparation = _scanSeparation ,
116
137
separationCounter = 0 ,
117
138
pixelIndices = _pixelIndices ,
118
139
directions = _directions ,
119
140
pixels = _texture . GetPixelData < Color > ( 0 ) ,
141
+ noises = _noises ,
120
142
points = points
121
143
} ;
122
144
}
123
145
124
146
protected override void UpdateSensor ( )
125
147
{
126
148
_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 ;
129
154
130
155
AsyncGPUReadback . Request ( _rt , 0 , request => {
131
156
if ( request . hasError )
@@ -140,7 +165,8 @@ protected override void UpdateSensor()
140
165
}
141
166
} ) ;
142
167
143
- _handle = _job . Schedule ( _pointsNum , 1 ) ;
168
+ JobHandle updateGaussianNoisesJobHandle = _updateGaussianNoisesJob . Schedule ( _pointsNum , 1 ) ;
169
+ _handle = _textureToPointsJob . Schedule ( _pointsNum , 1 , updateGaussianNoisesJobHandle ) ;
144
170
145
171
JobHandle . ScheduleBatchedJobs ( ) ;
146
172
}
@@ -153,13 +179,32 @@ public void CompleteJob()
153
179
private void OnDestroy ( )
154
180
{
155
181
_handle . Complete ( ) ;
182
+ _noises . Dispose ( ) ;
156
183
_pixelIndices . Dispose ( ) ;
157
184
_directions . Dispose ( ) ;
158
185
points . Dispose ( ) ;
159
186
160
187
_rt . Release ( ) ;
161
188
}
162
189
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
+
163
208
[ BurstCompile ]
164
209
private struct TextureToPointsJob : IJobParallelFor
165
210
{
@@ -174,6 +219,8 @@ private struct TextureToPointsJob : IJobParallelFor
174
219
175
220
[ ReadOnly ]
176
221
public NativeArray < Color > pixels ;
222
+ [ ReadOnly ]
223
+ public NativeArray < float > noises ;
177
224
178
225
public NativeArray < Vector3 > points ;
179
226
@@ -182,7 +229,7 @@ public void Execute(int index)
182
229
int offset = points . Length * separationCounter / scanSeparation ;
183
230
int pixelIndex = pixelIndices . AsReadOnly ( ) [ index + offset ] ;
184
231
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 ] ) ;
186
233
}
187
234
}
188
235
}
0 commit comments