Beispiel #1
0
        // [XenkoScript]
        public static async Task LoadCave(EngineContext engineContext, Entity animationEntity)
        {
            // Setup "fake" directional light for outdoor so that normal maps stand out.
            var outdoorLight = new DirectionalLight { LightDirection = new Vector3(-2.0f, -1.0f, -1.0f) };
            //outdoor.Permutations.Set(LightingPermutation.Key, new LightingPermutation { Lights = { outdoorLight } });
            //outdoor.Permutations.Set(LightingPermutation.Key, new LightingPermutation { Lights = { outdoorLight } });

            var effectMagma = engineContext.RenderContext.Effects.First(x => x.Name == "Magma");

            effectMagma.Parameters.AddSources(engineContext.DataContext.RenderPassPlugins.TryGetValue("NoisePlugin").Parameters);

            //var assetManager = new AssetManager(new ContentSerializerContextGenerator(engineContext.VirtualFileSystem, engineContext.PackageManager, ParameterContainerExtensions.DefaultSceneSerializer));
            var caveEntityPrefab1 = await engineContext.AssetManager.LoadAsync<Entity>("/global_data/gdc_demo/bg/bg1.hotei#");
            var caveEntityPrefab2 = await engineContext.AssetManager.LoadAsync<Entity>("/global_data/gdc_demo/bg/bg2.hotei#");

            var caveEntity1 = Prefab.Clone(caveEntityPrefab1);
            var caveEntity2 = Prefab.Clone(caveEntityPrefab2);

            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 magmaTexturePaths = new[]
                {
                    "/global_data/gdc_demo/bg/GDC2012_map_maguma_04.dds",
                    "/global_data/gdc_demo/bg/GDC2012_map_maguma_05.dds",
                    "/global_data/gdc_demo/bg/GDC2012_map_maguma_06.dds",

                    "/global_data/gdc_demo/bg/GDC2012_map_maguma_00.dds",
                    "/global_data/gdc_demo/bg/GDC2012_map_maguma_01.dds",
                    "/global_data/gdc_demo/bg/GDC2012_map_maguma_02.dds",
                    "/global_data/gdc_demo/bg/GDC2012_map_maguma_03.dds",
                    "/global_data/gdc_demo/bg/GDC2012_map_maguma_noise_00.dds",
                    "/global_data/gdc_demo/bg/GDC2012_map_maguma_noise_01.dds",
                    "/global_data/gdc_demo/bg/GDC2012_map_maguma_normals.dds",
                };
            var magmaTextures = new Texture2D[magmaTexturePaths.Length];
            for (int i = 0; i < magmaTextures.Length; i++)
            {
                magmaTextures[i] = (Texture2D)await engineContext.AssetManager.LoadAsync<Texture>(magmaTexturePaths[i]);
            }

            var lightVectors = new[]
                {
                    new Vector3(-1.0f, 0.3f, -1.0f),
                    new Vector3(1.0f, 0.0f, -1.0f),
                    new Vector3(1.0f, 0.0f, -1.0f),
                };


            var random = new Random(0);
            int planeIndex = 0;
            foreach (var entity in ParameterContainerExtensions.CollectEntityTree(caveEntity1))
            {
                var meshComponent = entity.Get(ModelComponent.Key);
                if (meshComponent == null)
                    continue;

                // Setup textures for magma
                if (entity.Name.StartsWith("maguma_"))
                {
                    meshComponent.MeshParameters.Set(LightKeys.LightDirection, lightVectors[planeIndex]);
                    meshComponent.MeshParameters.Set(ParameterKeys.IndexedKey(TexturingKeys.Texture0, 1), magmaTextures[planeIndex]);
                    planeIndex++;
                    for (int i = 3; i < magmaTextures.Length; i++)
                        meshComponent.MeshParameters.Set(ParameterKeys.IndexedKey(TexturingKeys.Texture0, i - 1), magmaTextures[i]);


                    foreach (var effectMesh in meshComponent.SubMeshes)
                    {
                        effectMesh.EffectData.Name = "Magma";
                    }

                    // Attach a bullet particle emitter to the magma
                    var emitter = new BulletParticleEmitterComponent()
                    {
                        //Count = 4096,
                        Count = 16384,
                        Description = new BulletEmitterDescription()
                        {
                            Target = new Vector3(-3016.261f, -70.52288f, 800.8788f),
                            BulletSize = 4.0f,
                            //MaxTimeTarget = 1000.0f + 5000.0f * (float)random.NextDouble(),
                            VelocityUp =  new Vector3(0, 0, 200),
                            MaxTimeUp = 5000.0f,
                            MaxTimeTarget = 20000.0f,
                            VelocityTarget = 200.0f,
                            Opacity = 1.0f,
                            //DistanceDragonRepulse = 1200.0f,
                            //DecayDragonRepulse = 70.0f,
                            DistanceDragonRepulse = 600.0f,
                            DecayDragonRepulse = 70.0f,
                            VelocityRepulse = 200.0f,
                        },
                        RootAnimation = animationEntity,
                    };
                    emitter.OnAddToSystem(engineContext.EntityManager, engineContext.RenderContext);
                    emitter.OnUpdateData();
                    entity.Set(ParticleEmitterComponent.Key, emitter);
                }

                foreach (var effectMesh in meshComponent.SubMeshes)
                {
                    effectMesh.Parameters.Set(ParameterKeys.IndexedKey(TexturingKeys.DiffuseTexture, 3), (Texture2D)await engineContext.AssetManager.LoadAsync<Texture>("/global_data/gdc_demo/bg/GDC2012_map_dis_ao.dds"));
                }
            }

            await engineContext.EntityManager.AddEntityAsync(caveEntity1);
            await engineContext.EntityManager.AddEntityAsync(caveEntity2);

            foreach (var entity in ParameterContainerExtensions.CollectEntityTree(caveEntity1).Concat(ParameterContainerExtensions.CollectEntityTree(caveEntity2)))
            {
                var meshComponent = entity.Get(ModelComponent.Key);
                if (meshComponent == null)
                    continue;

                foreach (var effectMesh in meshComponent.InstantiatedSubMeshes)
                {
                    effectMesh.Value.Parameters.Set(MaterialKeys.SpecularIntensity, 2.0f);
                    effectMesh.Value.Parameters.Set(MaterialKeys.SpecularPower, 0.1f);
                }
            }
        }
