示例#1
0
        public void SavePNG(string path, int _width, int _height)
        {
            var tex = new Texture2D(_width, _height, TextureParam.ms_TexFormat, false);

            if (IsGrey())
            {
                //for grey scale we have to create a new texture with alpha channel ==1

                RenderTexture rt = new RenderTexture(_width, _height, 0, RenderTextureFormat.ARGB32);

                Material m = TextureNode.GetMaterial("TextureOps");
                m.SetInt("_MainIsGrey", IsGrey() ? 1 : 0);
                m.SetInt("_TextureBIsGrey", 1);
                m.SetTexture("_GradientTex", GetWhite().m_Destination);

                Graphics.Blit(GetHWSourceTexture(), rt, m, (int)ShaderOp.CopyColorAndAlpha);

                RenderTexture.active = rt;
                tex.ReadPixels(new Rect(0, 0, rt.width, rt.height), 0, 0);
                //input.DestinationToTexture(m_Output);
                tex.Apply();
                RenderTexture.active = null;
                rt.DiscardContents();
                rt.Release();
                rt = null;
            }
            else
            {
                RenderTexture.active = m_Destination;
                tex.ReadPixels(new Rect(0, 0, m_Width, m_Height), 0, 0);
                tex.Apply();
                RenderTexture.active = null;
            }
            byte[] bytes = tex.EncodeToPNG();

            if (!string.IsNullOrEmpty(path))
            {
                File.WriteAllBytes(path, bytes);
            }
        }
示例#2
0
        //if we need the data extracted from a renderTexture (for eaxmple to get CPU access to pixles
        public Texture2D GetTex2D()
        {
            if (m_Tex != null)
            {
                return(m_Tex);
            }
            var tex = new Texture2D(m_Width, m_Height, TextureParam.ms_TexFormat, false);

            if (IsGrey())
            {
                //for grey scale we have to create a new texture with alpha channel ==1

                RenderTexture rt = new RenderTexture(m_Width, m_Height, 0, RenderTextureFormat.ARGB32);

                Material m = TextureNode.GetMaterial("TextureOps");
                m.SetInt("_MainIsGrey", IsGrey() ? 1 : 0);
                m.SetInt("_TextureBIsGrey", 1);
                m.SetTexture("_GradientTex", GetWhite().m_Destination);

                Graphics.Blit(GetHWSourceTexture(), rt, m, (int)ShaderOp.CopyColorAndAlpha);

                RenderTexture.active = rt;
                tex.ReadPixels(new Rect(0, 0, rt.width, rt.height), 0, 0);
                //input.DestinationToTexture(m_Output);
                tex.Apply();
                RenderTexture.active = null;
                rt.DiscardContents();
                rt.Release();
                rt = null;
            }
            else
            {
                RenderTexture.active = m_Destination;
                tex.ReadPixels(new Rect(0, 0, m_Width, m_Height), 0, 0);
                tex.Apply();
                RenderTexture.active = null;
            }
            return(tex);
        }
        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, 1.0f, 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);
        }
        void OnGUI()
        {
            if (Event.current.type == EventType.ScrollWheel)
            {
                //mous epos is in window cords 0 y is top, 40 is start of texture
                Vector2 adjustedMouse = Event.current.mousePosition;
                adjustedMouse.y -= 40;
                adjustedMouse.y  = position.height - adjustedMouse.y; // texture cords have 0,0 bottom left, mouse co ords are 0,0 top left
                Vector2 tPos = adjustedMouse;

                tPos.x *= m_Window.width / position.width;
                tPos.y *= m_Window.height / position.height;
                tPos.x += m_Window.x;
                tPos.y += m_Window.y;

                //we now have the cord we are zooming in on in texture space
                float size = m_Window.width * 0.5f;//(tPos - m_Window.min).magnitude;
                size           *= 1.0f + (Event.current.delta.y * 0.03f);
                m_Window.width  = size * 2;
                m_Window.height = size * 2;

                //after we zoom in or out we want tpos to be at the same mouse pos
                // new tposx=adjustedMouse*(m_Window.width/ position.width)+m_WindowX
                //m_WindowX=tposx-adjustedMouse*(m_Window.width/ position.width)
                //
                m_Window.x = tPos.x - adjustedMouse.x * (m_Window.width / position.width);
                m_Window.y = (tPos.y - adjustedMouse.y * (m_Window.height / position.height));



                Repaint();
//                Debug.Log(" mouse wheel "+ Event.current.delta+" scale "+m_Scale+" mouse pos "+ Event.current.mousePosition+" windowpos "+position+" tpos "+tPos+" "+m_Window);
            }
            if (Event.current.type == EventType.MouseDrag && Event.current.button == 2)
            {
                m_Window.x -= m_Window.width * Event.current.delta.x / position.width;   //steps .3 at a time
                m_Window.y += m_Window.height * Event.current.delta.y / position.height; //steps .3 at a time

                Repaint();
            }
            GUILayout.BeginHorizontal();
            m_Locked    = GUILayout.Toggle(m_Locked, "Locked");
            m_Histogram = GUILayout.Toggle(m_Histogram, "Histogram");

            GUILayout.EndHorizontal();


            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);
            }
            Rect texRect = new Rect(2, 20, position.width - 4, position.height - 24);

            if (Event.current.type == EventType.Repaint)
            {
                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();
                m_tex.filterMode = FilterMode.Point;
                GUILayout.BeginArea(texRect, GUI.skin.box);
                //GUI.DrawTexture(texRect, m_tex,ScaleMode.StretchToFill);//ScaleMode.StretchToFill);
                GUI.DrawTextureWithTexCoords(texRect, m_tex, m_Window);
                //new Rect(0+m_Scale,0 + m_Scale, 1- m_Scale, 1- m_Scale));//ScaleMode.StretchToFill);
                //GUI.DrawTexture(texRect, m_Preview, ScaleMode.ScaleToFit);//ScaleMode.StretchToFill);
                GUILayout.EndArea();
                RenderTexture.ReleaseTemporary(preview);
            }
            else
            {
                m_tex.filterMode = FilterMode.Point;
                GUILayout.BeginArea(texRect, GUI.skin.box);
                GUI.DrawTextureWithTexCoords(texRect, m_tex, m_Window);
                GUILayout.EndArea();
            }
        }
