Esempio n. 1
0
        public T GetParameterValue <T>(string id, string parameter)
        {
            string cid = id + "." + parameter;

            GraphParameterValue p = null;

            if (Parameters.TryGetValue(cid, out p))
            {
                if (p.IsFunction())
                {
                    FunctionGraph g = p.Value as FunctionGraph;

                    g.TryAndProcess();

                    if (g.Result == null)
                    {
                        return(default(T));
                    }

                    return((T)g.Result);
                }
                else
                {
                    return((T)p.Value);
                }
            }

            return(default(T));
        }
Esempio n. 2
0
        public string GetJson()
        {
            GraphParameterValueData d = new GraphParameterValueData();

            d.name        = Name;
            d.isFunction  = IsFunction();
            d.description = Description;
            d.type        = (int)Type;
            d.min         = Min;
            d.max         = Max;
            d.inputType   = (int)inputType;
            d.id          = Id;
            d.section     = Section;

            if (d.isFunction)
            {
                FunctionGraph g = Value as FunctionGraph;

                d.value = g.GetJson();
            }
            else
            {
                if (v is Matrix4)
                {
                    d.value = ((Matrix4)v).ToArray();
                }
                else
                {
                    d.value = v;
                }
            }

            return(JsonConvert.SerializeObject(d));
        }
Esempio n. 3
0
        public void SetParameterValue(string id, string parameter, object v)
        {
            string cid = id + "." + parameter;

            GraphParameterValue p = null;

            if (Parameters.TryGetValue(cid, out p))
            {
                if (p.IsFunction())
                {
                    FunctionGraph g = p.Value as FunctionGraph;
                    g.OnGraphUpdated -= Graph_OnGraphUpdated;
                    g.Dispose();
                }

                p.Value = v;
            }
            else
            {
                Parameters[cid] = new GraphParameterValue(parameter, v);
            }

            if (v is FunctionGraph)
            {
                (v as FunctionGraph).OnGraphUpdated += Graph_OnGraphUpdated;
            }

            Updated();
        }
Esempio n. 4
0
        protected void BuildShaderFunctionValue(GraphParameterValue param, StringBuilder builder)
        {
            FunctionGraph fn = param.Value as FunctionGraph;

            fn.TryAndProcess();
            object value = fn.Result;

            if (param.Type == NodeType.Bool)
            {
                builder.Append(Convert.ToBoolean(param.Value).ToString().ToLower() + ";\r\n");
            }
            else if (param.Type == NodeType.Float)
            {
                builder.Append(Convert.ToSingle(value).ToString() + ";\r\n");
            }
            else if (param.Type == NodeType.Float4 || param.Type == NodeType.Gray || param.Type == NodeType.Color)
            {
                MVector vec = new MVector();

                if (value is MVector)
                {
                    vec = (MVector)value;
                }

                builder.Append("vec4(" + vec.X + "," + vec.Y + "," + vec.Z + "," + vec.W + ");\r\n");
            }
            else if (param.Type == NodeType.Float2)
            {
                MVector vec = new MVector();

                if (value is MVector)
                {
                    vec = (MVector)value;
                }

                builder.Append("vec2(" + vec.X + "," + vec.Y + ");\r\n");
            }
            else if (param.Type == NodeType.Float3)
            {
                MVector vec = new MVector();

                if (value is MVector)
                {
                    vec = (MVector)value;
                }

                builder.Append("vec3(" + vec.X + "," + vec.Y + "," + vec.Z + ");\r\n");
            }
            else if (param.Type == NodeType.Matrix && value is Matrix4)
            {
                Matrix4 m4 = (Matrix4)value;
                //glsl matrices are column major order
                builder.Append("mat3(" + m4.Column0.X + ", " + m4.Column0.Y + ", " + m4.Column0.Z + ", " + m4.Column0.W + ", "
                               + m4.Column1.X + ", " + m4.Column1.Y + ", " + m4.Column1.Z + ", " + m4.Column1.W + ", "
                               + m4.Column2.X + ", " + m4.Column2.Y + ", " + m4.Column2.Z + ", " + m4.Column2.W + ","
                               + m4.Column3.X + ", " + m4.Column3.Y + ", " + m4.Column3.Z + ", " + m4.Column3.W + ");\r\n");
            }
        }
Esempio n. 5
0
        public void SetParameterValue(string id, string parameter, object v)
        {
            string cid = id + "." + parameter;

            GraphParameterValue p = null;

            if (Parameters.TryGetValue(cid, out p))
            {
                if (p.IsFunction())
                {
                    FunctionGraph g = p.Value as FunctionGraph;
                    g.OnGraphUpdated -= Graph_OnGraphUpdated;
                    g.Dispose();
                }

                if (v is float || v is int || v is double)
                {
                    p.Type = NodeType.Float;
                }
                else if (v is bool)
                {
                    p.Type = NodeType.Bool;
                }
                else if (v is MVector)
                {
                    p.Type = NodeType.Float4;
                }

                p.Value = v;
            }
            else
            {
                p = Parameters[cid] = new GraphParameterValue(parameter, v);

                if (v is float || v is int || v is double)
                {
                    p.Type = NodeType.Float;
                }
                else if (v is bool)
                {
                    p.Type = NodeType.Bool;
                }
                else if (v is MVector)
                {
                    p.Type = NodeType.Float4;
                }
            }

            if (v is FunctionGraph)
            {
                (v as FunctionGraph).OnGraphUpdated += Graph_OnGraphUpdated;
            }

            Updated();
        }
