Skip to content

Commit e1b556a

Browse files
committed
Updated samples for com.unity.physics@0.6.0-preview.3
1 parent 4704432 commit e1b556a

54 files changed

Lines changed: 8773 additions & 597 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

UnityPhysicsSamples/Assets/Common/Scripts/MousePick/MousePickBehaviour.cs

Lines changed: 1 addition & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -13,30 +13,6 @@
1313

1414
namespace Unity.Physics.Extensions
1515
{
16-
public class ColliderUtils
17-
{
18-
[BurstCompile]
19-
public static bool IsTrigger(BlobAssetReference<Collider> collider, ColliderKey key)
20-
{
21-
bool bIsTrigger = false;
22-
unsafe
23-
{
24-
var c = (Collider*)collider.GetUnsafePtr();
25-
{
26-
var cc = ((ConvexCollider*)c);
27-
if (cc->CollisionType != CollisionType.Convex)
28-
{
29-
c->GetLeaf(key, out ChildCollider child);
30-
cc = (ConvexCollider*)child.Collider;
31-
Assert.IsTrue(cc->CollisionType == CollisionType.Convex);
32-
}
33-
bIsTrigger = cc->Material.CollisionResponse == CollisionResponsePolicy.RaiseTriggerEvents;
34-
}
35-
}
36-
return bIsTrigger;
37-
}
38-
}
39-
4016
// A mouse pick collector which stores every hit. Based off the ClosestHitCollector
4117
[BurstCompile]
4218
public struct MousePickCollector : ICollector<RaycastHit>
@@ -71,8 +47,7 @@ public bool AddHit(RaycastHit hit)
7147
var isAcceptable = (hit.RigidBodyIndex >= 0) && (hit.RigidBodyIndex < NumDynamicBodies);
7248
if (IgnoreTriggers)
7349
{
74-
var body = Bodies[hit.RigidBodyIndex];
75-
isAcceptable = isAcceptable && !ColliderUtils.IsTrigger(body.Collider, hit.ColliderKey);
50+
isAcceptable = isAcceptable && hit.Material.CollisionResponse != CollisionResponsePolicy.RaiseTriggerEvents;
7651
}
7752

7853
if (!isAcceptable)

UnityPhysicsSamples/Assets/Common/Scripts/RayTracer/RayTracer.cs

Lines changed: 30 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -18,18 +18,24 @@ public class RayTracer : MonoBehaviour
1818
public float RayLength = 100.0f;
1919
public float AmbientLight = 0.2f;
2020
public GameObject DisplayTarget;
21-
int ImageRes = 100;
21+
22+
int imageRes = 100;
2223
float planeHalfExtents = 5.0f; /// Half extents of the created primitive plane
2324

24-
RayTracerSystem.RayResult lastResults;
25-
bool ExpectingResults;
25+
RayTracerSystem.RayResult nextResults;
2626

2727
private void OnDisable()
2828
{
29-
if (ExpectingResults)
29+
if (nextResults.PixelData.IsCreated)
3030
{
31-
lastResults.PixelData.Dispose();
32-
ExpectingResults = false;
31+
RayTracerSystem rbs;
32+
var world = World.DefaultGameObjectInjectionWorld;
33+
if (world != null && null != (rbs = world.GetExistingSystem<RayTracerSystem>()))
34+
{
35+
// Needed before calling PixelData.Dispose()
36+
rbs.GetOutputDependency().Complete();
37+
}
38+
nextResults.Dispose();
3339
}
3440
}
3541

@@ -50,8 +56,7 @@ void Start()
5056

5157
imageDisplay.GetComponent<MeshRenderer>().shadowCastingMode = ShadowCastingMode.Off;
5258

53-
// For 2019.1: // blasterTexture = new Texture2D(ImageRes, ImageRes, UnityEngine.Experimental.Rendering.GraphicsFormat.R32G32B32A32_UInt , UnityEngine.Experimental.Rendering.TextureCreationFlags.None);
54-
blasterTexture = new Texture2D(ImageRes, ImageRes);
59+
blasterTexture = new Texture2D(imageRes, imageRes);
5560
blasterTexture.filterMode = FilterMode.Point;
5661

5762
blasterMaterial = new UnityEngine.Material(imageDisplay.GetComponent<MeshRenderer>().materials[0]);
@@ -67,12 +72,19 @@ void Start()
6772

6873
void Update()
6974
{
70-
Vector3 imageCenter = transform.TransformPoint(new Vector3(0, 0, -ImagePlane));
75+
if (World.DefaultGameObjectInjectionWorld == null) return;
7176

72-
if (ExpectingResults)
77+
RayTracerSystem rbs = World.DefaultGameObjectInjectionWorld.GetExistingSystem<RayTracerSystem>();
78+
if (rbs == null || !rbs.IsEnabled || !rbs.GetOutputDependency().IsCompleted) return;
79+
80+
if (nextResults.PixelData.IsCreated)
7381
{
74-
NativeStream.Reader reader = lastResults.PixelData.AsReader();
75-
for (int i = 0; i < lastResults.PixelData.ForEachCount; i++)
82+
// rbs.GetOutputDependency().IsCompleted is true here
83+
// so this is only needed to quieten the JobsDebugger?
84+
rbs.GetOutputDependency().Complete();
85+
86+
NativeStream.Reader reader = nextResults.PixelData.AsReader();
87+
for (int i = 0; i < nextResults.PixelData.ForEachCount; i++)
7688
{
7789
reader.BeginForEachIndex(i);
7890
while (reader.RemainingItemCount > 0)
@@ -86,20 +98,13 @@ void Update()
8698
}
8799

88100
blasterTexture.Apply();
89-
lastResults.PixelData.Dispose();
90-
ExpectingResults = false;
91-
}
92101

93-
if (World.DefaultGameObjectInjectionWorld == null)
94-
{
95-
return;
102+
// NOTE: If this logic is moved to the likes of LateUpdate there will be order issues
103+
// with the RayTracerSystem.OnUpdate function as PixelData will be invalid.
104+
nextResults.Dispose();
96105
}
97106

98-
RayTracerSystem rbs = World.DefaultGameObjectInjectionWorld.GetExistingSystem<RayTracerSystem>();
99-
if (rbs == null || !rbs.IsEnabled)
100-
{
101-
return;
102-
}
107+
Vector3 imageCenter = transform.TransformPoint(new Vector3(0, 0, -ImagePlane));
103108

104109
Vector3 lightDir = new Vector3(0, 0, -1);
105110
GameObject sceneLight = GameObject.Find("Directional Light");
@@ -111,7 +116,7 @@ void Update()
111116
Vector3 up = transform.rotation * new Vector3(0, 1, 0);
112117
Vector3 right = transform.rotation * new Vector3(1, 0, 0);
113118

114-
lastResults = rbs.AddRequest(new RayTracerSystem.RayRequest
119+
nextResults = rbs.AddRequest(new RayTracerSystem.RayRequest
115120
{
116121
PinHole = transform.position,
117122
ImageCenter = imageCenter,
@@ -121,13 +126,12 @@ void Update()
121126
RayLength = RayLength,
122127
PlaneHalfExtents = planeHalfExtents,
123128
AmbientLight = AmbientLight,
124-
ImageResolution = ImageRes,
129+
ImageResolution = imageRes,
125130
AlternateKeys = AlternateKeys,
126131
CastSphere = CastSphere,
127132
Shadows = Shadows,
128133
CollisionFilter = CollisionFilter.Default
129134
});
130-
ExpectingResults = true;
131135
}
132136
}
133137
}

