void OnGUI()
    {
        GUILayout.BeginHorizontal();
        m_Locked    = GUILayout.Toggle(m_Locked, "Locked");
        m_Histogram = GUILayout.Toggle(m_Histogram, "Histogram");
        m_Scale     = EditorGUILayout.FloatField(m_Scale);
        GUILayout.EndHorizontal();
        //            GUILayout.Label("Base Settings", EditorStyles.boldLabel);

        if (m_Source == null || m_Source.m_Param == null || m_Source.m_Cached == null)
        {
            return;
        }
        int wantWidth  = m_Source.m_Cached.width;
        int wantHeight = m_Source.m_Cached.height;

        if (m_Histogram)
        {
            wantWidth  = 512;
            wantHeight = 512;
        }

        if (m_tex == null || m_tex.width != wantWidth || m_tex.height != wantHeight)
        {
            AllocTex(wantWidth, wantHeight);
        }

        RenderTexture preview = RenderTexture.GetTemporary(wantWidth, wantHeight, 0, RenderTextureFormat.ARGB32);

        Material m = TextureNode.GetMaterial("TextureOps");

        m.SetVector("_Multiply", new Vector4(1.0f, 0, 0, 0));
        if (m_Histogram)
        {
            CreatePreviewHistogram(preview);
        }
        else
        {
            Graphics.Blit(m_Source.m_Cached, preview, m, m_Source.m_TexMode == TextureNode.TexMode.Greyscale
                    ? (int)ShaderOp.CopyGrey: (int)ShaderOp.CopyColor);
        }
        m_tex.ReadPixels(new Rect(0, 0, wantWidth, wantHeight), 0, 0);
        m_tex.Apply();
        RenderTexture.active = null;

        //            EditorGUILayout.LabelField("\n Warning: Erases Current Canvas", EditorStyles.wordWrappedLabel);
        //            EditorGUILayout.Separator();
        Rect texRect = new Rect(2, 20, position.width - 4, position.height - 24);

        GUILayout.BeginArea(texRect, GUI.skin.box);
        GUI.DrawTexture(texRect, m_tex, ScaleMode.StretchToFill);//ScaleMode.StretchToFill);
        //GUI.DrawTexture(texRect, m_Preview, ScaleMode.ScaleToFit);//ScaleMode.StretchToFill);
        GUILayout.EndArea();
        RenderTexture.ReleaseTemporary(preview);
    }
    void CreatePreviewHistogram(RenderTexture preview)
    {
        if (m_ComputeShader2 == null)
        {
            m_ComputeShader2 = (ComputeShader)Resources.Load("EyeHistogram");
        }
        var cs = m_ComputeShader2;

        if (m_Buffer == null)
        {
            m_Buffer = new ComputeBuffer(512 * 1, sizeof(uint) << 2);
        }
        int kernel = cs.FindKernel("KHistogramClear");

        cs.SetBuffer(kernel, "_Histogram", m_Buffer);
        cs.Dispatch(kernel, 1, 1, 1);

        kernel = cs.FindKernel("KHistogramGather");
        cs.SetBuffer(kernel, "_Histogram", m_Buffer);
        Texture source = m_Source.m_Cached;

        cs.SetTexture(kernel, "_Source", source);
        cs.SetInt("_IsLinear", GraphicsUtils.isLinearColorSpace ? 1 : 0);
        cs.SetVector("_Res", new Vector4(source.width, source.height, 0f, 0f));
        cs.SetVector("_Channels", new Vector4(1f, 1f, 1f, 0f));

        cs.Dispatch(kernel, Mathf.CeilToInt(source.width / 16f), Mathf.CeilToInt(source.height / 16f), 1);

        kernel = cs.FindKernel("KHistogramScale");
        cs.SetBuffer(kernel, "_Histogram", m_Buffer);
        cs.SetVector("_Res", new Vector4(512, 512, m_Scale, 0f));
        cs.Dispatch(kernel, 1, 1, 1);
        Material m = TextureNode.GetMaterial("TextureOps");

        m.SetVector("_Multiply", new Vector4(preview.width, preview.height, 0, 0));
        m.SetBuffer("_Histogram", m_Buffer);
        Graphics.Blit(m_Source.m_Cached, preview, m, (int)ShaderOp.Histogram);
    }
Beispiel #3
0
    public override bool Calculate()
    {
        allInputsReady();

        if (m_Loops == 0 && (Inputs[0].connection == null || Inputs[0].connection.IsValueNull))//!allInputsReady())
        {
            Debug.LogError(" m_LoopCount set to 0 input 0 is null");
            m_Loops = 0;
            return(false);
        }
        if (m_Loops > 0 && (Inputs[1].connection == null || Inputs[1].connection.IsValueNull))//!allInputsReady())
        {
            Debug.LogError(" m_LoopCount set to 1 input 1 is null");
            m_Loops = 1;
            return(false);
        }
        if (m_Loops == 0)
        {
            Outputs[0].SetValue <TextureParam>(Inputs[0].GetValue <TextureParam>());
        }
        else
        {
            //check if any of our connects to Output Looped, output straight back to us
            //if so that creates a render case where the same renderTexture is the input and the output, so make a copy
            bool inputIsOutput = false;
            foreach (var c in Outputs[0].connections)
            {
                foreach (var o in c.body.Outputs)
                {
                    foreach (var c2 in o.connections)
                    {
                        if (c2.body == this)
                        {
//                            inputIsOutput = true;
                            Debug.LogError("found an in out is the same from " + o.body);
                        }
                    }
                }
            }
            if (inputIsOutput)
            {
                if (m_Temp == null)
                {
                    m_Temp = new TextureParam(Inputs[1].GetValue <TextureParam>());
                }
                Material m = TextureNode.GetMaterial("TextureOps");
                Graphics.Blit(Inputs[1].GetValue <TextureParam>().GetHWSourceTexture(), m_Temp.m_Destination, m,
                              (int)ShaderOp.CopyColorAndAlpha);

                Outputs[0].SetValue <TextureParam>(m_Temp);
            }
            else
            {
                Outputs[0].SetValue <TextureParam>(Inputs[1].GetValue <TextureParam>());
            }
        }
        Outputs[2].SetValue <float>((float)m_Loops / (float)m_LoopCount);
        m_Loops++;
//        Debug.LogError("Loop Count Inc" + m_Loops + " / " + m_LoopCount);
        if (m_Loops >= m_LoopCount)
        {
            Outputs[1].SetValue <TextureParam>(Inputs[1].GetValue <TextureParam>());
        }
        else
        {
            if (Outputs[2] != null)
            {
                Node.ms_GlobalDirtyID++;
                SetDirty(this);
                calculated = true; //so the descendant can calculate that uses output 0
                foreach (var c in Outputs[2].connections)
                {
                    if (c != null)
                    {
                        NodeEditor.ContinueCalculation(c.body);
                    }
                }
                calculated = false;
            }
            if (Outputs[0] != null)
            {
                Node.ms_GlobalDirtyID++;
                SetDirty(this);
                calculated = true; //so the descendant can calculate that uses output 0
                foreach (var c in Outputs[0].connections)
                {
                    if (c != null)
                    {
                        NodeEditor.ContinueCalculation(c.body);
                    }
                }
                calculated = false;
            }
        }
        return(m_Loops >= m_LoopCount);
    }