Esempio n. 6
0
        private void Graph_OnGraphUpdated(Graph g)
        {
            if (g is FunctionGraph)
            {
                FunctionGraph fg = g as FunctionGraph;

                if (fg.ParentNode != null)
                {
                    fg.ParentNode.TryAndProcess();
                }
            }
        }
Esempio n. 7
0
        public Graph TopGraph()
        {
            var p = parentGraph;

            if (p is FunctionGraph)
            {
                FunctionGraph g = p as FunctionGraph;
                return(g.ParentNode != null ? g.ParentNode.ParentGraph : g.ParentGraph);
            }

            return(p);
        }
Esempio n. 8
0
        public static GraphParameterValue FromJson(string data)
        {
            GraphParameterValueData d = JsonConvert.DeserializeObject <GraphParameterValueData>(data);

            if (d.isFunction)
            {
                FunctionGraph t = new FunctionGraph("temp");
                t.FromJson((string)d.value);
                return(new GraphParameterValue(d.name, t));
            }


            return(new GraphParameterValue(d.name, d.value));
        }
Esempio n. 9
0
        public static GraphParameterValue FromJson(string data, Node n)
        {
            GraphParameterValueData d = JsonConvert.DeserializeObject <GraphParameterValueData>(data);

            if (d.isFunction)
            {
                //we have a chicken and egg problem here...
                //which comes first the node population
                //or the parameter population?
                FunctionGraph t = new FunctionGraph("temp");
                t.FromJson((string)d.value);
                t.ParentNode = n;
                t.SetConnections();
                return(new GraphParameterValue(d.name, t, d.description, (NodeType)d.type, d.min, d.max));
            }


            return(new GraphParameterValue(d.name, d.value, d.description, (NodeType)d.type, d.min, d.max));
        }
Esempio n. 10
0
        public string GetJson()
        {
            GraphParameterValueData d = new GraphParameterValueData();

            d.name       = Name;
            d.isFunction = IsFunction();

            if (d.isFunction)
            {
                FunctionGraph g = Value as FunctionGraph;

                d.value = g.GetJson();
            }
            else
            {
                d.value = Value;
            }

            return(JsonConvert.SerializeObject(d));
        }
Esempio n. 11
0
        protected virtual void RemoveParameters()
        {
            var p = ParentGraph;

            if (p != null && p is FunctionGraph)
            {
                FunctionGraph fg = p as FunctionGraph;
                p = fg.ParentNode != null ? fg.ParentNode.ParentGraph : fg.ParentGraph;
            }

            if (p != null)
            {
                PropertyInfo[] infos = GetType().GetProperties();

                foreach (var info in infos)
                {
                    p.RemoveParameterValue(Id, info.Name);
                }
            }
        }
Esempio n. 12
0
        public void RemoveParameterValue(string id, string parameter)
        {
            string cid = id + "." + parameter;

            GraphParameterValue p = null;

            if (Parameters.TryGetValue(cid, out p))
            {
                if (p.IsFunction())
                {
                    FunctionGraph g = p.Value as FunctionGraph;
                    g.OnGraphUpdated -= Graph_OnGraphUpdated;
                    g.Dispose();
                }
            }

            Parameters.Remove(cid);

            Updated();
        }
Esempio n. 13
0
        public virtual void SetJsonReadyCustomFunctions(List <string> functions)
        {
            if (functions != null)
            {
                CustomFunctions = new List <FunctionGraph>();

                foreach (string k in functions)
                {
                    FunctionGraph g = new FunctionGraph("temp");
                    CustomFunctions.Add(g);
                    g.FromJson(k);
                }

                foreach (FunctionGraph g in CustomFunctions)
                {
                    //set parent graph
                    g.ParentGraph = this;
                    //finally set connections
                    g.SetConnections();
                }
            }
        }
Esempio n. 14
0
        public object GetParameterValue(string id, string parameter)
        {
            string cid = id + "." + parameter;

            GraphParameterValue p = null;

            if (Parameters.TryGetValue(cid, out p))
            {
                if (p.IsFunction())
                {
                    FunctionGraph g = p.Value as FunctionGraph;

                    g.TryAndProcess();

                    return(g.Result);
                }
                else
                {
                    return(p.Value);
                }
            }

            return(null);
        }
Esempio n. 15
0
        public string GetJson()
        {
            GraphParameterValueData d = new GraphParameterValueData();

            d.name        = Name;
            d.isFunction  = IsFunction();
            d.description = Description;
            d.type        = (int)Type;
            d.min         = Min;
            d.max         = Max;

            if (d.isFunction)
            {
                FunctionGraph g = Value as FunctionGraph;

                d.value = g.GetJson();
            }
            else
            {
                d.value = Value;
            }

            return(JsonConvert.SerializeObject(d));
        }
