Skip to content

Commit c72f529

Browse files
committed
Update UnityPhysicsSamples
Compatibility with Unity Physics 0.5.0-preview.1
1 parent ec16fed commit c72f529

File tree

205 files changed

+48571
-22936
lines changed

Some content is hidden

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

205 files changed

+48571
-22936
lines changed

UnityPhysicsSamples/Assets/Common/InputActions.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
#if UNITY_INPUT_SYSTEM_EXISTS
12
// GENERATED AUTOMATICALLY FROM 'Assets/Common/InputActions.inputactions'
23

34
using System;
@@ -6,7 +7,7 @@
67
using UnityEngine.InputSystem;
78
using UnityEngine.InputSystem.Utilities;
89

9-
public class @InputActions : IInputActionCollection, IDisposable
10+
public class @InputActions: IInputActionCollection, IDisposable
1011
{
1112
public InputActionAsset asset { get; }
1213
public @InputActions()
@@ -1112,3 +1113,4 @@ public interface IVehicleActions
11121113
void OnLook(InputAction.CallbackContext context);
11131114
}
11141115
}
1116+
#endif

UnityPhysicsSamples/Assets/Common/Prefabs/Shapes/DynamicCube.prefab

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,8 @@ MeshRenderer:
5757
m_MotionVectors: 1
5858
m_LightProbeUsage: 1
5959
m_ReflectionProbeUsage: 1
60+
m_RayTracingMode: 2
61+
m_RayTraceProcedural: 0
6062
m_RenderingLayerMask: 1
6163
m_RendererPriority: 0
6264
m_Materials:
@@ -68,6 +70,7 @@ MeshRenderer:
6870
m_ProbeAnchor: {fileID: 0}
6971
m_LightProbeVolumeOverride: {fileID: 0}
7072
m_ScaleInLightmap: 1
73+
m_ReceiveGI: 1
7174
m_PreserveUVs: 0
7275
m_IgnoreNormalsForChartDetection: 0
7376
m_ImportantGI: 0
@@ -80,6 +83,7 @@ MeshRenderer:
8083
m_SortingLayerID: 0
8184
m_SortingLayer: 0
8285
m_SortingOrder: 0
86+
m_AdditionalVertexStreams: {fileID: 0}
8387
--- !u!114 &7159071560948202830
8488
MonoBehaviour:
8589
m_ObjectHideFlags: 0
@@ -252,6 +256,7 @@ MonoBehaviour:
252256
m_Name:
253257
m_EditorClassIdentifier:
254258
m_MotionType: 0
259+
m_Smoothing: 1
255260
m_Mass: 1
256261
m_LinearDamping: 0.01
257262
m_AngularDamping: 0.05

UnityPhysicsSamples/Assets/Common/Prefabs/Shapes/DynamicSphere.prefab

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,8 @@ MeshRenderer:
5757
m_MotionVectors: 1
5858
m_LightProbeUsage: 1
5959
m_ReflectionProbeUsage: 1
60+
m_RayTracingMode: 2
61+
m_RayTraceProcedural: 0
6062
m_RenderingLayerMask: 1
6163
m_RendererPriority: 0
6264
m_Materials:
@@ -68,6 +70,7 @@ MeshRenderer:
6870
m_ProbeAnchor: {fileID: 0}
6971
m_LightProbeVolumeOverride: {fileID: 0}
7072
m_ScaleInLightmap: 1
73+
m_ReceiveGI: 1
7174
m_PreserveUVs: 0
7275
m_IgnoreNormalsForChartDetection: 0
7376
m_ImportantGI: 0
@@ -80,6 +83,7 @@ MeshRenderer:
8083
m_SortingLayerID: 0
8184
m_SortingLayer: 0
8285
m_SortingOrder: 0
86+
m_AdditionalVertexStreams: {fileID: 0}
8387
--- !u!114 &2079784548722200720
8488
MonoBehaviour:
8589
m_ObjectHideFlags: 0
@@ -252,6 +256,7 @@ MonoBehaviour:
252256
m_Name:
253257
m_EditorClassIdentifier:
254258
m_MotionType: 0
259+
m_Smoothing: 1
255260
m_Mass: 1
256261
m_LinearDamping: 0.01
257262
m_AngularDamping: 0.05