UnityPhysicsSamples/Assets/Common/Scripts/RayTracer/RayTracerSystem.cs

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,14 @@
99

1010
namespace Unity.Physics.Extensions
1111
{
12-
[UpdateInGroup(typeof(SimulationSystemGroup))]
12+
[UpdateInGroup(typeof(FixedStepSimulationSystemGroup))]
13+
[UpdateAfter(typeof(BuildPhysicsWorld))]
1314
public class RayTracerSystem : SystemBase
1415
{
1516
BuildPhysicsWorld m_BuildPhysicsWorldSystem;
17+
JobHandle m_OutputDependency;
18+
19+
public JobHandle GetOutputDependency() => m_OutputDependency;
1620

1721
public struct RayRequest
1822
{
@@ -34,6 +38,12 @@ public struct RayRequest
3438
public struct RayResult
3539
{
3640
public NativeStream PixelData;
41+
public void Dispose()
42+
{
43+
PixelData.Dispose();
44+
// setting default will reset PixelData.IsCreated
45+
PixelData = default;
46+
}
3747
}
3848

3949
public RayResult AddRequest(RayRequest req)
@@ -206,9 +216,9 @@ protected override void OnUpdate()
206216
return;
207217
}
208218

209-
var handle = JobHandle.CombineDependencies(Dependency, m_BuildPhysicsWorldSystem.GetOutputDependency());
219+
Dependency = JobHandle.CombineDependencies(Dependency, m_BuildPhysicsWorldSystem.GetOutputDependency());
210220

211-
JobHandle combinedJobs = handle;
221+
JobHandle combinedJobs = Dependency;
212222
for (int i = 0; i < m_Requests.Count; i++)
213223
{
214224
JobHandle rcj = new RaycastJob
@@ -217,15 +227,18 @@ protected override void OnUpdate()
217227
Request = m_Requests[0],
218228
World = m_BuildPhysicsWorldSystem.PhysicsWorld.CollisionWorld,
219229
NumDynamicBodies = m_BuildPhysicsWorldSystem.PhysicsWorld.NumDynamicBodies
220-
}.Schedule(m_Results[0].PixelData.ForEachCount, 1, handle);
221-
rcj.Complete(); //<todo.eoin How can we properly wait on this task when reading results?
230+
}.Schedule(m_Results[0].PixelData.ForEachCount, 1, Dependency);
222231
combinedJobs = JobHandle.CombineDependencies(combinedJobs, rcj);
223232
}
224233

