// Copy the node public Node(Node node) { InitializeComponent(); this.inode = node.inode.GetType().GetConstructor(new Type[]{}).Invoke(new object[]{}) as INode; DynamicCanvas.SetLeft(this, DynamicCanvas.GetLeft(node)); DynamicCanvas.SetTop(this, DynamicCanvas.GetTop(node)); Width = node.Width; Height = node.Height; // Add all variables List<Variable> vars = inode.GetVariables(); for (int i = 0; i < vars.Count; i++) { Variable nvar = vars[i]; Variable ovar = node.Variables[i]; nvar.Type = ovar.Type; nvar.InputType = ovar.InputType; nvar.Text = ovar.Text; nvar.inputFloat1.Text = ovar.inputFloat1.Text; nvar.inputFloat2.Text = ovar.inputFloat2.Text; nvar.inputFloat3.Text = ovar.inputFloat3.Text; nvar.inputFloat4.Text = ovar.inputFloat4.Text; nvar.inputColor.SelectedColor = ovar.inputColor.SelectedColor; nvar.inputBoolean.SelectedIndex = ovar.inputBoolean.SelectedIndex; nvar.inputVarying.SelectedIndex = ovar.inputVarying.SelectedIndex; nvar.typeMenuFloat1.IsChecked = ovar.IsFloat1(); nvar.typeMenuFloat2.IsChecked = ovar.IsFloat2(); nvar.typeMenuFloat3.IsChecked = ovar.IsFloat3(); nvar.typeMenuFloat4.IsChecked = ovar.IsFloat4(); nvar.typeMenuBoolean.IsChecked = true; AddVariable(nvar); } NodeName.Text = node.NodeName.Text; }
// Create a copy of the file state public FileState(FileState fileState, bool newState = true) { this.file = fileState.File; this.renderer = fileState.Renderer; // Copy the nodes dictionaryOldToNew = new Dictionary<Node, Node>(); foreach (Node node in fileState.Nodes) { Node newNode = new Node(node); dictionaryOldToNew[node] = newNode; Nodes.Add(newNode); } // Copy the connections foreach (Variable variable in fileState.Nodes.SelectMany(x => x.Variables).Where(v => v.Type == Variable.VariableType.Output && v.InputType == Variable.InputTypes.Link)) { foreach (Connection connection in variable.GetLinks()) { int outIndex = connection.OutputVariable.Node.Variables.IndexOf(connection.OutputVariable); int inIndex = connection.InputVariable.Node.Variables.IndexOf(connection.InputVariable); Connection newConnection = new Connection(); // newConnection.DesignArea = connection.DesignArea; newConnection.OutputVariable = dictionaryOldToNew[connection.OutputVariable.Node].Variables[outIndex]; newConnection.InputVariable = dictionaryOldToNew[connection.InputVariable.Node].Variables[inIndex]; } } if (newState) { PreviousState = fileState; fileState.NextStates.Add(this); } }
public VariableStruct(Node node, Variable.VariableType variableType) { this.node = node; // Create the identifier Identifier = "s" + getUniqueID(); // Create the hlsl description HLSLDescription = "struct " + Identifier + " {\n"; foreach (Variable variable in node.Variables) { if (variable.Type == variableType) { string variableIdentifier = "v" + getUniqueID(); dictonaryVariableIdentifiers[variable] = variableIdentifier; HLSLDescription += "\t" + determineTypeIdentifier(variable) + " " + variableIdentifier + ";\n"; } } HLSLDescription += "};\n\n"; }
public void RemoveNode(Node node) { Canvas.Children.Remove(node); }
// Methos to add nodes and connections public void AddNode(Node newNode) { Canvas.Children.Add(newNode); newNode.DesignArea = this; }
public void RemoveNode(Node node) { Nodes.Remove(node); File.FileView.DesignArea.RemoveNode(node); }
// Adds a new node to the graph public Node AddNewNode(Type nodeType, Point position) { INode inode = nodeType.GetConstructor(new Type[0]).Invoke(new Object[0]) as INode; Node node = new Node(inode); DynamicCanvas.SetLeft(node, position.X); DynamicCanvas.SetTop(node, position.Y); File.FileView.DesignArea.AddNode(node); Nodes.Add(node); return node; }
public void Compile() { SourceCode = "// Auto generated shader (HLSL) \n\n"; // Create dictionaries dictonaryInputStruct = new Dictionary<Node, VariableStruct>(); dictonaryOutputStruct = new Dictionary<Node, VariableStruct>(); // Varyings struct definition SourceCode += "// Varying struct definition \n\n"; varyingIdentifier = "IOVaryings"; SourceCode += "struct " + varyingIdentifier + " {\n"; SourceCode += "\t float4 position;\n"; SourceCode += "\t float4 normal;\n"; SourceCode += "\t float4 camera;\n"; SourceCode += "};\n\n"; // Add all input and output structs definitions SourceCode += "// Input and output struct definitions \n\n"; foreach (Node node in fileState.Nodes) { VariableStruct inputStruct = new VariableStruct(node, Variable.VariableType.Input); dictonaryInputStruct[node] = inputStruct; SourceCode += inputStruct.HLSLDescription; VariableStruct outputStruct = new VariableStruct(node, Variable.VariableType.Output); dictonaryOutputStruct[node] = outputStruct; SourceCode += outputStruct.HLSLDescription; } // Find the output node outputNode = null; foreach (Node node in fileState.Nodes) { if (node.inode.IsOutputNode()) { outputNode = node; break; } } dictionaryGetIdentifiers = new Dictionary<Node, string>(); if (outputNode == null) return; // Add all execute functions SourceCode += "// Execution functions \n\n"; dictionaryExecuteIdentifiers = new Dictionary<Node, string>(); foreach (Node node in fileState.Nodes) { VariableStruct inputStruct = dictonaryInputStruct[node]; VariableStruct outputStruct = dictonaryOutputStruct[node]; string outputType = outputStruct.Identifier; if (node == outputNode) outputType = inputStruct.Identifier; string executionIdentifier = "exec" + getUniqueID(); dictionaryExecuteIdentifiers[node] = executionIdentifier; SourceCode += outputType + " " + executionIdentifier + "(" + varyingIdentifier + " varyings, " + inputStruct.Identifier + " input) {\n"; SourceCode += "\t" + outputType + " output;\n"; Dictionary<Variable, string> mergedDictionary = new Dictionary<Variable,string>(); foreach (Variable variable in inputStruct.dictonaryVariableIdentifiers.Keys) mergedDictionary[variable] = inputStruct.dictonaryVariableIdentifiers[variable]; foreach (Variable variable in outputStruct.dictonaryVariableIdentifiers.Keys) mergedDictionary[variable] = outputStruct.dictonaryVariableIdentifiers[variable]; SourceCode += node.inode.GetSource(mergedDictionary); SourceCode += "\treturn output;\n"; SourceCode += "}\n\n"; } // Add all get functions SourceCode += "// Get functions \n\n"; generateGetFunctions(outputNode); // Add the interface function SourceCode += "float4 getColor(float4 position, float4 normal, float4 camera) {\n"; SourceCode += "\t" + varyingIdentifier + " varyings;\n"; SourceCode += "\tvaryings.position = position;\n"; SourceCode += "\tvaryings.normal = normal;\n"; SourceCode += "\tvaryings.camera = camera;\n"; SourceCode += "\t" + dictonaryInputStruct[outputNode].Identifier + " output;\n"; SourceCode += "\toutput = " + dictionaryGetIdentifiers[outputNode] + "(varyings); \n\n"; SourceCode += "\tfloat4 color = output." + dictonaryInputStruct[outputNode].dictonaryVariableIdentifiers[outputNode.Variables[0]] + ";\n"; SourceCode += "\treturn color;\n"; SourceCode += "}\n"; }
private void generateGetFunctions(Node node) { // Check if node was already generated if (dictionaryGetIdentifiers.Keys.Contains(node)) return; // Get identifier string getIdentifier = "get" + getUniqueID(); dictionaryGetIdentifiers[node] = getIdentifier; // Execute gets for all input nodes that are linked first foreach (Variable variable in node.Variables.Where(v => v.Type == Variable.VariableType.Input)) { if (variable.InputType == Variable.InputTypes.Link) { Connection connection = variable.GetLinks()[0]; Variable linkedVariable = connection.OutputVariable; generateGetFunctions(linkedVariable.Node); } } // Construct input object string outputType = dictonaryOutputStruct[node].Identifier; if (node == outputNode) outputType = dictonaryInputStruct[node].Identifier; SourceCode += outputType + " " + getIdentifier + "(IOVaryings varyings) {\n"; SourceCode += "\t" + dictonaryInputStruct[node].Identifier + " input; \n"; foreach (Variable variable in node.Variables.Where(v => v.Type == Variable.VariableType.Input)) { if (variable.InputType == Variable.InputTypes.Link) { Connection connection = variable.GetLinks()[0]; Variable linkedVariable = connection.OutputVariable; string linkedVariableIdentifier = dictonaryOutputStruct[linkedVariable.Node].dictonaryVariableIdentifiers[linkedVariable]; SourceCode += "\tinput." + dictonaryInputStruct[node].dictonaryVariableIdentifiers[variable] + " = " + dictionaryGetIdentifiers[linkedVariable.Node] + "(varyings)." + linkedVariableIdentifier + ";\n"; } else if (variable.InputType == Variable.InputTypes.Float1) { string variableValue = "float(" + variable.getFloat1() + ")"; SourceCode += "\tinput." + dictonaryInputStruct[node].dictonaryVariableIdentifiers[variable] + " = " + variableValue + ";\n"; } else if (variable.InputType == Variable.InputTypes.Float2) { string variableValue = "float2(" + variable.getFloat1() + ", " + variable.getFloat2() + ")"; SourceCode += "\tinput." + dictonaryInputStruct[node].dictonaryVariableIdentifiers[variable] + " = " + variableValue + ";\n"; } else if (variable.InputType == Variable.InputTypes.Float3) { string variableValue = "float3(" + variable.getFloat1() + ", " + variable.getFloat2() + ", " + variable.getFloat3() + ")"; SourceCode += "\tinput." + dictonaryInputStruct[node].dictonaryVariableIdentifiers[variable] + " = " + variableValue + ";\n"; } else if (variable.InputType == Variable.InputTypes.Float4) { string variableValue = "float4(" + variable.getFloat1() + ", " + variable.getFloat2() + ", " + variable.getFloat3() + ", " + variable.getFloat4() + ")"; SourceCode += "\tinput." + dictonaryInputStruct[node].dictonaryVariableIdentifiers[variable] + " = " + variableValue + ";\n"; } else if (variable.InputType == Variable.InputTypes.Color) { string variableValue = "float4(" + variable.getColor().R + " / 256.0f, " + variable.getColor().G + " / 256.0f, " + variable.getColor().B + " / 256.0f, " + variable.getColor().A + " / 256.0f)"; SourceCode += "\tinput." + dictonaryInputStruct[node].dictonaryVariableIdentifiers[variable] + " = " + variableValue + ";\n"; } else if (variable.InputType == Variable.InputTypes.Varying) { string variableValue = "varyings." + (variable.inputVarying.SelectedValue as ComboBoxItem).Content; SourceCode += "\tinput." + dictonaryInputStruct[node].dictonaryVariableIdentifiers[variable] + " = " + variableValue + ";\n"; } } // Invoke exection function SourceCode += "\n\t" + outputType + " output = " + dictionaryExecuteIdentifiers[node] + "(varyings, input);\n"; SourceCode += "\treturn output;\n"; SourceCode += "}\n\n"; }
private void writeNode(XmlWriter xmlWriter, Node node) { xmlWriter.WriteStartElement("Node"); // Type string guid = node.inode.GetIdentifier().ToString(); xmlWriter.WriteAttributeString("TypeID", guid); // Position double positionX = DynamicCanvas.GetLeft(node); string valueX = "" + ((int)(positionX * 100.0) / 100.0); double positionY = DynamicCanvas.GetTop(node); string valueY = "" + ((int)(positionY * 100.0) / 100.0); xmlWriter.WriteAttributeString("X", valueX); xmlWriter.WriteAttributeString("Y", valueY); // Write all variables xmlWriter.WriteStartElement("Variables"); foreach (Variable variable in node.Variables) { xmlWriter.WriteStartElement("Variable"); xmlWriter.WriteAttributeString("ID", variable.GetHashCode().ToString("X8")); xmlWriter.WriteAttributeString("Name", variable.Name); xmlWriter.WriteAttributeString("Text", variable.Text); xmlWriter.WriteAttributeString("Type", variable.InputType.ToString()); if (variable.InputType == Variable.InputTypes.Boolean) xmlWriter.WriteAttributeString("value", variable.getBoolean().ToString()); if (variable.InputType == Variable.InputTypes.Varying) xmlWriter.WriteAttributeString("value", (variable.inputVarying.SelectedValue as ComboBoxItem).Content as string); if (variable.InputType == Variable.InputTypes.Float1 || variable.InputType == Variable.InputTypes.Float2 || variable.InputType == Variable.InputTypes.Float3 || variable.InputType == Variable.InputTypes.Float4) xmlWriter.WriteAttributeString("value1", variable.getFloat1()); if (variable.InputType == Variable.InputTypes.Float2 || variable.InputType == Variable.InputTypes.Float3 || variable.InputType == Variable.InputTypes.Float4) xmlWriter.WriteAttributeString("value2", variable.getFloat2()); if (variable.InputType == Variable.InputTypes.Float3 || variable.InputType == Variable.InputTypes.Float4) xmlWriter.WriteAttributeString("value3", variable.getFloat3()); if (variable.InputType == Variable.InputTypes.Float4) xmlWriter.WriteAttributeString("value4", variable.getFloat4()); if (variable.InputType == Variable.InputTypes.Color) { xmlWriter.WriteAttributeString("value1", variable.getColor().R.ToString()); xmlWriter.WriteAttributeString("value2", variable.getColor().G.ToString()); xmlWriter.WriteAttributeString("value3", variable.getColor().B.ToString()); xmlWriter.WriteAttributeString("value4", variable.getColor().A.ToString()); } xmlWriter.WriteAttributeString("dim1", variable.IsFloat1().ToString()); xmlWriter.WriteAttributeString("dim2", variable.IsFloat2().ToString()); xmlWriter.WriteAttributeString("dim3", variable.IsFloat3().ToString()); xmlWriter.WriteAttributeString("dim4", variable.IsFloat4().ToString()); xmlWriter.WriteEndElement(); } xmlWriter.WriteEndElement(); xmlWriter.WriteEndElement(); }