UnityPhysicsSamples/Assets/Common/Prefabs/Shapes/DynamicTetrahedron.prefab

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,8 @@ MeshRenderer:
5757
m_MotionVectors: 1
5858
m_LightProbeUsage: 1
5959
m_ReflectionProbeUsage: 1
60+
m_RayTracingMode: 2
61+
m_RayTraceProcedural: 0
6062
m_RenderingLayerMask: 1
6163
m_RendererPriority: 0
6264
m_Materials:
@@ -68,6 +70,7 @@ MeshRenderer:
6870
m_ProbeAnchor: {fileID: 0}
6971
m_LightProbeVolumeOverride: {fileID: 0}
7072
m_ScaleInLightmap: 1
73+
m_ReceiveGI: 1
7174
m_PreserveUVs: 0
7275
m_IgnoreNormalsForChartDetection: 0
7376
m_ImportantGI: 0
@@ -80,6 +83,7 @@ MeshRenderer:
8083
m_SortingLayerID: 0
8184
m_SortingLayer: 0
8285
m_SortingOrder: 0
86+
m_AdditionalVertexStreams: {fileID: 0}
8387
--- !u!114 &6750154304470086249
8488
MonoBehaviour:
8589
m_ObjectHideFlags: 0
@@ -252,6 +256,7 @@ MonoBehaviour:
252256
m_Name:
253257
m_EditorClassIdentifier:
254258
m_MotionType: 0
259+
m_Smoothing: 1
255260
m_Mass: 1
256261
m_LinearDamping: 0.01
257262
m_AngularDamping: 0.05

UnityPhysicsSamples/Assets/Common/Scripts/BasePhysicsDemo.cs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@ void OnDisable()
6464
{
6565
Application.logMessageReceivedThreaded -= HandleLogEntry;
6666
}
67+
6768
#endif
6869