示例#5
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(true);
            }
            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(true);
            }
            if (m_LoopCount == 0)
            {
                Outputs[1].SetValue <TextureParam>(Inputs[0].GetValue <TextureParam>());
                return(true);
            }
            while (m_Loops < m_LoopCount)
            {
                if (m_Loops == 0)
                {
                    //First iteration set output to the first inteartion input
                    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)
                    {
                        Texture rt = Inputs[1].GetValue <TextureParam>().GetHWSourceTexture();
                        if (m_Temp == null || m_Temp.m_Destination == null || m_Temp.m_Destination.width != rt.width || m_Temp.m_Destination.height != rt.height || m_Temp.m_Destination.filterMode != rt.filterMode)
                        {
                            m_Temp = new TextureParam(Inputs[1].GetValue <TextureParam>());
                        }
                        Material m = TextureNode.GetMaterial("TextureOps");
                        Graphics.Blit(rt, m_Temp.m_Destination, m, (int)ShaderOp.CopyColorAndAlpha);

                        Outputs[0].SetValue <TextureParam>(m_Temp);
                    }
                    else
                    {
                        //general loop case set OutLoop to Loop Input
                        Outputs[0].SetValue <TextureParam>(Inputs[1].GetValue <TextureParam>());
                    }
                }

                Outputs[2].SetValue <float>((float)m_Loops / ((float)m_LoopCount - 1.0f));
#if DEBUG_LOOPBASIC
                Debug.LogError("Loop Count Inc" + m_Loops + " / " + m_LoopCount + " percentage " +
                               ((float)m_Loops / (float)m_LoopCount));
#endif
                m_Loops++;

                {
                    Node.ms_GlobalDirtyID++;
                    SetDirty(this);
                    var workList = new List <Node>();
                    if (Outputs[2] != null) //percentage
                    {
                        foreach (var c in Outputs[2].connections)
                        {
                            if (c != null)
                            {
                                if (!workList.Contains(c.body))
                                {
                                    workList.Add(c.body);
                                }
                            }
                        }
                    }
                    if (Outputs[0] != null) //out loop
                    {
                        foreach (var c in Outputs[0].connections)
                        {
                            if (c != null)
                            {
                                if (!workList.Contains(c.body))
                                {
                                    workList.Add(c.body);
                                }
                            }
                        }
                        calculated = false;
                    }
                    if (workList.Count > 0)
                    {
                        calculated = true;                     //so the descendant can calculate that uses output 0
//                    Debug.LogError(NodeEditor.GetPad() + " LoopBasic Executes Worklist");
                        NodeEditor.StartCalculation(workList); //go finish this worklist
//                    Debug.LogError(NodeEditor.GetPad() + " LoopBasic Finished Execute Worklist " + workList.Count);
                        calculated = false;
                    }

                    if (workList.Count != 0)
                    {
/*
 *                  Debug.LogError(NodeEditor.GetPad() + "workList had work left dec loop ");
 *                  foreach (var x in workList)
 *                      Debug.LogError(NodeEditor.GetPad() + "workList had work left " + x);
 */
                        m_Loops--; //we didnt execute
                        return(false);
                    }
                }
            }
            if (m_Loops >= m_LoopCount)
            {
                //Finished set final output
                Outputs[1].SetValue <TextureParam>(Inputs[1].GetValue <TextureParam>());
            }

            bool ret = m_Loops >= m_LoopCount;
//        Debug.Log(NodeEditor.GetPad() +"LoopBasic iterates:  "+m_Loops+"/"+m_LoopCount+" return "+ret);

            return(ret);
        }