10using System.Collections.Generic;
17 public static class MeshCache {
19 public static void Clear() {
20 _topologyCache.Clear();
26 private static Dictionary<Mesh, CachedTopology> _topologyCache =
new Dictionary<Mesh, CachedTopology>();
27 public static CachedTopology GetTopology(Mesh mesh) {
28 CachedTopology topology;
29 if (!_topologyCache.TryGetValue(mesh, out topology)) {
30 topology.tris = mesh.GetIndices(0);
31 topology.verts = mesh.vertices;
32 _topologyCache[mesh] = topology;
37 private static Dictionary<Mesh, Vector3[]> _normalCache =
new Dictionary<Mesh, Vector3[]>();
38 public static Vector3[] GetNormals(Mesh mesh) {
40 if (!_normalCache.TryGetValue(mesh, out normals)) {
41 normals = mesh.normals;
42 if (normals.Length != mesh.vertexCount) {
43 mesh.RecalculateNormals();
44 normals = mesh.normals;
47 _normalCache[mesh] = normals;
52 private static Dictionary<Mesh, Color[]> _colorCache =
new Dictionary<Mesh, Color[]>();
53 public static Color[] GetColors(Mesh mesh) {
55 if (!_colorCache.TryGetValue(mesh, out colors)) {
57 if (colors.Length != mesh.vertexCount) {
58 colors =
new Color[mesh.vertexCount].Fill(
Color.white);
61 _colorCache[mesh] = colors;
66 private static Dictionary<UvKey, List<Vector4>> _uvCache =
new Dictionary<UvKey, List<Vector4>>();
67 public static List<Vector4> GetUvs(Mesh mesh, UVChannelFlags channel) {
68 var key =
new UvKey() { mesh = mesh, channel = (int)channel };
70 if (!_uvCache.TryGetValue(key, out uvs)) {
71 uvs =
new List<Vector4>();
72 mesh.GetUVs(channel.Index(), uvs);
74 if (uvs.Count != mesh.vertexCount) {
75 uvs.Fill(mesh.vertexCount,
Vector4.zero);
88 private struct UvKey : IComparable<UvKey>, IEquatable<UvKey> {
92 public int CompareTo(UvKey other) {
93 if (other.mesh != mesh)
return 1;
94 if (other.channel != channel)
return 1;
98 public override int GetHashCode() {
99 return mesh.GetHashCode() + channel;
102 public bool Equals(UvKey other) {
103 return other.mesh == mesh && other.channel == channel;