You've already forked ParticleEffectForUGUI
mirror of
https://github.com/mob-sakai/ParticleEffectForUGUI.git
synced 2026-06-26 16:33:44 +00:00
153
Packages/src/Runtime/Internal/Extensions/MeshExtensions.cs
Normal file
153
Packages/src/Runtime/Internal/Extensions/MeshExtensions.cs
Normal file
@@ -0,0 +1,153 @@
|
|||||||
|
using System.Collections.Generic;
|
||||||
|
using UnityEngine;
|
||||||
|
using UnityEngine.UI;
|
||||||
|
|
||||||
|
namespace Coffee.UIParticleInternal
|
||||||
|
{
|
||||||
|
internal static class MeshExtensions
|
||||||
|
{
|
||||||
|
internal static readonly InternalObjectPool<Mesh> s_MeshPool = new InternalObjectPool<Mesh>(
|
||||||
|
() =>
|
||||||
|
{
|
||||||
|
var mesh = new Mesh
|
||||||
|
{
|
||||||
|
hideFlags = HideFlags.DontSave | HideFlags.NotEditable
|
||||||
|
};
|
||||||
|
mesh.MarkDynamic();
|
||||||
|
return mesh;
|
||||||
|
},
|
||||||
|
mesh => mesh,
|
||||||
|
mesh =>
|
||||||
|
{
|
||||||
|
if (mesh != null)
|
||||||
|
{
|
||||||
|
mesh.Clear();
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
public static Mesh Rent()
|
||||||
|
{
|
||||||
|
return s_MeshPool.Rent();
|
||||||
|
}
|
||||||
|
|
||||||
|
public static void Return(ref Mesh mesh)
|
||||||
|
{
|
||||||
|
s_MeshPool.Return(ref mesh);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static void CopyTo(this Mesh self, Mesh dst)
|
||||||
|
{
|
||||||
|
if (self == null || dst == null) return;
|
||||||
|
|
||||||
|
var vector3List = InternalListPool<Vector3>.Rent();
|
||||||
|
var vector4List = InternalListPool<Vector4>.Rent();
|
||||||
|
var color32List = InternalListPool<Color32>.Rent();
|
||||||
|
var intList = InternalListPool<int>.Rent();
|
||||||
|
|
||||||
|
dst.Clear(false);
|
||||||
|
|
||||||
|
self.GetVertices(vector3List);
|
||||||
|
dst.SetVertices(vector3List);
|
||||||
|
|
||||||
|
self.GetTriangles(intList, 0);
|
||||||
|
dst.SetTriangles(intList, 0);
|
||||||
|
|
||||||
|
self.GetNormals(vector3List);
|
||||||
|
dst.SetNormals(vector3List);
|
||||||
|
|
||||||
|
self.GetTangents(vector4List);
|
||||||
|
dst.SetTangents(vector4List);
|
||||||
|
|
||||||
|
self.GetColors(color32List);
|
||||||
|
dst.SetColors(color32List);
|
||||||
|
|
||||||
|
self.GetUVs(0, vector4List);
|
||||||
|
dst.SetUVs(0, vector4List);
|
||||||
|
|
||||||
|
self.GetUVs(1, vector4List);
|
||||||
|
dst.SetUVs(1, vector4List);
|
||||||
|
|
||||||
|
self.GetUVs(2, vector4List);
|
||||||
|
dst.SetUVs(2, vector4List);
|
||||||
|
|
||||||
|
self.GetUVs(3, vector4List);
|
||||||
|
dst.SetUVs(3, vector4List);
|
||||||
|
|
||||||
|
dst.RecalculateBounds();
|
||||||
|
InternalListPool<Vector3>.Return(ref vector3List);
|
||||||
|
InternalListPool<Vector4>.Return(ref vector4List);
|
||||||
|
InternalListPool<Color32>.Return(ref color32List);
|
||||||
|
InternalListPool<int>.Return(ref intList);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static void CopyTo(this VertexHelper self, Mesh dst)
|
||||||
|
{
|
||||||
|
if (self == null || dst == null) return;
|
||||||
|
|
||||||
|
self.FillMesh(dst);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static void CopyTo(this Mesh self, VertexHelper dst)
|
||||||
|
{
|
||||||
|
if (self == null || dst == null) return;
|
||||||
|
|
||||||
|
var vertexCount = self.vertexCount;
|
||||||
|
var indexCount = self.triangles.Length;
|
||||||
|
self.CopyTo(dst, vertexCount, indexCount);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static void CopyTo(this Mesh self, VertexHelper dst, int vertexCount, int indexCount)
|
||||||
|
{
|
||||||
|
if (self == null || dst == null) return;
|
||||||
|
|
||||||
|
var positions = InternalListPool<Vector3>.Rent();
|
||||||
|
var normals = InternalListPool<Vector3>.Rent();
|
||||||
|
var uv0 = InternalListPool<Vector4>.Rent();
|
||||||
|
var uv1 = InternalListPool<Vector4>.Rent();
|
||||||
|
var uv2 = InternalListPool<Vector4>.Rent();
|
||||||
|
var uv3 = InternalListPool<Vector4>.Rent();
|
||||||
|
var tangents = InternalListPool<Vector4>.Rent();
|
||||||
|
var colors = InternalListPool<Color32>.Rent();
|
||||||
|
var indices = InternalListPool<int>.Rent();
|
||||||
|
self.GetVertices(positions);
|
||||||
|
self.GetColors(colors);
|
||||||
|
self.GetUVs(0, uv0);
|
||||||
|
self.GetUVs(1, uv1);
|
||||||
|
self.GetUVs(2, uv1);
|
||||||
|
self.GetUVs(3, uv1);
|
||||||
|
self.GetNormals(normals);
|
||||||
|
self.GetTangents(tangents);
|
||||||
|
self.GetIndices(indices, 0);
|
||||||
|
|
||||||
|
dst.Clear();
|
||||||
|
for (var i = 0; i < vertexCount; i++)
|
||||||
|
{
|
||||||
|
dst.AddVert(positions.GetOrDefault(i), colors.GetOrDefault(i), uv0.GetOrDefault(i), uv1.GetOrDefault(i),
|
||||||
|
uv2.GetOrDefault(i), uv3.GetOrDefault(i), normals.GetOrDefault(i), tangents.GetOrDefault(i));
|
||||||
|
}
|
||||||
|
|
||||||
|
var count = Mathf.Clamp(indexCount, 0, indices.Count);
|
||||||
|
for (var i = 0; i < count - 2; i += 3)
|
||||||
|
{
|
||||||
|
dst.AddTriangle(indices[i], indices[i + 1], indices[i + 2]);
|
||||||
|
}
|
||||||
|
|
||||||
|
InternalListPool<Vector3>.Return(ref positions);
|
||||||
|
InternalListPool<Vector3>.Return(ref normals);
|
||||||
|
InternalListPool<Vector4>.Return(ref uv0);
|
||||||
|
InternalListPool<Vector4>.Return(ref uv1);
|
||||||
|
InternalListPool<Vector4>.Return(ref uv2);
|
||||||
|
InternalListPool<Vector4>.Return(ref uv3);
|
||||||
|
InternalListPool<Vector4>.Return(ref tangents);
|
||||||
|
InternalListPool<Color32>.Return(ref colors);
|
||||||
|
InternalListPool<int>.Return(ref indices);
|
||||||
|
}
|
||||||
|
|
||||||
|
private static T GetOrDefault<T>(this List<T> self, int index)
|
||||||
|
{
|
||||||
|
return 0 <= index && index < self.Count
|
||||||
|
? self[index]
|
||||||
|
: default;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,11 @@
|
|||||||
|
fileFormatVersion: 2
|
||||||
|
guid: 8c949d47f5c6d4d22bd7baebc60d48f3
|
||||||
|
MonoImporter:
|
||||||
|
externalObjects: {}
|
||||||
|
serializedVersion: 2
|
||||||
|
defaultReferences: []
|
||||||
|
executionOrder: 0
|
||||||
|
icon: {instanceID: 0}
|
||||||
|
userData:
|
||||||
|
assetBundleName:
|
||||||
|
assetBundleVariant:
|
||||||
@@ -26,6 +26,7 @@ namespace Coffee.UIExtensions
|
|||||||
private static readonly List<Material> s_Materials = new List<Material>(2);
|
private static readonly List<Material> s_Materials = new List<Material>(2);
|
||||||
private static MaterialPropertyBlock s_Mpb;
|
private static MaterialPropertyBlock s_Mpb;
|
||||||
private static readonly Vector3[] s_Corners = new Vector3[4];
|
private static readonly Vector3[] s_Corners = new Vector3[4];
|
||||||
|
private static readonly VertexHelper s_VertexHelper = new VertexHelper();
|
||||||
private bool _delay;
|
private bool _delay;
|
||||||
private int _index;
|
private int _index;
|
||||||
private bool _isPrevStored;
|
private bool _isPrevStored;
|
||||||
@@ -451,11 +452,15 @@ namespace Coffee.UIExtensions
|
|||||||
|
|
||||||
var components = InternalListPool<Component>.Rent();
|
var components = InternalListPool<Component>.Rent();
|
||||||
GetComponents(typeof(IMeshModifier), components);
|
GetComponents(typeof(IMeshModifier), components);
|
||||||
for (var i = 0; i < components.Count; i++)
|
if (0 < components.Count)
|
||||||
{
|
{
|
||||||
#pragma warning disable CS0618 // Type or member is obsolete
|
workerMesh.CopyTo(s_VertexHelper);
|
||||||
((IMeshModifier)components[i]).ModifyMesh(workerMesh);
|
for (var i = 0; i < components.Count; i++)
|
||||||
#pragma warning restore CS0618 // Type or member is obsolete
|
{
|
||||||
|
((IMeshModifier)components[i]).ModifyMesh(s_VertexHelper);
|
||||||
|
}
|
||||||
|
|
||||||
|
s_VertexHelper.FillMesh(workerMesh);
|
||||||
}
|
}
|
||||||
|
|
||||||
InternalListPool<Component>.Return(ref components);
|
InternalListPool<Component>.Return(ref components);
|
||||||
|
|||||||
Reference in New Issue
Block a user