/// <summary> /// Computes the bounding box for the specified xna model. /// </summary> public static BoundingBox ComputeBoundingBox(this Microsoft.Xna.Framework.Graphics.Model model) { ModelTag extensions = model.Tag as ModelTag; // Try to use collision tree. if (extensions != null && extensions.Collision != null) { return(extensions.Collision.CollisionTree.Bounds); } // Try to use vertices of the mesh BoundingBox result = new BoundingBox(); #if !SILVERLIGHT if (ComputeBoundingBoxFromVertices(model, out result)) { return(result); } #endif // Now use bounding spheres foreach (var mesh in model.Meshes) { BoundingBox box = BoundingBox.CreateFromSphere( mesh.BoundingSphere.Transform(GetAbsoluteTransform(mesh, Matrix.Identity))); result = BoundingBox.CreateMerged(result, box); } return(result); }
/// <summary> /// Gets the nearest intersection point from the specifed picking ray. /// </summary> /// <returns>Distance to the start of the ray.</returns> public static float?Intersects(this Microsoft.Xna.Framework.Graphics.Model model, Matrix world, Ray ray) { Ray local; ModelTag extensions = model.Tag as ModelTag; // Collision tree not found, use bounding sphere instead. if (extensions == null || extensions.Collision == null) { float?result = null; foreach (var mesh in model.Meshes) { local = ray.Transform(Matrix.Invert(GetAbsoluteTransform(mesh, world))); float?current = mesh.BoundingSphere.Intersects(local); if (result == null) { result = current; } else if (current.HasValue && current.Value < result.Value) { result = current.Value; } } return(result); } // Detect using collision tree. local = ray.Transform(Matrix.Invert(GetAbsoluteTransform(model.Meshes[0], world))); return(extensions.Collision.Intersects(local)); }
/// <summary> /// Gets the animation data associated with the specified model. /// Works with models that are processed by Nine.Pipeline.Processors.ExtendedModelProcessor. /// </summary> public static ICollection <string> GetAnimations(this Microsoft.Xna.Framework.Graphics.Model model) { ModelTag extensions = model.Tag as ModelTag; if (extensions != null && extensions.Animations != null) { return(extensions.Animations.Keys); } return(EmptyStringCollection); }
/// <summary> /// Gets the animation data associated with the specified model. /// Works with models that are processed by Nine.Pipeline.Processors.ExtendedModelProcessor. /// </summary> public static BoneAnimationClip GetAnimation(this Microsoft.Xna.Framework.Graphics.Model model, string name) { ModelTag extensions = model.Tag as ModelTag; if (extensions != null && extensions.Animations != null) { BoneAnimationClip clip = null; extensions.Animations.TryGetValue(name, out clip); return(clip); } return(null); }
/// <summary> /// Gets the animation data associated with the specified model. /// Works with models that are processed by Nine.Pipeline.Processors.ExtendedModelProcessor. /// </summary> public static BoneAnimationClip GetAnimation(this Microsoft.Xna.Framework.Graphics.Model model, int index) { BoneAnimationClip clip = null; ModelTag extensions = model.Tag as ModelTag; if (extensions != null && extensions.Animations != null && extensions.Animations.Count > 0) { foreach (string key in extensions.Animations.Keys) { if (index-- == 0) { extensions.Animations.TryGetValue(key, out clip); break; } } } return(clip); }
/// <summary> /// Gets wether the model contains the given point. /// </summary> public static bool Contains(this Microsoft.Xna.Framework.Graphics.Model model, Matrix world, Vector3 point) { Vector3 local; ModelTag extensions = model.Tag as ModelTag; // Collision tree not found, use bounding sphere instead. if (extensions == null || extensions.Collision == null) { foreach (var mesh in model.Meshes) { local = point - GetAbsoluteTransform(mesh, world).Translation; if (mesh.BoundingSphere.Contains(local) == ContainmentType.Contains) { return(true); } } return(false); } // Detect using collision tree. local = point - GetAbsoluteTransform(model.Meshes[0], world).Translation; return(extensions.Collision.Contains(local)); }
internal static ModelSkeletonData GetSkeletonData(this Microsoft.Xna.Framework.Graphics.Model model) { ModelTag extensions = model.Tag as ModelTag; return(extensions != null ? extensions.Skeleton : null); }
/// <summary> /// Gets whether the specified model has any skinning info attached to it. /// Works with models that are processed by Nine.Pipeline.Processors.ExtendedModelProcessor. /// </summary> public static bool IsSkinned(this Microsoft.Xna.Framework.Graphics.Model model) { ModelTag extensions = model.Tag as ModelTag; return(extensions != null && extensions.Skeleton != null); }