private void SaveRenderTextureToPNG(
        UnityEngine.RenderTexture renderTexture,
        UnityEngine.TextureFormat textureFormat,
        RenderCheck renderCheck)
    {
        string fileName = string.Empty;

        if (renderCheck.Match != null)
        {
            fileName = UnityEditor.AssetDatabase.GetAssetPath(renderCheck.Match);
        }
        else
        {
            fileName = UnityEditor.AssetDatabase.GenerateUniqueAssetPath(UnityEditor.AssetDatabase.GetAssetPath(renderCheck));
        }

        fileName = System.IO.Path.GetDirectoryName(fileName) + "/" + System.IO.Path.GetFileNameWithoutExtension(fileName) + ".png";

        if (renderCheck.Match != null && (renderCheck.Match.width != renderTexture.width || renderCheck.Match.height != renderTexture.height))
        {
            UnityEngine.GameObject.Destroy(renderCheck.Match);
        }

        if (renderCheck.Match == null)
        {
            renderCheck.Match = new UnityEngine.Texture2D(renderTexture.width, renderTexture.height, textureFormat, false, true);
        }

        UnityEngine.RenderTexture previousRT = UnityEngine.RenderTexture.active;
        UnityEngine.RenderTexture.active = renderTexture;
        UnityEngine.Texture2D linearTexture = new UnityEngine.Texture2D(renderTexture.width, renderTexture.height, UnityEngine.TextureFormat.RGBAFloat, false);
        linearTexture.ReadPixels(new UnityEngine.Rect(0.0f, 0.0f, (float)renderTexture.width, (float)renderTexture.height), 0, 0, false);
        //linearTexture.Apply();

        renderCheck.Match.SetPixels32(linearTexture.GetPixels32());
        UnityEngine.GameObject.DestroyImmediate(linearTexture);
        linearTexture = null;
        UnityEngine.RenderTexture.active = previousRT;

        renderCheck.Match.Apply(false, false);

        byte[] pngFile = UnityEngine.ImageConversion.EncodeToPNG(renderCheck.Match);

        try
        {
            using (System.IO.Stream fileStream = new System.IO.FileStream(fileName, System.IO.FileMode.Create))
            {
                System.IO.BinaryWriter streamWriter = new System.IO.BinaryWriter(fileStream);
                streamWriter.Write(pngFile);
                this.needRefresh = true;
                this.needReload.Add(new System.Collections.Generic.KeyValuePair <RenderCheck, string>(renderCheck, fileName));
            }
        }
        catch (System.Exception exception)
        {
            UnityEngine.Debug.LogError(string.Format("Saving texture at path {0} failed with exception {1}", fileName, exception.Message));
        }
    }
    private void DoGUI(UnityEngine.Rect r, RenderCheck renderCheck, bool capture, bool doGui)
    {
        UnityEngine.RenderTexture rt         = UnityEngine.RenderTexture.GetTemporary(renderCheck.Size.x, renderCheck.Size.y, 0, UnityEngine.RenderTextureFormat.Default, UnityEngine.RenderTextureReadWrite.sRGB);
        UnityEngine.RenderTexture previousRT = UnityEngine.RenderTexture.active;
        UnityEngine.RenderTexture.active = rt;
        UnityEngine.GL.PushMatrix();
        UnityEngine.GL.LoadPixelMatrix(0, rt.width, rt.height, 0);

        UnityEngine.Rect src  = new UnityEngine.Rect(0, 0, 1, 1);
        UnityEngine.Rect dest = new UnityEngine.Rect(0, 0, rt.width, rt.height);

        UnityEngine.Texture2D textureToUse = UnityEngine.Texture2D.whiteTexture;
        UnityEngine.Graphics.DrawTexture(dest, textureToUse, src, 0, 0, 0, 0, renderCheck.Material);

        if (capture)
        {
            this.SaveRenderTextureToPNG(rt, UnityEngine.TextureFormat.ARGB32, renderCheck);
            UnityEditor.EditorUtility.SetDirty(renderCheck);
        }

        UnityEngine.GL.PopMatrix();
        UnityEngine.RenderTexture.active = previousRT;

        if (doGui)
        {
            UnityEngine.Rect rectRT    = r;
            UnityEngine.Rect rectMatch = r;
            UnityEngine.Rect rectDiff  = r;
            rectRT.width    = rectRT.height;
            rectMatch.width = rectRT.height;
            rectDiff.width  = rectRT.height;
            rectMatch.x     = rectRT.xMax + 4;
            rectDiff.x      = rectMatch.xMax + 4;
            UnityEngine.GUI.Label(rectRT, rt);
            UnityEngine.GUI.Label(rectRT, "rt");
            if (renderCheck.Match != null)
            {
                UnityEngine.GUI.Label(rectMatch, renderCheck.Match);
                UnityEngine.GUI.Label(rectMatch, "ref");
                if (renderCheck.DiffMaterial != null)
                {
                    renderCheck.DiffMaterial.SetTexture("_MatchTex", renderCheck.Match);
                    UnityEditor.EditorGUI.DrawPreviewTexture(rectDiff, rt, renderCheck.DiffMaterial);
                    renderCheck.DiffMaterial.SetTexture("_MatchTex", null);
                    UnityEngine.GUI.Label(rectDiff, "diff");
                }
            }
        }

        UnityEngine.RenderTexture.ReleaseTemporary(rt);
        rt = null;
    }
    public override void OnInteractivePreviewGUI(UnityEngine.Rect r, UnityEngine.GUIStyle background)
    {
        RenderCheck renderCheck = this.target as RenderCheck;

        if (UnityEngine.Event.current.type == UnityEngine.EventType.Repaint &&
            renderCheck &&
            renderCheck.Material != null &&
            renderCheck.Size.x > 0 &&
            renderCheck.Size.y > 0 &&
            renderCheck.Size.x <= 4096 &&
            renderCheck.Size.y <= 4096)
        {
            DoGUI(r, renderCheck, false, true);
        }
    }
