Paradox Game Engine  v1.0.0 beta06
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Events Macros Pages
Model.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;
5 using System.Collections.Generic;
6 using System.Collections.Specialized;
7 using SiliconStudio.Core;
8 using SiliconStudio.Core.Collections;
9 using SiliconStudio.Core.Mathematics;
10 using SiliconStudio.Core.ReferenceCounting;
11 using SiliconStudio.Core.Serialization.Converters;
12 using SiliconStudio.Paradox.Graphics;
13 
15 
16 namespace SiliconStudio.Paradox.Effects
17 {
18  /// <summary>
19  /// Collection of <see cref="Mesh"/>, each one usually being a different LOD of the same Model.
20  /// The effect system will select the appropriate LOD depending on distance, current pass, and other effect-specific requirements.
21  /// </summary>
22  [DataConverter(AutoGenerate = true, ContentReference = true)]
23  public class Model : IEnumerable
24  {
25  private readonly List<Mesh> meshes = new List<Mesh>();
26  private IList<Model> children;
27  private Model parent;
28 
29  /// <summary>
30  /// Gets the views.
31  /// </summary>
32  /// <value>
33  /// The views.
34  /// </value>
35  [DataMemberConvert]
36  public IList<Model> Children
37  {
38  get { return children; }
39  set { children = value; }
40  }
41 
42  /// <summary>
43  /// Gets the meshes.
44  /// </summary>
45  /// <value>
46  /// The meshes.
47  /// </value>
48  [DataMemberConvert]
49  public List<Mesh> Meshes
50  {
51  get { return meshes; }
52  }
53 
54  /// <summary>
55  /// Gets or sets the hierarchy definition, which describes nodes name, default transformation and hierarchical parent.
56  /// </summary>
57  /// <value>
58  /// The hierarchy, which describes nodes name, default transformation and hierarchical parent.
59  /// </value>
60  [DataMemberConvert]
61  public ModelViewHierarchyDefinition Hierarchy { get; set; }
62 
63  /// <summary>
64  /// Gets or sets the bounding box encompassing all the <see cref="Meshes"/> (not including animation).
65  /// </summary>
66  /// <value>
67  /// The bounding box.
68  /// </value>
69  [DataMemberConvert]
70  public BoundingBox BoundingBox { get; set; }
71 
72  // Temporarily removed
73  //[DataMemberConvert]
74  //public ParameterCollection Parameters
75  //{
76  // get { return parameters; }
77  //}
78 
79  /// <summary>
80  /// Adds the specified model view (for collection initializers).
81  /// </summary>
82  /// <param name="model">The model view.</param>
83  public void Add(Model model)
84  {
85  children.Add(model);
86  }
87 
88  /// <summary>
89  /// Adds the specified mesh (for collection initializers).
90  /// </summary>
91  /// <param name="mesh">The mesh.</param>
92  public void Add(Mesh mesh)
93  {
94  Meshes.Add(mesh);
95  }
96 
97  IEnumerator IEnumerable.GetEnumerator()
98  {
99  throw new NotImplementedException();
100  }
101 
102  public static Model FromGeometricMeshData(GraphicsDevice graphicsDevice, GeometricMeshData<VertexPositionNormalTexture> geometryMesh, string effectName = "Default")
103  {
104  var vertices = geometryMesh.Vertices;
105 
106  // compute the bounding box of the primitive
107  var boundingBox = new BoundingBox();
108  for (int i = 0; i < vertices.Length; i++)
109  BoundingBox.Merge(ref boundingBox, ref vertices[i].Position, out boundingBox);
110 
111  return FromGeometricMeshData(graphicsDevice, geometryMesh, boundingBox, VertexPositionNormalTexture.Layout, effectName);
112  }
113 
114  public static Model FromGeometricMeshData(GraphicsDevice graphicsDevice, GeometricMeshData<VertexPositionNormalTangentMultiTexture> geometryMesh, string effectName = "Default")
115  {
116  var vertices = geometryMesh.Vertices;
117 
118  // compute the bounding box of the primitive
119  var boundingBox = new BoundingBox();
120  for (int i = 0; i < vertices.Length; i++)
121  BoundingBox.Merge(ref boundingBox, ref vertices[i].Position, out boundingBox);
122 
123  return FromGeometricMeshData(graphicsDevice, geometryMesh, boundingBox, VertexPositionNormalTangentMultiTexture.Layout, effectName);
124  }
125 
126  public static Model FromGeometricMeshData<T>(GraphicsDevice graphicsDevice, GeometricMeshData<T> geometryMesh, BoundingBox boundingBox, VertexDeclaration layout, string effectName = "Default") where T : struct, IVertex
127  {
128  var meshDraw = new MeshDraw();
129 
130  var vertices = geometryMesh.Vertices;
131  var indices = geometryMesh.Indices;
132 
133  if (indices.Length < 0xFFFF)
134  {
135  var indicesShort = new ushort[indices.Length];
136  for (int i = 0; i < indicesShort.Length; i++)
137  {
138  indicesShort[i] = (ushort)indices[i];
139  }
140  meshDraw.IndexBuffer = new IndexBufferBinding(Buffer.Index.New(graphicsDevice, indicesShort).RecreateWith(indicesShort), false, indices.Length);
141  }
142  else
143  {
144  if (graphicsDevice.Features.Profile <= GraphicsProfile.Level_9_3)
145  {
146  throw new InvalidOperationException("Cannot generate more than 65535 indices on feature level HW <= 9.3");
147  }
148 
149  meshDraw.IndexBuffer = new IndexBufferBinding(Buffer.Index.New(graphicsDevice, indices).RecreateWith(indices), true, indices.Length);
150  }
151 
152  meshDraw.VertexBuffers = new[] { new VertexBufferBinding(Buffer.Vertex.New(graphicsDevice, vertices).RecreateWith(vertices), layout, vertices.Length) };
153 
154  meshDraw.DrawCount = indices.Length;
155  meshDraw.PrimitiveType = PrimitiveType.TriangleList;
156 
157  var mesh = new Mesh { Draw = meshDraw, BoundingBox = boundingBox };
158  mesh.Parameters.Set(RenderingParameters.RenderLayer, RenderLayers.RenderLayerAll);
159 
160  var model = new Model { BoundingBox = boundingBox };
161  model.Add(mesh);
162 
163  return model;
164  }
165 
166  /// <summary>
167  /// Create a clone with its own ParameterCollection.
168  /// It allows reuse of a single Model for multiple ModelComponent.
169  /// </summary>
171  {
172  var result = new Model();
173  if (Children != null)
174  {
175  result.Children = new List<Model>();
176  foreach (var child in Children)
177  {
178  result.Children.Add(child.Instantiate());
179  }
180  }
181 
182  foreach (var mesh in Meshes)
183  {
184  var meshCopy = new Mesh(mesh);
185  meshCopy.Parameters = meshCopy.Parameters.Clone();
186  result.Meshes.Add(meshCopy);
187  }
188 
189  result.Hierarchy = Hierarchy;
190  result.BoundingBox = BoundingBox;
191 
192  return result;
193  }
194 
195  private void children_CollectionChanged(object sender, TrackingCollectionChangedEventArgs e)
196  {
197  var child = (Model)e.Item;
198  switch (e.Action)
199  {
200  case NotifyCollectionChangedAction.Add:
201  if (child.parent != null)
202  throw new InvalidOperationException("Model already have a parent.");
203  child.parent = this;
204  break;
205  case NotifyCollectionChangedAction.Remove:
206  if (child.parent != this)
207  throw new InvalidOperationException("Model doesn't have expected parent.");
208  child.parent = null;
209  break;
210  }
211  }
212  }
213 }
static readonly VertexDeclaration Layout
The vertex layout of this struct.
Represents an axis-aligned bounding box in three dimensional space.
Definition: BoundingBox.cs:42
The layout of a vertex buffer with a set of VertexElement.
Model Instantiate()
Create a clone with its own ParameterCollection. It allows reuse of a single Model for multiple Model...
Definition: Model.cs:170
static Model FromGeometricMeshData(GraphicsDevice graphicsDevice, GeometricMeshData< VertexPositionNormalTexture > geometryMesh, string effectName="Default")
Definition: Model.cs:102
Describes hiderarchical nodes in a flattened array.
static readonly VertexDeclaration Layout
The vertex layout of this struct.
Base class for converters to/from a data type.
Describes a custom vertex format structure that contains position, color and 10 texture coordinates i...
Describes a custom vertex format structure that contains position, normal and texture information...
void Add(Mesh mesh)
Adds the specified mesh (for collection initializers).
Definition: Model.cs:92
All-in-One Buffer class linked SharpDX.Direct3D11.Buffer.
Performs primitive-based rendering, creates resources, handles system-level variables, adjusts gamma ramp levels, and creates shaders. See The+GraphicsDevice+class to learn more about the class.
static Model FromGeometricMeshData(GraphicsDevice graphicsDevice, GeometricMeshData< VertexPositionNormalTangentMultiTexture > geometryMesh, string effectName="Default")
Definition: Model.cs:114
static Buffer New(GraphicsDevice device, BufferDescription description, PixelFormat viewFormat=PixelFormat.None)
Creates a new Buffer instance.
Definition: Buffer.cs:343
switch(inFormat)
void Add(Model model)
Adds the specified model view (for collection initializers).
Definition: Model.cs:83
object Item
Gets the added or removed item (if dictionary, value only).
NotifyCollectionChangedAction Action
Gets the type of action performed. Allowed values are NotifyCollectionChangedAction.Add and NotifyCollectionChangedAction.Remove.
SiliconStudio.Paradox.Graphics.Buffer Buffer
Definition: Model.cs:14
GraphicsProfile
Identifies the set of supported devices for the demo based on device capabilities.
The base interface for all the vertex data structure.
Definition: IVertex.cs:9
Collection of Mesh, each one usually being a different LOD of the same Model. The effect system will ...
Definition: Model.cs:23
static void Merge(ref BoundingBox value1, ref Vector3 value2, out BoundingBox result)
Constructs a SiliconStudio.Core.Mathematics.BoundingBox that is as large enough to contains the bound...
Definition: BoundingBox.cs:303
Binding structure that specifies a vertex buffer and other per-vertex parameters (such as offset and ...