/// <summary> /// Copy any referenced asset packages to the project's folder /// </summary> private static void CopyAssetPacks(SessionTemplateGeneratorParameters parameters) { var logger = parameters.Logger; var installDir = DirectoryHelper.GetInstallationDirectory("Xenko"); var assetPackagesDir = (DirectoryHelper.IsRootDevDirectory(installDir)) ? UDirectory.Combine(installDir, @"samples\Templates\Packs") : UDirectory.Combine(ProjectTemplateGeneratorHelper.GetTemplateDataDirectory(parameters.Description).GetParent(), @"Samples\Templates\Packs"); var assetPacks = parameters.TryGetTag(AssetsKey); if (assetPacks == null) { return; } try { var outAssetDir = UPath.Combine(parameters.OutputDirectory, (UDirectory)"Assets"); var outResourceDir = UPath.Combine(parameters.OutputDirectory, (UDirectory)"Resources"); foreach (var uDirectory in assetPacks) { var packageDir = UDirectory.Combine(assetPackagesDir, uDirectory); var inputAssetDir = UDirectory.Combine(packageDir, "Assets"); foreach (var directory in FileUtility.EnumerateDirectories(inputAssetDir, SearchDirection.Down)) { foreach (var file in directory.GetFiles()) { var relativeFile = new UFile(file.FullName).MakeRelative(inputAssetDir); var outputFile = UPath.Combine(outAssetDir, relativeFile); var outputFileDirectory = outputFile.GetParent(); if (!Directory.Exists(outputFileDirectory)) { Directory.CreateDirectory(outputFileDirectory); } File.Copy(file.FullName, outputFile, true); } } var inputResourceDir = UDirectory.Combine(packageDir, "Resources"); foreach (var directory in FileUtility.EnumerateDirectories(inputResourceDir, SearchDirection.Down)) { foreach (var file in directory.GetFiles()) { var relativeFile = new UFile(file.FullName).MakeRelative(inputResourceDir); var outputFile = UPath.Combine(outResourceDir, relativeFile); { // Create the output directory if needed var outputFileDirectory = outputFile.GetParent(); if (!Directory.Exists(outputFileDirectory)) { Directory.CreateDirectory(outputFileDirectory); } } File.Copy(file.FullName, outputFile, true); } } } } catch (Exception ex) { logger.Error("Unexpected exception while copying asset packages", ex); } }
/// <summary> /// Creates default scene, with a ground plane, sphere, directional light and camera. /// If graphicsProfile is 10+, add cubemap light, otherwise ambient light. /// Also properly setup graphics pipeline depending on if HDR is set or not /// </summary> /// <param name="parameters">The parameters.</param> /// <param name="package">The package in which to create these assets.</param> private void CreateAndSetNewScene(SessionTemplateGeneratorParameters parameters, Package package, ProjectReference projectGameReference, string projectGameName) { var logger = parameters.Logger; var graphicsProfile = parameters.GetTag(GraphicsProfileKey); var isHDR = parameters.GetTag(IsHDRKey); if (graphicsProfile < GraphicsProfile.Level_10_0) { isHDR = false; } // Create the material for the sphere var sphereMaterial = new MaterialAsset { Attributes = { Diffuse = new MaterialDiffuseMapFeature(new ComputeColor(Color.FromBgra(0xFF8C8C8C))), DiffuseModel = new MaterialDiffuseLambertModelFeature(), } }; if (isHDR) { // Create HDR part of material sphereMaterial.Attributes.Specular = new MaterialMetalnessMapFeature(new ComputeFloat(1.0f)); sphereMaterial.Attributes.SpecularModel = new MaterialSpecularMicrofacetModelFeature(); sphereMaterial.Attributes.MicroSurface = new MaterialGlossinessMapFeature(new ComputeFloat(0.65f)); } var sphereMaterialAssetItem = new AssetItem("Sphere Material", sphereMaterial); package.Assets.Add(sphereMaterialAssetItem); sphereMaterialAssetItem.IsDirty = true; // Create the sphere model var sphereModel = new ProceduralModelAsset { Type = new SphereProceduralModel { MaterialInstance = { Material = AttachedReferenceManager.CreateProxyObject <Material>(sphereMaterialAssetItem.Id, sphereMaterialAssetItem.Location) }, Tessellation = 30, }, }; var sphereModelAssetItem = new AssetItem("Sphere", sphereModel); package.Assets.Add(sphereModelAssetItem); sphereModelAssetItem.IsDirty = true; // Create sphere entity var sphereEntity = new Entity("Sphere") { new ModelComponent(AttachedReferenceManager.CreateProxyObject <Model>(sphereModelAssetItem.Id, sphereModelAssetItem.Location)) }; sphereEntity.Transform.Position = new Vector3(0.0f, 0.5f, 0.0f); // Create the material for the ground var groundMaterial = new MaterialAsset { Attributes = { Diffuse = new MaterialDiffuseMapFeature(new ComputeColor(Color.FromBgra(0xFF242424))), DiffuseModel = new MaterialDiffuseLambertModelFeature(), } }; if (isHDR) { // Create HDR part of material groundMaterial.Attributes.Specular = new MaterialMetalnessMapFeature(new ComputeFloat(0.0f)); groundMaterial.Attributes.SpecularModel = new MaterialSpecularMicrofacetModelFeature(); groundMaterial.Attributes.MicroSurface = new MaterialGlossinessMapFeature(new ComputeFloat(0.1f)); } var groundMaterialAssetItem = new AssetItem("Ground Material", groundMaterial); package.Assets.Add(groundMaterialAssetItem); groundMaterialAssetItem.IsDirty = true; // Create the ground model var groundModel = new ProceduralModelAsset { Type = new PlaneProceduralModel { Size = new Vector2(10.0f, 10.0f), MaterialInstance = { Material = AttachedReferenceManager.CreateProxyObject <Material>(groundMaterialAssetItem.Id, groundMaterialAssetItem.Location) }, }, }; var groundModelAssetItem = new AssetItem("Ground", groundModel); package.Assets.Add(groundModelAssetItem); groundModelAssetItem.IsDirty = true; // Create the ground entity var groundEntity = new Entity("Ground") { new ModelComponent(AttachedReferenceManager.CreateProxyObject <Model>(groundModelAssetItem.Id, groundModelAssetItem.Location)) }; // Copy file in Resources var skyboxFilename = (UFile)(isHDR ? "skybox_texture_hdr.dds" : "skybox_texture_ldr.dds"); try { var resources = UPath.Combine(parameters.OutputDirectory, (UDirectory)"Resources"); Directory.CreateDirectory(resources.ToWindowsPath()); // TODO: Hardcoded due to the fact that part of the template is in another folder in dev build // We might want to extend TemplateFolder to support those cases var dataDirectory = ProjectTemplateGeneratorHelper.GetTemplateDataDirectory(parameters.Description); var skyboxFullPath = UPath.Combine(dataDirectory, skyboxFilename).ToWindowsPath(); File.Copy(skyboxFullPath, UPath.Combine(resources, skyboxFilename).ToWindowsPath(), true); } catch (Exception ex) { logger.Error("Unexpected exception while copying cubemap", ex); } // Create the texture asset var skyboxTextureAsset = new TextureAsset { Source = Path.Combine(@"..\..\Resources", skyboxFilename), IsCompressed = isHDR, Type = new ColorTextureType { UseSRgbSampling = false } }; var skyboxTextureAssetItem = new AssetItem("Skybox texture", skyboxTextureAsset); package.Assets.Add(skyboxTextureAssetItem); skyboxTextureAssetItem.IsDirty = true; // Create the skybox asset var skyboxAsset = new SkyboxAsset { CubeMap = AttachedReferenceManager.CreateProxyObject <Texture>(skyboxTextureAssetItem.Id, skyboxTextureAssetItem.Location) }; var skyboxAssetItem = new AssetItem("Skybox", skyboxAsset); package.Assets.Add(skyboxAssetItem); skyboxAssetItem.IsDirty = true; // Create the scene var defaultSceneAsset = isHDR ? SceneHDRFactory.Create() : SceneLDRFactory.Create(); defaultSceneAsset.Hierarchy.Parts.Add(new EntityDesign(groundEntity)); defaultSceneAsset.Hierarchy.RootParts.Add(groundEntity); defaultSceneAsset.Hierarchy.Parts.Add(new EntityDesign(sphereEntity)); defaultSceneAsset.Hierarchy.RootParts.Add(sphereEntity); var sceneAssetItem = new AssetItem(GameSettingsAsset.DefaultSceneLocation, defaultSceneAsset); package.Assets.Add(sceneAssetItem); sceneAssetItem.IsDirty = true; // Sets the scene created as default in the shared profile var gameSettingsAsset = package.Assets.Find(GameSettingsAsset.GameSettingsLocation); if (gameSettingsAsset != null) { ((GameSettingsAsset)gameSettingsAsset.Asset).DefaultScene = AttachedReferenceManager.CreateProxyObject <Scene>(sceneAssetItem.Id, sceneAssetItem.Location); gameSettingsAsset.IsDirty = true; } var skyboxEntity = defaultSceneAsset.Hierarchy.Parts.Select(x => x.Value.Entity).Single(x => x.Name == SceneBaseFactory.SkyboxEntityName); skyboxEntity.Get <BackgroundComponent>().Texture = skyboxAsset.CubeMap; if (isHDR) { skyboxEntity.Get <LightComponent>().Type = new LightSkybox { Skybox = AttachedReferenceManager.CreateProxyObject <Skybox>(skyboxAssetItem.Id, skyboxAssetItem.Location) }; } var cameraEntity = defaultSceneAsset.Hierarchy.Parts.Select(x => x.Value.Entity).Single(x => x.Name == SceneBaseFactory.CameraEntityName); var graphicsCompositor = package.Assets.Select(x => x.Asset).OfType <GraphicsCompositorAsset>().Single(); cameraEntity.Components.Get <CameraComponent>().Slot = graphicsCompositor.Cameras.Single().ToSlotId(); // Let's add camera script CreateCameraScript(parameters, package, projectGameReference, projectGameName, cameraEntity, sceneAssetItem); }