4 using System.Collections.Generic;
7 using SiliconStudio.Assets;
8 using SiliconStudio.Core.Diagnostics;
9 using SiliconStudio.Core.Extensions;
10 using SiliconStudio.Core.IO;
11 using SiliconStudio.Core.Mathematics;
12 using SiliconStudio.Core.Serialization;
13 using SiliconStudio.Paradox.Assets.Materials;
14 using SiliconStudio.Paradox.Assets.Materials.Nodes;
15 using SiliconStudio.Paradox.Assets.Materials.Processor.Visitors;
16 using SiliconStudio.Paradox.Assets.Texture;
17 using SiliconStudio.Paradox.Data;
18 using SiliconStudio.Paradox.Effects;
19 using SiliconStudio.Paradox.Effects.Data;
20 using SiliconStudio.Paradox.Engine;
21 using SiliconStudio.Paradox.Engine.Data;
22 using SiliconStudio.Paradox.EntityModel.Data;
23 using SiliconStudio.Paradox.Importer.Common;
38 parameters.SelectedOutputTypes[typeof(
EntityAsset)] =
false;
61 var entityInfo = GetEntityInfo(localPath, importParameters.
Logger);
63 var isImportingEntity = importParameters.IsTypeSelectedForOutput<
EntityAsset>();
65 var isImportingModel = importParameters.IsTypeSelectedForOutput<
ModelAsset>() ||
68 var isImportingMaterial = importParameters.IsTypeSelectedForOutput<
MaterialAsset>() ||
71 var isImportingTexture = importParameters.IsTypeSelectedForOutput<
TextureAsset>() ||
74 var isImportingCamera = importParameters.IsTypeSelectedForOutput<CameraAsset>();
76 var isImportingLight = importParameters.IsTypeSelectedForOutput<LightAsset>();
80 if (isImportingTexture)
82 ImportTextures(assetReferences, localPath, entityInfo.TextureDependencies);
88 ImportAnimation(assetReferences, localPath, entityInfo.AnimationNodes);
92 if (isImportingMaterial)
94 ImportMaterials(assetReferences, localPath, entityInfo.Materials);
100 var modelItem = ImportModel(assetReferences, localPath, localPath, entityInfo);
103 if (isImportingEntity)
105 var entityAsset = ImportEntity(assetReferences, localPath, modelItem, entityInfo);
108 if (isImportingCamera)
109 ImportCameras(assetReferences, localPath, entityInfo, entityAsset, modelItem);
112 if (isImportingLight)
113 ImportLights(assetReferences, localPath, entityInfo, entityAsset, modelItem);
117 return assetReferences;
122 if (entityInfo.
Lights == null)
125 foreach (var light
in entityInfo.
Lights)
127 var lightUrl =
new UFile(localPath.
GetFileName() +
"_light_" + light.NodeName, null);
129 var cameraEntityAsset = CreateTrackingEntity(entityAsset, modelAsset, lightUrl, light.NodeName);
130 ((
EntityAsset)cameraEntityAsset.Asset).Data.Components.Add(LightComponent.Key, light.Data);
132 assetReferences.Add(cameraEntityAsset);
138 if (entityInfo.
Cameras == null)
141 foreach (var camera
in entityInfo.
Cameras)
143 var cameraUrl =
new UFile(localPath.
GetFileName() +
"_camera_" + camera.NodeName, null);
145 var cameraEntityAsset = CreateTrackingEntity(entityAsset, modelAsset, cameraUrl, camera.NodeName);
146 ((EntityAsset)cameraEntityAsset.Asset).Data.Components.Add(CameraComponent.Key, camera.Data);
148 if (camera.TargetNodeName != null)
151 var cameraTargetUrl =
new UFile(localPath.
GetFileName() +
"_cameratarget_" + camera.TargetNodeName, null);
152 var cameraTargetEntityAsset = CreateTrackingEntity(entityAsset, modelAsset, cameraTargetUrl, camera.TargetNodeName);
157 assetReferences.Add(cameraTargetEntityAsset);
160 assetReferences.Add(cameraEntityAsset);
166 var entity =
new EntityData { Name = nodeName };
176 Target = EntityComponentReference.New(rootEntityAsset.Id, rootEntityAsset.Location, ModelComponent.Key),
179 var asset =
new EntityAsset { Data = entity };
180 var entityAsset =
new AssetItem(cameraUrl, asset);
182 var parentEntity = (EntityAsset)rootEntityAsset.
Asset;
189 parentEntity.Data.Components.Add(TransformationComponent.Key, entityComponentData);
193 ((ModelAsset)modelAsset.
Asset).PreserveNodes(
new List<string> { nodeName });
197 EntityComponentReference.New(entityAsset.Id, entityAsset.Location, TransformationComponent.Key));
207 var asset =
new EntityAsset();
208 asset.Data.Name = entityUrl;
212 var assetReference =
new AssetItem(entityUrl, asset);
213 assetReferences.Add(assetReference);
215 return assetReference;
218 private static void ImportAnimation(
List<AssetItem> assetReferences,
UFile localPath, List<string> animationNodes)
220 if (animationNodes != null && animationNodes.Count > 0)
222 var assetSource = localPath;
225 var asset =
new AnimationAsset {
Source = assetSource };
227 assetReferences.Add(
new AssetItem(animUrl, asset));
233 var frontAxis = Vector3.Cross(entityInfo.UpAxis, Vector3.UnitZ).Length() < MathUtil.ZeroTolerance ? Vector3.UnitY : Vector3.UnitZ;
234 var asset =
new ModelAsset {
Source = assetSource, UpAxis = entityInfo.UpAxis, FrontAxis = frontAxis };
236 if (entityInfo.
Models != null)
238 var loadedMaterials = assetReferences.Where(x => x.Asset is
MaterialAsset).ToList();
239 foreach (var model
in entityInfo.
Models)
241 var meshParams =
new MeshMaterialParameters { Parameters = model.Parameters, NodeName = model.NodeName };
243 var matId = model.MaterialName;
244 var matName = GenerateFinalMaterialName(localPath, matId);
245 var foundMaterial = loadedMaterials.FirstOrDefault(x => x.Location ==
new UFile(matName, null));
246 if (foundMaterial != null)
249 meshParams.Material = matReference;
251 asset.MeshParameters.Add(model.MeshName, meshParams);
255 if (entityInfo.
Nodes != null)
257 foreach (var node
in entityInfo.
Nodes)
258 asset.Nodes.Add(
new NodeInformation(node.Name, node.Depth, node.Preserve));
262 asset.PreserveNodes(entityInfo.AnimationNodes);
265 var assetItem =
new AssetItem(modelUrl, asset);
266 assetReferences.Add(assetItem);
270 private static void ImportMaterials(
List<AssetItem> assetReferences,
UFile localPath, Dictionary<string, MaterialDescription> materials)
272 if (materials != null)
274 var assetSource = localPath;
275 var loadedTextures = assetReferences.Where(x => x.Asset is
TextureAsset).ToList();
277 foreach (var materialKeyValue
in materials)
279 AdjustForTransparency(materialKeyValue.Value);
280 var material = materialKeyValue.Value;
281 var materialUrl =
new UFile(GenerateFinalMaterialName(localPath, materialKeyValue.Key), null);
286 var textures = textureVisitor.GetAllTextureValues();
287 foreach (var texture
in textures)
290 var foundTexture = loadedTextures.FirstOrDefault(x => x.Location == GenerateFinalTextureUrl(localPath, texture.TextureReference.Location));
291 if (foundTexture != null)
295 var assetReference =
new AssetItem(materialUrl, asset);
296 assetReferences.Add(assetReference);
310 var diffuseNode = material.GetMaterialNode(MaterialParameters.AlbedoDiffuse);
313 var diffuseNodeName = material.ColorNodes[MaterialParameters.AlbedoDiffuse];
314 var transparentNodeName = material.ColorNodes[MaterialParameters.TransparencyMap];
316 var transparentNode = material.GetMaterialNode(MaterialParameters.TransparencyMap);
318 if (diffuseNode == null || transparentNode == null)
321 var foundTextureDiffuse = FindTextureNode(material, diffuseNodeName);
322 var foundTextureTransparent = FindTextureNode(material, transparentNodeName);
324 if (foundTextureDiffuse != null && foundTextureTransparent != null)
326 if (foundTextureDiffuse != foundTextureTransparent)
329 material.AddColorNode(MaterialParameters.AlbedoDiffuse,
"pdx_diffuseWithAlpha", alphaMixNode);
334 material.Parameters.Set(MaterialParameters.UseTransparent,
true);
339 var isTransparent =
false;
341 isTransparent = (
bool)material.Parameters[MaterialParameters.UseTransparent];
346 var diffuseName = material.ColorNodes[MaterialParameters.AlbedoDiffuse];
347 material.Nodes.Remove(diffuseName);
351 material.AddColorNode(MaterialParameters.AlbedoDiffuse,
"pdx_diffuseOpaque", opaqueNode);
365 var currentNode = material.FindNode(startNode);
368 var currentReferenceNode = (MaterialReferenceNode)currentNode;
369 currentNode = material.FindNode(currentReferenceNode.Name);
375 private static string GenerateFinalMaterialName(
UFile localPath,
string materialId)
377 return localPath.GetFileName() +
"_material_" + materialId;
380 private static UFile GenerateFinalTextureUrl(
UFile localPath,
string textureName)
385 private static void ImportTextures(
List<AssetItem> assetReferences,
UFile localPath, List<string> dependentTextures)
387 if (dependentTextures != null)
390 foreach (var textureFullPath
in dependentTextures.Distinct(x => x))
392 ImportTexture(assetReferences, localPath, textureFullPath);
397 private static void ImportTexture(
List<AssetItem> assetReferences,
UFile localPath,
string textureFullPath)
399 var texturePath =
new UFile(textureFullPath);
401 var source = texturePath;
405 var textureUrl = GenerateFinalTextureUrl(localPath, texturePath.
GetFileName());
408 assetReferences.Add(
new AssetItem(textureUrl, texture));
415 class CameraAsset :
Asset
424 class LightAsset :
Asset
override AssetImporterParameters GetDefaultParameters(bool isForReImport)
Gets the default parameters for this importer.
A node that describe a binary operation between two IMaterialNode
Description of a material.
static readonly ParameterKey< ShaderMixinSource > AlbedoDiffuse
bool IsTypeSelectedForOutput(Type type)
Determines whether the specified type is type selected for output by this importer.
Description of a material.
Parameters used by IAssetImporter.Import
Logger Logger
Gets or sets the logger to use during the import.
An asset item part of a Package accessible through SiliconStudio.Assets.Package.Assets.
override IEnumerable< AssetItem > Import(UFile localPath, AssetImporterParameters importParameters)
Imports the model.
Base implementation for ILogger.
Dictionary< ParameterKey< ShaderMixinSource >, string > ColorNodes
The tree used in this model.
MaterialBinaryOperand
Operands of the MaterialNode.
string GetFileName()
Gets the name of the file without its extension. Can be null.
List< CameraInfo^> Cameras
List< String^> AnimationNodes
Data type for SiliconStudio.Paradox.EntityModel.Entity.
Data type for SiliconStudio.Paradox.Engine.ModelComponent.
Describes a texture asset.
static readonly ParameterKey< bool > UseTransparent
Data type for SiliconStudio.Paradox.EntityModel.EntityComponent.
ParameterCollectionData Parameters
The parameters of this model.
List< MeshParameters^> Models
Contains user-friendly names and descriptions of an asset type.
static readonly ParameterKey< ShaderMixinSource > TransparencyMap
Data type for SiliconStudio.Paradox.Engine.ModelNodeLinkComponent.
Asset Asset
Gets or sets the asset.
Collection of Mesh, each one usually being a different LOD of the same Model. The effect system will ...
HRESULT PremultiplyAlpha(_In_ const Image &srcImage, _In_ DWORD flags, _Out_ ScratchImage &image)
Defines a normalized file path. See UPath for details. This class cannot be inherited.