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
228 lines (202 loc) · 9.41 KB
/
Copy pathRayTracerSystem.cs
File metadata and controls
228 lines (202 loc) · 9.41 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
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;
namespace Unity.Physics.Extensions
{
[UpdateAfter(typeof(BuildPhysicsWorld))]
public class RayTracerSystem : JobComponentSystem
{
BuildPhysicsWorld m_BuildPhysicsWorldSystem;
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;
}
public RayResult AddRequest(RayRequest req)
{
int numWorkItems = 5;
var res = new RayResult { PixelData = new NativeStream(numWorkItems, Allocator.TempJob) };
m_Requests.Add(req);
m_Results.Add(res);
return res;
}
List<RayRequest> m_Requests;
List<RayResult> m_Results;
public bool IsEnabled => m_Requests != null;
[BurstCompile]
protected struct RaycastJob : IJobParallelFor
{
public NativeStream.Writer Results;
public RayRequest Request;
[ReadOnly] public CollisionWorld World;
public int NumDynamicBodies;
public unsafe void Execute(int index)
{
Results.BeginForEachIndex(index);
int numRows = (Request.ImageResolution + Results.ForEachCount - 1) / Results.ForEachCount;
const float sphereRadius = 0.005f;
BlobAssetReference<Collider> sphere;
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
{
Collider = (Collider*)sphere.GetUnsafePtr(),
Orientation = quaternion.identity,
Start = Request.PinHole,
End = 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))
{
Collider* collider = World.Bodies[hit.RigidBodyIndex].Collider;
hit.ColliderKey.PopSubKey(collider->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 = false;
if (Request.CastSphere)
{
var start = hitPos + hit.SurfaceNormal * sphereRadius;
var input = new ColliderCastInput
{
Collider = (Collider*)sphere.GetUnsafePtr(),
Orientation = quaternion.identity,
Start = start,
End = start + (Request.LightDir * Request.RayLength),
};
ColliderCastHit colliderHit;
shadowHit = World.CastCollider(input, out colliderHit);
}
else
{
var rayCastInput = new RaycastInput
{
Start = hitPos,
End = hitPos + (Request.LightDir * Request.RayLength),
Filter = Request.CollisionFilter
};
RaycastHit shadowOutput;
shadowHit = World.CastRay(rayCastInput, out shadowOutput);
}
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);
}
}
Results.EndForEachIndex();
}
}
protected override void OnCreate()
{
m_BuildPhysicsWorldSystem = World.GetOrCreateSystem<BuildPhysicsWorld>();
m_Requests = new List<RayRequest>();
m_Results = new List<RayResult>();
}
protected override JobHandle OnUpdate(JobHandle inputDeps)
{
if (m_Requests == null || m_Requests.Count == 0)
{
return inputDeps;
}
inputDeps = JobHandle.CombineDependencies(inputDeps, m_BuildPhysicsWorldSystem.FinalJobHandle);
JobHandle combinedJobs = inputDeps;
for (int i = 0; i < m_Requests.Count; i++)
{
JobHandle rcj = new RaycastJob
{
Results = m_Results[0].PixelData.AsWriter(),
Request = m_Requests[0],
World = m_BuildPhysicsWorldSystem.PhysicsWorld.CollisionWorld,
NumDynamicBodies = m_BuildPhysicsWorldSystem.PhysicsWorld.NumDynamicBodies
}.Schedule(m_Results[0].PixelData.ForEachCount, 1, inputDeps);
rcj.Complete(); //<todo.eoin How can we properly wait on this task when reading results?
combinedJobs = JobHandle.CombineDependencies(combinedJobs, rcj);
}
m_Requests.Clear();
m_Results.Clear();
return combinedJobs;
}
}
}