6970
protected virtual void Start()
@@ -121,9 +122,9 @@ Entity CreateBody(float3 position, quaternion orientation, BlobAssetReference<Co
121122
{
122123
var entityManager = DefaultWorld.EntityManager;
123124

124-
Entity entity = entityManager.CreateEntity(new ComponentType[] { });
125+
Entity entity = entityManager.CreateEntity(new ComponentType[] {});
125126

126-
entityManager.AddComponentData(entity, new LocalToWorld { });
127+
entityManager.AddComponentData(entity, new LocalToWorld {});
127128
entityManager.AddComponentData(entity, new Translation { Value = position });
128129
entityManager.AddComponentData(entity, new Rotation { Value = orientation });
129130

@@ -166,7 +167,8 @@ protected Entity CreateDynamicBody(float3 position, quaternion orientation, Blob
166167
protected Entity CreateJoint(PhysicsJoint joint, Entity entityA, Entity entityB, bool enableCollision = false)
167168
{
168169
var entityManager = DefaultWorld.EntityManager;
169-
ComponentType[] componentTypes = {
170+
ComponentType[] componentTypes =
171+
{
170172
typeof(PhysicsConstrainedBodyPair),
171173
typeof(PhysicsJoint)
172174
};
@@ -178,7 +180,6 @@ protected Entity CreateJoint(PhysicsJoint joint, Entity entityA, Entity entityB,
178180
return jointEntity;
179181
}
180182

181-
182183
protected RigidTransform GetBodyTransform(Entity entity)
183184
{
184185
var entityManager = DefaultWorld.EntityManager;

UnityPhysicsSamples/Assets/Common/Scripts/CameraControl.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
using UnityEngine;
1+
using UnityEngine;
22

33
public class CameraControl : MonoBehaviour
44
{
@@ -10,7 +10,7 @@ public class CameraControl : MonoBehaviour
1010
private float yaw;
1111
private float pitch;
1212

13-
13+
1414
private void Start()
1515
{
1616
// x - right pitch
Lines changed: 113 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -1,91 +1,141 @@
1-
using Unity.Physics;
1+
using Unity.Collections;
2+
using Unity.Entities;
3+
using Unity.Mathematics;
4+
using Unity.Physics;
25
using Unity.Physics.Extensions;
6+
using Unity.Physics.GraphicsIntegration;
37
using Unity.Physics.Systems;
4-
using Unity.Entities;
8+
using Unity.Transforms;
59
using UnityEngine;
610
using RaycastHit = Unity.Physics.RaycastHit;
711

812
// Camera Utility to smoothly track a specified target from a specified location
913
// Camera location and target are interpolated each frame to remove overly sharp transitions
10-
public class CameraSmoothTrack : MonoBehaviour
14+
public class CameraSmoothTrack : MonoBehaviour, IConvertGameObjectToEntity
1115
{
16+
#pragma warning disable 649
1217
public GameObject Target;
1318
public GameObject LookTo;
14-
[Range(0,1)] public float LookToInterpolateFactor = 0.9f;
19+
[Range(0, 1)] public float LookToInterpolateFactor = 0.9f;
1520

1621
public GameObject LookFrom;
1722
[Range(0, 1)] public float LookFromInterpolateFactor = 0.9f;
23+
#pragma warning restore 649
1824

19-
private Vector3 oldPositionTo;
25+
void OnValidate()
26+
{
27+
LookToInterpolateFactor = math.clamp(LookToInterpolateFactor, 0f, 1f);
28+
LookFromInterpolateFactor = math.clamp(LookFromInterpolateFactor, 0f, 1f);
29+
}
2030

21-
// Start is called before the first frame update
22-
void Start()
31+
public void Convert(Entity entity, EntityManager dstManager, GameObjectConversionSystem conversionSystem)
2332
{
24-
if (LookTo == null)
25-
{
26-
oldPositionTo = (gameObject.transform.position + Vector3.forward);
27-
}
28-
else
33+
dstManager.AddComponentData(entity, new CameraSmoothTrackSettings
2934
{
35+
Target = conversionSystem.GetPrimaryEntity(Target),
36+
LookTo = conversionSystem.GetPrimaryEntity(LookTo),
37+
LookToInteroplateFactor = LookToInterpolateFactor,
38+
LookFrom = conversionSystem.GetPrimaryEntity(LookFrom),
39+
LookFromInterpolateFactor = LookFromInterpolateFactor
40+
});
41+
}
42+
}
3043

31-
oldPositionTo = LookTo.transform.position;
32-
}
44+
struct CameraSmoothTrackSettings : IComponentData
45+
{
46+
public Entity Target;
47+
public Entity LookTo;
48+
public float LookToInteroplateFactor;
49+
public Entity LookFrom;
50+
public float LookFromInterpolateFactor;
51+
public float3 OldPositionTo;
52+
}
53+
54+
[UpdateAfter(typeof(TransformSystemGroup))]
55+
class SmoothlyTrackCameraTarget : SystemBase
56+
{
57+
struct Initialized : ISystemStateComponentData {}
58+
59+
BuildPhysicsWorld m_BuildPhysicsWorld;
60+
RecordMostRecentFixedTime m_RecordMostRecentFixedTime;
61+
62+
protected override void OnCreate()
63+
{
64+
base.OnCreate();
65+
m_BuildPhysicsWorld = World.GetExistingSystem<BuildPhysicsWorld>();
66+
m_RecordMostRecentFixedTime = World.GetExistingSystem<RecordMostRecentFixedTime>();
3367
}
3468

35-
// Update is called once per frame
36-
void LateUpdate()
69+
protected override void OnUpdate()
3770
{
38-
if (!enabled) return;
71+
var commandBuffer = new EntityCommandBuffer(Allocator.TempJob);
72+
Entities
73+
.WithName("InitializeCameraOldPositionsJob")
74+
.WithBurst()
75+
.WithNone<Initialized>()
76+
.ForEach((Entity entity, ref CameraSmoothTrackSettings cameraSmoothTrack, in LocalToWorld localToWorld) =>
77+
{
78+
commandBuffer.AddComponent<Initialized>(entity);
79+
cameraSmoothTrack.OldPositionTo = HasComponent<LocalToWorld>(cameraSmoothTrack.LookTo)
80+
? GetComponent<LocalToWorld>(cameraSmoothTrack.LookTo).Position
81+
: localToWorld.Position + new float3(0f, 0f, 1f);
82+
}).Run();
83+
commandBuffer.Playback(EntityManager);
84+
commandBuffer.Dispose();
3985

40-
Vector3 newPositionFrom = (LookFrom == null) ? gameObject.transform.position : LookFrom.transform.position;
41-
Vector3 newPositionTo = Vector3.forward;
42-
if (LookTo == null)
43-
{
44-
newPositionTo = gameObject.transform.rotation * newPositionTo;
45-
}
46-
else
47-
{
48-
newPositionTo = LookTo.transform.position;
49-
}
86+
PhysicsWorld world = m_BuildPhysicsWorld.PhysicsWorld;
5087

51-
PhysicsWorld world = BasePhysicsDemo.DefaultWorld.GetExistingSystem<BuildPhysicsWorld>().PhysicsWorld;
52-
// check barrier
53-
{
54-
var rayInput = new RaycastInput
55-
{
56-
Start = newPositionFrom,
57-
End = newPositionTo,
58-
Filter = CollisionFilter.Default
59-
};
88+
var timestep = (float)m_RecordMostRecentFixedTime.MostRecentDeltaTime;
89+
var timeAhead = Time.DeltaTime / timestep;
6090

61-
if (world.CastRay(rayInput, out RaycastHit rayResult))
91+
Entities
92+
.WithName("SmoothlyTrackCameraTargetsJob")
93+
.WithoutBurst()
94+
.WithAll<Initialized>()
95+
.WithReadOnly(world)
96+
.ForEach((CameraSmoothTrack monoBehaviour, ref CameraSmoothTrackSettings cameraSmoothTrack, in LocalToWorld localToWorld) =>
6297
{
63-
newPositionFrom = rayResult.Position;
64-
}
65-
}
98+
var worldPosition = (float3)monoBehaviour.transform.position;
6699

67-
if (Target != null)
68-
{
69-
// add velocity
70-
var entityManager = BasePhysicsDemo.DefaultWorld.EntityManager;
71-
var goEntity = Target.GetComponent<GameObjectEntity>();
72-
if ((entityManager != default) && (goEntity != null) && (goEntity.Entity != Entity.Null))
73-
{
74-
Vector3 lv = world.GetLinearVelocity(world.GetRigidBodyIndex(goEntity.Entity));
75-
lv *= Time.fixedDeltaTime;
76-
newPositionFrom += lv;
77-
newPositionTo += lv;
78-
}
79-
}
80-
81-
newPositionFrom = Vector3.Lerp(gameObject.transform.position, newPositionFrom, LookFromInterpolateFactor);
82-
newPositionTo = Vector3.Lerp(oldPositionTo, newPositionTo, LookToInterpolateFactor);
83-
84-
Vector3 newForward = newPositionTo - newPositionFrom;
85-
newForward.Normalize();
86-
Quaternion newRotation = Quaternion.LookRotation(newForward, Vector3.up);
87-
88-
gameObject.transform.SetPositionAndRotation(newPositionFrom, newRotation);
89-
oldPositionTo = newPositionTo;
100+
float3 newPositionFrom = HasComponent<LocalToWorld>(cameraSmoothTrack.LookFrom)
101+
? GetComponent<LocalToWorld>(cameraSmoothTrack.LookFrom).Position
102+
: worldPosition;
103+
104+
float3 newPositionTo = HasComponent<LocalToWorld>(cameraSmoothTrack.LookTo)
105+
? GetComponent<LocalToWorld>(cameraSmoothTrack.LookTo).Position
106+
: worldPosition + localToWorld.Forward;
107+
108+
// check barrier
109+
var rayInput = new RaycastInput
110+
{
111+
Start = newPositionFrom,
112+
End = newPositionTo,
113+
Filter = CollisionFilter.Default
114+
};
115+
116+
if (world.CastRay(rayInput, out RaycastHit rayResult))
117+
{
118+
newPositionFrom = rayResult.Position;
119+
}
120+
121+
if (cameraSmoothTrack.Target != Entity.Null)
122+
{
123+
// add velocity
124+
float3 lv = world.GetLinearVelocity(world.GetRigidBodyIndex(cameraSmoothTrack.Target));
125+
lv *= timeAhead;
126+
newPositionFrom += lv;
127+
newPositionTo += lv;
128+
}
129+
130+
newPositionFrom = math.lerp(worldPosition, newPositionFrom, cameraSmoothTrack.LookFromInterpolateFactor);
131+
newPositionTo = math.lerp(cameraSmoothTrack.OldPositionTo, newPositionTo, cameraSmoothTrack.LookToInteroplateFactor);
132+
133+
float3 newForward = newPositionTo - newPositionFrom;
134+
newForward = math.normalizesafe(newForward);
135+
quaternion newRotation = quaternion.LookRotation(newForward, math.up());
136+
137+
monoBehaviour.transform.SetPositionAndRotation(newPositionFrom, newRotation);
138+
cameraSmoothTrack.OldPositionTo = newPositionTo;
139+
}).Run();
90140
}
91141
}

UnityPhysicsSamples/Assets/Common/Scripts/CameraSmoothTrack.cs.meta

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)