forked from Unity-Technologies/EntityComponentSystemSamples
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMeshBBConversionSystem.cs
More file actions
168 lines (140 loc) · 6.29 KB
/
Copy pathMeshBBConversionSystem.cs
File metadata and controls
168 lines (140 loc) · 6.29 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
using System;
using System.Collections.Generic;
using Unity.Burst;
using Unity.Collections;
using Unity.Entities;
using Unity.Jobs;
using Unity.Mathematics;
using Unity.Transforms;
using UnityEngine;
using UnityEngine.Profiling;
using Hash128 = Unity.Entities.Hash128;
public struct MeshBBFactorySettings
{
public Hash128 Hash;
public float MeshScale;
public int PointStartIndex;
public int PointCount;
public float3 MinBoundingBox;
public float3 MaxBoundingBox;
}
public class MeshBBConversionSystem : GameObjectConversionSystem
{
protected override void OnCreate()
{
base.OnCreate();
GetEntityQuery(ComponentType.ReadOnly<MeshToBoundingBoxsAuthoring>());
}
protected override void OnUpdate()
{
var blobFactoryPoints = new NativeList<float3>(Allocator.TempJob);
int curPointIndex = 0;
var vertices = new List<Vector3>(4096);
var processBlobAssets = new NativeList<Hash128>(32, Allocator.Temp);
Profiler.BeginSample("Conv_BuildHashAndPush");
using (var context = new BlobAssetComputationContext<MeshBBFactorySettings, MeshBBBlobAsset>(BlobAssetStore, 128, Allocator.Temp))
{
// First step: for all changed GameObjects we compute the hash of their blob asset then get the asset or register its computation
Entities.ForEach((MeshToBoundingBoxsAuthoring auth) =>
{
// Compute the blob asset hash based on Authoring properties
var hasMesh = auth.Mesh != null;
var meshHashCode = hasMesh ? auth.Mesh.GetHashCode() : 0;
var hash = new Hash128((uint)meshHashCode, (uint)auth.MeshScale.GetHashCode(), 0, 0);
// Query the context to determine if we need to build the BlobAsset
processBlobAssets.Add(hash);
context.AssociateBlobAssetWithGameObject(hash, auth.gameObject);
if (context.NeedToComputeBlobAsset(hash))
{
Profiler.BeginSample("CopyVertices");
float xp = float.MinValue, yp = float.MinValue, zp = float.MinValue;
float xn = float.MaxValue, yn = float.MaxValue, zn = float.MaxValue;
// Copy the mesh vertices into the point array
if (hasMesh)
{
auth.Mesh.GetVertices(vertices);
for (int i = 0; i < vertices.Count; i++)
{
var p = vertices[i];
xp = math.max(p.x, xp);
yp = math.max(p.y, yp);
zp = math.max(p.z, zp);
xn = math.min(p.x, xn);
yn = math.min(p.y, yn);
zn = math.min(p.z, zn);
blobFactoryPoints.Add(new float3(p.x, p.y, p.z));
}
}
else
{
xp = yp = zp = xn = yn = zn = 0;
}
Profiler.EndSample();
// Record this blob asset for computation
var vertexCount = hasMesh ? auth.Mesh.vertexCount : 0;
var setting = new MeshBBFactorySettings { Hash = hash, MeshScale = auth.MeshScale, PointStartIndex = curPointIndex, PointCount = vertexCount, MinBoundingBox = new float3(xn, yn, zn), MaxBoundingBox = new float3(xp, yp, zp)};
curPointIndex += vertexCount;
context.AddBlobAssetToCompute(hash, setting);
}
});
Profiler.EndSample();
Profiler.BeginSample("Conv_CreateBlobAssets");
using (var settings = context.GetSettings(Allocator.TempJob))
{
// Step two, compute BlobAssets
var job = new ComputeMeshBBAssetJob(settings, blobFactoryPoints.AsArray());
job.Schedule(job.Settings.Length, 1).Complete();
for (int i = 0; i < settings.Length; i++)
{
context.AddComputedBlobAsset(settings[i].Hash, job.BlobAssets[i]);
}
job.BlobAssets.Dispose();
}
Profiler.EndSample();
Profiler.BeginSample("Conv_CreateECS");
// Third step, create the ECS component with the associated blob asset
var index = 0;
Entities.ForEach((MeshToBoundingBoxsAuthoring auth) =>
{
context.GetBlobAsset(processBlobAssets[index++], out var blob);
// Create the ECS component for the given GameObject
var entity = GetPrimaryEntity(auth);
DstEntityManager.AddComponentData(entity, new MeshBBComponent(blob));
});
Profiler.EndSample();
blobFactoryPoints.Dispose();
processBlobAssets.Dispose();
}
}
}
[BurstCompile]
public struct ComputeMeshBBAssetJob : IJobParallelFor
{
[ReadOnly] public NativeArray<MeshBBFactorySettings> Settings;
[ReadOnly] public NativeArray<float3> Vertices;
public NativeArray<BlobAssetReference<MeshBBBlobAsset>> BlobAssets;
public ComputeMeshBBAssetJob(NativeArray<MeshBBFactorySettings> settings, NativeArray<float3> vertices)
{
Settings = settings;
Vertices = vertices;
BlobAssets = new NativeArray<BlobAssetReference<MeshBBBlobAsset>>(settings.Length, Allocator.TempJob);
}
public void Execute(int index)
{
var builder = new BlobBuilder(Allocator.Temp);
var settings = Settings[index];
ref var root = ref builder.ConstructRoot<MeshBBBlobAsset>();
var array = builder.Allocate(ref root.Vertices, settings.PointCount);
var s = settings.MeshScale;
root.MeshScale = s;
root.MinBoundingBox = settings.MinBoundingBox * s;
root.MaxBoundingBox = settings.MaxBoundingBox * s;
for (int i = 0; i < array.Length; i++)
{
var v1 = Vertices[settings.PointStartIndex + i];
array[i] = new float3(v1.x * s, v1.y * s, v1.z * s);
}
BlobAssets[index] = builder.CreateBlobAssetReference<MeshBBBlobAsset>(Allocator.Persistent);
builder.Dispose();
}
}