Paradox Game Engine  v1.0.0 beta06
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Events Macros Pages
ScriptCube.cs
Go to the documentation of this file.
1 using System;
2 using System.Diagnostics;
3 using System.Linq;
4 using System.Threading.Tasks;
5 
6 using SiliconStudio.Paradox.DataModel;
7 using SiliconStudio.Paradox.Effects.Modules;
8 #if PARADOX_YEBIS
9 using Paradox.Effects.Yebis;
10 #endif
11 using SiliconStudio.Paradox.Engine;
12 using SiliconStudio.Paradox.EntityModel;
13 using SiliconStudio.Paradox.Games;
14 using SiliconStudio.Paradox;
15 using SiliconStudio.Paradox.Effects;
16 using SiliconStudio.Core.Extensions;
17 using SiliconStudio.Paradox.Graphics;
18 using SiliconStudio.Paradox.Graphics.Data;
19 using SiliconStudio.Paradox.Games.IO;
20 using SiliconStudio.Paradox.Games.MicroThreading;
21 using SiliconStudio.Paradox.Games.Mathematics;
22 using Paradox.Framework.Shaders;
23 
24 namespace ScriptTest
25 {
26  [ParadoxScript]
27  public class ScriptCube
28  {
29  public void Dispose()
30  {
31  //depthTextureHolder.Release();
32  }
33 
34  class Sphere
35  {
36  public EffectMesh Mesh;
37  public float Phase;
38  public float Speed;
39  }
40 
41  struct LightInfo
42  {
43  public float Radius;
44  public float Phase;
45  public float Z;
46  }
47 
48  [ParadoxScript]
49  public static async Task GenerateSimpleCubeEffect(EngineContext engineContext)
50  {
51  var renderingSetup = RenderingSetup.Singleton;
52  renderingSetup.RegisterLighting(engineContext);
53 
54 #if PARADOX_YEBIS
55  YebisPlugin yebisPlugin;
56  if (engineContext.DataContext.RenderPassPlugins.TryGetValueCast("YebisPlugin", out yebisPlugin))
57  {
58  yebisPlugin.Glare.Enable = false;
59  yebisPlugin.DepthOfField.Enable = false;
60  yebisPlugin.ToneMap.AutoExposure.Enable = false;
61  yebisPlugin.ToneMap.Exposure = 1.0f;
62  yebisPlugin.ToneMap.Gamma = 2.2f;
63  }
64 #endif
65  var lightPrepassPlugin = (LightingPrepassPlugin)engineContext.DataContext.RenderPassPlugins.TryGetValue("LightingPrepassPlugin");
66  var gbufferPlugin = (GBufferPlugin)engineContext.DataContext.RenderPassPlugins.TryGetValue("GBufferPlugin");
67 
68  EffectOld effect = engineContext.RenderContext.BuildEffect("SimpleCube")
69  .Using(new BasicShaderPlugin("ShaderBase") { RenderPassPlugin = renderingSetup.MainTargetPlugin })
70  .Using(new BasicShaderPlugin("TransformationWVP") { RenderPassPlugin = renderingSetup.MainTargetPlugin })
71  .Using(new BasicShaderPlugin("AlbedoSpecularBase") { RenderPassPlugin = renderingSetup.MainTargetPlugin })
72  .Using(new BasicShaderPlugin("AlbedoDiffuseBase") { RenderPassPlugin = renderingSetup.MainTargetPlugin })
73  .Using(new BasicShaderPlugin("NormalVSGBuffer") { RenderPassPlugin = renderingSetup.MainTargetPlugin })
74  .Using(new BasicShaderPlugin("SpecularPowerPerMesh") { RenderPassPlugin = renderingSetup.MainTargetPlugin })
75  .Using(new BasicShaderPlugin("PositionVSGBuffer") { RenderPassPlugin = renderingSetup.MainTargetPlugin })
76  .Using(new BasicShaderPlugin("BRDFDiffuseLambert") { RenderPassPlugin = renderingSetup.MainTargetPlugin })
77  .Using(new BasicShaderPlugin("BRDFSpecularBlinnPhong") { RenderPassPlugin = renderingSetup.MainTargetPlugin })
78  .Using(new BasicShaderPlugin(new ShaderMixinSource() {
79  new ShaderComposition("albedoDiffuse", new ShaderClassSource("ComputeColorStream"))}) { RenderPassPlugin = renderingSetup.MainTargetPlugin })
80  .Using(new BasicShaderPlugin(new ShaderMixinSource() {
81  new ShaderComposition("albedoSpecular", new ShaderClassSource("ComputeColorSynthetic"))}) { RenderPassPlugin = renderingSetup.MainTargetPlugin })
82  .Using(new GBufferShaderPlugin { RenderPassPlugin = gbufferPlugin })
83  .Using(new DeferredLightingShaderPlugin() { RenderPassPlugin = lightPrepassPlugin })
84  .Using(new BasicShaderPlugin("LightDirectionalShading") { RenderPassPlugin = renderingSetup.MainTargetPlugin })
85  ;
86 
87  EffectOld effect2 = engineContext.RenderContext.BuildEffect("SimpleSkinning")
88  .Using(new BasicShaderPlugin("ShaderBase") { RenderPassPlugin = renderingSetup.MainTargetPlugin })
89  .Using(new BasicShaderPlugin("TransformationWVP") { RenderPassPlugin = renderingSetup.MainTargetPlugin })
90  .Using(new BasicShaderPlugin("TransformationSkinning") { RenderPassPlugin = renderingSetup.MainTargetPlugin })
91  .Using(new BasicShaderPlugin("AlbedoSpecularBase") { RenderPassPlugin = renderingSetup.MainTargetPlugin })
92  .Using(new BasicShaderPlugin("AlbedoDiffuseBase") { RenderPassPlugin = renderingSetup.MainTargetPlugin })
93  .Using(new BasicShaderPlugin("NormalVSGBuffer") { RenderPassPlugin = renderingSetup.MainTargetPlugin })
94  .Using(new BasicShaderPlugin("SpecularPowerPerMesh") { RenderPassPlugin = renderingSetup.MainTargetPlugin })
95  .Using(new BasicShaderPlugin("PositionVSGBuffer") { RenderPassPlugin = renderingSetup.MainTargetPlugin })
96  .Using(new BasicShaderPlugin("BRDFDiffuseLambert") { RenderPassPlugin = renderingSetup.MainTargetPlugin })
97  .Using(new BasicShaderPlugin("BRDFSpecularBlinnPhong") { RenderPassPlugin = renderingSetup.MainTargetPlugin })
98  .Using(new BasicShaderPlugin(new ShaderMixinSource() {
99  new ShaderClassSource("AlbedoDiffuseBase"),
100  new ShaderComposition("albedoDiffuse", new ShaderClassSource("ComputeColorTexture", TexturingKeys.DiffuseTexture.Name, "TEXCOORD")),
101  new ShaderComposition("albedoSpecular", new ShaderClassSource("ComputeColor")),
102  }) { RenderPassPlugin = renderingSetup.MainTargetPlugin })
103  .Using(new GBufferShaderPlugin { RenderPassPlugin = gbufferPlugin })
104  .Using(new DeferredLightingShaderPlugin() { RenderPassPlugin = (LightingPrepassPlugin)engineContext.DataContext.RenderPassPlugins.TryGetValue("LightingPrepassPlugin") })
105  .Using(new BasicShaderPlugin("LightDirectionalShading") { RenderPassPlugin = renderingSetup.MainTargetPlugin })
106  ;
107  }
108 
109  [ParadoxScript]
110  public static async Task GenerateTestPrefabs(EngineContext engineContext)
111  {
112  var entityCube = new Entity("Cube");
113  var meshComponent = new ModelComponent();
114  meshComponent.SubMeshes.Add(new EffectMeshData { EffectData = new EffectData("SimpleCube"), MeshData = MeshDataHelper.CreateBox(100.0f, 100.0f, 100.0f, Color.Gray) });
115  entityCube.Set(ModelComponent.Key, meshComponent);
116  entityCube.Set(TransformationComponent.Key, new TransformationComponent());
117  engineContext.AssetManager.Url.Set(entityCube, "/global_data/cube.hotei#/root");
118  engineContext.AssetManager.Save(entityCube);
119 
120  var entitySphere = new Entity("Cube");
121  meshComponent = new ModelComponent();
122  meshComponent.SubMeshes.Add(new EffectMeshData { EffectData = new EffectData("SimpleCube"), MeshData = MeshDataHelper.CreateSphere(100.0f, 30, 30, Color.Gray) });
123  entitySphere.Set(ModelComponent.Key, meshComponent);
124  entitySphere.Set(TransformationComponent.Key, new TransformationComponent());
125  engineContext.AssetManager.Url.Set(entitySphere, "/global_data/sphere.hotei#/root");
126  engineContext.AssetManager.Save(entitySphere);
127  }
128 
129  [ParadoxScript]
130  public static async Task Run(EngineContext engineContext)
131  {
132  var renderingSetup = RenderingSetup.Singleton;
133  renderingSetup.Initialize(engineContext);
134  renderingSetup.RegisterLighting(engineContext);
135 
136 #if PARADOX_YEBIS
137  YebisPlugin yebisPlugin;
138  if (engineContext.DataContext.RenderPassPlugins.TryGetValueCast("YebisPlugin", out yebisPlugin))
139  {
140  yebisPlugin.Glare.Enable = true;
141  yebisPlugin.ToneMap.Exposure = 1.0f;
142  yebisPlugin.ToneMap.Gamma = 2.2f;
143  }
144 #endif
145 
146  var lightPrepassPlugin = (LightingPrepassPlugin)engineContext.DataContext.RenderPassPlugins.TryGetValue("LightingPrepassPlugin");
147  var gbufferPlugin = (GBufferPlugin)engineContext.DataContext.RenderPassPlugins.TryGetValue("GBufferPlugin");
148 
149  EffectOld effect = engineContext.RenderContext.BuildEffect("SimpleCube")
150  .Using(new BasicShaderPlugin("ShaderBase") { RenderPassPlugin = renderingSetup.MainTargetPlugin })
151  .Using(new BasicShaderPlugin("TransformationWVP") { RenderPassPlugin = renderingSetup.MainTargetPlugin })
152  .Using(new BasicShaderPlugin("AlbedoSpecularBase") { RenderPassPlugin = renderingSetup.MainTargetPlugin })
153  .Using(new BasicShaderPlugin("AlbedoDiffuseBase") { RenderPassPlugin = renderingSetup.MainTargetPlugin })
154  .Using(new BasicShaderPlugin("NormalVSGBuffer") { RenderPassPlugin = renderingSetup.MainTargetPlugin })
155  .Using(new BasicShaderPlugin("SpecularPowerPerMesh") { RenderPassPlugin = renderingSetup.MainTargetPlugin })
156  .Using(new BasicShaderPlugin("PositionVSGBuffer") { RenderPassPlugin = renderingSetup.MainTargetPlugin })
157  .Using(new BasicShaderPlugin("BRDFDiffuseLambert") { RenderPassPlugin = renderingSetup.MainTargetPlugin })
158  .Using(new BasicShaderPlugin("BRDFSpecularBlinnPhong") { RenderPassPlugin = renderingSetup.MainTargetPlugin })
159  .Using(new BasicShaderPlugin(new ShaderMixinSource() {
160  new ShaderComposition("albedoDiffuse", new ShaderClassSource("ComputeColorStream"))}) { RenderPassPlugin = renderingSetup.MainTargetPlugin })
161  .Using(new BasicShaderPlugin(new ShaderMixinSource() {
162  new ShaderComposition("albedoSpecular", new ShaderClassSource("ComputeColorSynthetic"))}) { RenderPassPlugin = renderingSetup.MainTargetPlugin })
163  .Using(new GBufferShaderPlugin { RenderPassPlugin = gbufferPlugin })
164  .Using(new DeferredLightingShaderPlugin() { RenderPassPlugin = lightPrepassPlugin })
165  .Using(new LightingShaderPlugin() { RenderPassPlugin = (LightingPlugin)engineContext.DataContext.RenderPassPlugins.TryGetValue("LightingPlugin") })
166  .Using(new BasicShaderPlugin("LightDirectionalShading") { RenderPassPlugin = renderingSetup.MainTargetPlugin })
167  ;
168 
169  var shadowMap1 = new ShadowMap(new DirectionalLight() { LightColor = new Color3(1.0f, 1.0f, 1.0f), LightDirection = new Vector3(1.0f, 1.0f, 1.0f) });
170  effect.Permutations.Set(ShadowMapPermutationArray.Key, new ShadowMapPermutationArray { ShadowMaps = { shadowMap1 } });
171 
172  var r = new Random(0);
173 
174 
175  VirtualFileSystem.MountFileSystem("/global_data", "..\\..\\deps\\data\\");
176  VirtualFileSystem.MountFileSystem("/global_data2", "..\\..\\data\\");
177 
178  SkyBoxPlugin skyBoxPlugin;
179  if (engineContext.DataContext.RenderPassPlugins.TryGetValueCast("SkyBoxPlugin", out skyBoxPlugin))
180  {
181  var skyBoxTexture = (Texture2D)await engineContext.AssetManager.LoadAsync<Texture>("/global_data/gdc_demo/bg/GDC2012_map_sky.dds");
182  skyBoxPlugin.Texture = skyBoxTexture;
183  }
184 
185  var effectMeshGroup = new RenderPassListEnumerator();
186  engineContext.RenderContext.RenderPassEnumerators.Add(effectMeshGroup);
187 
188  var groundMesh = new EffectMesh(effect, MeshDataHelper.CreateBox(10000, 10000, 1, Color.White));
189  groundMesh.KeepAliveBy(engineContext.SimpleComponentRegistry);
190  effectMeshGroup.AddMesh(groundMesh);
191  groundMesh.Parameters.Set(TransformationKeys.World, Matrix.Translation(new Vector3(0, 0, 0)));
192 
193  // Lights
194  for (int i = 0; i < 1024; ++i)
195  {
196 
197  Color3 color = (Color3)Color.White;
198 
199  switch (i % 4)
200  {
201  case 0: color = (Color3)Color.DarkOrange; break;
202  case 1: color = (Color3)Color.DarkGoldenrod; break;
203  case 2: color = (Color3)Color.DarkSalmon; break;
204  case 3: color = (Color3)Color.DarkRed; break;
205  }
206  var effectMesh = new EffectMesh(lightPrepassPlugin.Lights);
207  effectMesh.Parameters.Set(LightKeys.LightRadius, (float)r.NextDouble() * 200 + 200.0f);
208  effectMesh.Parameters.Set(LightKeys.LightColor, color);
209  effectMesh.KeepAliveBy(engineContext.SimpleComponentRegistry);
210 
211  effectMeshGroup.AddMesh(effectMesh);
212  }
213 
214  EffectOld effectLight = lightPrepassPlugin.Lights;
215 
216  var lightInfo = new LightInfo[effectLight != null ? effectLight.Meshes.Count : 0];
217  for (int i = 0; i < lightInfo.Length; ++i)
218  {
219  lightInfo[i].Radius = (float)r.NextDouble() * 7000.0f + 500.0f;
220  lightInfo[i].Phase = (float)(r.NextDouble() * Math.PI * 2.0);
221  lightInfo[i].Z = (float)r.NextDouble() * 3000.0f; ;
222  }
223  float time = 0.0f;
224 
225 
226  // Meshes (quad) that will later be generated by the engine (light pre pass, SSAO, etc...)
227  // Lights
228  //var effectMesh = new EffectMesh(setup.LightingPrepassPlugin.Lights);
229  //effectMesh.Parameters.Set(LightKeys.LightRadius, 1000.0f);
230  //effectMesh.Parameters.Set(LightKeys.LightColor, new R32G32B32_Float(1.0f, 1.0f, 1.0f));
231  //effectMesh.Parameters.Set(LightKeys.LightPosition, new R32G32B32_Float(0, 0, 1200));
232 
233  //effectMesh.KeepAliveBy(engineContext.SimpleComponentRegistry);
234  //effectMeshGroup.AddMesh(effectMesh);
235 
236  //var boxMesh = new EffectMesh(effect, MeshDataHelper.CreateBox(300, R8G8B8A8.LightBlue));
237  //boxMesh.KeepAliveBy(engineContext.SimpleComponentRegistry);
238  //boxMesh.Parameters.Set(TransformationKeys.World, Matrix.Translation(new R32G32B32_Float(0, 0, 200)));
239  //effectMeshGroup.AddMesh(boxMesh);
240 
241 
242  var clock = new Stopwatch();
243  clock.Start();
244 
245 
246  int sizeX = 10;
247  int sizeY = 10;
248 
249  var spheres = new Sphere[sizeY,sizeX];
250 
251  Random random = new Random(0);
252 
253  int size = 200;
254  var meshData = MeshDataHelper.CreateSphere(size, 30, 30, Color.Gray);
255 
256  for (int iy = 0; iy < sizeY; iy++)
257  {
258  for (int ix = 0; ix < sizeX; ix++)
259  {
260  var sphere = new Sphere();
261 
262  sphere.Mesh = new EffectMesh(effect, meshData);
263  sphere.Phase = (float)random.NextDouble();
264  sphere.Speed = (float)random.NextDouble();
265 
266  spheres[iy, ix] = sphere;
267  effectMeshGroup.AddMesh(sphere.Mesh);
268  }
269  }
270 
271 
272  while (true)
273  {
274  await Scheduler.Current.NextFrame();
275 
276 
277  for (int iy = 0; iy < sizeY; iy++)
278  {
279  for (int ix = 0; ix < sizeX; ix++)
280  {
281  var iFactor = (float)(iy * sizeY + ix) / (sizeX * sizeY);
282 
283  var sphere = spheres[iy, ix];
284  var sphereMesh = sphere.Mesh;
285  var specularColor = Color.SmoothStep(Color.GreenYellow, Color.Gray, iFactor);
286 
287  // Matrix.RotationX((float)Math.PI/2.0f) * M
288  sphereMesh.Parameters.Set(
289  TransformationKeys.World,
290  Matrix.Translation(
291  new Vector3(
292  (ix - sizeX / 2) * (size * 1.2f) * 2.0f,
293  (iy - sizeY / 2) * (size * 1.2f) * 2.0f,
294  (float)(2000 * (0.5 + 0.5 * Math.Sin(clock.ElapsedMilliseconds / 1000.0f * sphere.Speed * 0.5f + Math.PI * sphere.Phase))))));
295  sphereMesh.Parameters.Set(MaterialKeys.SpecularPower, iFactor * 0.9f);
296  sphereMesh.Parameters.Set(MaterialKeys.SpecularColor, specularColor);
297  }
298  }
299 
300  time = clock.ElapsedMilliseconds / 1000.0f;
301 
302  if (lightInfo.Length > 0)
303  {
304  int index = 0;
305  foreach (var mesh in effectLight.Meshes)
306  {
307  mesh.Parameters.Set(LightKeys.LightPosition, new Vector3(lightInfo[index].Radius * (float)Math.Cos(-time * 0.17f + lightInfo[index].Phase), lightInfo[index].Radius * (float)Math.Sin(-time * 0.05f + lightInfo[index].Phase), lightInfo[index].Z * (0.5f + 0.5f * (float)Math.Sin(-time * 0.1f + lightInfo[index].Phase * 2.0f))));
308  index++;
309  }
310  }
311  }
312  }
313  }
314 }
static async Task Run(EngineContext engineContext)
Definition: ScriptCube.cs:130
Game entity. It usually aggregates multiple EntityComponent
Definition: Entity.cs:28
Plugin used to render to a GBuffer from a MainPlugin.
static async Task GenerateTestPrefabs(EngineContext engineContext)
Definition: ScriptCube.cs:110
Defines Position, Rotation and Scale of its Entity.
Represents a ShadowMap.
Definition: ShadowMap.cs:11
Shader effect used with GBufferPlugin
static readonly RenderingSetup Singleton
switch(inFormat)
SiliconStudio.Core.Mathematics.Color Color
Definition: ColorPicker.cs:14
A Texture 2D frontend to SharpDX.Direct3D11.Texture2D.
Definition: Texture2D.cs:37
Add a Model to an Entity, that will be used during rendering.
SiliconStudio.Core.Mathematics.Vector3 Vector3
static async Task GenerateSimpleCubeEffect(EngineContext engineContext)
Definition: ScriptCube.cs:49
_In_ size_t _In_ size_t size
Definition: DirectXTexP.h:175
Base class for texture resources.
Definition: Texture.cs:38