Beispiel #2
0
        // [ParadoxScript]
        public static async Task LoadCave(EngineContext engineContext, Entity animationEntity)
        {
            // Setup "fake" directional light for outdoor so that normal maps stand out.
            var outdoorLight = new DirectionalLight {
                LightDirection = new Vector3(-2.0f, -1.0f, -1.0f)
            };
            //outdoor.Permutations.Set(LightingPermutation.Key, new LightingPermutation { Lights = { outdoorLight } });
            //outdoor.Permutations.Set(LightingPermutation.Key, new LightingPermutation { Lights = { outdoorLight } });

            var effectMagma = engineContext.RenderContext.Effects.First(x => x.Name == "Magma");

            effectMagma.Parameters.AddSources(engineContext.DataContext.RenderPassPlugins.TryGetValue("NoisePlugin").Parameters);

            //var assetManager = new AssetManager(new ContentSerializerContextGenerator(engineContext.VirtualFileSystem, engineContext.PackageManager, ParameterContainerExtensions.DefaultSceneSerializer));
            var caveEntityPrefab1 = await engineContext.AssetManager.LoadAsync <Entity>("/global_data/gdc_demo/bg/bg1.hotei#");

            var caveEntityPrefab2 = await engineContext.AssetManager.LoadAsync <Entity>("/global_data/gdc_demo/bg/bg2.hotei#");

            var caveEntity1 = Prefab.Clone(caveEntityPrefab1);
            var caveEntity2 = Prefab.Clone(caveEntityPrefab2);

            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 magmaTexturePaths = new[]
            {
                "/global_data/gdc_demo/bg/GDC2012_map_maguma_04.dds",
                "/global_data/gdc_demo/bg/GDC2012_map_maguma_05.dds",
                "/global_data/gdc_demo/bg/GDC2012_map_maguma_06.dds",

                "/global_data/gdc_demo/bg/GDC2012_map_maguma_00.dds",
                "/global_data/gdc_demo/bg/GDC2012_map_maguma_01.dds",
                "/global_data/gdc_demo/bg/GDC2012_map_maguma_02.dds",
                "/global_data/gdc_demo/bg/GDC2012_map_maguma_03.dds",
                "/global_data/gdc_demo/bg/GDC2012_map_maguma_noise_00.dds",
                "/global_data/gdc_demo/bg/GDC2012_map_maguma_noise_01.dds",
                "/global_data/gdc_demo/bg/GDC2012_map_maguma_normals.dds",
            };
            var magmaTextures = new Texture2D[magmaTexturePaths.Length];

            for (int i = 0; i < magmaTextures.Length; i++)
            {
                magmaTextures[i] = (Texture2D)await engineContext.AssetManager.LoadAsync <Texture>(magmaTexturePaths[i]);
            }

            var lightVectors = new[]
            {
                new Vector3(-1.0f, 0.3f, -1.0f),
                new Vector3(1.0f, 0.0f, -1.0f),
                new Vector3(1.0f, 0.0f, -1.0f),
            };


            var random     = new Random(0);
            int planeIndex = 0;

            foreach (var entity in ParameterContainerExtensions.CollectEntityTree(caveEntity1))
            {
                var meshComponent = entity.Get(ModelComponent.Key);
                if (meshComponent == null)
                {
                    continue;
                }

                // Setup textures for magma
                if (entity.Name.StartsWith("maguma_"))
                {
                    meshComponent.MeshParameters.Set(LightKeys.LightDirection, lightVectors[planeIndex]);
                    meshComponent.MeshParameters.Set(ParameterKeys.IndexedKey(TexturingKeys.Texture0, 1), magmaTextures[planeIndex]);
                    planeIndex++;
                    for (int i = 3; i < magmaTextures.Length; i++)
                    {
                        meshComponent.MeshParameters.Set(ParameterKeys.IndexedKey(TexturingKeys.Texture0, i - 1), magmaTextures[i]);
                    }


                    foreach (var effectMesh in meshComponent.SubMeshes)
                    {
                        effectMesh.EffectData.Name = "Magma";
                    }

                    // Attach a bullet particle emitter to the magma
                    var emitter = new BulletParticleEmitterComponent()
                    {
                        //Count = 4096,
                        Count       = 16384,
                        Description = new BulletEmitterDescription()
                        {
                            Target     = new Vector3(-3016.261f, -70.52288f, 800.8788f),
                            BulletSize = 4.0f,
                            //MaxTimeTarget = 1000.0f + 5000.0f * (float)random.NextDouble(),
                            VelocityUp     = new Vector3(0, 0, 200),
                            MaxTimeUp      = 5000.0f,
                            MaxTimeTarget  = 20000.0f,
                            VelocityTarget = 200.0f,
                            Opacity        = 1.0f,
                            //DistanceDragonRepulse = 1200.0f,
                            //DecayDragonRepulse = 70.0f,
                            DistanceDragonRepulse = 600.0f,
                            DecayDragonRepulse    = 70.0f,
                            VelocityRepulse       = 200.0f,
                        },
                        RootAnimation = animationEntity,
                    };
                    emitter.OnAddToSystem(engineContext.EntityManager, engineContext.RenderContext);
                    emitter.OnUpdateData();
                    entity.Set(ParticleEmitterComponent.Key, emitter);
                }

                foreach (var effectMesh in meshComponent.SubMeshes)
                {
                    effectMesh.Parameters.Set(ParameterKeys.IndexedKey(TexturingKeys.DiffuseTexture, 3), (Texture2D)await engineContext.AssetManager.LoadAsync <Texture>("/global_data/gdc_demo/bg/GDC2012_map_dis_ao.dds"));
                }
            }

            await engineContext.EntityManager.AddEntityAsync(caveEntity1);

            await engineContext.EntityManager.AddEntityAsync(caveEntity2);

            foreach (var entity in ParameterContainerExtensions.CollectEntityTree(caveEntity1).Concat(ParameterContainerExtensions.CollectEntityTree(caveEntity2)))
            {
                var meshComponent = entity.Get(ModelComponent.Key);
                if (meshComponent == null)
                {
                    continue;
                }

                foreach (var effectMesh in meshComponent.InstantiatedSubMeshes)
                {
                    effectMesh.Value.Parameters.Set(MaterialKeys.SpecularIntensity, 2.0f);
                    effectMesh.Value.Parameters.Set(MaterialKeys.SpecularPower, 0.1f);
                }
            }
        }