/// <summary> /// Import into the mesh, using the skeleton provided, and /// assigning the animation data to a new animation. /// </summary> /// <param name="transform">the world transform to apply to this object</param> /// <param name="mesh">the mesh we will populate</param> /// <param name="skeleton">the skeleton to which we will add animations (or null if we are creating one)</param> /// <param name="animationName">the name that will be used for the animation</param> /// <param name="materialNamespace">namespace used for generation of material names</param> public void Import(Matrix4 transform, Mesh mesh, Skeleton skeleton, string animationName, string materialNamespace) { ColladaMeshReader reader = null; XmlDocument document = new XmlDocument(); document.Load(m_Stream); XmlElement rootElement = document.DocumentElement; // This is slightly weird. The client calls this method on this object, // but then we determine which version of collada we're actually looking // at, and create a new instances of a derived collada reader. As an // outcome, we have to copy fields from the factory-created instance // back to this instance. // TODO: Need a static factory method on the base class to create the // collada reader instance, then call that instance from the client; // that way we'll only have one instance in the first place. reader = GetColladaParser(rootElement); reader.m_ColladaRootNode = rootElement; reader.m_Document = document; reader.m_MaterialBuilder = new MaterialScriptBuilder(materialNamespace); ColladaMeshInfo meshInfo = new ColladaMeshInfo(mesh); reader.ReadCollada(rootElement, meshInfo); meshInfo.NoRiggingCulling = NoRiggingCulling; meshInfo.Process(transform, skeleton, m_BaseFile, animationName); this.m_MaterialBuilder = reader.MaterialBuilder; }
/// <summary> /// Loads the mesh data. /// </summary> protected override void LoadImpl() { // meshLoadMeter.Enter(); // load this bad boy if it is not to be manually defined if (!isManual) { // get the resource data from MeshManager Stream data = MeshManager.Instance.FindResourceData(name); string extension = Path.GetExtension(name); // mesh loading stats int before, after; // get the tick count before loading the mesh before = Environment.TickCount; if (extension == ".mesh") { // instantiate a mesh reader and pass in the stream data MeshSerializer meshReader = new MeshSerializer(); // import the .mesh file meshReader.ImportMesh(data, this); } else if (extension == ".xml") { OgreXmlMeshReader meshReader = new OgreXmlMeshReader(data); // import the .xml file meshReader.Import(this); } else if (extension == ".dae") { ColladaMeshReader meshReader = new ColladaMeshReader(data, "tmp"); // import the .dae file meshReader.Import(this); } else { data.Close(); throw new AxiomException("Unsupported mesh format '{0}'", extension); } // get the tick count after loading the mesh after = Environment.TickCount; // record the time elapsed while loading the mesh log.InfoFormat("Mesh: Loaded '{0}', took {1}ms", this.name, (after - before)); // close the stream (we don't need to leave it open here) data.Close(); } // prepare the mesh for a shadow volume? if (MeshManager.Instance.PrepareAllMeshesForShadowVolumes) { if (edgeListsBuilt || autoBuildEdgeLists) { PrepareForShadowVolume(); } if (!edgeListsBuilt && autoBuildEdgeLists) { BuildEdgeList(); } } // meshLoadMeter.Exit(); }
private static Mesh ReadMesh(Matrix4 transform, string srcDir, string meshFile) { Stream meshData = new FileStream(srcDir + meshFile, FileMode.Open); Mesh mesh = new Mesh(meshFile); if (meshFile.EndsWith(".mesh", StringComparison.CurrentCultureIgnoreCase)) { MeshSerializer meshReader = new MeshSerializer(); meshReader.ImportMesh(meshData, mesh); } else if (meshFile.EndsWith(".mesh.xml", StringComparison.CurrentCultureIgnoreCase)) { OgreXmlMeshReader meshReader = new OgreXmlMeshReader(meshData); meshReader.Import(mesh); } else if (meshFile.EndsWith(".dae", StringComparison.CurrentCultureIgnoreCase)) { string extension = Path.GetExtension(meshFile); string baseFile = Path.GetFileNameWithoutExtension(meshFile); string basename = meshFile.Substring(0, meshFile.Length - extension.Length); ColladaMeshReader meshReader = new ColladaMeshReader(meshData, baseFile); // import the .dae file meshReader.Import(transform, mesh, null, "idle", basename); // materialScript = meshReader.MaterialScript; } else { meshData.Close(); string extension = Path.GetExtension(meshFile); throw new AxiomException("Unsupported mesh format '{0}'", extension); } meshData.Close(); return mesh; }
/// <summary> /// Loads the mesh data. /// </summary> protected override void LoadImpl() { // meshLoadMeter.Enter(); // load this bad boy if it is not to be manually defined if (!isManual) { // get the resource data from MeshManager Stream data = MeshManager.Instance.FindResourceData(name); string extension = Path.GetExtension(name); // mesh loading stats int before, after; // get the tick count before loading the mesh before = Environment.TickCount; if (extension == ".mesh") { // instantiate a mesh reader and pass in the stream data MeshSerializer meshReader = new MeshSerializer(); // import the .mesh file meshReader.ImportMesh(data, this); } else if (extension == ".xml") { OgreXmlMeshReader meshReader = new OgreXmlMeshReader(data); // import the .xml file meshReader.Import(this); } else if (extension == ".dae") { ColladaMeshReader meshReader = new ColladaMeshReader(data, "tmp"); // import the .dae file meshReader.Import(this); } else { data.Close(); throw new AxiomException("Unsupported mesh format '{0}'", extension); } // get the tick count after loading the mesh after = Environment.TickCount; // record the time elapsed while loading the mesh log.InfoFormat("Mesh: Loaded '{0}', took {1}ms", this.name, (after - before)); // close the stream (we don't need to leave it open here) data.Close(); } // prepare the mesh for a shadow volume? if (MeshManager.Instance.PrepareAllMeshesForShadowVolumes) { if (edgeListsBuilt || autoBuildEdgeLists) { PrepareForShadowVolume(); } if (!edgeListsBuilt && autoBuildEdgeLists) { BuildEdgeList(); } } // meshLoadMeter.Exit(); }
/// <summary> /// Utility method to merge animations from other files into a single skeleton /// </summary> /// <param name="srcDir">the directory from which the new animations will be loaded</param> /// <param name="dstDir">the directory to which the modified skeleton will be saved</param> /// <param name="skelFile">the name of the file to which the modified skeleton will be written</param> /// <param name="transform">the transform to apply to the skeleton and animations</param> /// <param name="skeleton">the original skeleton</param> /// <param name="animations">the list of animations</param> private static void AddAnimations( string srcDir, string dstDir, string skelFile, Matrix4 transform, Skeleton skeleton, List<AnimationEntry> animations ) { // mesh loading stats int before, after; // get the tick count before loading the mesh before = Environment.TickCount; foreach( AnimationEntry entry in animations ) { Mesh mesh = new Mesh( "Mesh" ); Stream data = new FileStream( srcDir + entry.animation_file, FileMode.Open ); ColladaMeshReader meshReader = new ColladaMeshReader( data, null ); // import the .dae file meshReader.Import( transform, mesh, skeleton, entry.animation_name, null ); // close the stream (we don't need to leave it open here) data.Close(); } // get the tick count after loading the mesh after = Environment.TickCount; // record the time elapsed while loading the mesh log.InfoFormat( "Mesh: took {0}ms", (after - before) ); //// prepare the mesh for a shadow volume? //if (MeshManager.Instance.PrepareAllMeshesForShadowVolumes) { // if (edgeListsBuilt || autoBuildEdgeLists) { // PrepareForShadowVolume(); // } // if (!edgeListsBuilt && autoBuildEdgeLists) { // BuildEdgeList(); // } //} OgreSkeletonSerializer skelWriter = new OgreSkeletonSerializer(); skelWriter.ExportSkeleton( skeleton, dstDir + skelFile ); }
// mesh will be null if this fails static void ImportKMZFile( ref string materialScript, Matrix4 transform, string dstDir, string meshFile, FileStream kmzStream, Mesh mesh ) { string daeFile = ExtractKMZComponentFiles( dstDir, kmzStream ); if( ! String.IsNullOrEmpty( daeFile ) ) { string materialNamespace = Path.GetFileNameWithoutExtension( daeFile ); string animationNamespace = materialNamespace; FileStream daeStream = new FileStream( daeFile, FileMode.Open ); ColladaMeshReader meshReader = new ColladaMeshReader( daeStream, animationNamespace ); // Convert from a left handed system where z is up to a right handed system where y is up. Matrix4 yupTrans = new Matrix4( 1, 0, 0, 0, 0, 0, 1, 0, 0, -1, 0, 0, 0, 0, 0, 1 ); Matrix4 yupTransform = transform * yupTrans; // import the .dae file meshReader.Import( yupTransform, mesh, null, "base", materialNamespace ); materialScript = meshReader.MaterialScript; } }