Paradox Game Engine  v1.0.0 beta06
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Events Macros Pages
Script1.cs
Go to the documentation of this file.
1 using System;
2 using System.Collections.Generic;
3 using System.Linq;
4 using System.Threading.Tasks;
5 using System.Xml.Serialization;
6 using SiliconStudio.Paradox.DataModel;
7 using SiliconStudio.Paradox.Effects.Modules;
8 using SiliconStudio.Paradox.Engine;
9 using SiliconStudio.Paradox.EntityModel;
10 using SiliconStudio.Paradox.Games;
11 using SiliconStudio.Paradox;
12 using SiliconStudio.Paradox.Effects;
13 using SiliconStudio.Paradox.Configuration;
14 using SiliconStudio.Core.Extensions;
15 using SiliconStudio.Paradox.Graphics;
16 using SiliconStudio.Paradox.Games.IO;
17 using SiliconStudio.Paradox.Graphics.Data;
18 using SiliconStudio.Paradox.Games.MicroThreading;
19 using SiliconStudio.Paradox.Games.Mathematics;
20 using SiliconStudio.Paradox.Particles;
21 using SiliconStudio.Shaders;
22 using ScriptShader.Effects;
23 
24 using ScriptTest2;
25 #if NET45
26 using TaskEx = System.Threading.Tasks.Task;
27 #endif
28 
29 namespace ScriptTest
30 {
31  [ParadoxScript]
32  public class Script1
33  {
34  [ParadoxScript(ScriptFlags.None)]
35  public static async Task Run3()
36  {
37  for (int i = 0; i < 100; i++)
38  {
39  await Scheduler.Current.NextFrame();
40  if (i % 5 == 0)
41  System.Threading.Thread.Sleep(20);
42  }
43  }
44 
45  [ParadoxScript(ScriptFlags.None)]
46  public static async Task Run2(EngineContext engineContext)
47  {
48  for (int i = 0; i < 10; i++)
49  Scheduler.Current.Add(Run3);
50  }
51 
52  public static void CommonSetup(EngineContext engineContext)
53  {
54  VirtualFileSystem.MountFileSystem("/global_data", "..\\..\\deps\\data\\");
55  VirtualFileSystem.MountFileSystem("/global_data2", "..\\..\\data\\");
56  VirtualFileSystem.MountFileSystem("/shaders", "..\\..\\sources\\shaders\\");
57 
58  engineContext.EntityManager.Systems.Add(new MeshProcessor());
59  engineContext.EntityManager.Systems.Add(new HierarchicalProcessor());
60  engineContext.EntityManager.Systems.Add(new AnimationProcessor());
61  engineContext.EntityManager.Systems.Add(new TransformationProcessor());
62  engineContext.EntityManager.Systems.Add(new TransformationUpdateProcessor());
63  engineContext.EntityManager.Systems.Add(new SkinningProcessor());
64  engineContext.EntityManager.Systems.Add(new ModelConverterProcessor(engineContext));
65 
66  engineContext.AssetManager.RegisterSerializer(new GpuTextureSerializer(engineContext.RenderContext.GraphicsDevice));
67  engineContext.AssetManager.RegisterSerializer(new GpuSamplerStateSerializer(engineContext.RenderContext.GraphicsDevice));
68  engineContext.AssetManager.RegisterSerializer(new ImageSerializer());
69  }
70 
71  public static void PipelineSetup(EngineContext engineContext, string effectFilename = null)
72  {
73  var config = AppConfig.GetConfiguration<Config>("Script1");
74  var renderingSetup = RenderingSetup.Singleton;
75  var optionalFeatures = config.PipelineFeatures.Split(new[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
76  renderingSetup.Initialize(engineContext, effectFilename, optionalFeatures);
77  }
78 
79  [ParadoxScript(ScriptFlags.AssemblyStartup)]
80  public static async Task Run(EngineContext engineContext)
81  {
82  var config = AppConfig.GetConfiguration<Config>("Script1");
83 
84  CommonSetup(engineContext);
85 
86  if (config.Synthetic)
87  {
88  await ScriptCube.Run(engineContext);
89  }
90  else
91  {
92  PipelineSetup(engineContext);
93 
94  // Check config file
95  var configDebug = AppConfig.GetConfiguration<ScriptDebug.Config>("ScriptDebug");
96  if (configDebug.DebugManager)
97  engineContext.Scheduler.Add(() => ScriptDebug.RunDebug(engineContext));
98 
99  engineContext.Scheduler.Add(async () =>
100  {
101  if (config.Scene == "cave")
102  {
103  VirtualFileSystem.MountFileSystem("/sync", ".");
104  await ScriptCave.Run(engineContext);
105  }
106  else if (config.Scene == "sync")
107  {
108  ScriptSceneSerialization.gitFolder = "..\\..\\gittest\\" + config.SyncFolder + "\\";
109  VirtualFileSystem.MountFileSystem("/sync", ScriptSceneSerialization.gitFolder);
110  await ScriptCube.GenerateSimpleCubeEffect(engineContext);
111  }
112  else if (config.Scene == "factory")
113  {
114  await SetupFactory(engineContext);
115  await LightScript.MoveLights(engineContext);
116  }
117  else if (config.Scene == "particles")
118  {
119  //ScriptParticleSmoke.Run(engineContext);
120  }
121  else if (config.Scene == "cputest")
122  {
123  await ScriptMulticore.Run(engineContext);
124  }
125  else if (config.Scene == "permutation")
126  {
127  await ScriptPermutation.Run(engineContext);
128  }
129  });
130  }
131  }
132 
133  [ParadoxScript]
134  public static async Task LoadDude(EngineContext engineContext)
135  {
136  var mainPlugin = engineContext.RenderContext.RenderPassPlugins.OfType<MainPlugin>().FirstOrDefault();
137  EffectOld effect = engineContext.RenderContext.BuildEffect("SimpleSkinning")
138  .Using(new BasicShaderPlugin("ShaderBase") { RenderPassPlugin = mainPlugin })
139  .Using(new BasicShaderPlugin("TransformationWVP") { RenderPassPlugin = mainPlugin })
140  .Using(new BasicShaderPlugin(new ShaderMixinSource() {
141  new ShaderClassSource("AlbedoDiffuseBase"),
142  new ShaderComposition("albedoDiffuse", new ShaderClassSource("ComputeColorTexture", TexturingKeys.DiffuseTexture, "TEXCOORD")),
143  new ShaderComposition("albedoSpecular", new ShaderClassSource("ComputeColor")), // TODO: Default values!
144  }) { RenderPassPlugin = mainPlugin })
145  .Using(new BasicShaderPlugin("AlbedoFlatShading") { RenderPassPlugin = mainPlugin })
146  ;
147 
148  var characterEntity = await AnimScript.LoadFBXModel(engineContext, "/global_data/fbx/test_mesh.hotei#");
149  await AnimScript.AnimateFBXModel(engineContext, characterEntity);
150  }
151 
152  [ParadoxScript]
153  public static async Task SetupFactory(EngineContext engineContext, string effectName = "Simple")
154  {
155  var renderingSetup = RenderingSetup.Singleton;
156 
157  renderingSetup.RegisterLighting(engineContext);
158 
159  // Setup lighting
160  LightingPlugin lightingPlugin;
161  if (engineContext.DataContext.RenderPassPlugins.TryGetValueCast("LightingPlugin", out lightingPlugin))
162  {
163  var shadowMapEntity = new Entity();
164  shadowMapEntity.Set(TransformationComponent.Key, TransformationTRS.CreateComponent());
165  shadowMapEntity.Set(LightComponent.Key, new LightComponent { Type = LightType.Directional, Intensity = 0.9f, Color = new Color3(1.0f, 1.0f, 1.0f), LightDirection = new Vector3(-1.0f, -1.0f, -1.0f), ShadowMap = true, DecayStart = 40000.0f });
166  engineContext.EntityManager.AddEntity(shadowMapEntity);
167  }
168 
169  // Load asset
170  var entity = await engineContext.AssetManager.LoadAsync<Entity>("/global_data/factoryfbx.hotei#");
171 
172  // Flip it and scale it
173  var transformationComponent = (TransformationTRS)entity.Transformation.Value;
174  transformationComponent.Scaling *= 0.1f;
175 
176  //await engineContext.EntitySystem.Prepare(entity);
177  await engineContext.EntityManager.AddEntityAsync(entity);
178  }
179 
180  private static void SetupParticles(EngineContext engineContext)
181  {
182  // Create particle system
183  var particleSystem = new SiliconStudio.Paradox.Particles.ParticleSystem();
184 
185  // Set particle default size to 10.0f
186  particleSystem.GetOrCreateFieldWithDefault(ParticleFields.Size, 10.0f);
187 
188  // Add particle plugins
189  particleSystem.Plugins.Add(new ResetAcceleration());
190  particleSystem.Plugins.Add(new SimpleEmitter());
191  particleSystem.Plugins.Add(new RemoveOldParticles(10.0f));
192  particleSystem.Plugins.Add(new Gravity());
193  particleSystem.Plugins.Add(new UpdateVelocity());
194 
195  // Create particle system mesh for rendering.
196  var particleEffect = engineContext.RenderContext.Effects.First(x => x.Name == "DefaultParticle");
197  var particleMesh = new EffectMesh(particleEffect);
198  particleMesh.Parameters.Set(ParticleRendererPlugin.ParticleSystemKey, particleSystem);
199 
200  // Load particle texture
201  var smokeVolTexture = (Texture2D)engineContext.AssetManager.Load<Texture>("/global_data/gdc_demo/fx/smokevol.dds");
202  particleMesh.Parameters.Set(TexturingKeys.DiffuseTexture, smokeVolTexture);
203 
204  // Register it to rendering
205  engineContext.RenderContext.GlobalMeshes.AddMesh(particleMesh);
206  }
207 
208  [ParadoxScript]
209  public static async Task SetupPostEffects(EngineContext engineContext)
210  {
211  var config = AppConfig.GetConfiguration<Config>("Script1");
212 
213  var renderingSetup = RenderingSetup.Singleton;
214  renderingSetup.Initialize(engineContext);
215 
216  bool bloom = config.Bloom;
217  bool fxaa = config.FXAA;
218 
219  bool useHBAO = false;
220  bool doBlur = true;
221  bool mixAOWithColorImage = false;
222  bool halfResAO = true;
223 
224  var effectMeshGroup = new RenderPassListEnumerator();
225  engineContext.RenderContext.RenderPassEnumerators.Add(effectMeshGroup);
226 
227  PostEffectPlugin postEffectPlugin;
228  if (engineContext.DataContext.RenderPassPlugins.TryGetValueCast("PostEffectPlugin", out postEffectPlugin)
229  && (bloom || fxaa || useHBAO))
230  {
231  if (bloom)
232  {
233  // Create various effects required by the bloom effect
234  EffectOld brightPassFilter = engineContext.RenderContext.BuildEffect("BrightPass")
235  .Using(new PostEffectShaderPlugin() { RenderPassPlugin = postEffectPlugin })
236  .Using(new BasicShaderPlugin("ShadingTexturing"))
237  .Using(new BasicShaderPlugin("PostEffectBrightFilter"));
238 
239  EffectOld blurEffect = engineContext.RenderContext.BuildEffect("Blur")
240  .Using(new PostEffectShaderPlugin() { RenderPassPlugin = postEffectPlugin })
241  .Using(new BasicShaderPlugin("PostEffectBlur"));
242 
243  EffectOld downsampleEffect = engineContext.RenderContext.BuildEffect("DownSample")
244  .Using(new PostEffectShaderPlugin() { RenderPassPlugin = postEffectPlugin })
245  .Using(new BasicShaderPlugin("ShadingTexturing"));
246 
247  EffectOld mixEffect = engineContext.RenderContext.BuildEffect("Mix")
248  .Using(new PostEffectShaderPlugin() { RenderPassPlugin = postEffectPlugin })
249  .Using(new BasicShaderPlugin("ShadingTexturing"))
250  .Using(new BasicShaderPlugin("PosteffectTexturing2"));
251 
252  EffectOld fxaaEffect = engineContext.RenderContext.BuildEffect("Fxaa")
253  .Using(new PostEffectShaderPlugin() { RenderPassPlugin = postEffectPlugin })
254  .Using(new BasicShaderPlugin("PostEffectFXAA.pdxsl"));
255 
256 
257  // Create post effect meshes: downsampling and blurs
258  int bloomLevels = 6;
259  var downsampleMeshes = new EffectMesh[bloomLevels];
260  var lastBlurs = new EffectMesh[bloomLevels];
261  for (int i = 0; i < bloomLevels; ++i)
262  {
263  downsampleMeshes[i] = new EffectMesh(i == 0 ? brightPassFilter : downsampleEffect, name: "Downsample " + i);
264  postEffectPlugin.AddEffectMesh(downsampleMeshes[i]);
265 
266  // Blur effect
267  var blurQuadMesh = new EffectMesh[2];
268  for (int j = 0; j < 2; ++j)
269  {
270  blurQuadMesh[j] = new EffectMesh(blurEffect, name: string.Format("Blur level {0}:{1}", i, j));
271  blurQuadMesh[j].Parameters.Set(PostEffectBlurKeys.Coefficients, new[] { 0.30f, 0.20f, 0.20f, 0.15f, 0.15f });
272  var unit = j == 0 ? Vector2.UnitX : Vector2.UnitY;
273  blurQuadMesh[j].Parameters.Set(PostEffectBlurKeys.Offsets, new[] { Vector2.Zero, unit * -1.3862832f, unit * +1.3862832f, unit * -3.2534592f, unit * +3.2534592f });
274  postEffectPlugin.AddEffectMesh(blurQuadMesh[j]);
275  }
276  lastBlurs[i] = blurQuadMesh[1];
277  postEffectPlugin.AddLink(downsampleMeshes[i], RenderTargetKeys.RenderTarget, blurQuadMesh[0], TexturingKeys.Texture0, new TextureDescription { Width = 1024 >> (i + 1), Height = 768 >> (i + 1), Format = PixelFormat.R8G8B8A8_UNorm });
278  postEffectPlugin.AddLink(blurQuadMesh[0], RenderTargetKeys.RenderTarget, blurQuadMesh[1], TexturingKeys.Texture0);
279  if (i > 0)
280  postEffectPlugin.AddLink(downsampleMeshes[i - 1], RenderTargetKeys.RenderTarget, downsampleMeshes[i], TexturingKeys.Texture0);
281  }
282 
283  // Create post effect meshes: mix
284  EffectMesh lastMix = null;
285  for (int i = 0; i < bloomLevels; ++i)
286  {
287  var mixMesh = new EffectMesh(mixEffect, name: "Mix " + (bloomLevels - 1 - i));
288  mixMesh.Parameters.Set(PostEffectKeys.MixCoefficients, (i < bloomLevels - 1) ? new[] { 0.10f, 0.90f } : new[] { 1.0f, 3.0f });
289  postEffectPlugin.AddEffectMesh(mixMesh);
290 
291 
292  if (i < bloomLevels - 1)
293  postEffectPlugin.AddLink(lastBlurs[bloomLevels - 2 - i], RenderTargetKeys.RenderTarget, mixMesh, TexturingKeys.Texture0);
294  postEffectPlugin.AddLink(lastMix ?? lastBlurs[bloomLevels - 1], RenderTargetKeys.RenderTarget, mixMesh, TexturingKeys.Texture2);
295 
296  lastMix = mixMesh;
297  }
298 
299  EffectMesh lastEffectMesh = lastMix;
300 
301  //add fxaa?
302  if (fxaa)
303  {
304  var fxaaQuadMesh = new EffectMesh(fxaaEffect, name: "FXAA level");
305  postEffectPlugin.AddEffectMesh(fxaaQuadMesh);
306  postEffectPlugin.AddLink(lastMix, RenderTargetKeys.RenderTarget, fxaaQuadMesh, TexturingKeys.Texture0, new TextureDescription { Width = 1024, Height = 768, Format = PixelFormat.R8G8B8A8_UNorm });
307  lastEffectMesh = fxaaQuadMesh;
308  }
309 
310  engineContext.RenderContext.GraphicsResizeContext.SetupResize((resizeContext) =>
311  {
312  var renderTarget = renderingSetup.MainTargetPlugin.RenderTarget;
313  //blurQuadMesh[0].Parameters.Set(TextureFeature.Texture0, renderTarget);
314  //blurQuadMesh[1].Parameters.Set(RenderTargetKeys.RenderTarget, renderTarget);
315  downsampleMeshes[0].Parameters.SetWithResize(resizeContext, TexturingKeys.Texture0, (Texture2D)renderTarget.Texture);
316  lastMix.Parameters.SetWithResize(resizeContext, TexturingKeys.Texture0, (Texture2D)renderTarget.Texture);
317  lastMix.Parameters.SetWithResize(resizeContext, RenderTargetKeys.RenderTarget, engineContext.RenderContext.RenderTarget);
318 
319  lastEffectMesh.Parameters.SetWithResize(resizeContext, RenderTargetKeys.RenderTarget, engineContext.RenderContext.RenderTarget);
320  });
321  }
322  else if (fxaa)
323  {
324  //fxaa effect setup (fxaa only, no bloom effect)
325  EffectOld fxaaEffect = engineContext.RenderContext.BuildEffect("Fxaa")
326  .Using(new PostEffectShaderPlugin() { RenderPassPlugin = postEffectPlugin })
327  .Using(new BasicShaderPlugin("..\\..\\sources\\shaders\\posteffect_fxaa.pdxsl"));
328 
329  var fxaaQuadMesh = new EffectMesh(fxaaEffect, name: "FXAA level");
330 
331  fxaaQuadMesh.Parameters.Set(TexturingKeys.Texture0, (Texture2D)renderingSetup.MainTargetPlugin.RenderTarget.Texture);
332  fxaaQuadMesh.Parameters.Set(RenderTargetKeys.RenderTarget, engineContext.RenderContext.RenderTarget);
333  //fxaaQuadMesh.Parameters.Set(PostEffectFXAAKeys.FxaaQualitySubpix, 0);
334 
335  postEffectPlugin.AddEffectMesh(fxaaQuadMesh);
336 
337  //TODO, application will crashes if we resize or move the window!!
338  }
339 
340  foreach (var mesh in postEffectPlugin.Meshes)
341  effectMeshGroup.AddMesh(mesh);
342 
343  //engineContext.RenderContext.RootRenderPass.AddPass(postEffectPlugin.RenderPass);
344 
345  engineContext.RenderContext.GraphicsResizeContext.SetupResize((resizeContext) =>
346  {
347  // Link post effects (this will create intermediate surfaces)
348  postEffectPlugin.Resolve();
349  });
350  }
351  }
352 
353  public class Config
354  {
355  public Config()
356  {
357  PipelineFeatures = "Yebis HeatShimmering SkyBox";
358  Tessellation = true;
359  Scene = "cave";
360  SyncFolder = "hotei_data1";
361  Yebis = true;
362  LightShafts = true;
363  }
364 
365  [XmlAttribute("pipeline_features")]
366  public string PipelineFeatures { get; set; }
367 
368  [XmlAttribute("tessellation")]
369  public bool Tessellation { get; set; }
370 
371  [XmlAttribute("scene")]
372  public string Scene { get; set; }
373 
374  [XmlAttribute("syncfolder")]
375  public string SyncFolder { get; set; }
376 
377  [XmlAttribute("synthetic")]
378  public bool Synthetic { get; set; }
379 
380  [XmlAttribute("particles")]
381  public bool Particles { get; set; }
382 
383  [XmlAttribute("bloom")]
384  public bool Bloom { get; set; }
385 
386  [XmlAttribute("ao")]
387  public bool AO { get; set; }
388 
389  [XmlAttribute("yebis")]
390  public bool Yebis { get; set; }
391 
392  [XmlAttribute("lightshafts")]
393  public bool LightShafts { get; set; }
394 
395  [XmlAttribute("fxaa")]
396  public bool FXAA { get; set; }
397  }
398  }
399 
400  [ParadoxScript]
401  public class Script2
402  {
403  [ParadoxScript(ScriptFlags.None)]
404  public static async Task Run(EngineContext engineContext)
405  {
406  await TaskEx.Delay(1000);
407  }
408  }
409 
410  [ParadoxScript]
411  public class Script3
412  {
413  [ParadoxScript(ScriptFlags.None)]
414  public static async Task Run(EngineContext engineContext)
415  {
416  await TaskEx.Delay(1000);
417  }
418  }
419 }
static async Task Run(EngineContext engineContext)
Definition: Script1.cs:80
Add a light to an Entity, that will be used during rendering.
A particle system, containing particles and their updaters.
Game entity. It usually aggregates multiple EntityComponent
Definition: Entity.cs:28
static async Task Run3()
Definition: Script1.cs:35
SiliconStudio.Paradox.Engine.MeshProcessor MeshProcessor
Definition: Game.cs:25
Scene as exported by third-party exporters (FBX, Assimp, etc...)
Definition: Scene.cs:16
static async Task LoadDude(EngineContext engineContext)
Definition: Script1.cs:134
static async Task Run(EngineContext engineContext)
Definition: Script1.cs:414
System.Threading.Tasks.Task Task
TransformationComponent Transformation
Gets or sets the Transformation associated to this entity. Added for convenience over usual Get/Set m...
Definition: Entity.cs:74
Vector3 Scaling
The scaling relative to the parent transformation.
This processor will take care of adding/removing children of every Entity added/removed in the Entity...
Represents a ShadowMap.
Definition: ShadowMap.cs:11
static readonly RenderingSetup Singleton
static void PipelineSetup(EngineContext engineContext, string effectFilename=null)
Definition: Script1.cs:71
A Common description for all textures.
static async Task Run(EngineContext engineContext)
Definition: Script1.cs:404
SiliconStudio.Core.Mathematics.Color Color
Definition: ColorPicker.cs:14
Plugin used for the main rendering view.
Definition: MainPlugin.cs:15
A Texture 2D frontend to SharpDX.Direct3D11.Texture2D.
Definition: Texture2D.cs:37
static async Task SetupPostEffects(EngineContext engineContext)
Definition: Script1.cs:209
static async Task SetupFactory(EngineContext engineContext, string effectName="Simple")
Definition: Script1.cs:153
static async Task RunDebug(EngineContext engineContext)
Definition: ScriptDebug.cs:123
static async Task Run2(EngineContext engineContext)
Definition: Script1.cs:46
Updates TransformationComponent.WorldMatrix of entities.
static void CommonSetup(EngineContext engineContext)
Definition: Script1.cs:52
SiliconStudio.Core.Mathematics.Vector3 Vector3
Base class for texture resources.
Definition: Texture.cs:38