Advertisement
Guest User

CollisionContentProcessor.cs - WindowsPhoneDemoGame

a guest
Apr 12th, 2010
524
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.72 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using Microsoft.Xna.Framework;
  5. using Microsoft.Xna.Framework.Graphics;
  6. using Microsoft.Xna.Framework.Content.Pipeline;
  7. using Microsoft.Xna.Framework.Content.Pipeline.Graphics;
  8. using Microsoft.Xna.Framework.Content.Pipeline.Processors;
  9.  
  10.  
  11. namespace CollisionContentProcessor
  12. {
  13.     [ContentProcessor]
  14.     public class ModelCollisionProcessor : ModelProcessor
  15.     {
  16.         List<Vector3> vertices = new List<Vector3>();
  17.  
  18.  
  19.         /// <summary>
  20.         /// The main method in charge of processing the content.
  21.         /// </summary>
  22.         public override ModelContent Process(NodeContent input,
  23.                                              ContentProcessorContext context)
  24.         {
  25.             // Look up the input vertex positions.
  26.             FindVertices(input);
  27.  
  28.             // Chain to the base ModelProcessor class.
  29.             ModelContent model = base.Process(input, context);
  30.  
  31.             // You can store any type of object in the model Tag property. This
  32.             // sample only uses built-in types such as string, Vector3, BoundingSphere,
  33.             // dictionaries, and arrays, which the content pipeline knows how to
  34.             // serialize by default. We could also attach custom data types here, but
  35.             // then we would have to provide a ContentTypeWriter and ContentTypeReader
  36.             // implementation to tell the pipeline how to serialize our custom type.
  37.             //
  38.             // We are setting our model Tag to a dictionary that maps strings to
  39.             // objects, and then storing two different kinds of custom data into that
  40.             // dictionary. This is a useful pattern because it allows processors to
  41.             // combine many different kinds of information inside the single Tag value.
  42.  
  43.             Dictionary<string, object> tagData = new Dictionary<string, object>();
  44.  
  45.             model.Tag = tagData;
  46.  
  47.             // Store vertex information in the tag data, as an array of Vector3.
  48.             tagData.Add("Vertices", vertices.ToArray());
  49.  
  50.             // Also store a custom bounding sphere.
  51.             tagData.Add("BoundingSphere", BoundingSphere.CreateFromPoints(vertices));
  52.  
  53.             return model;
  54.         }
  55.  
  56.  
  57.         /// <summary>
  58.         /// Helper for extracting a list of all the vertex positions in a model.
  59.         /// </summary>
  60.         void FindVertices(NodeContent node)
  61.         {
  62.             // Is this node a mesh?
  63.             MeshContent mesh = node as MeshContent;
  64.  
  65.             if (mesh != null)
  66.             {
  67.                 // Look up the absolute transform of the mesh.
  68.                 Matrix absoluteTransform = mesh.AbsoluteTransform;
  69.  
  70.                 // Loop over all the pieces of geometry in the mesh.
  71.                 foreach (GeometryContent geometry in mesh.Geometry)
  72.                 {
  73.                     // Loop over all the indices in this piece of geometry.
  74.                     // Every group of three indices represents one triangle.
  75.                     foreach (int index in geometry.Indices)
  76.                     {
  77.                         // Look up the position of this vertex.
  78.                         Vector3 vertex = geometry.Vertices.Positions[index];
  79.  
  80.                         // Transform from local into world space.
  81.                         vertex = Vector3.Transform(vertex, absoluteTransform);
  82.  
  83.                         // Store this vertex.
  84.                         vertices.Add(vertex);
  85.                     }
  86.                 }
  87.             }
  88.  
  89.             // Recursively scan over the children of this node.
  90.             foreach (NodeContent child in node.Children)
  91.             {
  92.                 FindVertices(child);
  93.             }
  94.         }
  95.     }
  96. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement