public static void ReplaceShadersRealtime(VRC.Core.APIUser user, IEnumerable <Renderer> avatarRenderers, FallbackMaterialCache fallbackMaterialCache, bool debug = false)
 {
     ReplaceShaders(user, avatarRenderers, fallbackMaterialCache, debug);
 }
Exemplo n.º 2
0
        public static void ReplaceShaders(VRC.Core.APIUser user, List <Renderer> avatarRenderers, FallbackMaterialCache fallbackMaterialCache, bool debug = true)
        {
            foreach (Renderer avatarRenderer in avatarRenderers)
            {
                if (avatarRenderer == null)
                {
                    continue;
                }

                avatarRenderer.GetSharedMaterials(_replaceShadersWorkingList);
                bool materialNeedsReplacement = false;
                foreach (Material material in _replaceShadersWorkingList)
                {
                    // A fallback material exists and is being used.
                    if (fallbackMaterialCache.TryGetFallbackMaterial(material, out Material fallbackMaterial) && material == fallbackMaterial)
                    {
                        continue;
                    }

                    materialNeedsReplacement = true;
                    break;
                }

                if (!materialNeedsReplacement)
                {
                    return;
                }

                Material[] avatarRendererSharedMaterials = avatarRenderer.sharedMaterials;
                for (int i = 0; i < avatarRendererSharedMaterials.Length; ++i)
                {
                    Material currentMaterial = avatarRendererSharedMaterials[i];
                    if (currentMaterial == null)
                    {
                        continue;
                    }

                    if (fallbackMaterialCache.TryGetFallbackMaterial(currentMaterial, out Material fallbackMaterial))
                    {
                        if (debug)
                        {
                            Debug.Log($"<color=cyan>*** Using existing fallback: '{fallbackMaterial.shader.name}' </color>");
                        }
                    }
                    else
                    {
                        // The current material is not in our safe list so create a fallback.
                        fallbackMaterial = CreateFallbackMaterial(currentMaterial, user);

                        // Map the current material to the fallback and the fallback to itself.
                        fallbackMaterialCache.AddFallbackMaterial(currentMaterial, fallbackMaterial);
                        fallbackMaterialCache.AddFallbackMaterial(fallbackMaterial, fallbackMaterial);

                        if (debug)
                        {
                            Debug.Log($"<color=cyan>*** Creating new fallback: '{fallbackMaterial.shader.name}' </color>");
                        }
                    }

                    avatarRendererSharedMaterials[i] = fallbackMaterial;
                }

                avatarRenderer.sharedMaterials = avatarRendererSharedMaterials;
            }
        }
        public static void ReplaceShaders(VRC.Core.APIUser user, IEnumerable <Renderer> avatarRenderers, FallbackMaterialCache fallbackMaterialCache, bool debug = true)
        {
            foreach (Renderer avatarRenderer in avatarRenderers)
            {
                //TODO 2018.4 LTS: Replace this with avatarRenderer.GetSharedMaterials(List<Material> sharedMaterials);
                if (avatarRenderer == null)
                {
                    continue;
                }
                Material[] avatarRendererSharedMaterials = avatarRenderer.sharedMaterials;
                for (int i = 0; i < avatarRendererSharedMaterials.Length; ++i)
                {
                    Material currentMaterial = avatarRendererSharedMaterials[i];
                    if (currentMaterial == null)
                    {
                        continue;
                    }

                    Material fallbackMaterial;
                    if (fallbackMaterialCache.HasFallbackMaterial(currentMaterial))
                    {
                        // material is in our swap list, so its already a fallback
                        fallbackMaterial = fallbackMaterialCache.GetFallBackMaterial(currentMaterial);

                        if (debug)
                        {
                            Debug.Log(string.Format("<color=cyan>*** Using existing fallback: '{0}' </color>", fallbackMaterial.shader.name));
                        }
                    }
                    else
                    {
                        // The current material is not in our safe list so create a fallback.
                        fallbackMaterial = CreateFallbackMaterial(currentMaterial, user);

                        // Map the current material to the fallback and the fallback to itself.
                        fallbackMaterialCache.AddFallbackMaterial(currentMaterial, fallbackMaterial);

                        fallbackMaterialCache.AddFallbackMaterial(fallbackMaterial, fallbackMaterial);

                        if (debug)
                        {
                            Debug.Log(string.Format("<color=cyan>*** Creating new fallback: '{0}' </color>", fallbackMaterial.shader.name));
                        }
                    }

                    avatarRendererSharedMaterials[i] = fallbackMaterial;
                }

                avatarRenderer.sharedMaterials = avatarRendererSharedMaterials;
            }
        }
        public static void ReplaceShaders(VRC.Core.APIUser user, List <Renderer> avatarRenderers, FallbackMaterialCache fallbackMaterialCache, bool debug = false)
        {
            foreach (Renderer avatarRenderer in avatarRenderers)
            {
                if (avatarRenderer == null)
                {
                    continue;
                }

                avatarRenderer.GetSharedMaterials(_replaceShadersWorkingList);
                bool anyReplaced = false;
                for (int i = 0; i < _replaceShadersWorkingList.Count; ++i)
                {
                    Material currentMaterial = _replaceShadersWorkingList[i];
                    if (currentMaterial == null)
                    {
                        continue;
                    }

                    // Check if the material has a cached fallback material if not then create a new one.
                    if (!fallbackMaterialCache.TryGetFallbackMaterial(currentMaterial, out Material fallbackMaterial))
                    {
                        fallbackMaterial = CreateFallbackMaterial(currentMaterial, user);

                        // Map the current material to the fallback and the fallback to itself.
                        fallbackMaterialCache.AddFallbackMaterial(currentMaterial, fallbackMaterial);
                        fallbackMaterialCache.AddFallbackMaterial(fallbackMaterial, fallbackMaterial);

                        if (debug)
                        {
                            Debug.Log($"<color=cyan>*** Creating new fallback: '{fallbackMaterial.shader.name}' </color>");
                        }

                        if (fallbackMaterial == currentMaterial)
                        {
                            continue;
                        }

                        _replaceShadersWorkingList[i] = fallbackMaterial;
                        anyReplaced = true;
                        continue;
                    }

                    // If the material is the fallback then we don't need to change it.
                    if (currentMaterial == fallbackMaterial)
                    {
                        continue;
                    }

                    if (debug)
                    {
                        Debug.Log($"<color=cyan>*** Using existing fallback: '{fallbackMaterial.shader.name}' </color>");
                    }

                    _replaceShadersWorkingList[i] = fallbackMaterial;
                    anyReplaced = true;
                }

                if (anyReplaced)
                {
                    avatarRenderer.sharedMaterials = _replaceShadersWorkingList.ToArray();
                }
            }
        }