Exemplo n.º 1
0
        public static async Task Run(EngineContext engineContext)
        {
#if PARADOX_YEBIS
            YebisPlugin yebisPlugin;
            if (engineContext.DataContext.RenderPassPlugins.TryGetValueCast("YebisPlugin", out yebisPlugin))
            {
                yebisPlugin.Glare.Enable                = false;
                yebisPlugin.DepthOfField.Enable         = false;
                yebisPlugin.ToneMap.AutoExposure.Enable = false;
                yebisPlugin.ToneMap.Exposure            = 1.0f;
                yebisPlugin.ToneMap.Gamma               = 2.2f;
            }
#endif

            EffectOld effect = engineContext.RenderContext.Effects.First(x => x.Name == "Multicore");
            //Effect effect = engineContext.RenderContext.BuildEffect("Multicore")
            //    .Using(new BasicShaderPlugin("ShaderBase") { RenderPassPlugin = renderingSetup.MainDepthReadOnlyPlugin })
            //    .Using(new BasicShaderPlugin("TransformationWVP") { RenderPassPlugin = renderingSetup.MainDepthReadOnlyPlugin })
            //    .Using(new BasicShaderPlugin(new ShaderMixinSource()
            //                        {
            //                            "NormalVSStream",
            //                            "PositionVSStream",
            //                            new ShaderComposition("albedoDiffuse", new ShaderClassSource("ComputeColorStream")),
            //                            new ShaderComposition("albedoSpecular", new ShaderClassSource("ComputeColor")), // TODO: Default values!
            //                            "BRDFDiffuseLambert",
            //                            "BRDFSpecularBlinnPhong",
            //                        }) { RenderPassPlugin = renderingSetup.MainDepthReadOnlyPlugin })
            //    .Using(new BasicShaderPlugin("AlbedoFlatShading") { RenderPassPlugin = renderingSetup.MainDepthReadOnlyPlugin })
            //    .Using(new LightingShaderPlugin { RenderPassPlugin = renderingSetup.LightingPlugin })
            //    //.Using(new BasicShaderPlugin("LightDirectionalShading") { RenderPassPlugin = renderingSetup.MainDepthReadOnlyPlugin })
            //    ;

            //effect.Permutations.Set(LightingPermutation.Key, new LightingPermutation { Lights = new Light[] { new DirectionalLight { LightColor = new Color3(1.0f), LightDirection = new R32G32B32_Float(-1.0f, -1.0f, 1.0f) } } });

            var rand         = new Random();
            var cubeMeshData = Enumerable.Range(0, 10).Select(x => MeshDataHelper.CreateBox(10, 10, 10, new Color((float)rand.NextDouble(), (float)rand.NextDouble(), (float)rand.NextDouble(), 1.0f))).ToArray();

            var effectMeshGroup = new RenderPassListEnumerator();
            engineContext.RenderContext.RenderPassEnumerators.Add(effectMeshGroup);

            int objectSqrtCount = 31;
            int meshCount       = objectSqrtCount * objectSqrtCount * objectSqrtCount;

            for (int j = 0; j < meshCount; ++j)
            {
                var effectMesh = new EffectMesh(effect, cubeMeshData[(j / 25) % 10]);
                effectMesh.KeepAliveBy(engineContext.SimpleComponentRegistry);
                effectMeshGroup.AddMesh(effectMesh);

                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) - objectSqrtCount / 2) * 30.0f - 30.0f));

                effectMesh.Parameters.Set(TransformationKeys.World, w2);
            }
        }
Exemplo n.º 2
0
        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);
        }
Exemplo n.º 3
0
        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++;
                    }
                }
            }
        }
Exemplo n.º 4
0
        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]);
                }
            }
        }
