3 using System.Threading;
4 using System.Threading.Tasks;
5 using System.Xml.Serialization;
6 using SiliconStudio.Paradox;
7 using SiliconStudio.Paradox.DataModel;
8 using SiliconStudio.Paradox.Effects;
9 using SiliconStudio.Paradox.Effects.Modules;
11 using Paradox.Effects.Yebis;
13 using SiliconStudio.Paradox.Engine;
14 using SiliconStudio.Paradox.EntityModel;
15 using SiliconStudio.Paradox.Extensions;
16 using SiliconStudio.Paradox.Games;
17 using SiliconStudio.Paradox.Configuration;
18 using SiliconStudio.Core.Extensions;
19 using SiliconStudio.Paradox.Graphics;
20 using SiliconStudio.Paradox.Graphics.Data;
21 using SiliconStudio.Paradox.Games.Mathematics;
22 using SiliconStudio.Paradox.Games.MicroThreading;
23 using SiliconStudio.Paradox.Input;
24 using SiliconStudio.Paradox.Prefabs;
36 private const float CaveSceneTotalTime = 79.0f;
37 private const float FullExposureTime = CaveSceneTotalTime - 7.0f;
38 private const float LogoTimeBegin = FullExposureTime + 2.5f;
39 private const float LogoTimeFull = LogoTimeBegin + 0.8f;
40 private const float LogoTimeURL = LogoTimeFull + 3.5f;
41 private const float LogoTimeStartToEnd = LogoTimeURL + 0.8f;
42 private const float LogoTimeEnd = LogoTimeStartToEnd + 1f;
43 private const float CaveSceneEndBlack = LogoTimeEnd + 1.5f;
44 private const float CaveSceneRestart = CaveSceneEndBlack + 0.1f;
47 public static async
Task Run(EngineContext engineContext)
50 renderingSetup.RegisterLighting(engineContext);
53 if (engineContext.DataContext.RenderPassPlugins.TryGetValueCast(
"ParticlePlugin", out particlePlugin))
55 ScriptParticleSmoke.Run(engineContext);
59 var yebisPlugin = engineContext.RenderContext.RenderPassPlugins.OfType<
YebisPlugin>().FirstOrDefault();
60 if (yebisPlugin != null)
62 var yebisConfig = AppConfig.GetConfiguration<
YebisConfig>(
"Yebis");
65 yebisPlugin.ToneMap.Gamma = yebisConfig.Gamma;
66 yebisPlugin.ColorCorrection.Saturation = yebisConfig.Saturation;
67 yebisPlugin.ColorCorrection.Contrast = yebisConfig.Contrast;
68 yebisPlugin.ColorCorrection.Brightness = yebisConfig.Brightness;
69 yebisPlugin.ColorCorrection.ColorTemperature = yebisConfig.ColorTemperature;
70 yebisPlugin.Lens.Vignette.Enable =
true;
71 yebisPlugin.Lens.Vignette.PowerOfCosine = 5.0f;
72 yebisPlugin.Lens.Distortion.Enable =
true;
73 yebisPlugin.Lens.Distortion.EdgeRoundness = 0.1f;
74 yebisPlugin.Lens.Distortion.EdgeSmoothness = 1.0f;
80 var cameraEntityRootPrefab = await engineContext.AssetManager.LoadAsync<
Entity>(
"/global_data/gdc_demo/char/camera.hotei#");
81 var lightCamEntityRootPrefab = await engineContext.AssetManager.LoadAsync<
Entity>(
"/global_data/gdc_demo/char/light_cam.hotei#");
83 var lightCamEntityRoot = Prefab.Inherit(lightCamEntityRootPrefab);
84 var cameraEntityRoot = Prefab.Inherit(cameraEntityRootPrefab);
86 engineContext.EntityManager.AddEntity(cameraEntityRoot);
87 engineContext.EntityManager.AddEntity(lightCamEntityRoot);
88 Scheduler.Current.Add(() =>
AnimScript.
AnimateFBXModel(engineContext, cameraEntityRoot, CaveSceneTotalTime, CaveSceneRestart));
89 Scheduler.Current.Add(() =>
AnimScript.
AnimateFBXModel(engineContext, lightCamEntityRoot, CaveSceneTotalTime, CaveSceneRestart));
93 var lightComp = light.Get(LightComponent.Key);
94 if (lightComp != null)
97 if (!lightComp.ShadowMap && lightComp.Type ==
LightType.Directional)
99 lightComp.Intensity *= 0.1f;
104 var config = AppConfig.GetConfiguration<
Script1.Config>(
"Script1");
106 var shadowMap1 =
new Entity();
107 var shadowMap2 =
new Entity();
108 shadowMap1.Set(TransformationComponent.Key, TransformationTRS.CreateComponent());
109 shadowMap2.Set(TransformationComponent.Key, TransformationTRS.CreateComponent());
110 shadowMap1.Set(LightComponent.Key,
new LightComponent { Type = LightType.Directional, Intensity = 0.9f,
Color =
new Color3(0.9f, 0.9f, 1.0f), LightDirection =
new Vector3(-0.2f, -0.1f, -1.0f),
ShadowMap =
true, DecayStart = 40000.0f });
111 shadowMap2.Set(LightComponent.Key,
new LightComponent { Type = LightType.Directional,
Color =
new Color3(1.0f, 1.0f, 1.0f), LightDirection =
new Vector3(-0.5f, 0.1f, -1.0f),
ShadowMap =
true, DecayStart = 40000.0f });
112 shadowMap1.Set(LightShaftsComponent.Key,
new LightShaftsComponent {
Color =
new Color3(1.0f, 1.0f, 1.0f), LightShaftsBoundingBoxes =
114 new EffectMeshData { MeshData = MeshDataHelper.CreateBox(1, 1, 1, Color.White,
true), Parameters =
new ParameterCollection { { TransformationKeys.World, Matrix.Scaling(3000, 3500, 3000) * Matrix.Translation(-2500, 0, 1500) } } }
116 shadowMap2.Set(LightShaftsComponent.Key,
new LightShaftsComponent {
Color =
new Color3(1.0f, 1.0f, 1.0f), LightShaftsBoundingBoxes =
118 new EffectMeshData { MeshData = MeshDataHelper.CreateBox(1, 1, 1, Color.White,
true), Parameters =
new ParameterCollection { { TransformationKeys.World, Matrix.Scaling(3500, 3500, 3000) * Matrix.Translation(-3000, 0, 1500) } } }
121 engineContext.EntityManager.AddEntity(shadowMap1);
122 engineContext.EntityManager.AddEntity(shadowMap2);
126 await
LoadCave(engineContext, dragon);
128 var dragonHead = engineContext.EntityManager.Entities.FirstOrDefault(x => x.Name ==
"English DragonHead");
129 TransformationTRS headCameraTransfo = null;
130 if (dragonHead != null)
132 var headCamera =
new Entity(
"Head camera");
133 headCamera.Set(CameraComponent.Key,
new CameraComponent { AspectRatio = 16.0f / 9.0f, VerticalFieldOfView = (float)Math.PI * 0.3f,
Target = dragonHead, AutoFocus =
true, NearPlane = 10.0f });
134 headCamera.Set(TransformationComponent.Key,
new TransformationComponent(
new TransformationTRS { Translation =
new Vector3(100.0f, -100.0f, 300.0f) }));
136 dragonHead.Transformation.Children.Add(headCamera.Transformation);
139 engineContext.Scheduler.Add(() =>
AnimateLights(engineContext));
142 for (
int i = 0; i < 10; i++)
150 await engineContext.Scheduler.NextFrame();
152 if (headCameraTransfo != null)
154 var time = (double)DateTime.UtcNow.Ticks / (
double)TimeSpan.TicksPerSecond;
155 float rotationSpeed = 0.317f;
156 var position =
new Vector2((
float)Math.Cos(time * rotationSpeed), (float)Math.Sin(time * rotationSpeed)) * 330.0f * ((
float)Math.Sin(time * 0.23f) * 0.4f + 0.9f);
157 headCameraTransfo.Translation =
new Vector3(position.X, -150.0f + (
float)Math.Cos(time * rotationSpeed) * 50.0f, position.Y);
160 if (engineContext.InputManager.IsKeyPressed(
Keys.F1))
162 bool isWireframeEnabled = renderingSetup.ToggleWireframe();
163 if (yebisPlugin != null)
165 yebisPlugin.DepthOfField.Enable = !isWireframeEnabled;
166 yebisPlugin.Lens.Vignette.Enable = !isWireframeEnabled;
167 yebisPlugin.Lens.Distortion.Enable = !isWireframeEnabled;
171 if (engineContext.InputManager.IsKeyPressed(
Keys.D1))
172 engineContext.CurrentTime = TimeSpan.FromSeconds(0);
173 if (engineContext.InputManager.IsKeyPressed(
Keys.D2))
174 engineContext.CurrentTime = TimeSpan.FromSeconds(10);
175 if (engineContext.InputManager.IsKeyPressed(
Keys.D3))
176 engineContext.CurrentTime = TimeSpan.FromSeconds(20);
177 if (engineContext.InputManager.IsKeyPressed(
Keys.D4))
178 engineContext.CurrentTime = TimeSpan.FromSeconds(30);
179 if (engineContext.InputManager.IsKeyPressed(
Keys.D5))
180 engineContext.CurrentTime = TimeSpan.FromSeconds(40);
181 if (engineContext.InputManager.IsKeyPressed(
Keys.D6))
182 engineContext.CurrentTime = TimeSpan.FromSeconds(50);
183 if (engineContext.InputManager.IsKeyPressed(
Keys.D7))
184 engineContext.CurrentTime = TimeSpan.FromSeconds(60);
185 if (engineContext.InputManager.IsKeyPressed(
Keys.D8))
186 engineContext.CurrentTime = TimeSpan.FromSeconds(70);
188 if (engineContext.InputManager.IsKeyPressed(
Keys.T))
190 if (particlePlugin != null)
192 particlePlugin.EnableSorting = !particlePlugin.EnableSorting;
201 var yebisPlugin = engineContext.RenderContext.RenderPassPlugins.OfType<
YebisPlugin>().FirstOrDefault();
202 var slideShowPlugin = engineContext.RenderContext.RenderPassPlugins.OfType<
SlideShowPlugin>().FirstOrDefault();
205 if (yebisPlugin == null || slideShowPlugin == null)
210 var paradoxLogo = (
Texture2D)await engineContext.AssetManager.LoadAsync<
Texture>(
"/global_data/gdc_demo/bg/LogoParadox.dds");
211 var paradoxLogoURL = (
Texture2D)await engineContext.AssetManager.LoadAsync<
Texture>(
"/global_data/gdc_demo/bg/LogoParadoxURL.dds");
213 slideShowPlugin.TextureFrom = paradoxLogo;
214 slideShowPlugin.TextureTo = paradoxLogoURL;
215 slideShowPlugin.RenderPass.Enabled =
false;
218 float lastExposure = 0.0f;
220 yebisPlugin.ToneMap.AutoExposure.Enable =
true;
222 var savedToneMap = yebisPlugin.ToneMap;
223 var savedLens = yebisPlugin.Lens;
224 var savedColorCorrection = yebisPlugin.ColorCorrection;
228 await engineContext.Scheduler.NextFrame();
230 var animationTime = (float)engineContext.CurrentTime.TotalSeconds % CaveSceneRestart;
232 var deltaTime = (
float)(animationTime - lastTime);
235 if (deltaTime < 0.0f)
237 yebisPlugin.ToneMap = savedToneMap;
238 yebisPlugin.Lens = savedLens;
239 yebisPlugin.ColorCorrection = savedColorCorrection;
242 foreach (var pass
in engineContext.RenderContext.RootRenderPass.Passes)
244 if (pass.Name ==
"WireframePass")
249 slideShowPlugin.RenderPass.Enabled =
false;
250 yebisPlugin.ToneMap.Exposure = 0.005f;
251 yebisPlugin.ToneMap.AutoExposure.Enable =
true;
252 slideShowPlugin.TransitionFactor = 0.0f;
256 if (animationTime < FullExposureTime)
258 yebisPlugin.ToneMap.AutoExposure.Enable =
true;
260 else if (animationTime < LogoTimeBegin)
262 if (yebisPlugin.ToneMap.AutoExposure.Enable)
264 lastExposure = yebisPlugin.ToneMap.Exposure;
265 yebisPlugin.ToneMap.AutoExposure.Enable =
false;
268 yebisPlugin.ToneMap.Exposure = lastExposure + (60.0f - lastExposure) * (
float)Math.Pow((animationTime - FullExposureTime) / (LogoTimeBegin - FullExposureTime), 2.0f);
272 if (!slideShowPlugin.RenderPass.Enabled)
275 foreach (var pass
in engineContext.RenderContext.RootRenderPass.Passes)
277 pass.Enabled =
false;
279 yebisPlugin.RenderPass.Enabled =
true;
280 slideShowPlugin.RenderPass.Enabled =
true;
282 yebisPlugin.ToneMap.Type = ToneMapType.Linear;
283 yebisPlugin.Lens.Vignette.Enable =
false;
284 yebisPlugin.ToneMap.Gamma = 1.0f;
285 yebisPlugin.ColorCorrection.ColorTemperature = 6500;
286 yebisPlugin.ColorCorrection.Saturation = 1.0f;
287 yebisPlugin.ColorCorrection.Contrast = 1.0f;
288 yebisPlugin.ColorCorrection.Brightness = 1.0f;
289 lastExposure = yebisPlugin.ToneMap.Exposure;
290 slideShowPlugin.TransitionFactor = 0.0f;
293 if (animationTime < LogoTimeFull)
295 slideShowPlugin.ZoomFactor = 0.5f + (1.0f - 0.5f) * Quintic((animationTime - LogoTimeBegin) / (LogoTimeFull - LogoTimeBegin));
296 yebisPlugin.ToneMap.Exposure = lastExposure + (1.0f - lastExposure) * (
float)Math.Pow((animationTime - LogoTimeBegin) / (LogoTimeFull - LogoTimeBegin), 0.1f);
300 slideShowPlugin.ZoomFactor = 1.0f;
301 if (animationTime < LogoTimeURL)
303 yebisPlugin.ToneMap.Exposure = 1.0f;
305 else if (animationTime < LogoTimeStartToEnd)
307 slideShowPlugin.TransitionFactor = Math.Min(1.0f, (animationTime - LogoTimeURL) / (LogoTimeStartToEnd - LogoTimeURL));
309 else if (animationTime < LogoTimeEnd)
311 slideShowPlugin.TransitionFactor = 1.0f;
313 else if (animationTime < CaveSceneEndBlack)
315 yebisPlugin.ToneMap.Exposure = Math.Max(0.0f, 1.0f - (animationTime - LogoTimeEnd) / (CaveSceneEndBlack - LogoTimeEnd));
319 yebisPlugin.ToneMap.Exposure = 0f;
325 lastTime = animationTime;
333 await engineContext.Scheduler.NextFrame();
335 if (engineContext.InputManager.IsKeyPressed(
Keys.F3))
337 var lights = engineContext.EntityManager.Entities.Components(LightComponent.Key);
338 await engineContext.Scheduler.WhenAll(lights.Select((lightComponent, index) =>
339 engineContext.Scheduler.Add(() =>
AnimateLight(engineContext, index, lightComponent))).ToArray());
344 private static float Quintic(
float x)
346 return x * x * x * (x * (x * 6.0f - 15.0f) + 10.0f);
352 await TaskEx.Delay(index * 20);
353 var startIntensity = lightComponent.Intensity;
356 var startTime = DateTime.UtcNow;
359 await engineContext.Scheduler.NextFrame();
360 var elapsedTime = (DateTime.UtcNow - startTime).Seconds * 0.2f;
361 if (elapsedTime > 1.0f)
363 lightComponent.Intensity = startIntensity * (1.0f - elapsedTime);
368 lightComponent.Intensity = startIntensity;
375 renderingSetup.RegisterLighting(engineContext);
377 var characterEntity = await AnimScript.LoadFBXModel(engineContext,
"/global_data/gdc_demo/char/dragon_camera.hotei#");
378 characterEntity.Name =
"Dragon";
379 Scheduler.Current.Add(() =>
AnimScript.
AnimateFBXModel(engineContext, characterEntity, CaveSceneTotalTime, CaveSceneRestart));
384 var meshComponent = entity.Get(ModelComponent.Key);
385 if (meshComponent == null)
388 foreach (var effectMesh
in meshComponent.InstantiatedSubMeshes)
390 effectMesh.Value.Parameters.Set(TessellationKeys.DesiredTriangleSize, 4.0f);
392 switch (effectMesh.Key.EffectData.Part)
395 effectMesh.Value.Parameters.Set(MaterialKeys.SpecularPower, 0.4f);
396 effectMesh.Value.Parameters.Set(MaterialKeys.SpecularIntensity, 0.4f);
399 effectMesh.Value.Parameters.Set(MaterialKeys.SpecularPower, 0.3f);
400 effectMesh.Value.Parameters.Set(MaterialKeys.SpecularIntensity, 0.3f);
403 effectMesh.Value.Parameters.Set(MaterialKeys.SpecularPower, 0.5f);
404 effectMesh.Value.Parameters.Set(MaterialKeys.SpecularIntensity, 0.5f);
407 effectMesh.Value.Parameters.Set(MaterialKeys.SpecularPower, 0.4f);
408 effectMesh.Value.Parameters.Set(MaterialKeys.SpecularIntensity, 0.5f);
411 effectMesh.Value.Parameters.Set(MaterialKeys.SpecularPower, 0.4f);
412 effectMesh.Value.Parameters.Set(MaterialKeys.SpecularIntensity, 0.7f);
415 effectMesh.Value.Parameters.Set(MaterialKeys.SpecularPower, 0.7f);
416 effectMesh.Value.Parameters.Set(MaterialKeys.SpecularIntensity, 0.7f);
419 effectMesh.Value.Parameters.Set(MaterialKeys.SpecularPower, 0.3f);
420 effectMesh.Value.Parameters.Set(MaterialKeys.SpecularIntensity, 0.3f);
426 return characterEntity;
437 var effectMagma = engineContext.RenderContext.Effects.First(x => x.Name ==
"Magma");
439 effectMagma.Parameters.AddSources(engineContext.DataContext.RenderPassPlugins.TryGetValue(
"NoisePlugin").Parameters);
442 var caveEntityPrefab1 = await engineContext.AssetManager.LoadAsync<
Entity>(
"/global_data/gdc_demo/bg/bg1.hotei#");
443 var caveEntityPrefab2 = await engineContext.AssetManager.LoadAsync<
Entity>(
"/global_data/gdc_demo/bg/bg2.hotei#");
445 var caveEntity1 = Prefab.Clone(caveEntityPrefab1);
446 var caveEntity2 = Prefab.Clone(caveEntityPrefab2);
449 if (engineContext.DataContext.RenderPassPlugins.TryGetValueCast(
"SkyBoxPlugin", out skyBoxPlugin))
451 var skyBoxTexture = (
Texture2D)await engineContext.AssetManager.LoadAsync<
Texture>(
"/global_data/gdc_demo/bg/GDC2012_map_sky.dds");
452 skyBoxPlugin.Texture = skyBoxTexture;
455 var magmaTexturePaths =
new[]
457 "/global_data/gdc_demo/bg/GDC2012_map_maguma_04.dds",
458 "/global_data/gdc_demo/bg/GDC2012_map_maguma_05.dds",
459 "/global_data/gdc_demo/bg/GDC2012_map_maguma_06.dds",
461 "/global_data/gdc_demo/bg/GDC2012_map_maguma_00.dds",
462 "/global_data/gdc_demo/bg/GDC2012_map_maguma_01.dds",
463 "/global_data/gdc_demo/bg/GDC2012_map_maguma_02.dds",
464 "/global_data/gdc_demo/bg/GDC2012_map_maguma_03.dds",
465 "/global_data/gdc_demo/bg/GDC2012_map_maguma_noise_00.dds",
466 "/global_data/gdc_demo/bg/GDC2012_map_maguma_noise_01.dds",
467 "/global_data/gdc_demo/bg/GDC2012_map_maguma_normals.dds",
469 var magmaTextures =
new Texture2D[magmaTexturePaths.Length];
470 for (
int i = 0; i < magmaTextures.Length; i++)
472 magmaTextures[i] = (
Texture2D)await engineContext.AssetManager.LoadAsync<
Texture>(magmaTexturePaths[i]);
475 var lightVectors =
new[]
477 new Vector3(-1.0f, 0.3f, -1.0f),
478 new Vector3(1.0f, 0.0f, -1.0f),
479 new Vector3(1.0f, 0.0f, -1.0f),
483 var random =
new Random(0);
487 var meshComponent = entity.Get(ModelComponent.Key);
488 if (meshComponent == null)
492 if (entity.Name.StartsWith(
"maguma_"))
494 meshComponent.MeshParameters.Set(LightKeys.LightDirection, lightVectors[planeIndex]);
495 meshComponent.MeshParameters.Set(ParameterKeys.IndexedKey(TexturingKeys.Texture0, 1), magmaTextures[planeIndex]);
497 for (
int i = 3; i < magmaTextures.Length; i++)
501 foreach (var effectMesh
in meshComponent.SubMeshes)
503 effectMesh.EffectData.Name =
"Magma";
516 VelocityUp =
new Vector3(0, 0, 200),
518 MaxTimeTarget = 20000.0f,
519 VelocityTarget = 200.0f,
523 DistanceDragonRepulse = 600.0f,
524 DecayDragonRepulse = 70.0f,
525 VelocityRepulse = 200.0f,
527 RootAnimation = animationEntity,
529 emitter.OnAddToSystem(engineContext.EntityManager, engineContext.RenderContext);
530 emitter.OnUpdateData();
531 entity.Set(ParticleEmitterComponent.Key, emitter);
534 foreach (var effectMesh
in meshComponent.SubMeshes)
536 effectMesh.Parameters.Set(ParameterKeys.IndexedKey(TexturingKeys.DiffuseTexture, 3), (
Texture2D)await engineContext.AssetManager.LoadAsync<
Texture>(
"/global_data/gdc_demo/bg/GDC2012_map_dis_ao.dds"));
540 await engineContext.EntityManager.AddEntityAsync(caveEntity1);
541 await engineContext.EntityManager.AddEntityAsync(caveEntity2);
545 var meshComponent = entity.Get(ModelComponent.Key);
546 if (meshComponent == null)
549 foreach (var effectMesh
in meshComponent.InstantiatedSubMeshes)
551 effectMesh.Value.Parameters.Set(MaterialKeys.SpecularIntensity, 2.0f);
552 effectMesh.Value.Parameters.Set(MaterialKeys.SpecularPower, 0.1f);
575 [XmlAttribute(
"gamma")]
578 [XmlAttribute(
"saturation")]
581 [XmlAttribute(
"contrast")]
584 [XmlAttribute(
"brightness")]
587 [XmlAttribute(
"temperature")]
SiliconStudio.Paradox.Games.Mathematics.Vector2 Vector2
Add a light to an Entity, that will be used during rendering.
Game entity. It usually aggregates multiple EntityComponent
static async Task AnimateFBXModel(EngineContext engineContext, Entity characterEntity, float endAnimTime=0.0f, float loopTime=0.0f)
static async Task AnimateLight(EngineContext engineContext, int index, LightComponent lightComponent)
System.Threading.Tasks.Task Task
static HashSet< Entity > CollectEntityTree(Entity entity)
static async Task Run(EngineContext engineContext)
static readonly RenderingSetup Singleton
static async Task< Entity > LoadDragon(EngineContext engineContext)
Describes the camera projection and view.
SiliconStudio.Core.Mathematics.Color Color
static async Task LoadCave(EngineContext engineContext, Entity animationEntity)
A Texture 2D frontend to SharpDX.Direct3D11.Texture2D.
static readonly ParameterKey< Texture > Texture0
SiliconStudio.Core.Mathematics.Vector3 Vector3
static async Task AnimateLights(EngineContext engineContext)
static async Task AnimateIntroAndEndScene(EngineContext engineContext)
A container to handle a hierarchical collection of effect variables.
Base class for texture resources.