Paradox Game Engine  v1.0.0 beta06
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Events Macros Pages
SceneConverter.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.Linq;
6 using SiliconStudio.Paradox.Engine.Data;
7 using SiliconStudio.Paradox.EntityModel.Data;
8 using SiliconStudio.Core.IO;
9 using SiliconStudio.Core.Serialization.Assets;
10 
11 namespace SiliconStudio.Paradox.Engine
12 {
13  public static class SceneConverter
14  {
15  public static void ExportSceneData(EntityData entityData, string vfsOutputFilename, AssetManager assetManager, string effectName = null, string exportType = "entity", bool generateAEN = false)
16  {
17  object exportedObject;
18 
19  if (exportType == "animation")
20  {
21  throw new InvalidOperationException("Deprecated?");
22  //var animationData = ((AnimationComponentData)entityData.Components[AnimationComponent.Key]).Animations[0].AnimationData;
23  //exportedObject = animationData;
24  }
25  else if (exportType == "scenedata")
26  {
27  exportedObject = entityData;
28  }
29  else if (exportType == "entity")
30  {
31  var entityGroup = new EntityGroupData();
32 
33  // Additional processing
34  foreach (var node in EnumerateChildren(entityData))
35  {
36  entityGroup.Entities.Add(node);
37 
38  var modelComponent = node.Components.Values.OfType<ModelComponentData>().FirstOrDefault();
39  if (modelComponent == null)
40  continue;
41 
42  foreach (var effectMeshData in modelComponent.Model.Value.Meshes)
43  {
44  //if (effectMeshData.Value.DrawData != null)
45  //{
46  // //// Generate AEN adjacency index buffer (if requested)
47  // //if (generateAEN)
48  // //{
49  // // var subMeshDataAEN = effectMeshData.DrawData.Value.SubMeshDatas[Mesh.StandardSubMeshData].Clone();
50  // // subMeshDataAEN.GenerateIndexBufferAEN();
51  // // effectMeshData.DrawData.Value.SubMeshDatas["TessellationAEN"] = subMeshDataAEN;
52  // //}
53  // //
54  // //// Compact index buffer from 32 bits to 16 bits per index (if possible)
55  // //foreach (var subMeshData in effectMeshData.DrawData.Value.SubMeshDatas)
56  // //{
57  // // subMeshData.Value.CompactIndexBuffer();
58  // //}
59 
60  // // Force effect name (if requested)
61  // if (effectName != null)
62  // {
63  // effectMeshData.Value.EffectData.Value.Name = effectName;
64  // }
65  //}
66  }
67  }
68 
69  // Convert to Entity
70  exportedObject = entityGroup;
71  }
72  else
73  {
74  throw new InvalidOperationException("Unknown export type.");
75  }
76 
77  //contentManager = new ContentManager(new ContentSerializerContextGenerator(vfs, packageManager, ParameterContainerExtensions.DefaultSceneSerializer));
78  assetManager.Save(VirtualFileSystem.Drive.RootPath + vfsOutputFilename + "#/root", exportedObject);
79  }
80 
81  private static IEnumerable<EntityData> EnumerateChildren(EntityData nodeData)
82  {
83  // Enumerate self
84  yield return nodeData;
85 
86  // Apply on children
87  foreach (var child in ((TransformationComponentData)nodeData.Components[TransformationComponent.Key]).Children)
88  {
89  throw new NotImplementedException();
90  //foreach (var subChild in EnumerateChildren(child.Entity))
91  //{
92  // yield return subChild;
93  //}
94  }
95  }
96 
97  /*private static void Convert(EntityData entity, NodeData nodeData, Context sceneContext)
98  {
99  entity.Name = nodeData.Name;
100  sceneContext.NodeMapping[nodeData] = entity;
101 
102  var transformationComponent = new TransformationComponent();
103  entity.Set(TransformationComponent.Key, transformationComponent);
104 
105  // Create entity
106  foreach (var nodeProperty in nodeData.Properties)
107  {
108  if (nodeProperty is MeshData)
109  {
110  var effectMeshData = (MeshData)nodeProperty;
111  var ModelComponent = entity.GetOrCreate(ModelComponent.Key);
112  //ModelComponent.ContentManager = contentSerializerContext.ContentManager;
113  //ModelComponent.SubMeshes.Add(effectMeshData);
114  throw new NotImplementedException();
115 
116  sceneContext.EffectMeshMapping[effectMeshData] = ModelComponent;
117  }
118  else if (nodeProperty is CameraData)
119  {
120  var cameraData = (CameraData)nodeProperty;
121 
122  var cameraComponent = entity.GetOrCreate(CameraComponent.Key);
123  cameraComponent.NearPlane = cameraData.NearPlane;
124  cameraComponent.FarPlane = cameraData.FarPlane;
125  cameraComponent.AspectRatio = cameraData.AspectRatio;
126  cameraComponent.VerticalFieldOfView = cameraData.VerticalFieldOfView;
127  }
128  else if (nodeProperty is LightData)
129  {
130  var lightData = (LightData)nodeProperty;
131  var transformationData = nodeData.Properties.OfType<TransformationTRSData>().FirstOrDefault();
132 
133  var lightComponent = entity.GetOrCreate(LightComponent.Key);
134  lightComponent.Type = lightData.Type;
135  lightComponent.Color = lightData.Color;
136  lightComponent.Intensity = lightData.Intensity;
137  lightComponent.DecayStart = lightData.DecayStart;
138  if (transformationData != null)
139  {
140  lightComponent.LightDirection = lightData.LightDirection;
141  }
142  lightComponent.Deferred = lightData.Deferred;
143  }
144  else if (nodeProperty is TransformationMatrixData)
145  {
146  var transformationData = (TransformationMatrixData)nodeProperty;
147  transformationComponent.Value = new TransformationMatrix { Matrix = transformationData.Transformation };
148  }
149  else if (nodeProperty is TransformationTRSData)
150  {
151  var transformationData = (TransformationTRSData)nodeProperty;
152  transformationComponent.Value = new TransformationTRS
153  {
154  Translation = transformationData.Translation,
155  Rotation = transformationData.Rotation,
156  Scaling = transformationData.Scaling,
157  };
158  }
159  }
160 
161  // Recursively create children
162  foreach (var childNodeData in nodeData.Children)
163  {
164  var childEntity = new Entity();
165  Convert(childEntity, childNodeData, sceneContext);
166 
167  // Add node hierarchical relationship
168  transformationComponent.Children.Add(childEntity.Transformation);
169  }
170  }
171 
172  public static EntityData Convert(SceneData sceneData)
173  {
174  // Convert to "Entity"
175  var rootEntity = new EntityData();
176  rootEntity.Components = new Dictionary<Core.PropertyKey, EntityComponentData>();
177  var animationComponent = new AnimationComponentData();
178  rootEntity.Components.Add(AnimationComponent.Key, animationComponent);
179 
180  var sceneContext = new Context();
181 
182  // Recursively walk through the graph and create Entity and nodes
183  Convert(rootEntity, sceneData.RootNode, sceneContext);
184 
185  // Process animation
186  var animation = sceneData.Animation;
187  if (animation != null)
188  {
189  animationComponent.Animations.Add(new PlayingAnimation(AnimationData.FromAnimationChannels(animation.AnimationChannels)) { Weight = 0.0f });
190  }
191 
192  // Process skinning
193  foreach (var node in sceneContext.NodeMapping)
194  {
195  var skinningData = node.Key.Properties.OfType<SkinningData>().FirstOrDefault();
196  if (skinningData != null)
197  {
198  var entity = node.Value;
199  var skinningComponent = new SkinningComponent();
200  foreach (var clusterData in skinningData.Clusters)
201  {
202  var cluster = new SkinningCluster();
203  cluster.Link = sceneContext.NodeMapping[clusterData.Link].Transformation;
204  cluster.LinkToMeshMatrix = clusterData.MeshMatrix * Matrix.Invert(clusterData.LinkMatrix);
205 
206  skinningComponent.Clusters.Add(cluster);
207  }
208  entity.Set(SkinningComponent.Key, skinningComponent);
209  }
210 
211  var cameraData = node.Key.Properties.OfType<CameraData>().FirstOrDefault();
212  if (cameraData != null)
213  {
214  var entity = node.Value;
215  var cameraComponent = entity.Get(CameraComponent.Key);
216  if(cameraData.Target != null)
217  cameraComponent.Target = sceneContext.NodeMapping[cameraData.Target];
218  }
219  }
220 
221  return rootEntity;
222  }*/
223  }
224 }
static void ExportSceneData(EntityData entityData, string vfsOutputFilename, AssetManager assetManager, string effectName=null, string exportType="entity", bool generateAEN=false)
Data type for SiliconStudio.Paradox.Engine.TransformationComponent.
Definition: EngineData.cs:701
Defines Position, Rotation and Scale of its Entity.
Data type for SiliconStudio.Paradox.EntityModel.Entity.
Definition: EngineData.cs:739
Data type for SiliconStudio.Paradox.Engine.ModelComponent.
Definition: EngineData.cs:674
Data type for SiliconStudio.Paradox.EntityModel.EntityGroup.
Definition: EngineData.cs:644
static PropertyKey< TransformationComponent > Key