forked from Unity-Technologies/EntityComponentSystemSamples
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSceneCreationSystem.cs
More file actions
211 lines (173 loc) · 7.75 KB
/
Copy pathSceneCreationSystem.cs
File metadata and controls
211 lines (173 loc) · 7.75 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
using System;
using System.Collections;
using System.Collections.Generic;
using System.Reflection;
using Unity.Collections;
using Unity.Entities;
using Unity.Mathematics;
using Unity.Physics;
using Unity.Rendering;
using Unity.Transforms;
using UnityEngine;
using Collider = Unity.Physics.Collider;
using Material = UnityEngine.Material;
using Mesh = UnityEngine.Mesh;
public abstract class SceneCreationSettings : IComponentData
{
public Material DynamicMaterial;
public Material StaticMaterial;
}
public class SceneCreatedTag : IComponentData {};
// Base class of authoring components that create scene from code, using SceneCreationSystem
public abstract class SceneCreationAuthoring<T> : MonoBehaviour, IConvertGameObjectToEntity
where T : SceneCreationSettings, new()
{
public Material DynamicMaterial;
public Material StaticMaterial;
public virtual void Convert(Entity entity, EntityManager dstManager, GameObjectConversionSystem conversionSystem)
{
T sceneSettings = new T
{
DynamicMaterial = DynamicMaterial,
StaticMaterial = StaticMaterial
};
dstManager.AddComponentData(entity, sceneSettings);
}
}
[UpdateInGroup(typeof(InitializationSystemGroup))]
public abstract class SceneCreationSystem<T> : SystemBase
where T : SceneCreationSettings
{
private EntityQuery m_ScenesToCreateQuery;
protected Material DynamicMaterial;
protected Material StaticMaterial;
public NativeList<BlobAssetReference<Collider>> CreatedColliders;
protected override void OnCreate()
{
CreatedColliders = new NativeList<BlobAssetReference<Collider>>(Allocator.Persistent);
m_ScenesToCreateQuery = GetEntityQuery(new EntityQueryDesc
{
All = new ComponentType[] { typeof(T) },
None = new ComponentType[] { typeof(SceneCreatedTag) },
});
RequireForUpdate(GetEntityQuery(new ComponentType[] { typeof(T) }));
}
protected override void OnUpdate()
{
if (m_ScenesToCreateQuery.CalculateEntityCount() == 0) return;
using (var entities = m_ScenesToCreateQuery.ToEntityArray(Allocator.TempJob))
{
foreach (Entity entity in entities)
{
T settings = EntityManager.GetComponentObject<T>(entity);
DynamicMaterial = settings.DynamicMaterial;
StaticMaterial = settings.StaticMaterial;
CreateScene(settings);
EntityManager.AddComponentData(entity, new SceneCreatedTag());
}
}
}
protected override void OnDestroy()
{
foreach (var collider in CreatedColliders)
{
if (collider.IsCreated)
collider.Dispose();
}
CreatedColliders.Dispose();
}
public abstract void CreateScene(T sceneSettings);
#region Utilities
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");
public 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() });
}
public Entity CreateBody(float3 position, quaternion orientation, BlobAssetReference<Collider> collider,
float3 linearVelocity, float3 angularVelocity, float mass, bool isDynamic)
{
var entityManager = World.DefaultGameObjectInjectionWorld.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;
}
public Entity CreateStaticBody(float3 position, quaternion orientation, BlobAssetReference<Collider> collider)
{
return CreateBody(position, orientation, collider, float3.zero, float3.zero, 0.0f, false);
}
public Entity CreateDynamicBody(float3 position, quaternion orientation, BlobAssetReference<Collider> collider,
float3 linearVelocity, float3 angularVelocity, float mass)
{
return CreateBody(position, orientation, collider, linearVelocity, angularVelocity, mass, true);
}
public Entity CreateJoint(PhysicsJoint joint, Entity entityA, Entity entityB, bool enableCollision = false)
{
var entityManager = World.DefaultGameObjectInjectionWorld.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;
}
public static RigidTransform GetBodyTransform(Entity entity)
{
var entityManager = World.DefaultGameObjectInjectionWorld.EntityManager;
return new RigidTransform(
entityManager.GetComponentData<Rotation>(entity).Value,
entityManager.GetComponentData<Translation>(entity).Value);
}
#endregion
}