Paradox Game Engine  v1.0.0 beta06
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Events Macros Pages
ScriptPermutation.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 SiliconStudio.Paradox;
6 using SiliconStudio.Paradox.DataModel;
7 using SiliconStudio.Paradox.Effects;
8 using SiliconStudio.Paradox.Effects.Modules;
9 using SiliconStudio.Paradox.Games;
10 using SiliconStudio.Core.Extensions;
11 using SiliconStudio.Paradox.Games.Mathematics;
12 using SiliconStudio.Paradox.Games.MicroThreading;
13 using SiliconStudio.Paradox.Input;
14 using SiliconStudio.Shaders;
15 
16 #if NET45
17 using TaskEx = System.Threading.Tasks.Task;
18 #endif
19 
20 namespace ScriptTest
21 {
22  [ParadoxScript]
23  public class ScriptPermutation
24  {
25  [ParadoxScript]
26  public static async Task Run(EngineContext engineContext)
27  {
28  var renderingSetup = RenderingSetup.Singleton;
29  //engineContext.RenderContext.Register(renderingSetup.LightingPlugin);
30  //renderingSetup.RegisterLighting(engineContext);
31 
32  var shadowMapPlugin = new LightingPlugin { MainPlugin = renderingSetup.MainPlugin, RenderPass = engineContext.DataContext.RenderPasses.TryGetValue("ShadowMapPass") };
33  shadowMapPlugin.RenderContext = engineContext.RenderContext;
34 
35  var shadowMap1 = new ShadowMap(new DirectionalLight()) { Level = CascadeShadowMapLevel.X1, ShadowMapSize = 1024, ShadowDistance = 2000.0f }
36  .SetFilter(sm => new ShadowMapFilterDefault(sm));
37  var shadowMap2 = new ShadowMap(new DirectionalLight()) { Level = CascadeShadowMapLevel.X2, ShadowMapSize = 1024, ShadowDistance = 2000.0f }
38  .SetFilter(sm => new ShadowMapFilterVsm(sm));
39 
40  shadowMapPlugin.AddShadowMap(shadowMap1);
41  shadowMapPlugin.AddShadowMap(shadowMap2);
42 
43  shadowMap1.DirectionalLight.LightDirection = new Vector3(-1.0f, -1.0f, -1.0f);
44  shadowMap1.DirectionalLight.LightColor = new Color3(1.0f, 0.5f, 0.5f);
45  shadowMap2.DirectionalLight.LightDirection = new Vector3(-1.0f, 1.0f, -1.0f);
46  shadowMap2.DirectionalLight.LightColor = new Color3(0.5f, 0.5f, 1.0f);
47 
48  engineContext.RenderContext.Register(shadowMapPlugin);
49 
50  EffectOld effect = engineContext.RenderContext.BuildEffect("Permutation")
51  .Using(new BasicShaderPlugin("ShaderBase") { RenderPassPlugin = renderingSetup.MainTargetPlugin })
52  .Using(new BasicShaderPlugin("TransformationWVP") { RenderPassPlugin = renderingSetup.MainTargetPlugin })
53  .Using(new BasicShaderPlugin(new ShaderMixinSource()
54  {
55  "NormalVSStream",
56  "PositionVSStream",
57  new ShaderComposition("albedoDiffuse", new ShaderClassSource("ComputeColorStream")),
58  new ShaderComposition("albedoSpecular", new ShaderClassSource("ComputeColor")), // TODO: Default values!
59  "BRDFDiffuseLambert",
60  "BRDFSpecularBlinnPhong",
61  "ShadingBase",
62  }) { RenderPassPlugin = renderingSetup.MainTargetPlugin })
63  .Using(new LightingShaderPlugin() { RenderPassPlugin = shadowMapPlugin })
64  ;
65 
66  var sphereMeshData = MeshDataHelper.CreateSphere(10.0f, 20, 20, Color.White);
67 
68  var effectMeshGroup = new RenderPassListEnumerator();
69  engineContext.RenderContext.RenderPassEnumerators.Add(effectMeshGroup);
70 
71  int objectSqrtCount = 1;
72  int meshCount = objectSqrtCount * objectSqrtCount * objectSqrtCount;
73 
74  var shadowMapPermutation1 = new ShadowMapPermutationArray { ShadowMaps = { shadowMap1 } };
75  var shadowMapPermutation2 = new ShadowMapPermutationArray { ShadowMaps = { shadowMap2 } };
76  var shadowMapPermutation3 = new ShadowMapPermutationArray { ShadowMaps = { shadowMap1, shadowMap2 } };
77 
78  effect.Permutations.Set(ShadowMapPermutationArray.Key, shadowMapPermutation1);
79 
80  var groups2 = new[] { shadowMapPermutation1, shadowMapPermutation2, shadowMapPermutation3 };
81  int groupIndex2 = 0;
82 
83  var effectMeshes = new List<EffectMesh>();
84  for (int j = 0; j < meshCount; ++j)
85  {
86  var effectMesh = new EffectMesh(effect, sphereMeshData);
87  effectMesh.KeepAliveBy(engineContext.SimpleComponentRegistry);
88 
89  effect.Permutations.Set(ShadowMapPermutationArray.Key, groups2[2]);
90 
91  var w2 = Matrix.Scaling(1.0f)
92  * Matrix.Translation(new Vector3(
93  (j % objectSqrtCount - objectSqrtCount / 2) * 30.0f - 30.0f,
94  (((j / objectSqrtCount) % objectSqrtCount) - objectSqrtCount / 2) * 30.0f - 30.0f,
95  (j / (objectSqrtCount * objectSqrtCount)) * 30.0f + 30.0f));
96 
97  effectMesh.Parameters.Set(TransformationKeys.World, w2);
98  effectMeshes.Add(effectMesh);
99  }
100 
101  var groundMesh = new EffectMesh(effect, MeshDataHelper.CreateBox(1000, 1000, 1, Color.White));
102  groundMesh.KeepAliveBy(engineContext.SimpleComponentRegistry);
103  effectMeshGroup.AddMesh(groundMesh);
104  groundMesh.Parameters.Set(TransformationKeys.World, Matrix.Translation(0.0f, 0.0f, 0.0f));
105 
106  var groups = new[] { new int[] { 0, 1 }, new int[] { 0 }, new int[] { 1 } };
107  int groupIndex = 0;
108 
109  await TaskEx.Delay(1000);
110  foreach (var effectMesh in effectMeshes)
111  effectMeshGroup.AddMesh(effectMesh);
112 
113  while (true)
114  {
115  await engineContext.Scheduler.NextFrame();
116 
117  if (engineContext.InputManager.IsKeyPressed(Keys.F8))
118  {
119  var permutation = new LightingPermutation { LightBindings = { new Light() } };
120  effect.Permutations.Set(LightingPermutation.Key, permutation);
121  }
122  if (engineContext.InputManager.IsKeyPressed(Keys.F9))
123  {
124  effect.Permutations.Set(ShadowMapPermutationArray.Key, groups2[(groupIndex2++) % groups2.Length]);
125  }
126  }
127  }
128  }
129 }
Keys
Enumeration for keys.
Definition: Keys.cs:8
System.Threading.Tasks.Task Task
Represents a ShadowMap.
Definition: ShadowMap.cs:11
static readonly RenderingSetup Singleton
SiliconStudio.Core.Mathematics.Color Color
Definition: ColorPicker.cs:14
Plugin used for the main rendering view.
Definition: MainPlugin.cs:15
static async Task Run(EngineContext engineContext)
SiliconStudio.Core.Mathematics.Vector3 Vector3
RenderPass is a hierarchy that defines how to collect and render meshes.
Definition: RenderPass.cs:19