private void ReplaceTextures(AssetLoadedContext obj)
        {
            foreach (var objAsset in obj.Assets)
            {
                if (objAsset is GameObject go)
                {
                    var mrs = go.GetComponentsInChildren <MeshRenderer>();
                    foreach (var meshRenderer in mrs)
                    {
                        if (meshRenderer.material && meshRenderer.material.mainTexture)
                        {
                            var texName = meshRenderer.material.mainTexture.name;
                            Logger.LogDebug($"Trying to load tex {texName}");
                            if (!TexPaths.TryGetValue(texName, out var path))
                            {
                                continue;
                            }
                            Logger.LogDebug($"Loading {path}");
                            if (!TexCache.TryGetValue(texName, out var tex))
                            {
                                tex = TexCache[texName] = new Texture2D(1, 1, TextureFormat.ARGB32, false);
                                tex.LoadImage(File.ReadAllBytes(path));
                            }

                            meshRenderer.material.mainTexture = tex;
                        }
                    }
                }
            }
        }
        //internal void Update()
        //{
        //    if (Input.GetKeyDown(KeyCode.C))
        //    {
        //        Dictionary<string, int> shaders = new Dictionary<string, int>();
        //        foreach (var shader in Resources.FindObjectsOfTypeAll<Shader>())
        //            if (shaders.ContainsKey(shader.name))
        //                shaders[shader.name]++;
        //            else
        //                shaders[shader.name] = 1;
        //        foreach (var shader in shaders)
        //            Logger.LogInfo($"Shader:{shader.Key} {shader.Value}");
        //    }
        //}

        /// <summary>
        /// Every time an asset is loaded, swap its shader for the one loaded by MaterialEditor. This reduces the number of instances of a shader once they are cleaned up by garbage collection
        /// which reduce RAM usage, etc. Also fixes KK mods in EC by swapping them to the equivalent EC shader.
        /// </summary>
        private void AssetLoadedHook(AssetLoadedContext context)
        {
            if (!ShaderOptimization.Value)
            {
                return;
            }

            if (context.Asset is GameObject go)
            {
                foreach (var rend in go.GetComponentsInChildren <Renderer>())
                {
                    foreach (var mat in rend.materials)
                    {
#if EC
                        if (ShaderBlacklist.Contains(mat.shader.name))
                        {
                            continue;
                        }
#endif
                        if (LoadedShaders.TryGetValue(mat.shader.name, out var shaderData) && shaderData.Shader != null)
                        {
                            int renderQueue = mat.renderQueue;

                            mat.shader      = shaderData.Shader;
                            mat.renderQueue = renderQueue;
                        }
                    }
                }
            }
            else if (context.Asset is Material mat)
            {
#if EC
                if (ShaderBlacklist.Contains(mat.shader.name))
                {
                    return;
                }
#endif

                if (LoadedShaders.TryGetValue(mat.shader.name, out var shaderData) && shaderData.Shader != null)
                {
                    int renderQueue = mat.renderQueue;
                    mat.shader      = shaderData.Shader;
                    mat.renderQueue = renderQueue;
                }
            }
            else if (context.Asset is Shader shader)
            {
#if EC
                if (ShaderBlacklist.Contains(shader.name))
                {
                    return;
                }
#endif
                if (LoadedShaders.TryGetValue(shader.name, out var shaderData) && shaderData.Shader != null)
                {
                    context.Asset = shaderData.Shader;
                }
            }
        }
        public void AssetLoaded(AssetLoadedContext context)
        {
            if (context.Asset is Texture2D texture2d) // also acts as a null check
            {
                // TODO: Modify, replace or dump the texture

                context.Asset = texture2d; // only need to update the reference if you created a new texture
                context.Complete(
                    skipRemainingPostfixes: true);
            }
        }
示例#4
0
 private void PatchLoadedAsset(AssetLoadedContext ctx)
 {
     foreach (var obj in ctx.Assets)
     {
         var path = ctx.GetUniqueFileSystemAssetPath(obj);
         if (!(obj is GameObject go))
         {
             continue;
         }
         ReplaceMeshes(go, path);
     }
 }
示例#5
0
        /// <summary>
        /// Every time an asset is loaded, swap its shader for the one loaded by MaterialEditor. This reduces the number of instances of a shader once they are cleaned up by garbage collection
        /// which reduce RAM usage, etc. Also fixes KK mods in EC by swapping them to the equivalent EC shader.
        /// </summary>
        private static void AssetLoadedHook(AssetLoadedContext context)
        {
            if (!ShaderOptimization.Value)
            {
                return;
            }

            if (context.Asset is GameObject go)
            {
                var renderers = go.GetComponentsInChildren <Renderer>();
                for (var i = 0; i < renderers.Length; i++)
                {
                    var renderer = renderers[i];
                    for (var j = 0; j < renderer.materials.Length; j++)
                    {
                        var material = renderer.materials[j];
                        if (LoadedShaders.TryGetValue(material.shader.name, out var shaderData) && shaderData.Shader != null && shaderData.ShaderOptimization)
                        {
                            int renderQueue = material.renderQueue;
                            material.shader      = shaderData.Shader;
                            material.renderQueue = renderQueue;
                        }
                    }
                }
            }
            else if (context.Asset is Material mat)
            {
                if (LoadedShaders.TryGetValue(mat.shader.name, out var shaderData) && shaderData.Shader != null && shaderData.ShaderOptimization)
                {
                    int renderQueue = mat.renderQueue;
                    mat.shader      = shaderData.Shader;
                    mat.renderQueue = renderQueue;
                }
            }
            else if (context.Asset is Shader shader)
            {
                if (LoadedShaders.TryGetValue(shader.name, out var shaderData) && shaderData.Shader != null && shaderData.ShaderOptimization)
                {
                    context.Asset = shaderData.Shader;
                }
            }
        }
 private void HandleAsset(AssetLoadedContext context) => Handle(context);