Пример #4
0
        public void DataContextUpdate(CadObject cadObject)
        {
            this.IsEnabled = true;
            XUpDn.Value    = cadObject.X;
            XUpDn.SetBinding(NumericUpDown.ValueProperty, "X");
            XUpDn.DataContext = cadObject;

            YUpDn.Value = cadObject.Y;
            YUpDn.SetBinding(NumericUpDown.ValueProperty, "Y");
            YUpDn.DataContext = cadObject;

            ZUpDn.Value = cadObject.Z;
            ZUpDn.SetBinding(NumericUpDown.ValueProperty, "Z");
            ZUpDn.DataContext = cadObject;

            MirrorCheck.DataContext = cadObject;
            MirrorCheck.IsChecked   = cadObject.Mirror;
            MirrorCheck.SetBinding(CheckBox.IsCheckedProperty, "Mirror");

            RenderCheck.IsChecked = cadObject.Render;
            RenderCheck.SetBinding(CheckBox.IsCheckedProperty, "Render");
            RenderCheck.DataContext = cadObject;

            FixCheck.IsChecked = cadObject.IsFix;
            FixCheck.SetBinding(CheckBox.IsCheckedProperty, "IsFix");
            FixCheck.DataContext = cadObject;

            AngleZUpDn.Value = cadObject.AngleZ;
            AngleZUpDn.SetBinding(NumericUpDown.ValueProperty, "AngleZ");
            AngleZUpDn.DataContext = cadObject;

            AngleYUpDn.Value = cadObject.AngleY;
            AngleYUpDn.SetBinding(NumericUpDown.ValueProperty, "AngleY");
            AngleYUpDn.DataContext = cadObject;

            AngleXUpDn.Value = cadObject.AngleX;
            AngleXUpDn.SetBinding(NumericUpDown.ValueProperty, "AngleX");
            AngleXUpDn.DataContext = cadObject;

            WidthUpDn.Value    = cadObject.ScaleX;
            WidthUpDn.Interval = MonchaCadViewer.Properties.Settings.Default.stg_scale_percent == true ? 1 : 0.01;
            Binding bindingScaleX = new Binding("ScaleX");

            bindingScaleX.Converter = new ScaleConverter(MonchaCadViewer.Properties.Settings.Default.stg_scale_percent, MonchaCadViewer.Properties.Settings.Default.stg_scale_invert);
            WidthUpDn.SetBinding(NumericUpDown.ValueProperty, bindingScaleX);
            WidthUpDn.DataContext = cadObject;

            HeightUpDn.Value    = cadObject.ScaleY;
            HeightUpDn.Interval = MonchaCadViewer.Properties.Settings.Default.stg_scale_percent == true ? 1 : 0.01;
            Binding bindingScaleY = new Binding("ScaleY");

            bindingScaleY.Converter = new ScaleConverter(MonchaCadViewer.Properties.Settings.Default.stg_scale_percent, MonchaCadViewer.Properties.Settings.Default.stg_scale_invert);
            HeightUpDn.SetBinding(NumericUpDown.ValueProperty, bindingScaleY);
            HeightUpDn.DataContext = cadObject;

            DeepUpDn.Value    = cadObject.ScaleZ;
            DeepUpDn.Interval = MonchaCadViewer.Properties.Settings.Default.stg_scale_percent == true ? 1 : 0.01;
            Binding bindingScaleZ = new Binding("ScaleZ");

            bindingScaleZ.Converter = new ScaleConverter(MonchaCadViewer.Properties.Settings.Default.stg_scale_percent, MonchaCadViewer.Properties.Settings.Default.stg_scale_invert);
            DeepUpDn.SetBinding(NumericUpDown.ValueProperty, bindingScaleZ);
            DeepUpDn.DataContext = cadObject;
        }