forked from Unity-Technologies/EntityComponentSystemSamples
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBasePhysicsDemo.cs
More file actions
189 lines (158 loc) · 6.85 KB
/
Copy pathBasePhysicsDemo.cs
File metadata and controls
189 lines (158 loc) · 6.85 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
using System;
using System.Collections;
using System.Collections.Generic;
using System.Reflection;
using Unity.Entities;
using Unity.Mathematics;
using Unity.Physics;
using Unity.Physics.Authoring;
using Unity.Physics.Extensions;
using Unity.Physics.Systems;
using Unity.Rendering;
using Unity.Transforms;
using UnityEngine;
using Collider = Unity.Physics.Collider;
using Material = UnityEngine.Material;
using Mesh = UnityEngine.Mesh;
/// <summary>
/// Helper for demos set up in C# rather than in the editor
/// </summary>
public class BasePhysicsDemo : MonoBehaviour
{
public static World DefaultWorld => World.DefaultGameObjectInjectionWorld;
public static void ResetDefaultWorld()
{
DefaultWorld.EntityManager.CompleteAllJobs();
foreach (var system in DefaultWorld.Systems)
{
system.Enabled = false;
}
DefaultWorld.Dispose();
DefaultWorldInitialization.Initialize("Default World", false);
}
public Material dynamicMaterial;
public Material staticMaterial;
protected void init()
{
// Load assets
//dynamicMaterial = (Material)Resources.Load("Materials/PhysicsDynamicMaterial");
//staticMaterial = (Material)Resources.Load("Materials/PhysicsStaticMaterial");
}
#if !UNITY_EDITOR
void OnEnable()
{
Application.logMessageReceivedThreaded += HandleLogEntry;
}
void HandleLogEntry(string logEntry, string stackTrace, LogType logType)
{
if (logType == LogType.Exception)
{
// Log exception and exit with non-zero error code
UnityEngine.Debug.Log($"Caught an exception, exiting... \n {logEntry} \n {stackTrace}");
Application.Quit(1);
}
}
void OnDisable()
{
Application.logMessageReceivedThreaded -= HandleLogEntry;
}
#endif
protected virtual void Start()
{
init();
}
//
// Object creation
//
// TODO: add proper utility APIs for converting Collider into buffers usable for UnityEngine.Mesh and for drawing lines
static readonly Type k_DrawComponent = typeof(Unity.Physics.Authoring.DisplayBodyColliders)
.GetNestedType("DrawComponent", BindingFlags.NonPublic);
static readonly MethodInfo k_DrawComponent_BuildDebugDisplayMesh = k_DrawComponent
.GetMethod("BuildDebugDisplayMesh", BindingFlags.Static | BindingFlags.NonPublic, null, new[] { typeof(BlobAssetReference<Collider>) }, null);
static readonly Type k_DisplayResult = k_DrawComponent.GetNestedType("DisplayResult");
static readonly FieldInfo k_DisplayResultsMesh = k_DisplayResult.GetField("Mesh");
static readonly PropertyInfo k_DisplayResultsTransform = k_DisplayResult.GetProperty("Transform");
internal static void CreateRenderMeshForCollider(
EntityManager entityManager, Entity entity, BlobAssetReference<Collider> collider, Material material
)
{
var mesh = new Mesh { hideFlags = HideFlags.DontSave };
var instances = new List<CombineInstance>(8);
var numVertices = 0;
foreach (var displayResult in (IEnumerable)k_DrawComponent_BuildDebugDisplayMesh.Invoke(null, new object[] { collider }))
{
var instance = new CombineInstance
{
mesh = k_DisplayResultsMesh.GetValue(displayResult) as Mesh,
transform = (float4x4)k_DisplayResultsTransform.GetValue(displayResult)
};
instances.Add(instance);
numVertices += mesh.vertexCount;
}
mesh.indexFormat = numVertices > UInt16.MaxValue ? UnityEngine.Rendering.IndexFormat.UInt32 : UnityEngine.Rendering.IndexFormat.UInt16;
mesh.CombineMeshes(instances.ToArray());
mesh.RecalculateBounds();
entityManager.AddSharedComponentData(entity, new RenderMesh
{
mesh = mesh,
material = material
});
entityManager.AddComponentData(entity, new RenderBounds { Value = mesh.bounds.ToAABB() });
}
Entity CreateBody(float3 position, quaternion orientation, BlobAssetReference<Collider> collider,
float3 linearVelocity, float3 angularVelocity, float mass, bool isDynamic)
{
var entityManager = DefaultWorld.EntityManager;
Entity entity = entityManager.CreateEntity(new ComponentType[] { });
entityManager.AddComponentData(entity, new LocalToWorld { });
entityManager.AddComponentData(entity, new Translation { Value = position });
entityManager.AddComponentData(entity, new Rotation { Value = orientation });
var colliderComponent = new PhysicsCollider { Value = collider };
entityManager.AddComponentData(entity, colliderComponent);
CreateRenderMeshForCollider(entityManager, entity, collider, isDynamic ? dynamicMaterial : staticMaterial);
if (isDynamic)
{
entityManager.AddComponentData(entity, PhysicsMass.CreateDynamic(colliderComponent.MassProperties, mass));
float3 angularVelocityLocal = math.mul(math.inverse(colliderComponent.MassProperties.MassDistribution.Transform.rot), angularVelocity);
entityManager.AddComponentData(entity, new PhysicsVelocity
{
Linear = linearVelocity,
Angular = angularVelocityLocal
});
entityManager.AddComponentData(entity, new PhysicsDamping
{
Linear = 0.01f,
Angular = 0.05f
});
}
return entity;
}
protected Entity CreateStaticBody(float3 position, quaternion orientation, BlobAssetReference<Collider> collider)
{
return CreateBody(position, orientation, collider, float3.zero, float3.zero, 0.0f, false);
}
protected Entity CreateDynamicBody(float3 position, quaternion orientation, BlobAssetReference<Collider> collider,
float3 linearVelocity, float3 angularVelocity, float mass)
{
return CreateBody(position, orientation, collider, linearVelocity, angularVelocity, mass, true);
}
protected Entity CreateJoint(PhysicsJoint joint, Entity entityA, Entity entityB, bool enableCollision = false)
{
var entityManager = DefaultWorld.EntityManager;
ComponentType[] componentTypes = {
typeof(PhysicsConstrainedBodyPair),
typeof(PhysicsJoint)
};
Entity jointEntity = entityManager.CreateEntity(componentTypes);
entityManager.SetComponentData(jointEntity, new PhysicsConstrainedBodyPair(entityA, entityB, enableCollision));
entityManager.SetComponentData(jointEntity, joint);
return jointEntity;
}
protected RigidTransform GetBodyTransform(Entity entity)
{
var entityManager = DefaultWorld.EntityManager;
return new RigidTransform(
entityManager.GetComponentData<Rotation>(entity).Value,
entityManager.GetComponentData<Translation>(entity).Value);
}
}