Exemple #1
0
        public virtual void PrepareShader()
        {
            lastShaderCode = null;
            if (OutputNode == null)
            {
                return;
            }

            List <Node> ordered = OrderNodesForShader();

            string frag = "#version 330 core\r\n"
                          + "out vec4 FragColor;\r\n"
                          + "in vec2 UV;\r\n"
                          + "const float PI = 3.14159265359;\r\n"
                          + "const float Rad2Deg = (180.0 / PI);\r\n"
                          + "const float Deg2Rad = (PI / 180.0);\r\n"
                          + "const float RandomSeed = " + randomSeed + ";\r\n"
                          + "uniform sampler2D Input0;\r\n"
                          + "uniform sampler2D Input1;\r\n"
                          + "uniform sampler2D Input2;\r\n"
                          + "uniform sampler2D Input3;\r\n"
                          + GLSLHash;

            int count = calls.Count;

            for (int i = 0; i < count; i++)
            {
                CallNode m = calls[i];
                if (m.selectedFunction == this)
                {
                    continue;
                }

                string s = m.GetFunctionShaderCode();

                if (string.IsNullOrEmpty(s))
                {
                    return;
                }

                if (frag.IndexOf(s) == -1)
                {
                    frag += s;
                }
            }

            frag += "void main() {\r\n";

            string intern = GetInternalShaderCode(ordered);

            if (string.IsNullOrEmpty(intern))
            {
                return;
            }

            frag += intern + "}";

            //one last check to verify the output actually has the expected output
            if (!HasExpectedOutput)
            {
                return;
            }

            lastShaderCode = frag;
        }
Exemple #2
0
        public virtual string GetFunctionShaderCode()
        {
            if (OutputNode == null)
            {
                return("");
            }

            string otherCalls = "";
            string frag       = "";

            List <Node> ordered = OrderNodesForShader();

            //this is in case this function references
            //other functions
            int count = calls.Count;

            for (int i = 0; i < count; i++)
            {
                CallNode m = calls[i];

                //no need to recreate the function
                //if it is a recursive function!
                if (m.selectedFunction == this)
                {
                    continue;
                }

                string s = m.GetFunctionShaderCode();

                if (string.IsNullOrEmpty(s))
                {
                    return("");
                }

                if (otherCalls.IndexOf(s) == -1)
                {
                    otherCalls += s;
                }
            }

            NodeType?outtype = GetOutputType();

            if (outtype == null)
            {
                return("");
            }

            if (outtype.Value == NodeType.Float4 ||
                outtype.Value == NodeType.Color ||
                outtype.Value == NodeType.Gray)
            {
                frag += "vec4 ";
            }
            else if (outtype.Value == NodeType.Float3)
            {
                frag += "vec3 ";
            }
            else if (outtype.Value == NodeType.Float2)
            {
                frag += "vec2 ";
            }
            else if (outtype.Value == NodeType.Float)
            {
                frag += "float ";
            }
            else if (outtype.Value == NodeType.Bool)
            {
                frag += "bool ";
            }
            else if (outtype.Value == NodeType.Matrix)
            {
                frag += "mat4 ";
            }
            else
            {
                return("");
            }

            frag += Name.Replace(" ", "").Replace("-", "_") + "(";

            count = args.Count;
            for (int i = 0; i < count; i++)
            {
                ArgNode a = args[i];

                if (a.InputType == NodeType.Float)
                {
                    frag += "float " + a.InputName + ",";
                }
                else if (a.InputType == NodeType.Float2)
                {
                    frag += "vec2 " + a.InputName + ",";
                }
                else if (a.InputType == NodeType.Float3)
                {
                    frag += "vec3 " + a.InputName + ",";
                }
                else if (a.InputType == NodeType.Float4 || a.InputType == NodeType.Color || a.InputType == NodeType.Gray)
                {
                    frag += "vec4 " + a.InputName + ",";
                }
                else if (a.InputType == NodeType.Bool)
                {
                    frag += "bool " + a.InputName + ",";
                }
                else if (a.InputType == NodeType.Matrix)
                {
                    frag += "mat4 " + a.InputName + ",";
                }
            }

            if (args.Count > 0)
            {
                frag = frag.Substring(0, frag.Length - 1) + ") {\r\n";
            }
            else
            {
                frag += ") {\r\n";
            }

            string intern = GetInternalShaderCode(ordered, frag, true);

            if (string.IsNullOrEmpty(intern))
            {
                return("");
            }

            frag = intern + "}\r\n\r\n";

            return(otherCalls + frag);
        }