static void Main(string[] args) //fx_ps_DirectoryPath ShaderName [GeneratedNamespace] { if (args.Length < 3) { return; } string fxPath = args[0] + "\\" + args[1] + ".fx";//@"C:\Users\wenyunchun\Desktop\WpfTPL\shader\ToonShader.fx"; string psPath = args[0] + "\\" + args[1] + ".ps"; string GeneratedNamespace = args[2]; using (FileStream fs = new FileStream(fxPath, FileMode.Open, FileAccess.Read)) { using (StreamReader sr = new StreamReader(fs, Encoding.UTF8)) { //string psPath = @"C:\Users\wenyunchun\Desktop\WpfTPL\shader\ToonShader.ps"; CodeParser.GeneratedNamespace = !string.IsNullOrWhiteSpace(GeneratedNamespace) ? GeneratedNamespace : "Shaders"; // || 动态命名空间 || // ShaderModel _shaderModel = CodeParser.ParseShader(psPath, sr.ReadToEnd()); CreatePixelShaderClass.shaderPath = psPath; string _csText = CreatePixelShaderClass.GetSourceText(CodeDomProvider.CreateProvider("CSharp"), _shaderModel, false); //string _vbText = CreatePixelShaderClass.GetSourceText(CodeDomProvider.CreateProvider("VisualBasic"), _shaderModel, false); string topath = args[0] + "\\" + args[1] + "Effect.cs"; using (FileStream fs2 = new FileStream(topath, FileMode.OpenOrCreate, FileAccess.Write)) { using (StreamWriter sw = new StreamWriter(fs2, Encoding.UTF8)) { sw.Write(_csText); } } // || 添加动态资源文件 || // var ps = new PixelShader { UriSource = new Uri(psPath) }; Assembly autoAssembly = CreatePixelShaderClass.CompileInMemory(_csText); if (autoAssembly == null) { MessageBox.Show("Cannot compile the generated C# code.", "Compile error", MessageBoxButton.OK, MessageBoxImage.Error); //return; } else { Type type = autoAssembly.GetType(String.Format("{0}.{1}", _shaderModel.GeneratedNamespace, _shaderModel.GeneratedClassName)); //ShaderEffect se = (ShaderEffect)Activator.CreateInstance(type, new object[] { ps }); ShaderEffect se = (ShaderEffect)Activator.CreateInstance(type); Console.WriteLine(se); } } } }
public void RenderShader() { CompileShader(); if (_compiler.IsCompiled == false) { return; } try { this.InputControlsTab.IsEnabled = false; string path = Properties.Settings.Default.FolderPath_Output; if (!File.Exists(path + Constants.FileNames.TempShaderPs)) { return; } var ps = new PixelShader { UriSource = new Uri(path + Constants.FileNames.TempShaderPs) }; this._shaderModel = Shazzam.CodeGen.CodeParser.ParseShader(this._shaderTextEditor.FileName, this.CodeText); string codeString = CreatePixelShaderClass.GetSourceText(new CSharpCodeProvider(), this._shaderModel, true); Assembly autoAssembly = CreatePixelShaderClass.CompileInMemory(codeString); if (autoAssembly == null) { MessageBox.Show(ShazzamSwitchboard.MainWindow, "Cannot compile the generated C# code.", "Compile error", MessageBoxButton.OK, MessageBoxImage.Error); return; } Type t = autoAssembly.GetType(String.Format("{0}.{1}", _shaderModel.GeneratedNamespace, _shaderModel.GeneratedClassName)); this.FillEditControls(); var outputFolder = String.Format("{0}{1}", Properties.Settings.Default.FolderPath_Output, _shaderModel.GeneratedClassName); if (!Directory.Exists(outputFolder)) { Directory.CreateDirectory(outputFolder); } _csTextEditor.SaveFile(String.Format("{0}\\{1}.cs", outputFolder, _shaderModel.GeneratedClassName)); _vbTextEditor.SaveFile(String.Format("{0}\\{1}.vb", outputFolder, _shaderModel.GeneratedClassName)); CreateFileCopies(outputFolder + @"\", _shaderModel.GeneratedClassName); this.CurrentShaderEffect = (ShaderEffect)Activator.CreateInstance(t, new object[] { ps }); this.InputControlsTab.IsEnabled = true; } catch (Exception) { MessageBox.Show(ShazzamSwitchboard.MainWindow, "Cannot create a WPF shader from the code snippet.", "Compile error", MessageBoxButton.OK, MessageBoxImage.Error); } }
void apply(string psPath, ShaderModel _shaderModel, string _csText) { var ps = new PixelShader { UriSource = new Uri(psPath) }; Assembly autoAssembly = CreatePixelShaderClass.CompileInMemory(_csText); if (autoAssembly == null) { MessageBox.Show("Cannot compile the generated C# code.", "Compile error", MessageBoxButton.OK, MessageBoxImage.Error); //return; } else { Type type = autoAssembly.GetType(String.Format("{0}.{1}", _shaderModel.GeneratedNamespace, _shaderModel.GeneratedClassName)); //ShaderEffect se = (ShaderEffect)Activator.CreateInstance(type, new object[] { ps }); ShaderEffect se = (ShaderEffect)Activator.CreateInstance(type); eftImg.Effect = se; logText.Items.Insert(0, se); } }
void apply(string psPath, ShaderModel _shaderModel, string _csText) { var ps = new PixelShader { UriSource = new Uri(psPath) }; Assembly autoAssembly = CreatePixelShaderClass.CompileInMemory(_csText); if (autoAssembly == null) { MessageBox.Show("Cannot compile the generated C# code.", "Compile error", MessageBoxButton.OK, MessageBoxImage.Error); //return; } else { Type type = autoAssembly.GetType(String.Format("{0}.{1}", _shaderModel.GeneratedNamespace, _shaderModel.GeneratedClassName)); //ShaderEffect se = (ShaderEffect)Activator.CreateInstance(type, new object[] { ps }); ShaderEffect se = (ShaderEffect)Activator.CreateInstance(type); eftImg.Effect = se; stk.Children.Clear(); stk.DataContext = se; PropertyInfo[] pInfos = type.GetProperties(); for (int i = 0; i < pInfos.Length; i++) { if (pInfos[i].PropertyType == typeof(double)) { DoubleRangeControl sl = new DoubleRangeControl() { Margin = new Thickness(10) }; sl.ValueName = pInfos[i].Name + " : "; Binding b = new Binding(pInfos[i].Name); b.Mode = BindingMode.TwoWay; b.UpdateSourceTrigger = UpdateSourceTrigger.PropertyChanged; sl.SetBinding(DoubleRangeControl.CurValueProperty, b); stk.Children.Add(sl); } } logText.Items.Insert(0, se); } }