Paradox Game Engine  v1.0.0 beta06
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Events Macros Pages
LightScript.cs
Go to the documentation of this file.
1 using System;
2 using System.Linq;
3 using System.Threading.Tasks;
4 using System.Xml.Serialization;
5 using SiliconStudio.Paradox;
6 using SiliconStudio.Paradox.Effects.Modules;
7 //using Paradox.Mathematics;
8 using SiliconStudio.Paradox.Effects;
9 using System.Diagnostics;
10 
11 using SiliconStudio.Paradox.Games;
12 using SiliconStudio.Core.Extensions;
13 using SiliconStudio.Paradox.Games.MicroThreading;
14 using SiliconStudio.Paradox.Games.Mathematics;
15 using SiliconStudio.Paradox.Configuration;
16 
17 namespace ScriptTest
18 {
19  [ParadoxScript]
20  public class LightScript
21  {
22  struct LightInfo
23  {
24  public float Radius;
25  public float Phase;
26  public float Z;
27  }
28 
29  public class Config
30  {
31  public Config()
32  {
33  AnimatedLights = true;
34  }
35 
36 
37  [XmlAttribute("animatedLights")]
38  public bool AnimatedLights { get; set; }
39  }
40 
41  [ParadoxScript]
42  public static async Task MoveLights(EngineContext engineContext)
43  {
44  var r = new Random(0);
45 
46  var config = AppConfig.GetConfiguration<Config>("LightScript2");
47 
48  LightingPrepassPlugin lightingPrepassPlugin;
49  if (!engineContext.DataContext.RenderPassPlugins.TryGetValueCast("LightingPrepassPlugin", out lightingPrepassPlugin))
50  return;
51 
52  var effectMeshGroup = new RenderPassListEnumerator();
53  engineContext.RenderContext.RenderPassEnumerators.Add(effectMeshGroup);
54 
55  // Lights
56  for (int i = 0; i < 1024; ++i)
57  {
58  var effectMesh = new EffectMesh(lightingPrepassPlugin.Lights);
59 
60  Color3 color = (Color3)Color.White;
61  switch (i % 7)
62  {
63  case 0: color = new Color3(0.7f, 0.0f, 0.0f); break;
64  case 1: color = new Color3(0.0f, 0.7f, 0.0f); break;
65  case 2: color = new Color3(0.0f, 0.0f, 0.7f); break;
66  case 3: color = new Color3(0.7f, 0.7f, 0.0f); break;
67  case 4: color = new Color3(0.7f, 0.0f, 0.7f); break;
68  case 5: color = new Color3(0.0f, 0.7f, 0.7f); break;
69  case 6: color = new Color3(0.7f, 0.7f, 0.7f); break;
70  }
71  effectMesh.Parameters.Set(LightKeys.LightRadius, 60.0f);
72  effectMesh.Parameters.Set(LightKeys.LightColor, color);
73  effectMesh.Parameters.Set(LightKeys.LightIntensity, 1.0f);
74  effectMesh.KeepAliveBy(engineContext.SimpleComponentRegistry);
75 
76  effectMeshGroup.AddMesh(effectMesh);
77  }
78 
79  bool animatedLights = config.AnimatedLights;
80 
81  EffectOld effectLight = null;
82  try
83  {
84  effectLight = engineContext.RenderContext.RenderPassPlugins.OfType<LightingPrepassPlugin>().FirstOrDefault().Lights;
85  }
86  catch
87  {
88  return;
89  }
90 
91  var lightInfo = new LightInfo[effectLight != null ? effectLight.Meshes.Count : 0];
92  for (int i = 0; i < lightInfo.Length; ++i)
93  {
94  lightInfo[i].Radius = (float)r.NextDouble() * 1000.0f + 500.0f;
95  lightInfo[i].Phase = (float)r.NextDouble() * 10.0f;
96  lightInfo[i].Z = (float)r.NextDouble() * 150.0f + 150.0f;
97  }
98  float time = 0.0f;
99  var st = new Stopwatch();
100  var lastTickCount = 0;
101 
102  var st2 = new Stopwatch();
103  st2.Start();
104 
105  bool firstTime = true;
106  while (true)
107  {
108  await Scheduler.NextFrame();
109 
110  time += 0.003f;
111 
112  if (lightInfo.Length > 0)
113  {
114  if (animatedLights || firstTime)
115  {
116  int index = 0;
117  foreach (var mesh in effectLight.Meshes)
118  {
119  mesh.Parameters.Set(LightKeys.LightPosition, new Vector3(lightInfo[index].Radius * (float)Math.Cos(time * 3.0f + lightInfo[index].Phase), lightInfo[index].Radius * (float)Math.Sin(time * 3.0f + lightInfo[index].Phase), lightInfo[index].Z));
120  index++;
121  }
122 
123  firstTime = false;
124  }
125  }
126  }
127  }
128  }
129 }
switch(inFormat)
SiliconStudio.Core.Mathematics.Color Color
Definition: ColorPicker.cs:14
static async Task MoveLights(EngineContext engineContext)
Definition: LightScript.cs:42
SiliconStudio.Core.Mathematics.Vector3 Vector3