void UpdateChannelPreview(Info info, int i)
        {
            _infos[i] = info;
            var texture  = info.Texture;
            var channel  = info.Channel;
            var isInvert = info.Invert;

            if (texture != null)
            {
                var conf = new TextureCombinerConfiguration(_previwsSize.x, _previwsSize.y);
                conf.Set(texture, channel, TextureChannel.R, isInvert);
                conf.Set(texture, channel, TextureChannel.G, isInvert);
                conf.Set(texture, channel, TextureChannel.B, isInvert);
                conf.Set(texture, channel, TextureChannel.A, isInvert);

                var previewTexture = TextureCombiner.Create(conf);
                info.Preview = previewTexture;
                _infos[i]    = info;
            }
        }
        void Save()
        {
            string savePath = EditorUtility.SaveFilePanel("Save", Application.dataPath, "texture.png", _formats[_textureFormat].ToLowerInvariant());

            if (savePath != string.Empty)
            {
                var config = new TextureCombinerConfiguration(_resultSize.x, _resultSize.y);
                for (int i = 0; i < _infos.Length; i++)
                {
                    var info = _infos[i];
                    if (info.Enabled && info.Texture != null)
                    {
                        config.Set(info.Texture, info.Channel, (TextureChannel)i, info.Invert);
                    }
                }

                if (!_infos[(int)TextureChannel.A].Enabled)
                {
                    config.Set(Texture2D.whiteTexture, TextureChannel.R, TextureChannel.A, 0);
                }

                Texture2D output = TextureCombiner.Create(config);
                if (_textureFormat == 0)
                {
                    File.WriteAllBytes(savePath, output.EncodeToJPG());
                }
                else if (_textureFormat == 1)
                {
                    File.WriteAllBytes(savePath, output.EncodeToPNG());
                }
                else
                {
                    File.WriteAllBytes(savePath, output.EncodeToEXR());
                }

                AssetDatabase.Refresh();
            }
        }
        void UpdatePreview()
        {
            if (!(_previewChannel == (int)PreviewChannel.RGB || _previewChannel == (int)PreviewChannel.RGBA))
            {
                var config = new TextureCombinerConfiguration(_previwsSize.x, _previwsSize.y);
                var info   = _infos[_previewChannel - 2];
                if (info.Enabled)
                {
                    config.Set(info.Texture, info.Channel, info.Channel, info.Invert);
                }
                _preview = TextureCombiner.Internal.Create(config, true);
            }
            else
            {
                var config = new TextureCombinerConfiguration(_previwsSize.x, _previwsSize.y);
                var count  = _infos.Length - _previewChannel;
                for (int i = 0; i < count; i++)
                {
                    var info = _infos[i];
                    if (info.Enabled && info.Texture != null)
                    {
                        config.Set(info.Texture, info.Channel, (TextureChannel)i, info.Invert);
                    }
                }

                if (_previewChannel == (int)PreviewChannel.RGB || !_infos[(int)TextureChannel.A].Enabled)
                {
                    config.A = new TextureInfo
                    {
                        Channel = TextureChannel.R,
                        Texture = Texture2D.whiteTexture
                    };
                }
                _preview = TextureCombiner.Create(config);
            }
        }
示例#4
0
 public static Texture2D Create(TextureCombinerConfiguration configuration)
 {
     return(Internal.Create(configuration, false));
 }
示例#5
0
            public static Texture2D Create(TextureCombinerConfiguration configuration, bool preview)
            {
                var unlitColor    = Shader.Find("Unlit/Color");
                var unlitMaterial = new Material(unlitColor);

                unlitMaterial.color = configuration.DefaultColor;

                var shader   = Shader.Find("Utils/TextureCombiner");
                var material = new Material(shader);

                material.SetInt("_Preview", preview ? 1 : 0);

                var renderTexture = RenderTexture.GetTemporary(configuration.Width, configuration.Height);

                renderTexture.antiAliasing = 16;

                Graphics.Blit(Texture2D.whiteTexture, renderTexture, unlitMaterial);

                material.SetVector("_Invert", new Vector4(configuration.R.Invert, configuration.G.Invert, configuration.B.Invert, configuration.A.Invert));

                if (configuration.R.Texture)
                {
                    material.SetTexture("_R", configuration.R.Texture);
                }
                else
                {
                    material.SetTexture("_R", renderTexture);
                }
                material.SetVector("_BlendR", GetVectorByChannel(configuration.R.Channel));

                if (configuration.G.Texture)
                {
                    material.SetTexture("_G", configuration.G.Texture);
                }
                else
                {
                    material.SetTexture("_G", renderTexture);
                }
                material.SetVector("_BlendG", GetVectorByChannel(configuration.G.Channel));

                if (configuration.B.Texture)
                {
                    material.SetTexture("_B", configuration.B.Texture);
                }
                else
                {
                    material.SetTexture("_B", renderTexture);
                }
                material.SetVector("_BlendB", GetVectorByChannel(configuration.B.Channel));

                if (configuration.A.Texture)
                {
                    material.SetTexture("_A", configuration.A.Texture);
                }
                else
                {
                    material.SetTexture("_A", renderTexture);
                }
                material.SetVector("_BlendA", GetVectorByChannel(configuration.A.Channel));

                Graphics.Blit(renderTexture, renderTexture, material);

                RenderTexture.active = renderTexture;

                var texture = new Texture2D(renderTexture.width, renderTexture.height, TextureFormat.RGBA32, false)
                {
                    filterMode = FilterMode.Bilinear
                };

                texture.ReadPixels(new Rect(0, 0, renderTexture.width, renderTexture.height), 0, 0);
                texture.Apply();

                RenderTexture.active = null;
                RenderTexture.ReleaseTemporary(renderTexture);

                return(texture);
            }