internal static NodeBase ReadNode(BinarySerializer serializer) { ShaderGraphSerializer sg = (ShaderGraphSerializer)serializer; bool isNewNode = false; serializer.Serialize(ref isNewNode); if (isNewNode) { string outputType = null; serializer.Serialize(ref outputType); NodeBase node = (NodeBase)Activator.CreateInstance(System.Type.GetType(outputType)); node.Serialize(serializer); sg.MarkNodeAsParsed(node); return(node); } else { string nodeId = null; serializer.Serialize(ref nodeId); if (sg.IsNodeParsed(nodeId)) { return(sg.GetNode(nodeId)); } else { throw new InvalidOperationException(string.Format("Node '{0}' not found", nodeId)); } } }
internal static Variable ReadVariable(BinarySerializer serializer) { ShaderGraphSerializer sg = (ShaderGraphSerializer)serializer; bool isNewVar = false; serializer.Serialize(ref isNewVar); if (isNewVar) { string outputType = null; serializer.Serialize(ref outputType); var variable = (Variable)Activator.CreateInstance(System.Type.GetType(outputType)); variable.Serialize(serializer); sg.MarkVariableAsParsed(variable); return(variable); } else { string varId = null; serializer.Serialize(ref varId); if (sg.IsVariableParsed(varId)) { return(sg.GetVariable(varId)); } else { throw new InvalidOperationException(string.Format("Variable '{0}' not found", varId)); } } }
internal static MethodBase ReadMethod(BinarySerializer serializer) { ShaderGraphSerializer sg = (ShaderGraphSerializer)serializer; string outputType = null; serializer.Serialize(ref outputType); var method = (MethodBase)Activator.CreateInstance(System.Type.GetType(outputType)); method.Serialize(serializer); if (sg.IsMethodParsed(method.Name)) { return(sg.GetMethod(method.Name)); } else { sg.MarkMethodAsParsed(method); return(method); } }
internal static void WriteNode(BinarySerializer serializer, INode node) { ShaderGraphSerializer sg = (ShaderGraphSerializer)serializer; NodeBase n = (NodeBase)node; // Is this node new one or a reference to another one encountered before? bool isNewNode = !sg.IsNodeParsed(n.id); serializer.Serialize(ref isNewNode); if (!isNewNode) { serializer.Serialize(ref n.id); } else { string outputType = node.GetType().FullName; serializer.Serialize(ref outputType); n.Serialize(serializer); sg.MarkNodeAsParsed(n); } }
public void Save() { SaveFileDialog saveDialog = new SaveFileDialog { Filter = "Odyssey Shader Graph|*.osg" }; bool?result = saveDialog.ShowDialog(); if (result == false) { return; } Mouse.SetCursor(Cursors.Wait); try { var techniqueGraph = (from t in techniques select new TechniqueDescription(t.Name, t.TechniqueMapping.Key, from s in shaderList.OfType <ShaderDescriptionViewModel>() where s.IsAssignedTo(t) select s.ShaderDescriptionModel.Shader)).ToArray(); foreach (var shader in techniqueGraph.SelectMany(t => t.Shaders)) { shader.Build(); } using (FileStream fs = new FileStream(saveDialog.FileName, FileMode.Create)) { ShaderGraphSerializer sgSerializer = new ShaderGraphSerializer(fs, SerializerMode.Write); sgSerializer.Save(techniqueGraph); } } catch (Exception e) { LogEvent.Tool.Error(e); } Mouse.SetCursor(Cursors.Arrow); }
internal static void WriteVariable(BinarySerializer serializer, IVariable variable) { ShaderGraphSerializer sg = (ShaderGraphSerializer)serializer; Variable v = (Variable)variable; // Is this node new one or a reference to another one encountered before? bool isNewVar = !sg.IsVariableParsed(v.Id); serializer.Serialize(ref isNewVar); if (!isNewVar) { string varName = v.Id; serializer.Serialize(ref varName); } else { string outputType = v.GetType().FullName; serializer.Serialize(ref outputType); v.Serialize(serializer); sg.MarkVariableAsParsed(v); } }
public bool Open(bool clear = true) { OpenFileDialog openDialog = new OpenFileDialog { Filter = "Odyssey Shader Graph|*.osg" }; bool?result = openDialog.ShowDialog(); if (result == false) { return(false); } List <TechniqueDescription> shaderGraph; try { using (FileStream fs = new FileStream(openDialog.FileName, FileMode.Open)) { ShaderGraphSerializer sgSerializer = new ShaderGraphSerializer(fs, SerializerMode.Read); shaderGraph = new List <TechniqueDescription>(sgSerializer.Load(fs)); } } catch (IOException ex) { const string errorMsg = "Failed to open file {0}"; MessageBox.Show(string.Format(errorMsg, Path.GetFileName(openDialog.FileName)), "Error", MessageBoxButton.OK, MessageBoxImage.Error); Log.Daedalus.Error(ex); return(false); } if (!shaderGraph.Any()) { MessageBox.Show("No techniques were found.", "Error", MessageBoxButton.OK, MessageBoxImage.Error); return(false); } var techniques = shaderGraph.Select((td, index) => new TechniqueMappingViewModel { Index = index + 1, TechniqueMapping = new TechniqueMapping(td.Name) { Key = td.Key } }).ToList(); foreach (var technique in techniques.Where(technique => !shaderCollection.Contains(technique.Name))) { RegisterTechnique(technique); } var parsedShaders = (from td in shaderGraph from s in td.Shaders group s by s.Name into shaderGroup select shaderGroup.First()); var shaderVMs = from shader in parsedShaders select new ShaderDescriptionViewModel() { ShaderDescriptionModel = new Model.ShaderDescription { Shader = shader }, Techniques = new ObservableCollection <TechniqueMappingViewModel>(from td in shaderGraph where td.Shaders.Any(s => s.Name == shader.Name) select techniques.Find(t => t.Name == td.Name)) }; if (clear) { shaderList.Clear(); } foreach (var shaderVM in shaderVMs.Where(shaderVM => shaderList.All(s => s.Name != shaderVM.Name))) { shaderList.Add(shaderVM); } var vmLocator = ((ViewModelLocator)System.Windows.Application.Current.FindResource("Locator")); vmLocator.Main.ShaderCollectionName = Path.GetFileNameWithoutExtension(openDialog.FileName); RelayCommand cmd = (RelayCommand)vmLocator.Main.CompileShaderGraphCommand; cmd.RaiseCanExecuteChanged(); return(true); }