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
170 lines (135 loc) · 5.83 KB
/
Copy pathSceneCreationSystem.cs
File metadata and controls
170 lines (135 loc) · 5.83 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
using Unity.Collections;
using Unity.Entities;
using Unity.Mathematics;
using Unity.Physics;
using Unity.Physics.Extensions;
using Unity.Rendering;
using Unity.Transforms;
using UnityEngine;
using Collider = Unity.Physics.Collider;
using Material = UnityEngine.Material;
public abstract class SceneCreationSettings : IComponentData
{
public Material DynamicMaterial;
public Material StaticMaterial;
}
public class SceneCreatedTag : IComponentData
{
};
[UpdateInGroup(typeof(InitializationSystemGroup))]
public abstract partial 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<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
public static void CreateRenderMeshForCollider(
EntityManager entityManager, Entity entity, BlobAssetReference<Collider> collider, Material material
)
{
var mesh = collider.Value.ToMesh();
var renderMesh = new RenderMeshArray(
new[] { material },
new[] { mesh });
var renderMeshDescription = new RenderMeshDescription(UnityEngine.Rendering.ShadowCastingMode.Off);
RenderMeshUtility.AddComponents(entity, entityManager, renderMeshDescription, renderMesh, MaterialMeshInfo.FromRenderMeshArrayIndices(0, 0));
entityManager.AddComponentData(entity, new LocalToWorld());
}
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, LocalTransform.FromPositionRotation(position, orientation));
var colliderComponent = new PhysicsCollider { Value = collider };
entityManager.AddComponentData(entity, colliderComponent);
EntityManager.AddSharedComponent(entity, new PhysicsWorldIndex());
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);
EntityManager.AddSharedComponent(jointEntity, new PhysicsWorldIndex());
return jointEntity;
}
public static RigidTransform GetBodyTransform(Entity entity)
{
var entityManager = World.DefaultGameObjectInjectionWorld.EntityManager;
var localTransform = entityManager.GetComponentData<LocalTransform>(entity);
return new RigidTransform(
localTransform.Rotation,
localTransform.Position);
}
#endregion
}