Example #1
0
        private void Awake()
        {
            Instance = this;
            Logger   = base.Logger;
            Directory.CreateDirectory(ExportPath);

            UIScale = Config.Bind("Config", "UI Scale", 1.75f, new ConfigDescription("Controls the size of the window.", new AcceptableValueRange <float>(1f, 3f), new ConfigurationManagerAttributes {
                Order = 5
            }));
            UIWidth = Config.Bind("Config", "UI Width", 0.3f, new ConfigDescription("Controls the size of the window.", new AcceptableValueRange <float>(0f, 1f), new ConfigurationManagerAttributes {
                Order = 4, ShowRangeAsPercent = false
            }));
            UIHeight = Config.Bind("Config", "UI Height", 0.3f, new ConfigDescription("Controls the size of the window.", new AcceptableValueRange <float>(0f, 1f), new ConfigurationManagerAttributes {
                Order = 3, ShowRangeAsPercent = false
            }));
            WatchTexChanges = Config.Bind("Config", "Watch File Changes", true, new ConfigDescription("Watch for file changes and reload textures on change. Can be toggled in the UI.", null, new ConfigurationManagerAttributes {
                Order = 2
            }));
            ShaderOptimization = Config.Bind("Config", "Shader Optimization", true, new ConfigDescription("Replaces every loaded shader with the MaterialEditor copy of the shader. Reduces the number of copies of shaders loaded which reduces RAM usage and improves performance.", null, new ConfigurationManagerAttributes {
                Order = 1
            }));

            UIScale.SettingChanged            += MaterialEditorUI.UISettingChanged;
            UIWidth.SettingChanged            += MaterialEditorUI.UISettingChanged;
            UIHeight.SettingChanged           += MaterialEditorUI.UISettingChanged;
            WatchTexChanges.SettingChanged    += WatchTexChanges_SettingChanged;
            ShaderOptimization.SettingChanged += ShaderOptimization_SettingChanged;

            ResourceRedirection.RegisterAssetLoadedHook(HookBehaviour.OneCallbackPerResourceLoaded, AssetLoadedHook);
            LoadXML();
        }
Example #2
0
        public virtual void Awake()
        {
            Instance = this;
            Logger   = base.Logger;
            Directory.CreateDirectory(ExportPath);

            UIScale = Config.Bind("Config", "UI Scale", 1.75f, new ConfigDescription("Controls the size of the window.", new AcceptableValueRange <float>(1f, 3f), new ConfigurationManagerAttributes {
                Order = 5
            }));
            UIWidth = Config.Bind("Config", "UI Width", 0.3f, new ConfigDescription("Controls the size of the window.", new AcceptableValueRange <float>(0f, 1f), new ConfigurationManagerAttributes {
                Order = 4, ShowRangeAsPercent = false
            }));
            UIHeight = Config.Bind("Config", "UI Height", 0.3f, new ConfigDescription("Controls the size of the window.", new AcceptableValueRange <float>(0f, 1f), new ConfigurationManagerAttributes {
                Order = 3, ShowRangeAsPercent = false
            }));
            WatchTexChanges = Config.Bind("Config", "Watch File Changes", true, new ConfigDescription("Watch for file changes and reload textures on change. Can be toggled in the UI.", null, new ConfigurationManagerAttributes {
                Order = 2
            }));
            ShaderOptimization = Config.Bind("Config", "Shader Optimization", true, new ConfigDescription("Replaces every loaded shader with the MaterialEditor copy of the shader. Reduces the number of copies of shaders loaded which reduces RAM usage and improves performance.", null, new ConfigurationManagerAttributes {
                Order = 1
            }));
            ExportBakedMesh = Config.Bind("Config", "Export Baked Mesh", false, new ConfigDescription("When enabled, skinned meshes will be exported in their current state with all customization applied as well as in the current pose.", null, new ConfigurationManagerAttributes {
                Order = 1
            }));
            ExportBakedWorldPosition = Config.Bind("Config", "Export Baked World Position", false, new ConfigDescription("When enabled, objects will be exported with their position changes intact so that, i.e. when exporting two objects they retain their position relative to each other.\nOnly works when Export Baked Mesh is also enabled.", null, new ConfigurationManagerAttributes {
                Order = 1
            }));
            ConfigExportPath = Config.Bind("Config", "Export Path Override", "", new ConfigDescription($"Textures and models will be exported to this folder. If empty, exports to {ExportPathDefault}", null, new ConfigurationManagerAttributes {
                Order = 1
            }));

            UIScale.SettingChanged            += MaterialEditorUI.UISettingChanged;
            UIWidth.SettingChanged            += MaterialEditorUI.UISettingChanged;
            UIHeight.SettingChanged           += MaterialEditorUI.UISettingChanged;
            WatchTexChanges.SettingChanged    += WatchTexChanges_SettingChanged;
            ShaderOptimization.SettingChanged += ShaderOptimization_SettingChanged;
            ConfigExportPath.SettingChanged   += ConfigExportPath_SettingChanged;
            SetExportPath();

            ResourceRedirection.RegisterAssetLoadedHook(HookBehaviour.OneCallbackPerResourceLoaded, AssetLoadedHook);
            LoadXML();
        }
