private ModelMeshContent ProcessMesh(MeshContent mesh, ModelBoneContent parent, ContentProcessorContext context) { var bounds = new BoundingSphere(); var parts = new List <ModelMeshPartContent>(); var vertexBuffer = new VertexBufferContent(); var indexBuffer = new IndexCollection(); var startVertex = 0; foreach (var geometry in mesh.Geometry) { var vertices = geometry.Vertices; var vertexCount = vertices.VertexCount; var geomBuffer = geometry.Vertices.CreateVertexBuffer(); vertexBuffer.Write(vertexBuffer.VertexData.Length, 1, geomBuffer.VertexData); var startIndex = indexBuffer.Count; indexBuffer.AddRange(geometry.Indices); var partContent = new ModelMeshPartContent(vertexBuffer, indexBuffer, startVertex, vertexCount, startIndex, geometry.Indices.Count / 3); partContent.Material = geometry.Material; parts.Add(partContent); // Update mesh bounding box bounds = BoundingSphere.CreateMerged(bounds, BoundingSphere.CreateFromPoints(geometry.Vertices.Positions)); // Geoms are supposed to all have the same decl, so just steal one of these vertexBuffer.VertexDeclaration = geomBuffer.VertexDeclaration; startVertex += vertexCount; } return(new ModelMeshContent(mesh.Name, mesh, parent, bounds, parts)); }
internal ModelMeshContent(string name, MeshContent sourceMesh, ModelBoneContent parentBone, BoundingSphere boundingSphere, IList <ModelMeshPartContent> meshParts) { _name = name; _sourceMesh = sourceMesh; _parentBone = parentBone; _boundingSphere = boundingSphere; _meshParts = new ModelMeshPartContentCollection(meshParts); }
internal ModelMeshContent(string name, MeshContent sourceMesh, ModelBoneContent parentBone, BoundingSphere boundingSphere, IList<ModelMeshPartContent> meshParts) { _name = name; _sourceMesh = sourceMesh; _parentBone = parentBone; _boundingSphere = boundingSphere; _meshParts = new ModelMeshPartContentCollection(meshParts); }
public override ModelContent Process(NodeContent input, ContentProcessorContext context) { _identity = input.Identity; _logger = context.Logger; // Gather all the nodes in tree traversal order. var nodes = input.AsEnumerable().SelectDeep(n => n.Children).ToList(); var meshes = nodes.FindAll(n => n is MeshContent).Cast <MeshContent>().ToList(); var geometries = meshes.SelectMany(m => m.Geometry).ToList(); var distinctMaterials = geometries.Select(g => g.Material).Distinct().ToList(); // Loop through all distinct materials, passing them through the conversion method // only once, and then processing all geometries using that material. foreach (var inputMaterial in distinctMaterials) { var geomsWithMaterial = geometries.Where(g => g.Material == inputMaterial).ToList(); var material = ConvertMaterial(inputMaterial, context); ProcessGeometryUsingMaterial(material, geomsWithMaterial, context); } // Hierarchy var bones = nodes.OfType <BoneContent>().ToList(); var modelBones = new List <ModelBoneContent>(); for (var i = 0; i < bones.Count; i++) { var bone = bones[i]; // Find the parent var parentIndex = bones.IndexOf(bone.Parent as BoneContent); ModelBoneContent parent = null; if (parentIndex > -1) { parent = modelBones[parentIndex]; } modelBones.Add(new ModelBoneContent(bone.Name, i, bone.Transform, parent)); } foreach (var bone in modelBones) { bone.Children = new ModelBoneContentCollection(modelBones.FindAll(b => b.Parent == bone)); } return(new ModelContent(modelBones[0], modelBones, _meshes)); }
private ModelBoneContent ProcessNode(NodeContent node, ModelBoneContent parent, List <ModelBoneContent> boneList, List <ModelMeshContent> meshList, ContentProcessorContext context) { var result = new ModelBoneContent(node.Name, boneList.Count, node.Transform, parent); boneList.Add(result); if (node is MeshContent) { meshList.Add(ProcessMesh(node as MeshContent, result, context)); } var children = new List <ModelBoneContent>(); foreach (var child in node.Children) { children.Add(ProcessNode(child, result, boneList, meshList, context)); } result.Children = new ModelBoneContentCollection(children); return(result); }
private ModelMeshContent ProcessMesh(MeshContent mesh, ModelBoneContent parent, ContentProcessorContext context) { var parts = new List <ModelMeshPartContent>(); var vertexBuffer = new VertexBufferContent(); var indexBuffer = new IndexCollection(); if (GenerateTangentFrames) { context.Logger.LogMessage("Generating tangent frames."); foreach (GeometryContent geom in mesh.Geometry) { if (!geom.Vertices.Channels.Contains(VertexChannelNames.Normal(0))) { MeshHelper.CalculateNormals(geom, true); } if (!geom.Vertices.Channels.Contains(VertexChannelNames.Tangent(0)) || !geom.Vertices.Channels.Contains(VertexChannelNames.Binormal(0))) { MeshHelper.CalculateTangentFrames(geom, VertexChannelNames.TextureCoordinate(0), VertexChannelNames.Tangent(0), VertexChannelNames.Binormal(0)); } } } var startVertex = 0; foreach (var geometry in mesh.Geometry) { var vertices = geometry.Vertices; var vertexCount = vertices.VertexCount; ModelMeshPartContent partContent; if (vertexCount == 0) { partContent = new ModelMeshPartContent(); } else { var geomBuffer = geometry.Vertices.CreateVertexBuffer(); vertexBuffer.Write(vertexBuffer.VertexData.Length, 1, geomBuffer.VertexData); var startIndex = indexBuffer.Count; indexBuffer.AddRange(geometry.Indices); partContent = new ModelMeshPartContent(vertexBuffer, indexBuffer, startVertex, vertexCount, startIndex, geometry.Indices.Count / 3); // Geoms are supposed to all have the same decl, so just steal one of these vertexBuffer.VertexDeclaration = geomBuffer.VertexDeclaration; startVertex += vertexCount; } partContent.Material = geometry.Material; parts.Add(partContent); } var bounds = new BoundingSphere(); if (mesh.Positions.Count > 0) { bounds = BoundingSphere.CreateFromPoints(mesh.Positions); } return(new ModelMeshContent(mesh.Name, mesh, parent, bounds, parts)); }
private void CalculateAbsoluteTransforms(ModelBoneContent bone, Matrix[] transforms) { if (bone.Parent == null) transforms[bone.Index] = bone.Transform; else { transforms[bone.Index] = bone.Transform * transforms[bone.Parent.Index]; } foreach (ModelBoneContent child in bone.Children) CalculateAbsoluteTransforms(child, transforms); }
protected virtual string GetBoneName(ModelBoneContent mbc) { if (mbc.Name != null) return mbc.Name; string ret; if (!boneNames_.TryGetValue(mbc, out ret)) { ret = String.Format("_GenBone{0}", ++nBonesGenerated_); boneNames_.Add(mbc, ret); } return ret; }
internal ModelContent(ModelBoneContent root, IList <ModelBoneContent> bones, IList <ModelMeshContent> meshes) { _root = root; _bones = new ModelBoneContentCollection(bones); _meshes = new ModelMeshContentCollection(meshes); }
internal ModelContent(ModelBoneContent root, IList<ModelBoneContent> bones, IList<ModelMeshContent> meshes) { _root = root; _bones = new ModelBoneContentCollection(bones); _meshes = new ModelMeshContentCollection(meshes); }