Paradox Game Engine  v1.0.0 beta06
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Events Macros Pages
ModelAsset.cs
Go to the documentation of this file.
1 // Copyright (c) 2014 Silicon Studio Corp. (http://siliconstudio.co.jp)
2 // This file is distributed under GPL v3. See LICENSE.md for details.
3 using System;
4 using System.Collections.Generic;
5 using System.ComponentModel;
6 using System.Linq;
7 
8 using SharpYaml;
9 using SharpYaml.Serialization;
10 
11 using SiliconStudio.Assets;
12 using SiliconStudio.Assets.Compiler;
13 using SiliconStudio.Core;
14 using SiliconStudio.Core.Diagnostics;
15 using SiliconStudio.Core.Mathematics;
16 using SiliconStudio.Core.Yaml;
17 using SiliconStudio.Paradox.Effects;
18 
19 namespace SiliconStudio.Paradox.Assets.Model
20 {
21  [DataContract("Model")]
22  [AssetFileExtension(FileExtension)]
23  [AssetCompiler(typeof(ModelAssetCompiler))]
24  [ThumbnailCompiler(PreviewerCompilerNames.ModelThumbnailCompilerQualifiedName)]
25  [AssetFactory((Type)null)]
26  [AssetDescription("Model", "A 3D model", true)]
27  [AssetFormatVersion(AssetFormatVersion, typeof(Upgrader))]
28  public sealed class ModelAsset : AssetImportTracked
29  {
30  public const int AssetFormatVersion = 1;
31 
32  /// <summary>
33  /// The default file extension used by the <see cref="ModelAsset"/>.
34  /// </summary>
35  public const string FileExtension = ".pdxm3d";
36 
37  /// <summary>
38  /// Initializes a new instance of the <see cref="ModelAsset"/> class.
39  /// </summary>
40  public ModelAsset()
41  {
42  MeshParameters = new Dictionary<string, MeshMaterialParameters>();
43  Nodes = new List<NodeInformation>();
44  SetDefaults();
45  }
46 
47  /// <summary>
48  /// List that stores if a node should be preserved
49  /// </summary>
50  /// <userdoc>
51  /// The nodes of the model.
52  /// </userdoc>
53  [DataMember(20)]
54  public List<NodeInformation> Nodes { get; private set; }
55 
56  /// <summary>
57  /// Gets or sets the view direction to use when the importer is finding transparent polygons. Default is float3(0, 0, -1)
58  /// </summary>
59  /// <value>The view direction for transparent z sort.</value>
60  /// <userdoc>
61  /// The direction used to sort the polygons of the mesh.
62  /// </userdoc>
63  [DataMember(30)]
64  [DefaultValue(null)]
65  public Vector3? ViewDirectionForTransparentZSort { get; set; }
66 
67  /// <summary>
68  /// Gets or sets the axis representing the up axis of the object
69  /// </summary>
70  /// <userdoc>
71  /// The up axis of the model (for editor preview only).
72  /// </userdoc>
73  [DataMember(35)]
74  public Vector3 UpAxis { get; set; }
75 
76  /// <summary>
77  /// Gets or sets the axis representing the up axis of the object
78  /// </summary>
79  /// <userdoc>
80  /// The front axis of the model (for editor preview only).
81  /// </userdoc>
82  [DataMember(38)]
83  public Vector3 FrontAxis { get; set; }
84 
85  /// <summary>
86  /// The mesh parameters.
87  /// </summary>
88  /// <userdoc>
89  /// The list of all the meshes in the model.
90  /// </userdoc>
91  [DataMember(40)]
92  public Dictionary<string, MeshMaterialParameters> MeshParameters { get; private set; }
93 
94  /// <summary>
95  /// Gets or sets if the mesh will be compacted (meshes will be merged).
96  /// </summary>
97  [DataMemberIgnore]
98  public bool Compact
99  {
100  get
101  {
102  return Nodes.Any(x => !x.Preserve);
103  }
104  }
105 
106  /// <summary>
107  /// Returns to list of nodes that are preserved (they cannot be merged with other ones).
108  /// </summary>
109  /// <userdoc>
110  /// Checking nodes will garantee them to be available at runtime. Otherwise, it may be merged with their parents (for optimization purposes).
111  /// </userdoc>
112  [DataMemberIgnore]
113  public List<string> PreservedNodes
114  {
115  get
116  {
117  return Nodes.Where(x => x.Preserve).Select(x => x.Name).ToList();
118  }
119  }
120 
121  /// <summary>
122  /// Preserve the nodes.
123  /// </summary>
124  /// <param name="nodesToPreserve">List of nodes to preserve.</param>
125  public void PreserveNodes(List<string> nodesToPreserve)
126  {
127  foreach (var nodeName in nodesToPreserve)
128  {
129  foreach (var node in Nodes)
130  {
131  if (node.Name.Equals(nodeName))
132  node.Preserve = true;
133  }
134  }
135  }
136 
137  /// <summary>
138  /// No longer preserve any node.
139  /// </summary>
140  public void PreserveNoNode()
141  {
142  foreach (var node in Nodes)
143  node.Preserve = false;
144  }
145 
146  /// <summary>
147  /// Preserve all the nodes.
148  /// </summary>
149  public void PreserveAllNodes()
150  {
151  foreach (var node in Nodes)
152  node.Preserve = true;
153  }
154 
155  /// <summary>
156  /// Invert the preservation of the nodes.
157  /// </summary>
158  public void InvertPreservation()
159  {
160  foreach (var node in Nodes)
161  node.Preserve = !node.Preserve;
162  }
163 
164  public override void SetDefaults()
165  {
166  BuildOrder = 500;
167  UpAxis = Vector3.UnitY;
168  FrontAxis = Vector3.UnitZ;
169  if (Nodes != null)
170  Nodes.Clear();
171  }
172 
173  class Upgrader : AssetUpgraderBase
174  {
175  protected override void UpgradeAsset(ILogger log, dynamic asset)
176  {
177  foreach (var keyValue in asset.MeshParameters)
178  {
179  var parameters = asset.MeshParameters[keyValue.Key].Parameters["~Items"];
180  parameters.Node.Style = YamlStyle.Block;
181 
182  MoveToParameters(asset, parameters, keyValue.Key, "CastShadows", LightingKeys.CastShadows);
183  MoveToParameters(asset, parameters, keyValue.Key, "ReceiveShadows", LightingKeys.ReceiveShadows);
184  MoveToParameters(asset, parameters, keyValue.Key, "Layer", RenderingParameters.RenderLayer);
185  }
186 
187  // Get the Model, and generate an Id if the previous one wasn't the empty one
188  var emptyGuid = Guid.Empty.ToString().ToLowerInvariant();
189  var id = asset.Id;
190  if (id != null && id.Node.Value != emptyGuid)
191  asset.Id = Guid.NewGuid().ToString().ToLowerInvariant();
192 
193  // Bump asset version -- make sure it is stored right after Id
194  asset.SerializedVersion = AssetFormatVersion;
195  asset.MoveChild("SerializedVersion", asset.IndexOf("Id") + 1);
196  }
197 
198  public void MoveToParameters(dynamic asset, dynamic parameters, object key, string paramName, ParameterKey pk)
199  {
200  var paramValue = asset.MeshParameters[key][paramName];
201  if (paramValue != null)
202  {
203  parameters[pk.Name] = paramValue;
204  asset.MeshParameters[key].RemoveChild(paramName);
205  }
206  }
207  }
208  }
209 }
Key of an effect parameter.
Definition: ParameterKey.cs:15
ModelAsset()
Initializes a new instance of the ModelAsset class.
Definition: ModelAsset.cs:40
static readonly ParameterKey< RenderLayers > RenderLayer
The compact style (style embraced by [] or {})
Represents a three dimensional mathematical vector.
Definition: Vector3.cs:42
void PreserveAllNodes()
Preserve all the nodes.
Definition: ModelAsset.cs:149
override void SetDefaults()
Sets the defaults values for this instance
Definition: ModelAsset.cs:164
void PreserveNoNode()
No longer preserve any node.
Definition: ModelAsset.cs:140
static readonly ParameterKey< bool > ReceiveShadows
Flag stating if the mesh receives shadows.
Definition: LightingKeys.cs:76
An importable asset with a content that need to be tracked if original asset is changing.
static readonly ParameterKey< bool > CastShadows
Flag stating if the mesh casts shadows.
Definition: LightingKeys.cs:68
void PreserveNodes(List< string > nodesToPreserve)
Preserve the nodes.
Definition: ModelAsset.cs:125
Contains user-friendly names and descriptions of an asset type.
void InvertPreservation()
Invert the preservation of the nodes.
Definition: ModelAsset.cs:158
Interface for logging.
Definition: ILogger.cs:8
Collection of Mesh, each one usually being a different LOD of the same Model. The effect system will ...
Definition: Model.cs:23