forked from Unity-Technologies/EntityComponentSystemSamples
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRayTracerSystem.cs
More file actions
288 lines (249 loc) · 10.6 KB
/
Copy pathRayTracerSystem.cs
File metadata and controls
288 lines (249 loc) · 10.6 KB
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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
using System.Collections.Generic;
using Unity.Physics.Systems;
using Unity.Burst;
using Unity.Collections;
using Unity.Entities;
using Unity.Jobs;
using Unity.Mathematics;
using UnityEngine;
using Unity.Assertions;
namespace Unity.Physics.Extensions
{
[UpdateInGroup(typeof(PhysicsSystemGroup))]
[UpdateAfter(typeof(PhysicsInitializeGroup))]
public partial class RayTracerSystem : SystemBase
{
internal JobHandle FinalJobHandle => Dependency;
internal bool HasRequests => m_Requests.Count > 0;
public struct RayRequest
{
public float3 PinHole;
public float3 ImageCenter;
public float3 Up;
public float3 Right;
public float3 LightDir;
public float RayLength;
public float PlaneHalfExtents;
public float AmbientLight;
public int ImageResolution;
public bool AlternateKeys;
public bool CastSphere;
public bool Shadows;
public CollisionFilter CollisionFilter;
}
public struct RayResult
{
public NativeStream PixelData;
internal bool m_Assigned;
internal JobHandle m_JobHandle;
public void Dispose()
{
if (PixelData.IsCreated)
PixelData.Dispose();
//reset isCreated
PixelData = default;
m_Assigned = false;
m_JobHandle = default;
}
}
List<RayRequest> m_Requests;
List<RayResult> m_Results;
public bool IsEnabled => m_Requests != null;
protected override void OnCreate()
{
m_Requests = new List<RayRequest>();
m_Results = new List<RayResult>();
}
protected override void OnDestroy()
{
for (int i = 0; i < m_Results.Count; ++i)
m_Results[i].Dispose();
m_Requests.Clear();
m_Results.Clear();
}
protected override void OnUpdate()
{
if (m_Requests == null || m_Requests.Count == 0) return;
var world = SystemAPI.GetSingleton<PhysicsWorldSingleton>().PhysicsWorld;
JobHandle combinedJobs = Dependency;
for (int i = 0; i < m_Requests.Count; i++)
{
if (!m_Results[i].m_Assigned)
{
JobHandle rcj = new RaycastJob
{
Results = m_Results[i].PixelData.AsWriter(),
Request = m_Requests[i],
World = world.CollisionWorld,
NumDynamicBodies = world.NumDynamicBodies
}.Schedule(m_Results[i].PixelData.ForEachCount, 1, Dependency);
combinedJobs = JobHandle.CombineDependencies(combinedJobs, rcj);
var res = m_Results[i];
res.m_JobHandle = rcj;
res.m_Assigned = true;
m_Results[i] = res;
}
}
Dependency = combinedJobs;
}
public int AddRequest(RayRequest req)
{
int index = -1;
// Find an empty slot?
for (int i = 0; i < m_Results.Count; i++)
{
if (!m_Results[i].PixelData.IsCreated)
{
index = i;
}
}
// None found so add a new one.
if (index == -1)
{
m_Requests.Add(default);
m_Results.Add(default);
index = m_Results.Count - 1;
}
m_Requests[index] = req;
m_Results[index] = new RayResult { PixelData = new NativeStream(5, Allocator.TempJob) };
return index;
}
public bool DisposeRequest(int index)
{
Assert.IsTrue(0 <= index && index < m_Results.Count);
if (m_Results[index].PixelData.IsCreated)
{
// Mark slot as unused
m_Results[index].m_JobHandle.Complete();
m_Results[index].Dispose();
m_Results[index] = default;
return true;
}
return false;
}
public bool TryGetResults(int index, out NativeStream.Reader results)
{
Assert.IsTrue(0 <= index && index < m_Results.Count);
var res = m_Results[index];
if (res.m_Assigned && res.m_JobHandle.IsCompleted && res.PixelData.IsCreated)
{
results = m_Results[index].PixelData.AsReader();
return true;
}
results = default;
return false;
}
[BurstCompile]
protected struct RaycastJob : IJobParallelFor
{
public NativeStream.Writer Results;
public RayRequest Request;
[ReadOnly] public CollisionWorld World;
public int NumDynamicBodies;
public void Execute(int index)
{
Results.BeginForEachIndex(index);
int numRows = (Request.ImageResolution + Results.ForEachCount - 1) / Results.ForEachCount;
const float sphereRadius = 0.005f;
BlobAssetReference<Collider> sphere = default;
if (Request.CastSphere)
{
sphere = SphereCollider.Create(new SphereGeometry
{
Center = float3.zero,
Radius = sphereRadius
}, Request.CollisionFilter);
}
for (int yCoord = index * numRows; yCoord < math.min(Request.ImageResolution, (index + 1) * numRows); yCoord++)
{
for (int xCoord = 0; xCoord < Request.ImageResolution; xCoord++)
{
float xFrac = 2.0f * ((xCoord / (float)Request.ImageResolution) - 0.5f);
float yFrac = 2.0f * ((yCoord / (float)Request.ImageResolution) - 0.5f);
float3 targetImagePlane = Request.ImageCenter + Request.Up * Request.PlaneHalfExtents * yFrac + Request.Right * Request.PlaneHalfExtents * xFrac;
float3 rayDir = Request.RayLength * (Request.PinHole - targetImagePlane);
RaycastHit hit;
bool hasHit;
if (Request.CastSphere)
{
var input = new ColliderCastInput(sphere, Request.PinHole, Request.PinHole + rayDir);
hasHit = World.CastCollider(input, out ColliderCastHit colliderHit);
hit = new RaycastHit
{
Fraction = colliderHit.Fraction,
Position = colliderHit.Position,
SurfaceNormal = colliderHit.SurfaceNormal,
RigidBodyIndex = colliderHit.RigidBodyIndex,
ColliderKey = colliderHit.ColliderKey
};
}
else
{
var rayCastInput = new RaycastInput
{
Start = Request.PinHole,
End = Request.PinHole + rayDir,
Filter = Request.CollisionFilter
};
hasHit = World.CastRay(rayCastInput, out hit);
}
Color hitColor = Color.black;
if (hasHit)
{
if (hit.RigidBodyIndex < NumDynamicBodies)
{
hitColor = Color.yellow;
}
else
{
hitColor = Color.grey;
}
// Lighten alternate keys
if (Request.AlternateKeys && !hit.ColliderKey.Equals(ColliderKey.Empty))
{
hit.ColliderKey.PopSubKey(World.Bodies[hit.RigidBodyIndex].Collider.Value.NumColliderKeyBits, out uint key);
if (key % 2 == 0)
{
Color.RGBToHSV(hitColor, out float h, out float s, out float v);
hitColor = Color.HSVToRGB(h, s, v + 0.25f);
}
}
if (Request.Shadows)
{
float3 hitPos = Request.PinHole + rayDir * hit.Fraction + hit.SurfaceNormal * 0.001f;
bool shadowHit;
if (Request.CastSphere)
{
var start = hitPos + hit.SurfaceNormal * sphereRadius;
var input = new ColliderCastInput(sphere, start, start + (Request.LightDir * Request.RayLength));
shadowHit = World.CastCollider(input);
}
else
{
var rayCastInput = new RaycastInput
{
Start = hitPos,
End = hitPos + (Request.LightDir * Request.RayLength),
Filter = Request.CollisionFilter
};
shadowHit = World.CastRay(rayCastInput);
}
if (shadowHit)
{
hitColor *= 0.4f;
}
}
}
float lighting = math.min(1.0f, math.max(Request.AmbientLight, Vector3.Dot(hit.SurfaceNormal, Request.LightDir)));
Results.Write(xCoord);
Results.Write(yCoord);
Results.Write(hitColor * lighting);
}
}
if (sphere.IsCreated)
sphere.Dispose();
Results.EndForEachIndex();
}
}
}
}