public static async Task GenerateTestPrefabs(EngineContext engineContext) { var entityCube = new Entity("Cube"); var meshComponent = new ModelComponent(); meshComponent.SubMeshes.Add(new EffectMeshData { EffectData = new EffectData("SimpleCube"), MeshData = MeshDataHelper.CreateBox(100.0f, 100.0f, 100.0f, Color.Gray) }); entityCube.Set(ModelComponent.Key, meshComponent); entityCube.Set(TransformationComponent.Key, new TransformationComponent()); engineContext.AssetManager.Url.Set(entityCube, "/global_data/cube.hotei#/root"); engineContext.AssetManager.Save(entityCube); var entitySphere = new Entity("Cube"); meshComponent = new ModelComponent(); meshComponent.SubMeshes.Add(new EffectMeshData { EffectData = new EffectData("SimpleCube"), MeshData = MeshDataHelper.CreateSphere(100.0f, 30, 30, Color.Gray) }); entitySphere.Set(ModelComponent.Key, meshComponent); entitySphere.Set(TransformationComponent.Key, new TransformationComponent()); engineContext.AssetManager.Url.Set(entitySphere, "/global_data/sphere.hotei#/root"); engineContext.AssetManager.Save(entitySphere); }
public static async Task Run(EngineContext engineContext) { var renderingSetup = RenderingSetup.Singleton; renderingSetup.Initialize(engineContext); renderingSetup.RegisterLighting(engineContext); #if XENKO_YEBIS YebisPlugin yebisPlugin; if (engineContext.DataContext.RenderPassPlugins.TryGetValueCast("YebisPlugin", out yebisPlugin)) { yebisPlugin.Glare.Enable = true; yebisPlugin.ToneMap.Exposure = 1.0f; yebisPlugin.ToneMap.Gamma = 2.2f; } #endif var lightPrepassPlugin = (LightingPrepassPlugin)engineContext.DataContext.RenderPassPlugins.TryGetValue("LightingPrepassPlugin"); var gbufferPlugin = (GBufferPlugin)engineContext.DataContext.RenderPassPlugins.TryGetValue("GBufferPlugin"); EffectOld effect = engineContext.RenderContext.BuildEffect("SimpleCube") .Using(new BasicShaderPlugin("ShaderBase") { RenderPassPlugin = renderingSetup.MainTargetPlugin }) .Using(new BasicShaderPlugin("TransformationWVP") { RenderPassPlugin = renderingSetup.MainTargetPlugin }) .Using(new BasicShaderPlugin("AlbedoSpecularBase") { RenderPassPlugin = renderingSetup.MainTargetPlugin }) .Using(new BasicShaderPlugin("AlbedoDiffuseBase") { RenderPassPlugin = renderingSetup.MainTargetPlugin }) .Using(new BasicShaderPlugin("NormalVSGBuffer") { RenderPassPlugin = renderingSetup.MainTargetPlugin }) .Using(new BasicShaderPlugin("SpecularPowerPerMesh") { RenderPassPlugin = renderingSetup.MainTargetPlugin }) .Using(new BasicShaderPlugin("PositionVSGBuffer") { RenderPassPlugin = renderingSetup.MainTargetPlugin }) .Using(new BasicShaderPlugin("BRDFDiffuseLambert") { RenderPassPlugin = renderingSetup.MainTargetPlugin }) .Using(new BasicShaderPlugin("BRDFSpecularBlinnPhong") { RenderPassPlugin = renderingSetup.MainTargetPlugin }) .Using(new BasicShaderPlugin(new ShaderMixinSource() { new ShaderComposition("albedoDiffuse", new ShaderClassSource("ComputeColorStream")) }) { RenderPassPlugin = renderingSetup.MainTargetPlugin }) .Using(new BasicShaderPlugin(new ShaderMixinSource() { new ShaderComposition("albedoSpecular", new ShaderClassSource("ComputeColorSynthetic")) }) { RenderPassPlugin = renderingSetup.MainTargetPlugin }) .Using(new GBufferShaderPlugin { RenderPassPlugin = gbufferPlugin }) .Using(new DeferredLightingShaderPlugin() { RenderPassPlugin = lightPrepassPlugin }) .Using(new LightingShaderPlugin() { RenderPassPlugin = (LightingPlugin)engineContext.DataContext.RenderPassPlugins.TryGetValue("LightingPlugin") }) .Using(new BasicShaderPlugin("LightDirectionalShading") { RenderPassPlugin = renderingSetup.MainTargetPlugin }) ; var shadowMap1 = new ShadowMap(new DirectionalLight() { LightColor = new Color3(1.0f, 1.0f, 1.0f), LightDirection = new Vector3(1.0f, 1.0f, 1.0f) }); effect.Permutations.Set(ShadowMapPermutationArray.Key, new ShadowMapPermutationArray { ShadowMaps = { shadowMap1 } }); var r = new Random(0); VirtualFileSystem.MountFileSystem("/global_data", "..\\..\\deps\\data\\"); VirtualFileSystem.MountFileSystem("/global_data2", "..\\..\\data\\"); SkyBoxPlugin skyBoxPlugin; if (engineContext.DataContext.RenderPassPlugins.TryGetValueCast("SkyBoxPlugin", out skyBoxPlugin)) { var skyBoxTexture = (Texture2D)await engineContext.AssetManager.LoadAsync <Texture>("/global_data/gdc_demo/bg/GDC2012_map_sky.dds"); skyBoxPlugin.Texture = skyBoxTexture; } var effectMeshGroup = new RenderPassListEnumerator(); engineContext.RenderContext.RenderPassEnumerators.Add(effectMeshGroup); var groundMesh = new EffectMesh(effect, MeshDataHelper.CreateBox(10000, 10000, 1, Color.White)); groundMesh.KeepAliveBy(engineContext.SimpleComponentRegistry); effectMeshGroup.AddMesh(groundMesh); groundMesh.Parameters.Set(TransformationKeys.World, Matrix.Translation(new Vector3(0, 0, 0))); // Lights for (int i = 0; i < 1024; ++i) { Color3 color = (Color3)Color.White; switch (i % 4) { case 0: color = (Color3)Color.DarkOrange; break; case 1: color = (Color3)Color.DarkGoldenrod; break; case 2: color = (Color3)Color.DarkSalmon; break; case 3: color = (Color3)Color.DarkRed; break; } var effectMesh = new EffectMesh(lightPrepassPlugin.Lights); effectMesh.Parameters.Set(LightKeys.LightRadius, (float)r.NextDouble() * 200 + 200.0f); effectMesh.Parameters.Set(LightKeys.LightColor, color); effectMesh.KeepAliveBy(engineContext.SimpleComponentRegistry); effectMeshGroup.AddMesh(effectMesh); } EffectOld effectLight = lightPrepassPlugin.Lights; var lightInfo = new LightInfo[effectLight != null ? effectLight.Meshes.Count : 0]; for (int i = 0; i < lightInfo.Length; ++i) { lightInfo[i].Radius = (float)r.NextDouble() * 7000.0f + 500.0f; lightInfo[i].Phase = (float)(r.NextDouble() * Math.PI * 2.0); lightInfo[i].Z = (float)r.NextDouble() * 3000.0f;; } float time = 0.0f; // Meshes (quad) that will later be generated by the engine (light pre pass, SSAO, etc...) // Lights //var effectMesh = new EffectMesh(setup.LightingPrepassPlugin.Lights); //effectMesh.Parameters.Set(LightKeys.LightRadius, 1000.0f); //effectMesh.Parameters.Set(LightKeys.LightColor, new R32G32B32_Float(1.0f, 1.0f, 1.0f)); //effectMesh.Parameters.Set(LightKeys.LightPosition, new R32G32B32_Float(0, 0, 1200)); //effectMesh.KeepAliveBy(engineContext.SimpleComponentRegistry); //effectMeshGroup.AddMesh(effectMesh); //var boxMesh = new EffectMesh(effect, MeshDataHelper.CreateBox(300, R8G8B8A8.LightBlue)); //boxMesh.KeepAliveBy(engineContext.SimpleComponentRegistry); //boxMesh.Parameters.Set(TransformationKeys.World, Matrix.Translation(new R32G32B32_Float(0, 0, 200))); //effectMeshGroup.AddMesh(boxMesh); var clock = new Stopwatch(); clock.Start(); int sizeX = 10; int sizeY = 10; var spheres = new Sphere[sizeY, sizeX]; Random random = new Random(0); int size = 200; var meshData = MeshDataHelper.CreateSphere(size, 30, 30, Color.Gray); for (int iy = 0; iy < sizeY; iy++) { for (int ix = 0; ix < sizeX; ix++) { var sphere = new Sphere(); sphere.Mesh = new EffectMesh(effect, meshData); sphere.Phase = (float)random.NextDouble(); sphere.Speed = (float)random.NextDouble(); spheres[iy, ix] = sphere; effectMeshGroup.AddMesh(sphere.Mesh); } } while (true) { await Scheduler.Current.NextFrame(); for (int iy = 0; iy < sizeY; iy++) { for (int ix = 0; ix < sizeX; ix++) { var iFactor = (float)(iy * sizeY + ix) / (sizeX * sizeY); var sphere = spheres[iy, ix]; var sphereMesh = sphere.Mesh; var specularColor = Color.SmoothStep(Color.GreenYellow, Color.Gray, iFactor); // Matrix.RotationX((float)Math.PI/2.0f) * M sphereMesh.Parameters.Set( TransformationKeys.World, Matrix.Translation( new Vector3( (ix - sizeX / 2) * (size * 1.2f) * 2.0f, (iy - sizeY / 2) * (size * 1.2f) * 2.0f, (float)(2000 * (0.5 + 0.5 * Math.Sin(clock.ElapsedMilliseconds / 1000.0f * sphere.Speed * 0.5f + Math.PI * sphere.Phase)))))); sphereMesh.Parameters.Set(MaterialKeys.SpecularPower, iFactor * 0.9f); sphereMesh.Parameters.Set(MaterialKeys.SpecularColor, specularColor); } } time = clock.ElapsedMilliseconds / 1000.0f; if (lightInfo.Length > 0) { int index = 0; foreach (var mesh in effectLight.Meshes) { 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)))); index++; } } } }
public static async Task Run(EngineContext engineContext) { var renderingSetup = RenderingSetup.Singleton; //engineContext.RenderContext.Register(renderingSetup.LightingPlugin); //renderingSetup.RegisterLighting(engineContext); var shadowMapPlugin = new LightingPlugin { MainPlugin = renderingSetup.MainPlugin, RenderPass = engineContext.DataContext.RenderPasses.TryGetValue("ShadowMapPass") }; shadowMapPlugin.RenderContext = engineContext.RenderContext; var shadowMap1 = new ShadowMap(new DirectionalLight()) { Level = CascadeShadowMapLevel.X1, ShadowMapSize = 1024, ShadowDistance = 2000.0f } .SetFilter(sm => new ShadowMapFilterDefault(sm)); var shadowMap2 = new ShadowMap(new DirectionalLight()) { Level = CascadeShadowMapLevel.X2, ShadowMapSize = 1024, ShadowDistance = 2000.0f } .SetFilter(sm => new ShadowMapFilterVsm(sm)); shadowMapPlugin.AddShadowMap(shadowMap1); shadowMapPlugin.AddShadowMap(shadowMap2); shadowMap1.DirectionalLight.LightDirection = new Vector3(-1.0f, -1.0f, -1.0f); shadowMap1.DirectionalLight.LightColor = new Color3(1.0f, 0.5f, 0.5f); shadowMap2.DirectionalLight.LightDirection = new Vector3(-1.0f, 1.0f, -1.0f); shadowMap2.DirectionalLight.LightColor = new Color3(0.5f, 0.5f, 1.0f); engineContext.RenderContext.Register(shadowMapPlugin); EffectOld effect = engineContext.RenderContext.BuildEffect("Permutation") .Using(new BasicShaderPlugin("ShaderBase") { RenderPassPlugin = renderingSetup.MainTargetPlugin }) .Using(new BasicShaderPlugin("TransformationWVP") { RenderPassPlugin = renderingSetup.MainTargetPlugin }) .Using(new BasicShaderPlugin(new ShaderMixinSource() { "NormalVSStream", "PositionVSStream", new ShaderComposition("albedoDiffuse", new ShaderClassSource("ComputeColorStream")), new ShaderComposition("albedoSpecular", new ShaderClassSource("ComputeColor")), // TODO: Default values! "BRDFDiffuseLambert", "BRDFSpecularBlinnPhong", "ShadingBase", }) { RenderPassPlugin = renderingSetup.MainTargetPlugin }) .Using(new LightingShaderPlugin() { RenderPassPlugin = shadowMapPlugin }) ; var sphereMeshData = MeshDataHelper.CreateSphere(10.0f, 20, 20, Color.White); var effectMeshGroup = new RenderPassListEnumerator(); engineContext.RenderContext.RenderPassEnumerators.Add(effectMeshGroup); int objectSqrtCount = 1; int meshCount = objectSqrtCount * objectSqrtCount * objectSqrtCount; var shadowMapPermutation1 = new ShadowMapPermutationArray { ShadowMaps = { shadowMap1 } }; var shadowMapPermutation2 = new ShadowMapPermutationArray { ShadowMaps = { shadowMap2 } }; var shadowMapPermutation3 = new ShadowMapPermutationArray { ShadowMaps = { shadowMap1, shadowMap2 } }; effect.Permutations.Set(ShadowMapPermutationArray.Key, shadowMapPermutation1); var groups2 = new[] { shadowMapPermutation1, shadowMapPermutation2, shadowMapPermutation3 }; int groupIndex2 = 0; var effectMeshes = new List <EffectMesh>(); for (int j = 0; j < meshCount; ++j) { var effectMesh = new EffectMesh(effect, sphereMeshData); effectMesh.KeepAliveBy(engineContext.SimpleComponentRegistry); effect.Permutations.Set(ShadowMapPermutationArray.Key, groups2[2]); var w2 = Matrix.Scaling(1.0f) * Matrix.Translation(new Vector3( (j % objectSqrtCount - objectSqrtCount / 2) * 30.0f - 30.0f, (((j / objectSqrtCount) % objectSqrtCount) - objectSqrtCount / 2) * 30.0f - 30.0f, (j / (objectSqrtCount * objectSqrtCount)) * 30.0f + 30.0f)); effectMesh.Parameters.Set(TransformationKeys.World, w2); effectMeshes.Add(effectMesh); } var groundMesh = new EffectMesh(effect, MeshDataHelper.CreateBox(1000, 1000, 1, Color.White)); groundMesh.KeepAliveBy(engineContext.SimpleComponentRegistry); effectMeshGroup.AddMesh(groundMesh); groundMesh.Parameters.Set(TransformationKeys.World, Matrix.Translation(0.0f, 0.0f, 0.0f)); var groups = new[] { new int[] { 0, 1 }, new int[] { 0 }, new int[] { 1 } }; int groupIndex = 0; await TaskEx.Delay(1000); foreach (var effectMesh in effectMeshes) { effectMeshGroup.AddMesh(effectMesh); } while (true) { await engineContext.Scheduler.NextFrame(); if (engineContext.InputManager.IsKeyPressed(Keys.F8)) { var permutation = new LightingPermutation { LightBindings = { new Light() } }; effect.Permutations.Set(LightingPermutation.Key, permutation); } if (engineContext.InputManager.IsKeyPressed(Keys.F9)) { effect.Permutations.Set(ShadowMapPermutationArray.Key, groups2[(groupIndex2++) % groups2.Length]); } } }
public static async Task Run(EngineContext engineContext) { var renderingSetup = RenderingSetup.Singleton; renderingSetup.Initialize(engineContext); renderingSetup.RegisterLighting(engineContext); #if PARADOX_YEBIS YebisPlugin yebisPlugin; if (engineContext.DataContext.RenderPassPlugins.TryGetValueCast("YebisPlugin", out yebisPlugin)) { yebisPlugin.Glare.Enable = true; yebisPlugin.ToneMap.Exposure = 1.0f; yebisPlugin.ToneMap.Gamma = 2.2f; } #endif EffectOld effect = engineContext.RenderContext.BuildEffect("SimpleCube") .Using(new BasicShaderPlugin("ShaderBase") { RenderPassPlugin = renderingSetup.MainTargetPlugin }) .Using(new BasicShaderPlugin("TransformationWVP") { RenderPassPlugin = renderingSetup.MainTargetPlugin }) .Using(new BasicShaderPlugin("AlbedoSpecularBase") { RenderPassPlugin = renderingSetup.MainTargetPlugin }) .Using(new BasicShaderPlugin("AlbedoDiffuseBase") { RenderPassPlugin = renderingSetup.MainTargetPlugin }) .Using(new BasicShaderPlugin("NormalVSGBuffer") { RenderPassPlugin = renderingSetup.MainTargetPlugin }) .Using(new BasicShaderPlugin("SpecularPowerPerMesh") { RenderPassPlugin = renderingSetup.MainTargetPlugin }) .Using(new BasicShaderPlugin("PositionVSGBuffer") { RenderPassPlugin = renderingSetup.MainTargetPlugin }) .Using(new BasicShaderPlugin("BRDFDiffuseLambert") { RenderPassPlugin = renderingSetup.MainTargetPlugin }) .Using(new BasicShaderPlugin("BRDFSpecularBlinnPhong") { RenderPassPlugin = renderingSetup.MainTargetPlugin }) .Using(new BasicShaderPlugin(new ShaderMixinSource() { new ShaderComposition("albedoDiffuse", new ShaderClassSource("ComputeColorStream")) }) { RenderPassPlugin = renderingSetup.MainTargetPlugin }) .Using(new BasicShaderPlugin(new ShaderMixinSource() { new ShaderComposition("albedoSpecular", new ShaderClassSource("ComputeColorSynthetic")) }) { RenderPassPlugin = renderingSetup.MainTargetPlugin }) .Using(new GBufferShaderPlugin { RenderPassPlugin = (GBufferPlugin)engineContext.DataContext.RenderPassPlugins.TryGetValue("GBufferPlugin") }) .Using(new LightingShaderPlugin() { RenderPassPlugin = (LightingPlugin)engineContext.DataContext.RenderPassPlugins.TryGetValue("LightingPlugin") }) .Using(new BasicShaderPlugin("LightDirectionalShading") { RenderPassPlugin = renderingSetup.MainTargetPlugin }) ; var shadowMap1 = new ShadowMap(new DirectionalLight() { LightColor = new Color3(1.0f, 1.0f, 1.0f), LightDirection = new Vector3(1.0f, 1.0f, 1.0f) }); effect.Permutations.Set(ShadowMapPermutationArray.Key, new ShadowMapPermutationArray { ShadowMaps = { shadowMap1 } }); var r = new Random(0); VirtualFileSystem.MountFileSystem("/global_data", "..\\..\\deps\\data\\"); VirtualFileSystem.MountFileSystem("/global_data2", "..\\..\\data\\"); SkyBoxPlugin skyBoxPlugin; if (engineContext.DataContext.RenderPassPlugins.TryGetValueCast("SkyBoxPlugin", out skyBoxPlugin)) { var skyBoxTexture = (Texture2D)await engineContext.AssetManager.LoadAsync <Texture>("/global_data/gdc_demo/bg/GDC2012_map_sky.dds"); skyBoxPlugin.Texture = skyBoxTexture; } var effectMeshGroup = new RenderPassListEnumerator(); engineContext.RenderContext.RenderPassEnumerators.Add(effectMeshGroup); var groundMesh = new EffectMesh(effect, MeshDataHelper.CreateBox(10000, 10000, 1, Color.White)); groundMesh.KeepAliveBy(engineContext.SimpleComponentRegistry); effectMeshGroup.AddMesh(groundMesh); groundMesh.Parameters.Set(TransformationKeys.World, Matrix.Translation(new Vector3(0, 0, 0))); var meshData = MeshDataHelper.CreateSphere(200, 30, 30, Color.Gray); var sphereMesh = new EffectMesh(effect, meshData); sphereMesh.Parameters.Set(TransformationKeys.World, Matrix.Translation(new Vector3(0, 0, 50))); sphereMesh.Parameters.Set(MaterialKeys.SpecularPower, 0f); sphereMesh.Parameters.Set(MaterialKeys.SpecularColor, Color.White); effectMeshGroup.AddMesh(sphereMesh); //while (true) //{ // await Scheduler.Current.WaitFrame(); // for (int iy = 0; iy < sizeY; iy++) // { // for (int ix = 0; ix < sizeX; ix++) // { // var iFactor = (float)(iy * sizeY + ix) / (sizeX * sizeY); // var sphere = spheres[iy, ix]; // var sphereMesh = sphere.Mesh; // var specularColor = R8G8B8A8.SmoothStep(R8G8B8A8.GreenYellow, R8G8B8A8.Gray, iFactor); // // Matrix.RotationX((float)Math.PI/2.0f) * M // sphereMesh.Parameters.Set( // TransformationKeys.World, // Matrix.Translation( // new R32G32B32_Float( // (ix - sizeX / 2) * (size * 1.2f) * 2.0f, // (iy - sizeY / 2) * (size * 1.2f) * 2.0f, // (float)(2000 * (0.5 + 0.5 * Math.Sin(clock.ElapsedMilliseconds / 1000.0f * sphere.Speed * 0.5f + Math.PI * sphere.Phase)))))); // sphereMesh.Parameters.Set(MaterialKeys.SpecularPower, iFactor * 0.9f); // sphereMesh.Parameters.Set(MaterialKeys.SpecularColor, specularColor); // } // } // time = clock.ElapsedMilliseconds / 1000.0f; // if (lightInfo.Length > 0) // { // int index = 0; // foreach (var mesh in effectLight.Meshes) // { // mesh.Parameters.Set(LightKeys.LightPosition, new R32G32B32_Float(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)))); // index++; // } // } //} }