CustomModelContent.cs 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. #region File Description
  2. //-----------------------------------------------------------------------------
  3. // CustomModelContent.cs
  4. //
  5. // Microsoft XNA Community Game Platform
  6. // Copyright (C) Microsoft Corporation. All rights reserved.
  7. //-----------------------------------------------------------------------------
  8. #endregion
  9. #region Using Statements
  10. using System.Collections.Generic;
  11. using Microsoft.Xna.Framework.Graphics;
  12. using Microsoft.Xna.Framework.Content;
  13. using Microsoft.Xna.Framework.Content.Pipeline.Graphics;
  14. using Microsoft.Xna.Framework.Content.Pipeline.Processors;
  15. using Microsoft.Xna.Framework.Content.Pipeline.Serialization.Compiler;
  16. #endregion
  17. namespace CustomModelPipeline
  18. {
  19. /// <summary>
  20. /// Content Pipeline class provides a design time equivalent of the runtime
  21. /// CustomModel class. This stores the output from the CustomModelProcessor,
  22. /// right before it gets written into the XNB binary. This class is similar
  23. /// in shape to the runtime CustomModel, but stores the data as simple managed
  24. /// objects rather than GPU data types. This avoids us having to instantiate
  25. /// any actual GPU objects during the Content Pipeline build process, which
  26. /// is essential when building graphics for Xbox. The build always runs on
  27. /// Windows, and it would be problematic if we tried to instantiate Xbox
  28. /// types on the Windows GPU during this process!
  29. /// </summary>
  30. [ContentSerializerRuntimeType("CustomModelSample.CustomModel, CustomModelSample")]
  31. public class CustomModelContent
  32. {
  33. // Internally our custom model is made up from a list of model parts.
  34. [ContentSerializer]
  35. List<ModelPart> modelParts = new List<ModelPart>();
  36. // Each model part represents a piece of geometry that uses one single
  37. // effect. Multiple parts are needed to represent models that use more
  38. // than one effect.
  39. [ContentSerializerRuntimeType("CustomModelSample.CustomModel+ModelPart, CustomModelSample")]
  40. class ModelPart
  41. {
  42. public int TriangleCount;
  43. public int VertexCount;
  44. // These properties are not the same type as their equivalents in the
  45. // runtime CustomModel! Here, we are using design time managed classes,
  46. // while the runtime CustomModel uses actual GPU types. The Content
  47. // Pipeline knows about the relationship between the design time and
  48. // runtime types (thanks to the ContentTypeWriter.GetRuntimeType method),
  49. // so it can automatically translate one to the other. At design time we
  50. // use things like VertexBufferContent, IndexCollection and MaterialContent,
  51. // but when the serializer reads this data back at runtime, it will load
  52. // into the corresponding VertexBuffer, IndexBuffer, and Effect classes.
  53. public VertexBufferContent VertexBufferContent;
  54. public IndexCollection IndexCollection;
  55. // A single material instance may be shared by more than one ModelPart,
  56. // in which case we only want to write a single copy of the material
  57. // data into the XNB file. The SharedResource attribute tells the
  58. // serializer to take care of this merging for us.
  59. [ContentSerializer(SharedResource = true)]
  60. public MaterialContent MaterialContent;
  61. }
  62. /// <summary>
  63. /// Helper function used by the CustomModelProcessor
  64. /// to add new ModelPart information.
  65. /// </summary>
  66. public void AddModelPart(int triangleCount, int vertexCount,
  67. VertexBufferContent vertexBufferContent,
  68. IndexCollection indexCollection,
  69. MaterialContent materialContent)
  70. {
  71. ModelPart modelPart = new ModelPart();
  72. modelPart.TriangleCount = triangleCount;
  73. modelPart.VertexCount = vertexCount;
  74. modelPart.VertexBufferContent = vertexBufferContent;
  75. modelPart.IndexCollection = indexCollection;
  76. modelPart.MaterialContent = materialContent;
  77. modelParts.Add(modelPart);
  78. }
  79. }
  80. }