2020-09-28 20:35:40 +08:00
|
|
|
|
using System.Collections.Generic;
|
2020-08-20 03:42:16 +08:00
|
|
|
|
using UnityEngine;
|
2020-09-02 01:39:05 +08:00
|
|
|
|
using UnityEngine.Profiling;
|
2020-08-20 03:42:16 +08:00
|
|
|
|
|
2020-09-28 20:35:40 +08:00
|
|
|
|
namespace Coffee.UIParticleExtensions
|
2020-08-20 03:42:16 +08:00
|
|
|
|
{
|
|
|
|
|
internal static class MeshHelper
|
|
|
|
|
{
|
2020-10-27 23:39:22 +08:00
|
|
|
|
public static List<bool> activeMeshIndices { get; private set; }
|
2020-09-02 01:39:05 +08:00
|
|
|
|
private static readonly List<CombineInstanceEx> s_CachedInstance;
|
|
|
|
|
private static int count;
|
|
|
|
|
|
|
|
|
|
public static void Init()
|
2020-08-20 03:42:16 +08:00
|
|
|
|
{
|
2020-10-27 23:39:22 +08:00
|
|
|
|
activeMeshIndices = new List<bool>();
|
2020-08-20 03:42:16 +08:00
|
|
|
|
}
|
|
|
|
|
|
2020-09-02 01:39:05 +08:00
|
|
|
|
static MeshHelper()
|
2020-08-20 03:42:16 +08:00
|
|
|
|
{
|
2020-09-02 01:39:05 +08:00
|
|
|
|
s_CachedInstance = new List<CombineInstanceEx>(8);
|
|
|
|
|
for (var i = 0; i < 8; i++)
|
|
|
|
|
{
|
|
|
|
|
s_CachedInstance.Add(new CombineInstanceEx());
|
|
|
|
|
}
|
|
|
|
|
}
|
2020-08-20 03:42:16 +08:00
|
|
|
|
|
2020-09-02 01:39:05 +08:00
|
|
|
|
private static CombineInstanceEx Get(int index, long hash)
|
|
|
|
|
{
|
|
|
|
|
if (0 < count && s_CachedInstance[count - 1].hash == hash)
|
|
|
|
|
return s_CachedInstance[count - 1];
|
2020-08-20 03:42:16 +08:00
|
|
|
|
|
2020-09-02 01:39:05 +08:00
|
|
|
|
if (s_CachedInstance.Count <= count)
|
2020-08-20 03:42:16 +08:00
|
|
|
|
{
|
2020-09-02 01:39:05 +08:00
|
|
|
|
var newInst = new CombineInstanceEx();
|
|
|
|
|
s_CachedInstance.Add(newInst);
|
2020-08-20 03:42:16 +08:00
|
|
|
|
}
|
|
|
|
|
|
2020-09-02 01:39:05 +08:00
|
|
|
|
var inst = s_CachedInstance[count];
|
|
|
|
|
inst.hash = hash;
|
|
|
|
|
if (inst.index != -1) return inst;
|
2020-08-20 03:42:16 +08:00
|
|
|
|
|
2020-09-02 01:39:05 +08:00
|
|
|
|
inst.index = index;
|
|
|
|
|
count++;
|
|
|
|
|
return inst;
|
2020-08-28 13:38:13 +08:00
|
|
|
|
}
|
|
|
|
|
|
2020-09-02 01:39:05 +08:00
|
|
|
|
public static Mesh GetTemporaryMesh()
|
2020-08-28 13:38:13 +08:00
|
|
|
|
{
|
2020-09-02 01:39:05 +08:00
|
|
|
|
return MeshPool.Rent();
|
2020-08-28 13:38:13 +08:00
|
|
|
|
}
|
|
|
|
|
|
2020-09-02 01:39:05 +08:00
|
|
|
|
public static void Push(int index, long hash, Mesh mesh, Matrix4x4 transform)
|
2020-08-28 13:38:13 +08:00
|
|
|
|
{
|
2020-09-02 01:39:05 +08:00
|
|
|
|
if (mesh.vertexCount <= 0)
|
|
|
|
|
{
|
|
|
|
|
DiscardTemporaryMesh(mesh);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Profiler.BeginSample("[UIParticle] MeshHelper > Get CombineInstanceEx");
|
|
|
|
|
var inst = Get(index, hash);
|
|
|
|
|
Profiler.EndSample();
|
|
|
|
|
|
|
|
|
|
Profiler.BeginSample("[UIParticle] MeshHelper > Push To Mesh Helper");
|
|
|
|
|
inst.Push(mesh, transform);
|
|
|
|
|
Profiler.EndSample();
|
|
|
|
|
|
2020-10-27 23:39:22 +08:00
|
|
|
|
activeMeshIndices[inst.index] = true;
|
2020-08-20 03:42:16 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static void Clear()
|
|
|
|
|
{
|
2020-09-02 01:39:05 +08:00
|
|
|
|
count = 0;
|
2020-10-27 23:39:22 +08:00
|
|
|
|
activeMeshIndices.Clear();
|
2020-09-02 01:39:05 +08:00
|
|
|
|
foreach (var inst in s_CachedInstance)
|
2020-08-20 03:42:16 +08:00
|
|
|
|
{
|
2020-09-02 01:39:05 +08:00
|
|
|
|
inst.Clear();
|
2020-08-20 03:42:16 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-08-28 13:38:13 +08:00
|
|
|
|
public static void CombineMesh(Mesh result)
|
2020-08-20 03:42:16 +08:00
|
|
|
|
{
|
2020-09-02 01:39:05 +08:00
|
|
|
|
if (count == 0) return;
|
|
|
|
|
|
|
|
|
|
for (var i = 0; i < count; i++)
|
|
|
|
|
{
|
|
|
|
|
Profiler.BeginSample("[UIParticle] MeshHelper > Combine Mesh Internal");
|
|
|
|
|
s_CachedInstance[i].Combine();
|
|
|
|
|
Profiler.EndSample();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Profiler.BeginSample("[UIParticle] MeshHelper > Combine Mesh");
|
|
|
|
|
var cis = CombineInstanceArrayPool.Get(s_CachedInstance, count);
|
|
|
|
|
result.CombineMeshes(cis, false, true);
|
|
|
|
|
cis.Clear();
|
|
|
|
|
Profiler.EndSample();
|
2020-08-20 03:42:16 +08:00
|
|
|
|
|
|
|
|
|
result.RecalculateBounds();
|
|
|
|
|
}
|
|
|
|
|
|
2020-09-02 01:39:05 +08:00
|
|
|
|
public static void DiscardTemporaryMesh(Mesh mesh)
|
2020-08-20 03:42:16 +08:00
|
|
|
|
{
|
2020-09-02 01:39:05 +08:00
|
|
|
|
MeshPool.Return(mesh);
|
2020-08-20 03:42:16 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|