| | 1 | | using System.Collections; |
| | 2 | | using System.Collections.Generic; |
| | 3 | | using System.Linq; |
| | 4 | | using System.Text; |
| | 5 | | using UnityEngine; |
| | 6 | | using UnityGLTF.Cache; |
| | 7 | |
|
| | 8 | | namespace DCL.Helpers |
| | 9 | | { |
| | 10 | | public static class MaterialCachingHelper |
| | 11 | | { |
| | 12 | | [System.Flags] |
| | 13 | | public enum Mode |
| | 14 | | { |
| | 15 | | NONE = 0, |
| | 16 | | CACHE_MATERIALS = 1, |
| | 17 | | CACHE_SHADERS = 2, |
| | 18 | | CACHE_EVERYTHING = CACHE_MATERIALS | CACHE_SHADERS, |
| | 19 | | } |
| | 20 | |
|
| 11 | 21 | | public static bool timeBudgetEnabled => CommonScriptableObjects.rendererState.Get(); |
| 1 | 22 | | public static float timeBudgetMax = 0.003f; |
| 1 | 23 | | public static float timeBudget = 0; |
| | 24 | |
|
| 1 | 25 | | public static Dictionary<string, Shader> shaderByHash = new Dictionary<string, Shader>(); |
| | 26 | |
|
| | 27 | | private static Shader mainShader; |
| | 28 | |
|
| 0 | 29 | | public static string ComputeHash(Material mat) { return mat.ComputeCRC().ToString(); } |
| | 30 | |
|
| | 31 | | static Shader EnsureMainShader() |
| | 32 | | { |
| 0 | 33 | | if (mainShader == null) |
| | 34 | | { |
| 0 | 35 | | mainShader = Shader.Find("DCL/Universal Render Pipeline/Lit"); |
| | 36 | | } |
| | 37 | |
|
| 0 | 38 | | return mainShader; |
| | 39 | | } |
| | 40 | |
|
| | 41 | | public static Material ProcessSingleMaterial(Material mat, Mode cachingFlags = Mode.CACHE_EVERYTHING) |
| | 42 | | { |
| 11 | 43 | | if ((cachingFlags & Mode.CACHE_SHADERS) != 0) |
| | 44 | | { |
| 11 | 45 | | string shaderHash = mat.shader.name; |
| | 46 | |
|
| 11 | 47 | | if (!shaderByHash.ContainsKey(shaderHash)) |
| | 48 | | { |
| 1 | 49 | | if (!mat.shader.name.Contains("Error")) |
| | 50 | | { |
| 1 | 51 | | shaderByHash.Add(shaderHash, Shader.Find(mat.shader.name)); |
| 1 | 52 | | } |
| | 53 | | else |
| | 54 | | { |
| 0 | 55 | | shaderByHash.Add(shaderHash, EnsureMainShader()); |
| | 56 | | } |
| | 57 | | } |
| | 58 | |
|
| 11 | 59 | | mat.shader = shaderByHash[shaderHash]; |
| | 60 | | } |
| | 61 | |
|
| | 62 | | //NOTE(Brian): Have to copy material because will be unloaded later. |
| 11 | 63 | | var materialCopy = new Material(mat); |
| 11 | 64 | | SRPBatchingHelper.OptimizeMaterial(materialCopy); |
| | 65 | |
|
| 11 | 66 | | if ((cachingFlags & Mode.CACHE_MATERIALS) != 0) |
| | 67 | | { |
| 11 | 68 | | int crc = mat.ComputeCRC(); |
| 11 | 69 | | string hash = crc.ToString(); |
| | 70 | |
|
| | 71 | | RefCountedMaterialData refCountedMat; |
| | 72 | |
|
| 11 | 73 | | if (!PersistentAssetCache.MaterialCacheByCRC.ContainsKey(hash)) |
| | 74 | | { |
| | 75 | | #if UNITY_EDITOR |
| 2 | 76 | | materialCopy.name += $" (crc: {materialCopy.ComputeCRC()})"; |
| | 77 | | #endif |
| 2 | 78 | | PersistentAssetCache.MaterialCacheByCRC.Add(hash, new RefCountedMaterialData(hash, materialCopy)); |
| | 79 | | } |
| | 80 | |
|
| 11 | 81 | | refCountedMat = PersistentAssetCache.MaterialCacheByCRC[hash]; |
| 11 | 82 | | refCountedMat.IncreaseRefCount(); |
| 11 | 83 | | return refCountedMat.material; |
| | 84 | | } |
| | 85 | |
|
| 0 | 86 | | return materialCopy; |
| | 87 | | } |
| | 88 | |
|
| | 89 | | public static IEnumerator Process(List<Renderer> renderers, bool enableRenderers = true, Mode cachingFlags = Mod |
| | 90 | | { |
| 11 | 91 | | if (renderers == null) |
| 0 | 92 | | yield break; |
| | 93 | |
|
| 11 | 94 | | int renderersCount = renderers.Count; |
| | 95 | |
|
| 11 | 96 | | if (renderersCount == 0) |
| 0 | 97 | | yield break; |
| | 98 | |
|
| 11 | 99 | | var matList = new List<Material>(1); |
| | 100 | |
|
| 44 | 101 | | for (int i = 0; i < renderersCount; i++) |
| | 102 | | { |
| 11 | 103 | | Renderer r = renderers[i]; |
| | 104 | |
|
| 11 | 105 | | if (!enableRenderers) |
| 11 | 106 | | r.enabled = false; |
| | 107 | |
|
| 11 | 108 | | matList.Clear(); |
| | 109 | |
|
| 11 | 110 | | var sharedMats = r.sharedMaterials; |
| | 111 | |
|
| 44 | 112 | | for (int i1 = 0; i1 < sharedMats.Length; i1++) |
| | 113 | | { |
| 11 | 114 | | Material mat = sharedMats[i1]; |
| | 115 | |
|
| 11 | 116 | | float elapsedTime = Time.realtimeSinceStartup; |
| | 117 | |
|
| 11 | 118 | | matList.Add( ProcessSingleMaterial(mat, cachingFlags) ); |
| | 119 | |
|
| 11 | 120 | | if (timeBudgetEnabled) |
| | 121 | | { |
| 11 | 122 | | elapsedTime = Time.realtimeSinceStartup - elapsedTime; |
| 11 | 123 | | timeBudget -= elapsedTime; |
| | 124 | |
|
| 11 | 125 | | if (timeBudget < 0) |
| | 126 | | { |
| 1 | 127 | | yield return null; |
| 1 | 128 | | timeBudget += timeBudgetMax; |
| | 129 | | } |
| | 130 | | } |
| | 131 | | } |
| | 132 | |
|
| 11 | 133 | | if (r != null) |
| 11 | 134 | | r.sharedMaterials = matList.ToArray(); |
| 11 | 135 | | } |
| 11 | 136 | | } |
| | 137 | | } |
| | 138 | | } |