-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Expand file tree
/
Copy pathWeakObjectLoadingSystem.cs
More file actions
183 lines (159 loc) · 7.76 KB
/
WeakObjectLoadingSystem.cs
File metadata and controls
183 lines (159 loc) · 7.76 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
using Unity.Collections;
using Unity.Entities;
using Unity.Entities.Content;
using Unity.Rendering;
using UnityEngine;
using UnityEngine.Rendering;
namespace ContentManagement.Sample
{
// This system looks for the entities that have the components added by WeakRenderedObjectAuthoring
// and turns them into renderable entities by:
// 1. asynchronously loading their weakly referenced meshes and materials
// 2. once the assets have loaded, adding the required rendering components
public partial struct WeakObjectLoadingSystem : ISystem
{
private EntityQuery weakQuery;
private EntityQuery weakUntypedQuery;
public void OnCreate(ref SystemState state)
{
// Renderable entities have the RenderBounds component (among other rendering components), so
// these queries match only entities that haven't yet been made renderable.
weakQuery = SystemAPI.QueryBuilder().WithAll<WeakMesh, WeakMaterial>().WithNone<RenderBounds>().Build();
weakUntypedQuery = SystemAPI.QueryBuilder().WithAll<WeakMeshUntyped, WeakMaterialUntyped>()
.WithNone<RenderBounds>().Build();
// The system should update only if entities exist which still need to be made renderable.
var query = SystemAPI.QueryBuilder().WithAny<WeakMesh, WeakMeshUntyped>().WithNone<RenderBounds>().Build();
state.RequireForUpdate(query);
}
public void OnUpdate(ref SystemState state)
{
#region WeakObjectReference
var weakEntities = weakQuery.ToEntityArray(Allocator.Temp);
var weakMeshes = weakQuery.ToComponentDataArray<WeakMesh>(Allocator.Temp);
// note that we can't use SystemAPI.Query for this loop because
// are making structural changes to the entities
for (int i = 0; i < weakEntities.Length; i++)
{
var loaded = true;
var entity = weakEntities[i];
var mesh = weakMeshes[i];
var materials = state.EntityManager.GetBuffer<WeakMaterial>(entity);
// mesh load status
var meshStatus = mesh.Value.LoadingStatus;
if (meshStatus == ObjectLoadingStatus.None)
{
Debug.Log("Initiate mesh LOAD");
mesh.Value.LoadAsync(); // trigger load
}
if (meshStatus != ObjectLoadingStatus.Completed)
{
loaded = false;
}
// material load status
for (int j = 0; j < materials.Length; j++)
{
var mat = materials[j];
var materialStatus = mat.Value.LoadingStatus;
if (materialStatus == ObjectLoadingStatus.None)
{
Debug.Log("Initiate material LOAD");
mat.Value.LoadAsync(); // trigger load
}
if (materialStatus != ObjectLoadingStatus.Completed)
{
loaded = false;
}
}
if (loaded)
{
Debug.Log("Creating rendered entity");
// each rendered object has a single mesh, but the mesh may
// have submeshes, each with their own material
var meshArray = new Mesh[] { mesh.Value.Result };
var materialArray = new Material[materials.Length];
var indices = new MaterialMeshIndex[materials.Length];
for (int j = 0; j < materials.Length; j++)
{
materialArray[j] = materials[j].Value.Result;
indices[j] = new MaterialMeshIndex
{
MeshIndex = 0,
MaterialIndex = j,
SubMeshIndex = j,
};
}
// Add the rendering components to the entity
// (including RenderBounds, so the entity will hereafter
// be excluded from this system's queries)
RenderMeshUtility.AddComponents(entity, state.EntityManager,
new RenderMeshDescription(ShadowCastingMode.On),
new RenderMeshArray(materialArray, meshArray, indices),
MaterialMeshInfo.FromMaterialMeshIndexRange(0, materialArray.Length)
);
}
}
#endregion
#region UntypedWeakReferenceId
// very similar to above but for UntypedWeakReferenceId...
var weakUntypedEntities = weakUntypedQuery.ToEntityArray(Allocator.Temp);
var untypedWeakMeshes = weakUntypedQuery.ToComponentDataArray<WeakMeshUntyped>(Allocator.Temp);
for (int i = 0; i < weakUntypedEntities.Length; i++)
{
var loaded = true;
var entity = weakUntypedEntities[i];
var mesh = untypedWeakMeshes[i];
var materials = state.EntityManager.GetBuffer<WeakMaterialUntyped>(entity);
// mesh load status
var meshStatus = RuntimeContentManager.GetObjectLoadingStatus(mesh.Value);
if (meshStatus == ObjectLoadingStatus.None)
{
RuntimeContentManager.LoadObjectAsync(mesh.Value); // trigger load
}
if (meshStatus != ObjectLoadingStatus.Completed)
{
loaded = false;
}
// material load status
for (int j = 0; j < materials.Length; j++)
{
var mat = materials[j];
var materialStatus = RuntimeContentManager.GetObjectLoadingStatus(mat.Value);
if (materialStatus == ObjectLoadingStatus.None)
{
RuntimeContentManager.LoadObjectAsync(mat.Value); // trigger load
}
if (materialStatus != ObjectLoadingStatus.Completed)
{
loaded = false;
}
}
if (loaded)
{
// each rendered object has a single mesh, but the mesh may
// have submeshes, each with their own material
var meshArray = new Mesh[] { RuntimeContentManager.GetObjectValue<Mesh>(mesh.Value) };
var materialArray = new Material[materials.Length];
var indices = new MaterialMeshIndex[materials.Length];
for (int j = 0; j < materials.Length; j++)
{
materialArray[j] = RuntimeContentManager.GetObjectValue<Material>(materials[j].Value);
indices[j] = new MaterialMeshIndex
{
MeshIndex = 0,
MaterialIndex = j,
SubMeshIndex = j,
};
}
// Add the rendering components to the entity
// (including RenderBounds, so the entity will hereafter be excluded from this system's queries)
RenderMeshUtility.AddComponents(entity, state.EntityManager,
new RenderMeshDescription(ShadowCastingMode.On),
new RenderMeshArray(materialArray, meshArray, indices),
MaterialMeshInfo.FromMaterialMeshIndexRange(0, materialArray.Length)
);
}
}
#endregion
}
}
}