Tanoda
MeshCache.cs
Go to the documentation of this file.
1/******************************************************************************
2 * Copyright (C) Ultraleap, Inc. 2011-2020. *
3 * *
4 * Use subject to the terms of the Apache License 2.0 available at *
5 * http://www.apache.org/licenses/LICENSE-2.0, or another agreement *
6 * between Ultraleap and you, your company or other organization. *
7 ******************************************************************************/
8
9using System;
10using System.Collections.Generic;
11using UnityEngine;
12using UnityEngine.Rendering;
13using Leap.Unity.Query;
14
16
17 public static class MeshCache {
18
19 public static void Clear() {
20 _topologyCache.Clear();
21 _normalCache.Clear();
22 _colorCache.Clear();
23 _uvCache.Clear();
24 }
25
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;
33 }
34 return topology;
35 }
36
37 private static Dictionary<Mesh, Vector3[]> _normalCache = new Dictionary<Mesh, Vector3[]>();
38 public static Vector3[] GetNormals(Mesh mesh) {
39 Vector3[] normals;
40 if (!_normalCache.TryGetValue(mesh, out normals)) {
41 normals = mesh.normals;
42 if (normals.Length != mesh.vertexCount) {
43 mesh.RecalculateNormals();
44 normals = mesh.normals;
45 }
46
47 _normalCache[mesh] = normals;
48 }
49 return normals;
50 }
51
52 private static Dictionary<Mesh, Color[]> _colorCache = new Dictionary<Mesh, Color[]>();
53 public static Color[] GetColors(Mesh mesh) {
54 Color[] colors;
55 if (!_colorCache.TryGetValue(mesh, out colors)) {
56 colors = mesh.colors;
57 if (colors.Length != mesh.vertexCount) {
58 colors = new Color[mesh.vertexCount].Fill(Color.white);
59 }
60
61 _colorCache[mesh] = colors;
62 }
63 return colors;
64 }
65
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 };
69 List<Vector4> uvs;
70 if (!_uvCache.TryGetValue(key, out uvs)) {
71 uvs = new List<Vector4>();
72 mesh.GetUVs(channel.Index(), uvs);
73
74 if (uvs.Count != mesh.vertexCount) {
75 uvs.Fill(mesh.vertexCount, Vector4.zero);
76 }
77
78 _uvCache[key] = uvs;
79 }
80 return uvs;
81 }
82
83 public struct CachedTopology {
84 public Vector3[] verts;
85 public int[] tris;
86 }
87
88 private struct UvKey : IComparable<UvKey>, IEquatable<UvKey> {
89 public Mesh mesh;
90 public int channel;
91
92 public int CompareTo(UvKey other) {
93 if (other.mesh != mesh) return 1;
94 if (other.channel != channel) return 1;
95 return 0;
96 }
97
98 public override int GetHashCode() {
99 return mesh.GetHashCode() + channel;
100 }
101
102 public bool Equals(UvKey other) {
103 return other.mesh == mesh && other.channel == channel;
104 }
105 }
106 }
107}
UnityEngine.Color Color
Definition: TestScript.cs:32