private void OnShowPreviewShader(object sender, EventArgs e) { var nodeGraph = ModelConversion.ToShaderPatcherLayer(graphControl); var shader = ShaderPatcherLayer.NodeGraph.GeneratePreviewShader(nodeGraph, AttachedId(sender)); MessageBox.Show(shader, "Generated shader", MessageBoxButtons.OK, MessageBoxIcon.Asterisk); }
private void OnGenerateTestScript(object sender, ExecuteEventArgs e) { // // Convert the editor nodes into "ShaderPatcherLayer" representation // This can be used for serialisation & for output to a shader // var nodeGraph = ModelConversion.ToShaderPatcherLayer(graphControl); var shader = ShaderPatcherLayer.NodeGraph.GenerateShader(nodeGraph, "Test"); MessageBox.Show(shader, "Generated shader", MessageBoxButtons.OK, MessageBoxIcon.Asterisk); }
private void OnSaveAs(object sender, ExecuteEventArgs e) { using (SaveFileDialog dialog = new SaveFileDialog()) { dialog.Filter = "Shader file (*.sh)|*.sh|All files (*.*)|*.*"; dialog.FilterIndex = 0; dialog.RestoreDirectory = true; if (dialog.ShowDialog() == DialogResult.OK) { using (var stream = dialog.OpenFile()) { using (var xmlStream = new System.IO.MemoryStream()) { var nodeGraph = ModelConversion.ToShaderPatcherLayer(graphControl); var serializer = new System.Runtime.Serialization.DataContractSerializer( typeof(ShaderPatcherLayer.NodeGraph)); var settings = new System.Xml.XmlWriterSettings() { Indent = true, IndentChars = "\t", Encoding = System.Text.Encoding.ASCII }; // write the xml to a memory stream to begin with using (var writer = System.Xml.XmlWriter.Create(xmlStream, settings)) { serializer.WriteObject(writer, nodeGraph); } // we hide the memory stream within a comment, and write // out a hlsl shader file // The HLSL compiler doesn't like UTF files... It just wants plain ASCII encoding using (var sw = new System.IO.StreamWriter(stream, System.Text.Encoding.ASCII)) { var shader = ShaderPatcherLayer.NodeGraph.GenerateShader(nodeGraph, System.IO.Path.GetFileNameWithoutExtension(dialog.FileName)); sw.Write(shader); sw.Write("/* **** **** NodeEditor **** **** \r\nNEStart{"); sw.Flush(); xmlStream.WriteTo(stream); sw.Write("}NEEnd\r\n **** **** */\r\n"); } } } } } }
public override void Render(Graphics graphics, SizeF minimumSize, PointF location) { if (Node.Tag is ShaderFragmentNodeTag) { SizeF size = Measure(graphics); if (!graphics.IsVisible(new Rectangle() { X = (int)location.X, Y = (int)location.Y, Width = (int)size.Width, Height = (int)size.Height })) { return; } if (_builder == null) { var nodeGraph = ModelConversion.ToShaderPatcherLayer(_graphControl); var shader = ShaderPatcherLayer.NodeGraph.GeneratePreviewShader(nodeGraph, ((ShaderFragmentNodeTag)Node.Tag).Id); _builder = PreviewRender.Manager.Instance.CreatePreview(shader); } // (assuming no rotation on this transformation -- scale is easy to find) Size idealSize = new Size((int)(graphics.Transform.Elements[0] * size.Width), (int)(graphics.Transform.Elements[3] * size.Height)); if (_builder != null && _builder.Bitmap != null) { // compare the current bitmap size to the size we'd like Size bitmapSize = _builder.Bitmap.Size; float difference = System.Math.Max(System.Math.Abs(1.0f - bitmapSize.Width / (float)(idealSize.Width)), System.Math.Abs(1.0f - bitmapSize.Height / (float)(idealSize.Height))); if (difference > 0.1f) { _builder.Invalidate(); } } if (_builder.Bitmap == null) { _builder.Update(_document, idealSize); } if (_builder.Bitmap != null) { graphics.DrawImage(_builder.Bitmap, new RectangleF() { X = location.X, Y = location.Y, Width = size.Width, Height = size.Height }); } } }
private void largePreviewToolStripMenuItem_Click(object sender, EventArgs e) { var n = ShaderFragmentNodeUtil.GetShaderFragmentNode(graphControl, AttachedId(sender)); if (n != null) { var nodeId = ((ShaderFragmentNodeTag)n.Tag).Id; // generate a preview builder for this specific node... var nodeGraph = ModelConversion.ToShaderPatcherLayer(graphControl); var shader = ShaderPatcherLayer.NodeGraph.GeneratePreviewShader(nodeGraph, nodeId); var builder = PreviewRender.Manager.Instance.CreatePreview(shader); // create a "LargePreview" window new LargePreview(builder, _document).Show(); } }
private bool LoadFromXML(System.IO.Stream stream) { var serializer = new System.Runtime.Serialization.DataContractSerializer( typeof(ShaderPatcherLayer.NodeGraph)); using (var xmlStream = System.Xml.XmlReader.Create(stream)) { var o = serializer.ReadObject(xmlStream); if (o != null && o is ShaderPatcherLayer.NodeGraph) { graphControl.RemoveNodes(graphControl.Nodes.ToList()); ModelConversion.AddToHyperGraph((ShaderPatcherLayer.NodeGraph)o, graphControl, _document); return(true); } } return(false); }