Esempio n. 16
0
        public virtual void SetJson(GraphParameterValueData d, Node n)
        {
            Name = d.name;

            type      = (NodeType)d.type;
            inputType = (ParameterInputType)d.inputType;
            Id        = d.id;
            min       = d.min;
            max       = d.max;

            if (d.isFunction)
            {
                FunctionGraph t = new FunctionGraph("temp");
                t.AssignParentNode(n);
                t.FromJson((string)d.value);
                t.ExpectedOutput = (NodeType)d.type;
                t.SetConnections();
                v = t;
            }
            else
            {
                v = d.value;
            }

            //then it is a matrix
            if (v is Newtonsoft.Json.Linq.JArray && type == NodeType.Matrix)
            {
                float[] m = DeserializeValueArray <float[]>(v);
                //if this fails then the ValidateValue
                //will simply fill it in with the corresponding
                //matrix identity
                if (m != null)
                {
                    //4x4 matrix
                    if (m.Length == 16)
                    {
                        Matrix4 m4 = new Matrix4();
                        m4.FromArray(m);
                        v = m4;
                    }
                }
            }
            //handle this in case parser actually returns it as
            //a float[] instead of a JArray
            //not sure which it will return at the moment
            //when it encodes it from the value field
            //which is classified as a generic object
            else if (v is float[] && type == NodeType.Matrix)
            {
                float[] m = (float[])v;

                if (m != null && m.Length == 16)
                {
                    Matrix4 m4 = new Matrix4();
                    m4.FromArray(m);
                    v = m4;
                }
            }

            Description = d.description;
            Section     = d.section;

            //whoops forgot to check for this!
            //otherwise d.section from a file
            //without d.section is null!
            if (string.IsNullOrEmpty(Section))
            {
                Section = "Default";
            }

            ValidateValue();
        }
Esempio n. 17
0
        protected void BuildShaderFunctionValue(GraphParameterValue param, StringBuilder builder)
        {
            FunctionGraph fn = param.Value as FunctionGraph;

            fn.TryAndProcess();
            object value = fn.Result;

            if (param.Type == NodeType.Bool)
            {
                try
                {
                    builder.Append(Convert.ToBoolean(value).ToString().ToLower() + ";\r\n");
                }
                catch (Exception e)
                {
                    Log.Error(e);
                    Log.Info("Defaulting to false for value of parameter " + param.Name);
                    builder.Append("false;\r\n");
                }
            }
            else if (param.Type == NodeType.Float)
            {
                try
                {
                    builder.Append(Convert.ToSingle(value).ToCodeString() + ";\r\n");
                }
                catch (Exception e)
                {
                    Log.Error(e);
                    Log.Info("Defaulting to 0 for value of parameter " + param.Name);
                    builder.Append("0;\r\n");
                }
            }
            else if (param.Type == NodeType.Float4 || param.Type == NodeType.Gray || param.Type == NodeType.Color)
            {
                MVector vec = new MVector();

                if (value is MVector)
                {
                    vec = (MVector)value;
                }

                builder.Append("vec4(" + vec.X.ToCodeString() + "," + vec.Y.ToCodeString() + "," + vec.Z.ToCodeString() + "," + vec.W.ToCodeString() + ");\r\n");
            }
            else if (param.Type == NodeType.Float2)
            {
                MVector vec = new MVector();

                if (value is MVector)
                {
                    vec = (MVector)value;
                }

                builder.Append("vec2(" + vec.X.ToCodeString() + "," + vec.Y.ToCodeString() + ");\r\n");
            }
            else if (param.Type == NodeType.Float3)
            {
                MVector vec = new MVector();

                if (value is MVector)
                {
                    vec = (MVector)value;
                }

                builder.Append("vec3(" + vec.X.ToCodeString() + "," + vec.Y.ToCodeString() + "," + vec.Z.ToCodeString() + ");\r\n");
            }
            else if (param.Type == NodeType.Matrix && value is Matrix4)
            {
                Matrix4 m4 = (Matrix4)value;
                //glsl matrices are column major order
                builder.Append("mat3(" + m4.Column0.X.ToCodeString() + ", " + m4.Column0.Y.ToCodeString() + ", " + m4.Column0.Z.ToCodeString() + ", " + m4.Column0.W.ToCodeString() + ", "
                               + m4.Column1.X.ToCodeString() + ", " + m4.Column1.Y.ToCodeString() + ", " + m4.Column1.Z.ToCodeString() + ", " + m4.Column1.W.ToCodeString() + ", "
                               + m4.Column2.X.ToCodeString() + ", " + m4.Column2.Y.ToCodeString() + ", " + m4.Column2.Z.ToCodeString() + ", " + m4.Column2.W.ToCodeString() + ","
                               + m4.Column3.X.ToCodeString() + ", " + m4.Column3.Y.ToCodeString() + ", " + m4.Column3.Z.ToCodeString() + ", " + m4.Column3.W.ToCodeString() + ");\r\n");
            }
        }