225234
m_Requests.Clear();
226235
m_Results.Clear();
227236

228237
Dependency = combinedJobs;
238+
239+
m_OutputDependency = Dependency;
240+
// Inform next system in the pipeline of its dependency
241+
m_BuildPhysicsWorldSystem.AddInputDependencyToComplete(m_OutputDependency);
229242
}
230243
}
231244
}

UnityPhysicsSamples/Assets/Common/Scripts/SpawnRandomObjectsAuthoring.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -73,9 +73,9 @@ internal virtual int GetRandomSeed(T spawnSettings)
7373
return seed;
7474
}
7575

76-
internal virtual void OnBeforeInstantiatePrefab(T spawnSettings) {}
76+
internal virtual void OnBeforeInstantiatePrefab(ref T spawnSettings) {}
7777

78-
internal virtual void ConfigureInstance(Entity instance, T spawnSettings) {}
78+
internal virtual void ConfigureInstance(Entity instance, ref T spawnSettings) {}
7979

8080
protected override void OnUpdate()
8181
{
@@ -89,7 +89,7 @@ protected override void OnUpdate()
8989

9090
var count = spawnSettings.Count;
9191

92-
OnBeforeInstantiatePrefab(spawnSettings);
92+
OnBeforeInstantiatePrefab(ref spawnSettings);
9393

9494
var instances = new NativeArray<Entity>(count, Allocator.Temp);
9595
EntityManager.Instantiate(spawnSettings.Prefab, instances);
@@ -103,7 +103,7 @@ protected override void OnUpdate()
103103
var instance = instances[i];
104104
EntityManager.SetComponentData(instance, new Translation { Value = positions[i] });
105105
EntityManager.SetComponentData(instance, new Rotation { Value = rotations[i] });
106-
ConfigureInstance(instance, spawnSettings);
106+
ConfigureInstance(instance, ref spawnSettings);
107107
}
108108

109109
EntityManager.RemoveComponent<T>(entity);

0 commit comments

Comments
 (0)