Exemplo n.º 5
0
        public static async Task Run(EngineContext engineContext)
        {
            var renderingSetup = RenderingSetup.Singleton;

            renderingSetup.RegisterLighting(engineContext);

            ParticlePlugin particlePlugin;

            if (engineContext.DataContext.RenderPassPlugins.TryGetValueCast("ParticlePlugin", out particlePlugin))
            {
                ScriptParticleSmoke.Run(engineContext);
            }


            var yebisPlugin = engineContext.RenderContext.RenderPassPlugins.OfType <YebisPlugin>().FirstOrDefault();

            if (yebisPlugin != null)
            {
                var yebisConfig = AppConfig.GetConfiguration <YebisConfig>("Yebis");

                // yebisPlugin.ToneMap.Type = ToneMapType.Linear;
                yebisPlugin.ToneMap.Gamma = yebisConfig.Gamma;
                yebisPlugin.ColorCorrection.Saturation       = yebisConfig.Saturation;
                yebisPlugin.ColorCorrection.Contrast         = yebisConfig.Contrast;
                yebisPlugin.ColorCorrection.Brightness       = yebisConfig.Brightness;
                yebisPlugin.ColorCorrection.ColorTemperature = yebisConfig.ColorTemperature;
                yebisPlugin.Lens.Vignette.Enable             = true;
                yebisPlugin.Lens.Vignette.PowerOfCosine      = 5.0f;
                yebisPlugin.Lens.Distortion.Enable           = true;
                yebisPlugin.Lens.Distortion.EdgeRoundness    = 0.1f;
                yebisPlugin.Lens.Distortion.EdgeSmoothness   = 1.0f;
            }

            // Run the script to animate the intro fade-in/fade-out
            engineContext.Scheduler.Add(async() => await AnimateIntroAndEndScene(engineContext));

            var cameraEntityRootPrefab = await engineContext.AssetManager.LoadAsync <Entity>("/global_data/gdc_demo/char/camera.hotei#");

            var lightCamEntityRootPrefab = await engineContext.AssetManager.LoadAsync <Entity>("/global_data/gdc_demo/char/light_cam.hotei#");

            var lightCamEntityRoot = Prefab.Inherit(lightCamEntityRootPrefab);
            var cameraEntityRoot   = Prefab.Inherit(cameraEntityRootPrefab);

            engineContext.EntityManager.AddEntity(cameraEntityRoot);
            engineContext.EntityManager.AddEntity(lightCamEntityRoot);
            Scheduler.Current.Add(() => AnimScript.AnimateFBXModel(engineContext, cameraEntityRoot, CaveSceneTotalTime, CaveSceneRestart));
            Scheduler.Current.Add(() => AnimScript.AnimateFBXModel(engineContext, lightCamEntityRoot, CaveSceneTotalTime, CaveSceneRestart));

            foreach (var light in ParameterContainerExtensions.CollectEntityTree(lightCamEntityRoot))
            {
                var lightComp = light.Get(LightComponent.Key);
                if (lightComp != null)
                {
                    if (!lightComp.ShadowMap && lightComp.Type == LightType.Directional)
                    {
                        lightComp.Intensity *= 0.1f;
                    }
                }
            }

            var config = AppConfig.GetConfiguration <Script1.Config>("Script1");

            var shadowMap1 = new Entity();
            var shadowMap2 = new Entity();

            shadowMap1.Set(TransformationComponent.Key, TransformationTRS.CreateComponent());
            shadowMap2.Set(TransformationComponent.Key, TransformationTRS.CreateComponent());
            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
            });
            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
            });
            shadowMap1.Set(LightShaftsComponent.Key, new LightShaftsComponent {
                Color = new Color3(1.0f, 1.0f, 1.0f), LightShaftsBoundingBoxes =
                {
                    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) }
                        }
                    }
                }
            });
            shadowMap2.Set(LightShaftsComponent.Key, new LightShaftsComponent {
                Color = new Color3(1.0f, 1.0f, 1.0f), LightShaftsBoundingBoxes =
                {
                    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) }
                        }
                    }
                }
            });

            engineContext.EntityManager.AddEntity(shadowMap1);
            engineContext.EntityManager.AddEntity(shadowMap2);


            var dragon = await LoadDragon(engineContext);

            await LoadCave(engineContext, dragon);

            var dragonHead = engineContext.EntityManager.Entities.FirstOrDefault(x => x.Name == "English DragonHead");
            TransformationTRS headCameraTransfo = null;

            if (dragonHead != null)
            {
                var headCamera = new Entity("Head camera");
                headCamera.Set(CameraComponent.Key, new CameraComponent {
                    AspectRatio = 16.0f / 9.0f, VerticalFieldOfView = (float)Math.PI * 0.3f, Target = dragonHead, AutoFocus = true, NearPlane = 10.0f
                });
                headCamera.Set(TransformationComponent.Key, new TransformationComponent(new TransformationTRS {
                    Translation = new Vector3(100.0f, -100.0f, 300.0f)
                }));
                //engineContext.EntitySystem.Entities.Add(headCamera);
                dragonHead.Transformation.Children.Add(headCamera.Transformation);
            }

            engineContext.Scheduler.Add(() => AnimateLights(engineContext));

            // Performs several full GC after the scene has been loaded
            for (int i = 0; i < 10; i++)
            {
                GC.Collect();
                Thread.Sleep(1);
            }

            while (true)
            {
                await engineContext.Scheduler.NextFrame();

                if (headCameraTransfo != null)
                {
                    var   time          = (double)DateTime.UtcNow.Ticks / (double)TimeSpan.TicksPerSecond;
                    float rotationSpeed = 0.317f;
                    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);
                    headCameraTransfo.Translation = new Vector3(position.X, -150.0f + (float)Math.Cos(time * rotationSpeed) * 50.0f, position.Y);
                }

                if (engineContext.InputManager.IsKeyPressed(Keys.F1))
                {
                    bool isWireframeEnabled = renderingSetup.ToggleWireframe();
                    if (yebisPlugin != null)
                    {
                        yebisPlugin.DepthOfField.Enable    = !isWireframeEnabled;
                        yebisPlugin.Lens.Vignette.Enable   = !isWireframeEnabled;
                        yebisPlugin.Lens.Distortion.Enable = !isWireframeEnabled;
                    }
                }

                if (engineContext.InputManager.IsKeyPressed(Keys.D1))
                {
                    engineContext.CurrentTime = TimeSpan.FromSeconds(0);
                }
                if (engineContext.InputManager.IsKeyPressed(Keys.D2))
                {
                    engineContext.CurrentTime = TimeSpan.FromSeconds(10);
                }
                if (engineContext.InputManager.IsKeyPressed(Keys.D3))
                {
                    engineContext.CurrentTime = TimeSpan.FromSeconds(20);
                }
                if (engineContext.InputManager.IsKeyPressed(Keys.D4))
                {
                    engineContext.CurrentTime = TimeSpan.FromSeconds(30);
                }
                if (engineContext.InputManager.IsKeyPressed(Keys.D5))
                {
                    engineContext.CurrentTime = TimeSpan.FromSeconds(40);
                }
                if (engineContext.InputManager.IsKeyPressed(Keys.D6))
                {
                    engineContext.CurrentTime = TimeSpan.FromSeconds(50);
                }
                if (engineContext.InputManager.IsKeyPressed(Keys.D7))
                {
                    engineContext.CurrentTime = TimeSpan.FromSeconds(60);
                }
                if (engineContext.InputManager.IsKeyPressed(Keys.D8))
                {
                    engineContext.CurrentTime = TimeSpan.FromSeconds(70);
                }

                if (engineContext.InputManager.IsKeyPressed(Keys.T))
                {
                    if (particlePlugin != null)
                    {
                        particlePlugin.EnableSorting = !particlePlugin.EnableSorting;
                    }
                }
            }
        }
Exemplo n.º 6
0
        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++;
            //        }
            //    }
            //}
        }