Example #3
0
        /// <summary>
        /// Exports the UV map(s) of the SkinnedMeshRenderer or MeshRenderer
        /// </summary>
        public static void ExportUVMaps(Renderer rend)
        {
            bool   openedFile   = false;
            Shader shader       = Shader.Find("Hidden/Internal-Colored");
            var    lineMaterial = new Material(shader)
            {
                hideFlags = HideFlags.HideAndDontSave
            };

            lineMaterial.SetInt("_SrcBlend", (int)UnityEngine.Rendering.BlendMode.SrcAlpha);
            lineMaterial.SetInt("_DstBlend", (int)UnityEngine.Rendering.BlendMode.OneMinusSrcAlpha);
            lineMaterial.SetInt("_Cull", (int)UnityEngine.Rendering.CullMode.Off);
            lineMaterial.SetInt("_ZWrite", 0);

            Mesh mr;

            if (rend is MeshRenderer meshRenderer)
            {
                mr = meshRenderer.GetComponent <MeshFilter>().mesh;
            }
            else if (rend is SkinnedMeshRenderer skinnedMeshRenderer)
            {
                mr = skinnedMeshRenderer.sharedMesh;
            }
            else
            {
                return;
            }

            for (int x = 0; x < mr.subMeshCount; x++)
            {
                var tris = mr.GetTriangles(x);
                var uvs  = mr.uv;

                const int size           = 4096;
                var       _renderTexture = RenderTexture.GetTemporary(size, size);
                var       lineColor      = Color.black;
                Graphics.SetRenderTarget(_renderTexture);
                GL.PushMatrix();
                GL.LoadOrtho();
                //GL.LoadPixelMatrix(); // * 2 - 1, maps differently.
                GL.Clear(false, true, Color.clear);

                lineMaterial.SetPass(0);
                GL.Begin(GL.LINES);
                GL.Color(lineColor);

                for (var i = 0; i < tris.Length; i += 3)
                {
                    var v  = uvs[tris[i]];
                    var n1 = uvs[tris[i + 1]];
                    var n2 = uvs[tris[i + 2]];

                    GL.Vertex(v);
                    GL.Vertex(n1);

                    GL.Vertex(v);
                    GL.Vertex(n2);

                    GL.Vertex(n1);
                    GL.Vertex(n2);
                }
                GL.End();

                GL.PopMatrix();
                Graphics.SetRenderTarget(null);

                var png = MaterialEditorPluginBase.GetT2D(_renderTexture);
                RenderTexture.ReleaseTemporary(_renderTexture);

                string filename = Path.Combine(MaterialEditorPluginBase.ExportPath, $"{rend.NameFormatted()}_{x}.png");
                File.WriteAllBytes(filename, png.EncodeToPNG());
                Object.DestroyImmediate(png);
                MaterialEditorPluginBase.Logger.LogInfo($"Exported {filename}");
                if (!openedFile)
                {
                    Utilities.OpenFileInExplorer(filename);
                }
                openedFile = true;
            }
        }